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

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

* empty log message *

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