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

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

* empty log message *

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