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

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