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

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

* empty log message *

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