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

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

* empty log message *

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