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

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

* empty log message *

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