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

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

* empty log message *

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