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

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

* empty log message *

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