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

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

* empty log message *

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