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

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