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

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

* empty log message *

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