close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/polynomial.lisp@ 3647

Last change on this file since 3647 was 3647, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 15.9 KB
Line 
1;;----------------------------------------------------------------
2;; File: polynomial.lisp
3;;----------------------------------------------------------------
4;;
5;; Author: Marek Rychlik (rychlik@u.arizona.edu)
6;; Date: Thu Aug 27 09:41:24 2015
7;; Copying: (C) Marek Rychlik, 2010. All rights reserved.
8;;
9;;----------------------------------------------------------------
10;;; -*- Mode: Lisp -*-
11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12;;;
13;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
14;;;
15;;; This program is free software; you can redistribute it and/or modify
16;;; it under the terms of the GNU General Public License as published by
17;;; the Free Software Foundation; either version 2 of the License, or
18;;; (at your option) any later version.
19;;;
20;;; This program is distributed in the hope that it will be useful,
21;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;;; GNU General Public License for more details.
24;;;
25;;; You should have received a copy of the GNU General Public License
26;;; along with this program; if not, write to the Free Software
27;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28;;;
29;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30
31(defpackage "POLYNOMIAL"
32 (:use :cl :utils :monom :copy)
33 (:export "POLY"
34 "POLY-DIMENSION"
35 "POLY-TERMLIST"
36 "POLY-TERM-ORDER"
37 "POLY-INSERT-TERM"
38 "LEADING-TERM"
39 "LEADING-COEFFICIENT"
40 "ADD-TO"
41 "ADD"
42 "SUBTRACT-FROM"
43 "SUBTRACT"
44 "CHANGE-TERM-ORDER"
45 "STANDARD-EXTENSION"
46 "STANDARD-EXTENSION-1"
47 "STANDARD-SUM"
48 "SATURATION-EXTENSION"
49 "ALIST->POLY")
50 (:documentation "Implements polynomials. A polynomial is essentially
51a mapping of monomials of the same degree to coefficients. The
52momomials are ordered according to a monomial order."))
53
54(in-package :polynomial)
55
56(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
57
58(defclass poly ()
59 ((dimension :initform nil
60 :initarg :dimension
61 :accessor poly-dimension
62 :documentation "Shared dimension of all terms, the number of variables")
63 (termlist :initform nil :initarg :termlist :accessor poly-termlist
64 :documentation "List of terms.")
65 (order :initform #'lex> :initarg :order :accessor poly-term-order
66 :documentation "Monomial/term order."))
67 (:default-initargs :dimension nil :termlist nil :order #'lex>)
68 (:documentation "A polynomial with a list of terms TERMLIST, ordered
69according to term order ORDER, which defaults to LEX>."))
70
71(defmethod print-object ((self poly) stream)
72 (print-unreadable-object (self stream :type t :identity t)
73 (with-accessors ((dimension poly-dimension)
74 (termlist poly-termlist)
75 (order poly-term-order))
76 self
77 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
78 dimension termlist order))))
79
80(defgeneric change-term-order (self other)
81 (:documentation "Change term order of SELF to the term order of OTHER.")
82 (:method ((self poly) (other poly))
83 (unless (eq (poly-term-order self) (poly-term-order other))
84 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
85 (poly-term-order self) (poly-term-order other)))
86 self))
87
88(defgeneric poly-insert-term (self term)
89 (:documentation "Insert a term TERM into SELF before all other
90 terms. Order is not enforced.")
91 (:method ((self poly) (term term))
92 (cond ((null (poly-dimension self))
93 (setf (poly-dimension self) (monom-dimension term)))
94 (t (assert (= (poly-dimension self) (monom-dimension term)))))
95 (push term (poly-termlist self))
96 self))
97
98(defgeneric poly-append-term (self term)
99 (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
100 (:method ((self poly) (term term))
101 (cond ((null (poly-dimension self))
102 (setf (poly-dimension self) (monom-dimension term)))
103 (t (assert (= (poly-dimension self) (monom-dimension term)))))
104 (setf (cdr (last (poly-termlist self))) (list term))
105 self))
106
107(defun alist->poly (alist &aux (poly (make-instance 'poly)))
108 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
109It can be used to enter simple polynomials by hand, e.g the polynomial
110in two variables, X and Y, given in standard notation as:
111
112 3*X^2*Y^3+2*Y+7
113
114can be entered as
115(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
116
117NOTE: The primary use is for low-level debugging of the package."
118 (dolist (x alist poly)
119 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
120
121(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
122 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
123 (reinitialize-instance new
124 :dimension (monom-dimension old)
125 :termlist (list (cons old 1))))
126
127(defmethod universal-equalp ((self poly) (other poly))
128 "Implements equality of polynomials."
129 (and (eql (poly-dimension self) (poly-dimension other))
130 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
131 (eq (poly-term-order self) (poly-term-order other))))
132
133(defgeneric leading-term (object)
134 (:method ((self poly))
135 (car (poly-termlist self)))
136 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
137
138(defgeneric second-leading-term (object)
139 (:method ((self poly))
140 (cadar (poly-termlist self)))
141 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
142
143(defgeneric leading-coefficient (object)
144 (:method ((self poly))
145 (term-coeff (leading-term self)))
146 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
147
148
149(defgeneric second-leading-coefficient (object)
150 (:method ((self poly))
151 (term-coeff (second-leading-term self)))
152 (:documentation "The second leading coefficient of a polynomial. It
153 signals error for a polynomial with at most one term."))
154
155(defmethod universal-zerop ((self poly))
156 "Return T iff SELF is a zero polynomial."
157 (null (poly-termlist self)))
158
159(defgeneric poly-length (self)
160 (:documentation "Return the number of terms.")
161 (:method ((self poly))
162 (length (poly-termlist self))))
163
164(defmethod multiply-by ((self poly) (other monom))
165 "Multiply a polynomial SELF by OTHER."
166 (mapc #'(lambda (term) (multiply-by term other))
167 (poly-termlist self))
168 self)
169
170(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
171 "Return an expression which will efficiently adds/subtracts two
172polynomials, P and Q. The addition/subtraction of coefficients is
173performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
174is supplied, it is used to negate the coefficients of Q which do not
175have a corresponding coefficient in P. The code implements an
176efficient algorithm to add two polynomials represented as sorted lists
177of terms. The code destroys both arguments, reusing the terms to build
178the result."
179 `(macrolet ((lc (x) `(term-coeff (car ,x))))
180 (do ((p ,p)
181 (q ,q)
182 r)
183 ((or (endp p) (endp q))
184 ;; NOTE: R contains the result in reverse order. Can it
185 ;; be more efficient to produce the terms in correct order?
186 (unless (endp q)
187 ;; Upon subtraction, we must change the sign of
188 ;; all coefficients in q
189 ,@(when uminus-fn
190 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
191 (setf r (nreconc r q)))
192 r)
193 (multiple-value-bind
194 (greater-p equal-p)
195 (funcall ,order-fn (car p) (car q))
196 (cond
197 (greater-p
198 (rotatef (cdr p) r p)
199 )
200 (equal-p
201 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
202 (cond
203 ((universal-zerop s)
204 (setf p (cdr p))
205 )
206 (t
207 (setf (lc p) s)
208 (rotatef (cdr p) r p))))
209 (setf q (cdr q))
210 )
211 (t
212 ;;Negate the term of Q if UMINUS provided, signallig
213 ;;that we are doing subtraction
214 ,(when uminus-fn
215 `(setf (lc q) (funcall ,uminus-fn (lc q))))
216 (rotatef (cdr q) r q)))))))
217
218
219(defgeneric add-to (self other)
220 (:documentation "Add OTHER to SELF.")
221 (:method ((self number) (other number))
222 (+ self other)))
223
224(defgeneric subtract-from (self other)
225 (:documentation "Subtract OTHER from SELF."))
226
227
228(defmacro def-add/subtract-method (add/subtract-method-name
229 uminus-method-name
230 &optional
231 (doc-string nil doc-string-supplied-p))
232 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
233 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
234 ,@(when doc-string-supplied-p `(,doc-string))
235 ;; Ensure orders are compatible
236 (change-term-order other self)
237 (setf (poly-termlist self) (fast-add/subtract
238 (poly-termlist self) (poly-termlist other)
239 (poly-term-order self)
240 #',add/subtract-method-name
241 ,(when uminus-method-name `(function ,uminus-method-name))))
242 self))
243
244(eval-when (:compile-toplevel :load-toplevel :execute)
245
246 (def-add/subtract-method add-to nil
247 "Adds to polynomial SELF another polynomial OTHER.
248This operation destructively modifies both polynomials.
249The result is stored in SELF. This implementation does
250no consing, entirely reusing the sells of SELF and OTHER.")
251
252 (def-add/subtract-method subtract-from unary-minus
253 "Subtracts from polynomial SELF another polynomial OTHER.
254This operation destructively modifies both polynomials.
255The result is stored in SELF. This implementation does
256no consing, entirely reusing the sells of SELF and OTHER.")
257 )
258
259(defmethod unary-minus ((self poly))
260 "Destructively modifies the coefficients of the polynomial SELF,
261by changing their sign."
262 (mapc #'unary-minus (poly-termlist self))
263 self)
264
265(defun add-termlists (p q order-fn)
266 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
267 (fast-add/subtract p q order-fn #'add-to nil))
268
269(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
270 &optional (reverse-arg-order-P nil))
271 "Multiplies term TERM by a list of term, TERMLIST.
272Takes into accound divisors of zero in the ring, by
273deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
274is T, change the order of arguments; this may be important
275if we extend the package to non-commutative rings."
276 `(mapcan #'(lambda (other-term)
277 (let ((prod (multiply
278 ,@(cond
279 (reverse-arg-order-p
280 `(other-term ,term))
281 (t
282 `(,term other-term))))))
283 (cond
284 ((universal-zerop prod) nil)
285 (t (list prod)))))
286 ,termlist))
287
288(defun multiply-termlists (p q order-fn)
289 "A version of polynomial multiplication, operating
290directly on termlists."
291 (cond
292 ((or (endp p) (endp q))
293 ;;p or q is 0 (represented by NIL)
294 nil)
295 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
296 ((endp (cdr p))
297 (multiply-term-by-termlist-dropping-zeros (car p) q))
298 ((endp (cdr q))
299 (multiply-term-by-termlist-dropping-zeros (car q) p t))
300 (t
301 (cons (multiply (car p) (car q))
302 (add-termlists
303 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
304 (multiply-termlists (cdr p) q order-fn)
305 order-fn)))))
306
307(defmethod multiply-by ((self poly) (other poly))
308 (change-term-order other self)
309 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
310 (poly-termlist other)
311 (poly-term-order self)))
312 self)
313
314(defun add (object1 object2)
315 "Non-destructively add POLY1 by POLY2."
316 (add-to (copy-instance object1) (copy-instance object2)))
317
318(defun subtract (minuend &rest subtrahends)
319 "Non-destructively subtract MINUEND and SUBTRAHENDS."
320 (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))
321
322(defmethod left-tensor-product-by ((self poly) (other monom))
323 (setf (poly-termlist self)
324 (mapcan #'(lambda (term)
325 (let ((prod (left-tensor-product-by term other)))
326 (cond
327 ((universal-zerop prod) nil)
328 (t (list prod)))))
329 (poly-termlist self)))
330 (incf (poly-dimension self) (monom-dimension other))
331 self)
332
333(defmethod right-tensor-product-by ((self poly) (other monom))
334 (setf (poly-termlist self)
335 (mapcan #'(lambda (term)
336 (let ((prod (right-tensor-product-by term other)))
337 (cond
338 ((universal-zerop prod) nil)
339 (t (list prod)))))
340 (poly-termlist self)))
341 (incf (poly-dimension self) (monom-dimension other))
342 self)
343
344
345(defun standard-extension (plist &aux (k (length plist)) (i 0))
346 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
347is a list of polynomials. Destructively modifies PLIST elements."
348 (mapc #'(lambda (poly)
349 (left-tensor-product-by
350 poly
351 (prog1
352 (make-monom-variable k i)
353 (incf i))))
354 plist))
355
356(defun standard-extension-1 (plist
357 &aux
358 (plist (standard-extension plist))
359 (nvars (poly-dimension (car plist))))
360 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
361Firstly, new K variables U1, U2, ..., UK, are inserted into each
362polynomial. Subsequently, P1, P2, ..., PK are destructively modified
363tantamount to replacing PI with UI*PI-1. It assumes that all
364polynomials have the same dimension, and only the first polynomial
365is examined to determine this dimension."
366 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
367 ;; 1 from each polynomial; since UI*PI has no constant term,
368 ;; we just need to append the constant term at the end
369 ;; of each termlist.
370 (flet ((subtract-1 (p)
371 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
372 (setf plist (mapc #'subtract-1 plist)))
373 plist)
374
375
376(defun standard-sum (plist
377 &aux
378 (plist (standard-extension plist))
379 (nvars (poly-dimension (car plist))))
380 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
381Firstly, new K variables, U1, U2, ..., UK, are inserted into each
382polynomial. Subsequently, P1, P2, ..., PK are destructively modified
383tantamount to replacing PI with UI*PI, and the resulting polynomials
384are added. Finally, 1 is subtracted. It should be noted that the term
385order is not modified, which is equivalent to using a lexicographic
386order on the first K variables."
387 (flet ((subtract-1 (p)
388 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
389 (subtract-1
390 (make-instance
391 'poly
392 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
393
394#|
395
396(defun saturation-extension-1 (ring f p)
397 "Calculate [F, U*P-1]. It destructively modifies F."
398 (declare (type ring ring))
399 (polysaturation-extension ring f (list p)))
400
401
402
403
404(defun spoly (ring-and-order f g
405 &aux
406 (ring (ro-ring ring-and-order)))
407 "It yields the S-polynomial of polynomials F and G."
408 (declare (type ring-and-order ring-and-order) (type poly f g))
409 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
410 (mf (monom-div lcm (poly-lm f)))
411 (mg (monom-div lcm (poly-lm g))))
412 (declare (type monom mf mg))
413 (multiple-value-bind (c cf cg)
414 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
415 (declare (ignore c))
416 (poly-sub
417 ring-and-order
418 (scalar-times-poly ring cg (monom-times-poly mf f))
419 (scalar-times-poly ring cf (monom-times-poly mg g))))))
420
421
422(defun poly-primitive-part (ring p)
423 "Divide polynomial P with integer coefficients by gcd of its
424coefficients and return the result."
425 (declare (type ring ring) (type poly p))
426 (if (poly-zerop p)
427 (values p 1)
428 (let ((c (poly-content ring p)))
429 (values (make-poly-from-termlist
430 (mapcar
431 #'(lambda (x)
432 (make-term :monom (term-monom x)
433 :coeff (funcall (ring-div ring) (term-coeff x) c)))
434 (poly-termlist p))
435 (poly-sugar p))
436 c))))
437
438(defun poly-content (ring p)
439 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
440to compute the greatest common divisor."
441 (declare (type ring ring) (type poly p))
442 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
443
444|#
Note: See TracBrowser for help on using the repository browser.