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

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

* empty log message *

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