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@ 3124

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

* empty log message *

File size: 14.0 KB
RevLine 
[1201]1;;; -*- Mode: Lisp -*-
[77]2;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3;;;
4;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
5;;;
6;;; This program is free software; you can redistribute it and/or modify
7;;; it under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 2 of the License, or
9;;; (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19;;;
20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
[431]22(defpackage "POLYNOMIAL"
[3055]23 (:use :cl :utils :ring :monom :order :term #| :infix |# )
[2596]24 (:export "POLY"
25 "POLY-TERMLIST"
[3016]26 "POLY-TERM-ORDER"
[3071]27 "CHANGE-TERM-ORDER"
[3099]28 "STANDARD-EXTENSION"
[3101]29 "STANDARD-EXTENSION-1"
[3109]30 "STANDARD-SUM"
[3094]31 "SATURATION-EXTENSION"
32 "ALIST->POLY")
[2522]33 (:documentation "Implements polynomials"))
[143]34
[431]35(in-package :polynomial)
36
[1927]37(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]38
[2442]39(defclass poly ()
[2697]40 ((termlist :initarg :termlist :accessor poly-termlist
41 :documentation "List of terms.")
42 (order :initarg :order :accessor poly-term-order
43 :documentation "Monomial/term order."))
[2695]44 (:default-initargs :termlist nil :order #'lex>)
45 (:documentation "A polynomial with a list of terms TERMLIST, ordered
[2696]46according to term order ORDER, which defaults to LEX>."))
[2442]47
[2471]48(defmethod print-object ((self poly) stream)
[2600]49 (format stream "#<POLY TERMLIST=~A ORDER=~A>"
[2595]50 (poly-termlist self)
51 (poly-term-order self)))
[2469]52
[3015]53(defgeneric change-term-order (self other)
[3012]54 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]55 (:method ((self poly) (other poly))
56 (unless (eq (poly-term-order self) (poly-term-order other))
57 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
58 (poly-term-order self) (poly-term-order other)))
[3012]59 self))
[3010]60
[3095]61(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3093]62 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...)."
[3099]63 (dolist (x alist poly)
[3095]64 (insert-item poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
[3092]65
66
[2650]67(defmethod r-equalp ((self poly) (other poly))
[2680]68 "POLY instances are R-EQUALP if they have the same
69order and if all terms are R-EQUALP."
[2651]70 (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
71 (eq (poly-term-order self) (poly-term-order other))))
[2650]72
[2513]73(defmethod insert-item ((self poly) (item term))
74 (push item (poly-termlist self))
[2514]75 self)
[2464]76
[2513]77(defmethod append-item ((self poly) (item term))
78 (setf (cdr (last (poly-termlist self))) (list item))
79 self)
[2466]80
[52]81;; Leading term
[2442]82(defgeneric leading-term (object)
83 (:method ((self poly))
[2525]84 (car (poly-termlist self)))
85 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]86
87;; Second term
[2442]88(defgeneric second-leading-term (object)
89 (:method ((self poly))
[2525]90 (cadar (poly-termlist self)))
91 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]92
93;; Leading coefficient
[2442]94(defgeneric leading-coefficient (object)
95 (:method ((self poly))
[2526]96 (r-coeff (leading-term self)))
[2545]97 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]98
99;; Second coefficient
[2442]100(defgeneric second-leading-coefficient (object)
101 (:method ((self poly))
[2526]102 (r-coeff (second-leading-term self)))
[2906]103 (:documentation "The second leading coefficient of a polynomial. It
104 signals error for a polynomial with at most one term."))
[52]105
106;; Testing for a zero polynomial
[2445]107(defmethod r-zerop ((self poly))
108 (null (poly-termlist self)))
[52]109
110;; The number of terms
[2445]111(defmethod r-length ((self poly))
112 (length (poly-termlist self)))
[52]113
[2483]114(defmethod multiply-by ((self poly) (other monom))
[2501]115 (mapc #'(lambda (term) (multiply-by term other))
116 (poly-termlist self))
[2483]117 self)
[2469]118
[3120]119(defmethod multiply-by ((self poly) (other term))
120 (mapc #'(lambda (term) (multiply-by term other))
121 (poly-termlist self))
122 self)
123
[2501]124(defmethod multiply-by ((self poly) (other scalar))
[2502]125 (mapc #'(lambda (term) (multiply-by term other))
[2501]126 (poly-termlist self))
[2487]127 self)
128
[2607]129
[2761]130(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]131 "Return an expression which will efficiently adds/subtracts two
132polynomials, P and Q. The addition/subtraction of coefficients is
133performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
134is supplied, it is used to negate the coefficients of Q which do not
[2756]135have a corresponding coefficient in P. The code implements an
136efficient algorithm to add two polynomials represented as sorted lists
137of terms. The code destroys both arguments, reusing the terms to build
138the result."
[2742]139 `(macrolet ((lc (x) `(r-coeff (car ,x))))
140 (do ((p ,p)
141 (q ,q)
142 r)
143 ((or (endp p) (endp q))
144 ;; NOTE: R contains the result in reverse order. Can it
145 ;; be more efficient to produce the terms in correct order?
[2774]146 (unless (endp q)
[2776]147 ;; Upon subtraction, we must change the sign of
148 ;; all coefficients in q
[2774]149 ,@(when uminus-fn
[2775]150 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]151 (setf r (nreconc r q)))
[2742]152 r)
153 (multiple-value-bind
154 (greater-p equal-p)
[2766]155 (funcall ,order-fn (car p) (car q))
[2742]156 (cond
157 (greater-p
158 (rotatef (cdr p) r p)
159 )
160 (equal-p
[2766]161 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]162 (cond
163 ((r-zerop s)
164 (setf p (cdr p))
165 )
166 (t
167 (setf (lc p) s)
168 (rotatef (cdr p) r p))))
169 (setf q (cdr q))
170 )
171 (t
[2743]172 ;;Negate the term of Q if UMINUS provided, signallig
173 ;;that we are doing subtraction
[2908]174 ,(when uminus-fn
175 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[2743]176 (rotatef (cdr q) r q)))))))
[2585]177
[2655]178
[2763]179(defmacro def-add/subtract-method (add/subtract-method-name
[2752]180 uminus-method-name
181 &optional
[2913]182 (doc-string nil doc-string-supplied-p))
[2615]183 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]184 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]185 ,@(when doc-string-supplied-p `(,doc-string))
[2769]186 ;; Ensure orders are compatible
[3015]187 (change-term-order other self)
[2772]188 (setf (poly-termlist self) (fast-add/subtract
189 (poly-termlist self) (poly-termlist other)
190 (poly-term-order self)
191 #',add/subtract-method-name
192 ,(when uminus-method-name `(function ,uminus-method-name))))
[2609]193 self))
[2487]194
[2916]195(eval-when (:compile-toplevel :load-toplevel :execute)
[2777]196
197 (def-add/subtract-method add-to nil
198 "Adds to polynomial SELF another polynomial OTHER.
[2610]199This operation destructively modifies both polynomials.
200The result is stored in SELF. This implementation does
[2752]201no consing, entirely reusing the sells of SELF and OTHER.")
[2609]202
[2777]203 (def-add/subtract-method subtract-from unary-minus
[2753]204 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]205This operation destructively modifies both polynomials.
206The result is stored in SELF. This implementation does
[2752]207no consing, entirely reusing the sells of SELF and OTHER.")
[2916]208 )
[2777]209
[2691]210(defmethod unary-minus ((self poly))
[2694]211 "Destructively modifies the coefficients of the polynomial SELF,
212by changing their sign."
[2692]213 (mapc #'unary-minus (poly-termlist self))
[2683]214 self)
[52]215
[2795]216(defun add-termlists (p q order-fn)
[2794]217 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
[2917]218 (fast-add/subtract p q order-fn #'add-to nil))
[2794]219
[2800]220(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]221 &optional (reverse-arg-order-P nil))
[2799]222 "Multiplies term TERM by a list of term, TERMLIST.
[2792]223Takes into accound divisors of zero in the ring, by
[2927]224deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]225is T, change the order of arguments; this may be important
[2927]226if we extend the package to non-commutative rings."
[2800]227 `(mapcan #'(lambda (other-term)
[2907]228 (let ((prod (r*
[2923]229 ,@(cond
[2930]230 (reverse-arg-order-p
[2925]231 `(other-term ,term))
232 (t
233 `(,term other-term))))))
[2800]234 (cond
235 ((r-zerop prod) nil)
236 (t (list prod)))))
237 ,termlist))
[2790]238
[2796]239(defun multiply-termlists (p q order-fn)
[2787]240 (cond
[2917]241 ((or (endp p) (endp q))
242 ;;p or q is 0 (represented by NIL)
243 nil)
[2789]244 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]245 ((endp (cdr p))
[2918]246 (multiply-term-by-termlist-dropping-zeros (car p) q))
247 ((endp (cdr q))
[2919]248 (multiply-term-by-termlist-dropping-zeros (car q) p t))
249 (t
[2948]250 (cons (r* (car p) (car q))
[2949]251 (add-termlists
252 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
253 (multiply-termlists (cdr p) q order-fn)
254 order-fn)))))
[2793]255
[2803]256(defmethod multiply-by ((self poly) (other poly))
[3014]257 (change-term-order other self)
[2803]258 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
259 (poly-termlist other)
260 (poly-term-order self)))
261 self)
262
[2939]263(defmethod r* ((poly1 poly) (poly2 poly))
264 "Non-destructively multiply POLY1 by POLY2."
265 (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
[2916]266
[3044]267(defmethod left-tensor-product-by ((self poly) (other term))
268 (setf (poly-termlist self)
269 (mapcan #'(lambda (term)
[3047]270 (let ((prod (left-tensor-product-by term other)))
[3044]271 (cond
272 ((r-zerop prod) nil)
273 (t (list prod)))))
[3048]274 (poly-termlist self)))
[3044]275 self)
276
277(defmethod right-tensor-product-by ((self poly) (other term))
[3045]278 (setf (poly-termlist self)
279 (mapcan #'(lambda (term)
[3046]280 (let ((prod (right-tensor-product-by term other)))
[3045]281 (cond
282 ((r-zerop prod) nil)
283 (t (list prod)))))
[3048]284 (poly-termlist self)))
[3045]285 self)
[3044]286
[3062]287(defmethod left-tensor-product-by ((self poly) (other monom))
288 (setf (poly-termlist self)
289 (mapcan #'(lambda (term)
290 (let ((prod (left-tensor-product-by term other)))
291 (cond
292 ((r-zerop prod) nil)
293 (t (list prod)))))
294 (poly-termlist self)))
295 self)
[3044]296
[3062]297(defmethod right-tensor-product-by ((self poly) (other monom))
298 (setf (poly-termlist self)
299 (mapcan #'(lambda (term)
300 (let ((prod (right-tensor-product-by term other)))
301 (cond
302 ((r-zerop prod) nil)
303 (t (list prod)))))
304 (poly-termlist self)))
305 self)
306
307
[3084]308(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]309 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]310is a list of polynomials. Destructively modifies PLIST elements."
[3061]311 (mapc #'(lambda (poly)
[3085]312 (left-tensor-product-by
313 poly
314 (prog1
315 (make-monom-variable k i)
316 (incf i))))
[3061]317 plist))
[52]318
[3091]319(defmethod poly-dimension ((poly poly))
320 (cond ((r-zerop poly) -1)
321 (t (monom-dimension (leading-term poly)))))
322
[3087]323(defun standard-extension-1 (plist
324 &aux
[3096]325 (plist (standard-extension plist))
[3087]326 (nvars (poly-dimension (car plist))))
[3081]327 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]328Firstly, new K variables U1, U2, ..., UK, are inserted into each
329polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]330tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]331polynomials have the same dimension, and only the first polynomial
332is examined to determine this dimension."
[3089]333 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
334 ;; 1 from each polynomial; since UI*PI has no constant term,
335 ;; we just need to append the constant term at the end
336 ;; of each termlist.
[3064]337 (flet ((subtract-1 (p)
[3104]338 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
[3083]339 (setf plist (mapc #'subtract-1 plist)))
[3077]340 plist)
[52]341
342
[3107]343(defun standard-sum (plist
344 &aux
345 (plist (standard-extension plist))
346 (nvars (poly-dimension (car plist))))
[3087]347 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
348Firstly, new K variables, U1, U2, ..., UK, are inserted into each
349polynomial. Subsequently, P1, P2, ..., PK are destructively modified
350tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]351are added. Finally, 1 is subtracted. It should be noted that the term
352order is not modified, which is equivalent to using a lexicographic
353order on the first K variables."
[3107]354 (flet ((subtract-1 (p)
355 (append-item p (make-instance 'term :coeff -1 :dimension nvars))))
[3108]356 (subtract-1
357 (make-instance
358 'poly
[3115]359 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]360
[3122]361#|
362
[1477]363(defun saturation-extension-1 (ring f p)
[1497]364 "Calculate [F, U*P-1]. It destructively modifies F."
[1908]365 (declare (type ring ring))
[1477]366 (polysaturation-extension ring f (list p)))
[53]367
[3122]368
[53]369
370
[1189]371(defun spoly (ring-and-order f g
372 &aux
373 (ring (ro-ring ring-and-order)))
[55]374 "It yields the S-polynomial of polynomials F and G."
[1911]375 (declare (type ring-and-order ring-and-order) (type poly f g))
[55]376 (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
[2913]377 (mf (monom-div lcm (poly-lm f)))
378 (mg (monom-div lcm (poly-lm g))))
[55]379 (declare (type monom mf mg))
380 (multiple-value-bind (c cf cg)
381 (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
382 (declare (ignore c))
383 (poly-sub
[1189]384 ring-and-order
[55]385 (scalar-times-poly ring cg (monom-times-poly mf f))
386 (scalar-times-poly ring cf (monom-times-poly mg g))))))
[53]387
388
[55]389(defun poly-primitive-part (ring p)
390 "Divide polynomial P with integer coefficients by gcd of its
391coefficients and return the result."
[1912]392 (declare (type ring ring) (type poly p))
[55]393 (if (poly-zerop p)
394 (values p 1)
[2913]395 (let ((c (poly-content ring p)))
396 (values (make-poly-from-termlist
397 (mapcar
398 #'(lambda (x)
399 (make-term :monom (term-monom x)
400 :coeff (funcall (ring-div ring) (term-coeff x) c)))
401 (poly-termlist p))
402 (poly-sugar p))
403 c))))
[55]404
405(defun poly-content (ring p)
406 "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
407to compute the greatest common divisor."
[1913]408 (declare (type ring ring) (type poly p))
[55]409 (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
[1066]410
[2456]411|#
Note: See TracBrowser for help on using the repository browser.