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

Last change on this file since 3909 was 3908, checked in by Marek Rychlik, 8 years ago

* empty log message *

File size: 23.6 KB
RevLine 
[3400]1;;----------------------------------------------------------------
[1201]2;;; -*- Mode: Lisp -*-
[77]3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4;;;
5;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
6;;;
7;;; This program is free software; you can redistribute it and/or modify
8;;; it under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 2 of the License, or
10;;; (at your option) any later version.
11;;;
12;;; This program is distributed in the hope that it will be useful,
13;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with this program; if not, write to the Free Software
19;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20;;;
21;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22
[431]23(defpackage "POLYNOMIAL"
[3643]24 (:use :cl :utils :monom :copy)
[2596]25 (:export "POLY"
[3270]26 "POLY-DIMENSION"
[2596]27 "POLY-TERMLIST"
[3016]28 "POLY-TERM-ORDER"
[3509]29 "POLY-INSERT-TERM"
[3690]30 "SCALAR-MULTIPLY-BY"
31 "SCALAR-DIVIDE-BY"
[3642]32 "LEADING-TERM"
[3657]33 "LEADING-MONOMIAL"
[3642]34 "LEADING-COEFFICIENT"
[3657]35 "SECOND-LEADING-TERM"
36 "SECOND-LEADING-MONOMIAL"
37 "SECOND-LEADING-COEFFICIENT"
[3642]38 "ADD-TO"
[3646]39 "ADD"
[3642]40 "SUBTRACT-FROM"
[3646]41 "SUBTRACT"
[3071]42 "CHANGE-TERM-ORDER"
[3099]43 "STANDARD-EXTENSION"
[3101]44 "STANDARD-EXTENSION-1"
[3109]45 "STANDARD-SUM"
[3094]46 "SATURATION-EXTENSION"
[3655]47 "ALIST->POLY"
[3852]48 "->INFIX"
[3655]49 "UNIVERSAL-EZGCD"
[3678]50 "S-POLYNOMIAL"
[3686]51 "POLY-CONTENT"
[3692]52 "POLY-PRIMITIVE-PART"
[3714]53 "SATURATION-EXTENSION-1"
[3737]54 "MAKE-POLY-VARIABLE"
[3821]55 "MAKE-POLY-CONSTANT"
[3778]56 "UNIVERSAL-EXPT"
[3900]57 "POLY-P"
[3901]58 "+LIST-MARKER+"
[3900]59 "POLY-EVAL")
[3489]60 (:documentation "Implements polynomials. A polynomial is essentially
61a mapping of monomials of the same degree to coefficients. The
62momomials are ordered according to a monomial order."))
[143]63
[431]64(in-package :polynomial)
65
[1927]66(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]67
[2442]68(defclass poly ()
[3253]69 ((dimension :initform nil
[3250]70 :initarg :dimension
71 :accessor poly-dimension
[3242]72 :documentation "Shared dimension of all terms, the number of variables")
[3250]73 (termlist :initform nil :initarg :termlist :accessor poly-termlist
[3619]74 :documentation "List of terms.")
[3250]75 (order :initform #'lex> :initarg :order :accessor poly-term-order
[2697]76 :documentation "Monomial/term order."))
[3262]77 (:default-initargs :dimension nil :termlist nil :order #'lex>)
[2695]78 (:documentation "A polynomial with a list of terms TERMLIST, ordered
[2696]79according to term order ORDER, which defaults to LEX>."))
[2442]80
[2471]81(defmethod print-object ((self poly) stream)
[3241]82 (print-unreadable-object (self stream :type t :identity t)
[3243]83 (with-accessors ((dimension poly-dimension)
84 (termlist poly-termlist)
85 (order poly-term-order))
[3237]86 self
[3244]87 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
88 dimension termlist order))))
[2469]89
[3015]90(defgeneric change-term-order (self other)
[3012]91 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]92 (:method ((self poly) (other poly))
93 (unless (eq (poly-term-order self) (poly-term-order other))
[3620]94 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
[3010]95 (poly-term-order self) (poly-term-order other)))
[3012]96 self))
[3010]97
[3621]98(defgeneric poly-insert-term (self term)
[3622]99 (:documentation "Insert a term TERM into SELF before all other
[3621]100 terms. Order is not enforced.")
101 (:method ((self poly) (term term))
[3510]102 (cond ((null (poly-dimension self))
[3621]103 (setf (poly-dimension self) (monom-dimension term)))
104 (t (assert (= (poly-dimension self) (monom-dimension term)))))
105 (push term (poly-termlist self))
[3510]106 self))
107
[3622]108(defgeneric poly-append-term (self term)
109 (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
110 (:method ((self poly) (term term))
[3510]111 (cond ((null (poly-dimension self))
[3622]112 (setf (poly-dimension self) (monom-dimension term)))
113 (t (assert (= (poly-dimension self) (monom-dimension term)))))
114 (setf (cdr (last (poly-termlist self))) (list term))
[3510]115 self))
116
[3095]117(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3126]118 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
119It can be used to enter simple polynomials by hand, e.g the polynomial
120in two variables, X and Y, given in standard notation as:
121
122 3*X^2*Y^3+2*Y+7
123
124can be entered as
125(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
126
127NOTE: The primary use is for low-level debugging of the package."
[3099]128 (dolist (x alist poly)
[3705]129 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
[3092]130
[3877]131(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
[3786]132 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
[3401]133 (reinitialize-instance new
134 :dimension (monom-dimension old)
[3786]135 :termlist (list old)))
[3796]136
[3877]137(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
[3796]138 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
139 (reinitialize-instance new
140 :dimension (monom-dimension old)
[3797]141 :termlist (list (change-class old 'term))))
[3403]142
[3624]143(defmethod universal-equalp ((self poly) (other poly))
144 "Implements equality of polynomials."
145 (and (eql (poly-dimension self) (poly-dimension other))
146 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
147 (eq (poly-term-order self) (poly-term-order other))))
[2650]148
[3624]149(defgeneric leading-term (object)
[2442]150 (:method ((self poly))
[2525]151 (car (poly-termlist self)))
152 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]153
[3625]154(defgeneric second-leading-term (object)
[2442]155 (:method ((self poly))
[2525]156 (cadar (poly-termlist self)))
157 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]158
[3656]159(defgeneric leading-monomial (object)
160 (:method ((self poly))
161 (change-class (copy-instance (leading-term self)) 'monom))
162 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
163
164(defgeneric second-leading-monomial (object)
165 (:method ((self poly))
166 (change-class (copy-instance (second-leading-term self)) 'monom))
167 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
168
[3625]169(defgeneric leading-coefficient (object)
[2442]170 (:method ((self poly))
[3642]171 (term-coeff (leading-term self)))
[2545]172 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]173
[2442]174(defgeneric second-leading-coefficient (object)
175 (:method ((self poly))
[3645]176 (term-coeff (second-leading-term self)))
[2906]177 (:documentation "The second leading coefficient of a polynomial. It
178 signals error for a polynomial with at most one term."))
[52]179
[3629]180(defmethod universal-zerop ((self poly))
181 "Return T iff SELF is a zero polynomial."
[3639]182 (null (poly-termlist self)))
[52]183
[3518]184(defgeneric poly-length (self)
[3630]185 (:documentation "Return the number of terms.")
[3518]186 (:method ((self poly))
187 (length (poly-termlist self))))
[52]188
[3689]189(defgeneric scalar-multiply-by (self other)
190 (:documentation "Multiply vector SELF by a scalar OTHER.")
191 (:method ((self poly) other)
192 (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
193 (poly-termlist self))
194 self))
195
196(defgeneric scalar-divide-by (self other)
197 (:documentation "Divide vector SELF by a scalar OTHER.")
198 (:method ((self poly) other)
199 (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
200 (poly-termlist self))
201 self))
202
[3663]203(defmethod multiply-by ((self poly) (other monom))
[3630]204 "Multiply a polynomial SELF by OTHER."
205 (mapc #'(lambda (term) (multiply-by term other))
206 (poly-termlist self))
207 self)
[2469]208
[3672]209(defmethod multiply-by ((self poly) (other term))
210 "Multiply a polynomial SELF by OTHER."
211 (mapc #'(lambda (term) (multiply-by term other))
212 (poly-termlist self))
213 self)
214
[2761]215(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]216 "Return an expression which will efficiently adds/subtracts two
217polynomials, P and Q. The addition/subtraction of coefficients is
[3878]218performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
219used to negate the coefficients of Q which do not have a corresponding
220coefficient in P. The code implements an efficient algorithm to add
221two polynomials represented as sorted lists of terms. The code
222destroys both arguments, reusing the terms to build the result."
[3631]223 `(macrolet ((lc (x) `(term-coeff (car ,x))))
[2742]224 (do ((p ,p)
225 (q ,q)
226 r)
227 ((or (endp p) (endp q))
228 ;; NOTE: R contains the result in reverse order. Can it
229 ;; be more efficient to produce the terms in correct order?
[2774]230 (unless (endp q)
[2776]231 ;; Upon subtraction, we must change the sign of
232 ;; all coefficients in q
[2774]233 ,@(when uminus-fn
[2775]234 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]235 (setf r (nreconc r q)))
[3887]236 (unless (endp p)
237 (setf r (nreconc r p)))
238 r)
[2742]239 (multiple-value-bind
240 (greater-p equal-p)
[3632]241 (funcall ,order-fn (car p) (car q))
[2742]242 (cond
243 (greater-p
244 (rotatef (cdr p) r p)
245 )
246 (equal-p
[2766]247 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]248 (cond
[3640]249 ((universal-zerop s)
[2742]250 (setf p (cdr p))
251 )
252 (t
253 (setf (lc p) s)
254 (rotatef (cdr p) r p))))
255 (setf q (cdr q))
256 )
257 (t
[2743]258 ;;Negate the term of Q if UMINUS provided, signallig
259 ;;that we are doing subtraction
[2908]260 ,(when uminus-fn
261 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[3887]262 (rotatef (cdr q) r q))))
263 ;;(format t "P:~A~%" p)
264 ;;(format t "Q:~A~%" q)
265 ;;(format t "R:~A~%" r)
266 )))
[2585]267
[2655]268
[3887]269
[3647]270(defgeneric add-to (self other)
271 (:documentation "Add OTHER to SELF.")
272 (:method ((self number) (other number))
[3819]273 (+ self other))
274 (:method ((self poly) (other number))
[3865]275 (add-to self (make-poly-constant (poly-dimension self) other)))
276 (:method ((self number) (other poly))
277 (add-to (make-poly-constant (poly-dimension other) self) other)))
[3819]278
[3647]279
280(defgeneric subtract-from (self other)
[3648]281 (:documentation "Subtract OTHER from SELF.")
282 (:method ((self number) (other number))
[3830]283 (- self other))
284 (:method ((self poly) (other number))
285 (subtract-from self (make-poly-constant (poly-dimension self) other))))
[3647]286
[3884]287#|
[3908]288
[3750]289(defmacro def-add/subtract-method (add/subtract-method-name
[3749]290 uminus-method-name
291 &optional
292 (doc-string nil doc-string-supplied-p))
[3647]293 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]294 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]295 ,@(when doc-string-supplied-p `(,doc-string))
[2769]296 ;; Ensure orders are compatible
[3015]297 (change-term-order other self)
[2772]298 (setf (poly-termlist self) (fast-add/subtract
299 (poly-termlist self) (poly-termlist other)
300 (poly-term-order self)
301 #',add/subtract-method-name
302 ,(when uminus-method-name `(function ,uminus-method-name))))
[3748]303 self))
[3908]304
305(eval-when (:load-toplevel :execute)
306
307 (def-add/subtract-method add-to nil
308 "Adds to polynomial SELF another polynomial OTHER.
309This operation destructively modifies both polynomials.
310The result is stored in SELF. This implementation does
311no consing, entirely reusing the sells of SELF and OTHER.")
312
313 (def-add/subtract-method subtract-from unary-minus
314 "Subtracts from polynomial SELF another polynomial OTHER.
315This operation destructively modifies both polynomials.
316The result is stored in SELF. This implementation does
317no consing, entirely reusing the sells of SELF and OTHER.")
318 )
319
[3884]320|#
[2487]321
[3880]322(defmethod unary-minus ((self poly))
323 "Destructively modifies the coefficients of the polynomial SELF,
324by changing their sign."
325 (mapc #'unary-minus (poly-termlist self))
326 self)
327
328(defun add-termlists (p q order-fn)
329 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
330 (fast-add/subtract p q order-fn #'add-to nil))
331
[3881]332(defun subtract-termlists (p q order-fn)
[3885]333 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
[3882]334 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
[3881]335
[3879]336(defmethod add-to ((self poly) (other poly))
337 "Adds to polynomial SELF another polynomial OTHER.
[2610]338This operation destructively modifies both polynomials.
339The result is stored in SELF. This implementation does
[3879]340no consing, entirely reusing the sells of SELF and OTHER."
341 (change-term-order other self)
342 (setf (poly-termlist self) (add-termlists
343 (poly-termlist self) (poly-termlist other)
[3883]344 (poly-term-order self)))
345 self)
[3879]346
[2609]347
[3879]348(defmethod subtract-from ((self poly) (other poly))
[2753]349 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]350This operation destructively modifies both polynomials.
351The result is stored in SELF. This implementation does
[3879]352no consing, entirely reusing the sells of SELF and OTHER."
353 (change-term-order other self)
354 (setf (poly-termlist self) (subtract-termlists
355 (poly-termlist self) (poly-termlist other)
[3883]356 (poly-term-order self)))
357 self)
[2777]358
[2800]359(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]360 &optional (reverse-arg-order-P nil))
[2799]361 "Multiplies term TERM by a list of term, TERMLIST.
[2792]362Takes into accound divisors of zero in the ring, by
[2927]363deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]364is T, change the order of arguments; this may be important
[2927]365if we extend the package to non-commutative rings."
[2800]366 `(mapcan #'(lambda (other-term)
[3633]367 (let ((prod (multiply
[2923]368 ,@(cond
[2930]369 (reverse-arg-order-p
[2925]370 `(other-term ,term))
371 (t
372 `(,term other-term))))))
[2800]373 (cond
[3633]374 ((universal-zerop prod) nil)
[2800]375 (t (list prod)))))
376 ,termlist))
[2790]377
[2796]378(defun multiply-termlists (p q order-fn)
[3127]379 "A version of polynomial multiplication, operating
380directly on termlists."
[2787]381 (cond
[2917]382 ((or (endp p) (endp q))
383 ;;p or q is 0 (represented by NIL)
384 nil)
[2789]385 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]386 ((endp (cdr p))
[2918]387 (multiply-term-by-termlist-dropping-zeros (car p) q))
388 ((endp (cdr q))
[2919]389 (multiply-term-by-termlist-dropping-zeros (car q) p t))
390 (t
[3633]391 (cons (multiply (car p) (car q))
[2949]392 (add-termlists
393 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
394 (multiply-termlists (cdr p) q order-fn)
395 order-fn)))))
[2793]396
[2803]397(defmethod multiply-by ((self poly) (other poly))
[3014]398 (change-term-order other self)
[2803]399 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
400 (poly-termlist other)
401 (poly-term-order self)))
402 self)
403
[3804]404(defgeneric add-2 (object1 object2)
405 (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
[3813]406 (:method ((object1 t) (object2 t))
[3804]407 (add-to (copy-instance object1) (copy-instance object2))))
[3374]408
[3803]409(defun add (&rest summands)
410 "Non-destructively adds list SUMMANDS."
411 (cond ((endp summands) 0)
[3818]412 (t (reduce #'add-2 summands))))
[3803]413
[3634]414(defun subtract (minuend &rest subtrahends)
[3427]415 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3851]416 (cond ((endp subtrahends) (unary-minus minuend))
417 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
[3374]418
[3062]419(defmethod left-tensor-product-by ((self poly) (other monom))
420 (setf (poly-termlist self)
421 (mapcan #'(lambda (term)
422 (let ((prod (left-tensor-product-by term other)))
423 (cond
[3640]424 ((universal-zerop prod) nil)
[3062]425 (t (list prod)))))
426 (poly-termlist self)))
[3249]427 (incf (poly-dimension self) (monom-dimension other))
[3062]428 self)
[3044]429
[3062]430(defmethod right-tensor-product-by ((self poly) (other monom))
431 (setf (poly-termlist self)
432 (mapcan #'(lambda (term)
433 (let ((prod (right-tensor-product-by term other)))
434 (cond
[3640]435 ((universal-zerop prod) nil)
[3062]436 (t (list prod)))))
437 (poly-termlist self)))
[3249]438 (incf (poly-dimension self) (monom-dimension other))
[3062]439 self)
440
441
[3084]442(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]443 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]444is a list of polynomials. Destructively modifies PLIST elements."
[3061]445 (mapc #'(lambda (poly)
[3085]446 (left-tensor-product-by
447 poly
448 (prog1
449 (make-monom-variable k i)
450 (incf i))))
[3061]451 plist))
[52]452
[3087]453(defun standard-extension-1 (plist
454 &aux
[3096]455 (plist (standard-extension plist))
[3087]456 (nvars (poly-dimension (car plist))))
[3081]457 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]458Firstly, new K variables U1, U2, ..., UK, are inserted into each
459polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]460tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]461polynomials have the same dimension, and only the first polynomial
462is examined to determine this dimension."
[3089]463 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
464 ;; 1 from each polynomial; since UI*PI has no constant term,
465 ;; we just need to append the constant term at the end
466 ;; of each termlist.
[3064]467 (flet ((subtract-1 (p)
[3641]468 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3083]469 (setf plist (mapc #'subtract-1 plist)))
[3077]470 plist)
[52]471
472
[3107]473(defun standard-sum (plist
474 &aux
475 (plist (standard-extension plist))
476 (nvars (poly-dimension (car plist))))
[3087]477 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
478Firstly, new K variables, U1, U2, ..., UK, are inserted into each
479polynomial. Subsequently, P1, P2, ..., PK are destructively modified
480tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]481are added. Finally, 1 is subtracted. It should be noted that the term
482order is not modified, which is equivalent to using a lexicographic
483order on the first K variables."
[3107]484 (flet ((subtract-1 (p)
[3641]485 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3108]486 (subtract-1
487 (make-instance
488 'poly
[3115]489 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]490
[3653]491(defgeneric universal-ezgcd (x y)
492 (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
493C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
494the Euclidean algorithm.")
495 (:method ((x integer) (y integer)
496 &aux (c (gcd x y)))
497 (values c (/ x c) (/ y c)))
498 )
499
[3655]500(defgeneric s-polynomial (object1 object2)
[3651]501 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
502 (:method ((f poly) (g poly))
503 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
504 (mf (divide lcm (leading-monomial f)))
505 (mg (divide lcm (leading-monomial g))))
506 (multiple-value-bind (c cf cg)
[3652]507 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
[3651]508 (declare (ignore c))
509 (subtract
[3673]510 (multiply f (change-class mf 'term :coeff cg))
511 (multiply g (change-class mg 'term :coeff cf)))))))
[3651]512
[3676]513(defgeneric poly-content (object)
514 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
[3677]515 (:method ((self poly))
516 (reduce #'universal-gcd
[3679]517 (mapcar #'term-coeff (rest (poly-termlist self)))
518 :initial-value (leading-coefficient self))))
[3676]519
[3684]520(defun poly-primitive-part (object)
[3685]521 "Divide polynomial OBJECT by gcd of its
[3684]522coefficients. Return the resulting polynomial."
[3688]523 (scalar-divide-by object (poly-content object)))
[3682]524
[3700]525(defun poly-insert-variables (self k)
[3697]526 (left-tensor-product-by self (make-instance 'monom :dimension k)))
527
[3698]528(defun saturation-extension (f plist &aux (k (length plist)))
[3708]529 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
530PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
[3711]531as first K variables. It destructively modifies F and PLIST."
[3700]532 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3699]533 (standard-extension-1 plist)))
[3694]534
[3699]535(defun polysaturation-extension (f plist &aux (k (length plist)))
[3708]536 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
537and F' is F with variables U1,U2,...,UK inserted as first K
[3711]538variables. It destructively modifies F and PLIST."
[3700]539 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3703]540 (list (standard-sum plist))))
[3694]541
[3691]542(defun saturation-extension-1 (f p)
[3712]543 "Given family of polynomials F and a polynomial P, calculate [F',
544U*P-1], where F' is F with variable inserted as the first variable. It
545destructively modifies F and P."
[3693]546 (polysaturation-extension f (list p)))
[3713]547
[3717]548(defmethod multiply-by ((object1 number) (object2 poly))
[3720]549 (scalar-multiply-by (copy-instance object2) object1))
[3716]550
[3781]551(defun make-poly-variable (nvars pos &optional (power 1))
552 (change-class (make-monom-variable nvars pos power) 'poly))
[3736]553
[3821]554(defun make-poly-constant (nvars coeff)
555 (change-class (make-term-constant nvars coeff) 'poly))
556
[3713]557(defgeneric universal-expt (x y)
[3721]558 (:documentation "Raises X to power Y.")
[3713]559 (:method ((x number) (y integer)) (expt x y))
560 (:method ((x t) (y integer))
561 (declare (type fixnum y))
562 (cond
563 ((minusp y) (error "universal-expt: Negative exponent."))
564 ((universal-zerop x) (if (zerop y) 1))
565 (t
566 (do ((k 1 (ash k 1))
567 (q x (multiply q q)) ;keep squaring
568 (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
569 ((> k y) p)
[3778]570 (declare (fixnum k)))))))
571
572(defgeneric poly-p (object)
573 (:documentation "Checks if an object is a polynomial.")
[3779]574 (:method ((self poly)) t)
[3778]575 (:method ((self t)) nil))
[3830]576
[3904]577(defmethod ->infix :before ((self poly) &optional vars)
[3905]578 "Ensures that the number of variables in VARS maches the polynomial dimension of the
579polynomial SELF."
[3904]580 (assert (= (length vars) (poly-dimension self))))
581
[3830]582(defmethod ->infix ((self poly) &optional vars)
[3905]583 "Converts a polynomial SELF to a sexp."
[3830]584 (cons '+ (mapcar #'(lambda (x) (->infix x vars))
585 (poly-termlist self))))
[3899]586
[3903]587(defparameter +list-marker+ :[
588 "A sexp with this head is considered a list of polynomials.")
589
[3906]590(defmethod ->infix ((self cons) &optional vars)
591 (assert (eql (car self) +list-marker+))
[3907]592 (cons +list-marker+ (mapcar #'(lambda (p) (->infix p vars)) (cdr self))))
[3906]593
594
[3899]595(defun poly-eval (expr vars order)
596 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
597variables VARS. Return the resulting polynomial or list of
598polynomials. Standard arithmetical operators in form EXPR are
599replaced with their analogues in the ring of polynomials, and the
600resulting expression is evaluated, resulting in a polynomial or a list
601of polynomials in internal form. A similar operation in another computer
602algebra system could be called 'expand' or so."
603 (labels ((p-eval (p) (poly-eval p vars order))
604 (p-eval-scalar (p) (poly-eval p '() order))
605 (p-eval-list (plist) (mapcar #'p-eval plist)))
606 (cond
607 ((eq expr 0)
608 (make-instance 'poly :dimension (length vars)))
609 ((member expr vars :test #'equalp)
610 (let ((pos (position expr vars :test #'equalp)))
611 (make-poly-variable (length vars) pos)))
612 ((atom expr)
613 expr)
614 ((eq (car expr) +list-marker+)
615 (cons +list-marker+ (p-eval-list (cdr expr))))
616 (t
617 (case (car expr)
618 (+ (reduce #'add (p-eval-list (cdr expr))))
619 (- (apply #'subtract (p-eval-list (cdr expr))))
620 (*
621 (if (endp (cddr expr)) ;unary
622 (p-eval (cadr expr))
623 (reduce #'multiply (p-eval-list (cdr expr)))))
624 (/
625 ;; A polynomial can be divided by a scalar
626 (cond
627 ((endp (cddr expr))
628 ;; A special case (/ ?), the inverse
629 (divide (cadr expr)))
630 (t
631 (let ((num (p-eval (cadr expr)))
632 (denom-inverse (apply #'divide (mapcar #'p-eval-scalar (cddr expr)))))
633 (multiply denom-inverse num)))))
634 (expt
635 (cond
636 ((member (cadr expr) vars :test #'equalp)
637 ;;Special handling of (expt var pow)
638 (let ((pos (position (cadr expr) vars :test #'equalp)))
639 (make-poly-variable (length vars) pos (caddr expr))))
640 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
641 ;; Negative power means division in coefficient ring
642 ;; Non-integer power means non-polynomial coefficient
643 expr)
644 (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
645 (otherwise
646 expr))))))
Note: See TracBrowser for help on using the repository browser.