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

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

* empty log message *

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