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

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

* empty log message *

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