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

Last change on this file since 3902 was 3901, checked in by Marek Rychlik, 8 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"
[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#|
[3750]288(defmacro def-add/subtract-method (add/subtract-method-name
[3749]289 uminus-method-name
290 &optional
291 (doc-string nil doc-string-supplied-p))
[3647]292 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]293 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]294 ,@(when doc-string-supplied-p `(,doc-string))
[2769]295 ;; Ensure orders are compatible
[3015]296 (change-term-order other self)
[2772]297 (setf (poly-termlist self) (fast-add/subtract
298 (poly-termlist self) (poly-termlist other)
299 (poly-term-order self)
300 #',add/subtract-method-name
301 ,(when uminus-method-name `(function ,uminus-method-name))))
[3748]302 self))
[3884]303|#
[2487]304
[3880]305(defmethod unary-minus ((self poly))
306 "Destructively modifies the coefficients of the polynomial SELF,
307by changing their sign."
308 (mapc #'unary-minus (poly-termlist self))
309 self)
310
311(defun add-termlists (p q order-fn)
312 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
313 (fast-add/subtract p q order-fn #'add-to nil))
314
[3881]315(defun subtract-termlists (p q order-fn)
[3885]316 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
[3882]317 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
[3881]318
[3879]319(defmethod add-to ((self poly) (other poly))
320 "Adds to polynomial SELF another polynomial OTHER.
[2610]321This operation destructively modifies both polynomials.
322The result is stored in SELF. This implementation does
[3879]323no consing, entirely reusing the sells of SELF and OTHER."
324 (change-term-order other self)
325 (setf (poly-termlist self) (add-termlists
326 (poly-termlist self) (poly-termlist other)
[3883]327 (poly-term-order self)))
328 self)
[3879]329
[2609]330
[3879]331(defmethod subtract-from ((self poly) (other poly))
[2753]332 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]333This operation destructively modifies both polynomials.
334The result is stored in SELF. This implementation does
[3879]335no consing, entirely reusing the sells of SELF and OTHER."
336 (change-term-order other self)
337 (setf (poly-termlist self) (subtract-termlists
338 (poly-termlist self) (poly-termlist other)
[3883]339 (poly-term-order self)))
340 self)
[2777]341
[2800]342(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]343 &optional (reverse-arg-order-P nil))
[2799]344 "Multiplies term TERM by a list of term, TERMLIST.
[2792]345Takes into accound divisors of zero in the ring, by
[2927]346deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]347is T, change the order of arguments; this may be important
[2927]348if we extend the package to non-commutative rings."
[2800]349 `(mapcan #'(lambda (other-term)
[3633]350 (let ((prod (multiply
[2923]351 ,@(cond
[2930]352 (reverse-arg-order-p
[2925]353 `(other-term ,term))
354 (t
355 `(,term other-term))))))
[2800]356 (cond
[3633]357 ((universal-zerop prod) nil)
[2800]358 (t (list prod)))))
359 ,termlist))
[2790]360
[2796]361(defun multiply-termlists (p q order-fn)
[3127]362 "A version of polynomial multiplication, operating
363directly on termlists."
[2787]364 (cond
[2917]365 ((or (endp p) (endp q))
366 ;;p or q is 0 (represented by NIL)
367 nil)
[2789]368 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]369 ((endp (cdr p))
[2918]370 (multiply-term-by-termlist-dropping-zeros (car p) q))
371 ((endp (cdr q))
[2919]372 (multiply-term-by-termlist-dropping-zeros (car q) p t))
373 (t
[3633]374 (cons (multiply (car p) (car q))
[2949]375 (add-termlists
376 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
377 (multiply-termlists (cdr p) q order-fn)
378 order-fn)))))
[2793]379
[2803]380(defmethod multiply-by ((self poly) (other poly))
[3014]381 (change-term-order other self)
[2803]382 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
383 (poly-termlist other)
384 (poly-term-order self)))
385 self)
386
[3804]387(defgeneric add-2 (object1 object2)
388 (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
[3813]389 (:method ((object1 t) (object2 t))
[3804]390 (add-to (copy-instance object1) (copy-instance object2))))
[3374]391
[3803]392(defun add (&rest summands)
393 "Non-destructively adds list SUMMANDS."
394 (cond ((endp summands) 0)
[3818]395 (t (reduce #'add-2 summands))))
[3803]396
[3634]397(defun subtract (minuend &rest subtrahends)
[3427]398 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3851]399 (cond ((endp subtrahends) (unary-minus minuend))
400 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
[3374]401
[3062]402(defmethod left-tensor-product-by ((self poly) (other monom))
403 (setf (poly-termlist self)
404 (mapcan #'(lambda (term)
405 (let ((prod (left-tensor-product-by term other)))
406 (cond
[3640]407 ((universal-zerop prod) nil)
[3062]408 (t (list prod)))))
409 (poly-termlist self)))
[3249]410 (incf (poly-dimension self) (monom-dimension other))
[3062]411 self)
[3044]412
[3062]413(defmethod right-tensor-product-by ((self poly) (other monom))
414 (setf (poly-termlist self)
415 (mapcan #'(lambda (term)
416 (let ((prod (right-tensor-product-by term other)))
417 (cond
[3640]418 ((universal-zerop prod) nil)
[3062]419 (t (list prod)))))
420 (poly-termlist self)))
[3249]421 (incf (poly-dimension self) (monom-dimension other))
[3062]422 self)
423
424
[3084]425(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]426 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]427is a list of polynomials. Destructively modifies PLIST elements."
[3061]428 (mapc #'(lambda (poly)
[3085]429 (left-tensor-product-by
430 poly
431 (prog1
432 (make-monom-variable k i)
433 (incf i))))
[3061]434 plist))
[52]435
[3087]436(defun standard-extension-1 (plist
437 &aux
[3096]438 (plist (standard-extension plist))
[3087]439 (nvars (poly-dimension (car plist))))
[3081]440 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]441Firstly, new K variables U1, U2, ..., UK, are inserted into each
442polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]443tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]444polynomials have the same dimension, and only the first polynomial
445is examined to determine this dimension."
[3089]446 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
447 ;; 1 from each polynomial; since UI*PI has no constant term,
448 ;; we just need to append the constant term at the end
449 ;; of each termlist.
[3064]450 (flet ((subtract-1 (p)
[3641]451 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3083]452 (setf plist (mapc #'subtract-1 plist)))
[3077]453 plist)
[52]454
455
[3107]456(defun standard-sum (plist
457 &aux
458 (plist (standard-extension plist))
459 (nvars (poly-dimension (car plist))))
[3087]460 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
461Firstly, new K variables, U1, U2, ..., UK, are inserted into each
462polynomial. Subsequently, P1, P2, ..., PK are destructively modified
463tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]464are added. Finally, 1 is subtracted. It should be noted that the term
465order is not modified, which is equivalent to using a lexicographic
466order on the first K variables."
[3107]467 (flet ((subtract-1 (p)
[3641]468 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3108]469 (subtract-1
470 (make-instance
471 'poly
[3115]472 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]473
[3653]474(defgeneric universal-ezgcd (x y)
475 (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
476C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
477the Euclidean algorithm.")
478 (:method ((x integer) (y integer)
479 &aux (c (gcd x y)))
480 (values c (/ x c) (/ y c)))
481 )
482
[3655]483(defgeneric s-polynomial (object1 object2)
[3651]484 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
485 (:method ((f poly) (g poly))
486 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
487 (mf (divide lcm (leading-monomial f)))
488 (mg (divide lcm (leading-monomial g))))
489 (multiple-value-bind (c cf cg)
[3652]490 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
[3651]491 (declare (ignore c))
492 (subtract
[3673]493 (multiply f (change-class mf 'term :coeff cg))
494 (multiply g (change-class mg 'term :coeff cf)))))))
[3651]495
[3676]496(defgeneric poly-content (object)
497 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
[3677]498 (:method ((self poly))
499 (reduce #'universal-gcd
[3679]500 (mapcar #'term-coeff (rest (poly-termlist self)))
501 :initial-value (leading-coefficient self))))
[3676]502
[3684]503(defun poly-primitive-part (object)
[3685]504 "Divide polynomial OBJECT by gcd of its
[3684]505coefficients. Return the resulting polynomial."
[3688]506 (scalar-divide-by object (poly-content object)))
[3682]507
[3700]508(defun poly-insert-variables (self k)
[3697]509 (left-tensor-product-by self (make-instance 'monom :dimension k)))
510
[3698]511(defun saturation-extension (f plist &aux (k (length plist)))
[3708]512 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
513PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
[3711]514as first K variables. It destructively modifies F and PLIST."
[3700]515 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3699]516 (standard-extension-1 plist)))
[3694]517
[3699]518(defun polysaturation-extension (f plist &aux (k (length plist)))
[3708]519 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
520and F' is F with variables U1,U2,...,UK inserted as first K
[3711]521variables. It destructively modifies F and PLIST."
[3700]522 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3703]523 (list (standard-sum plist))))
[3694]524
[3691]525(defun saturation-extension-1 (f p)
[3712]526 "Given family of polynomials F and a polynomial P, calculate [F',
527U*P-1], where F' is F with variable inserted as the first variable. It
528destructively modifies F and P."
[3693]529 (polysaturation-extension f (list p)))
[3713]530
[3717]531(defmethod multiply-by ((object1 number) (object2 poly))
[3720]532 (scalar-multiply-by (copy-instance object2) object1))
[3716]533
[3781]534(defun make-poly-variable (nvars pos &optional (power 1))
535 (change-class (make-monom-variable nvars pos power) 'poly))
[3736]536
[3821]537(defun make-poly-constant (nvars coeff)
538 (change-class (make-term-constant nvars coeff) 'poly))
539
[3713]540(defgeneric universal-expt (x y)
[3721]541 (:documentation "Raises X to power Y.")
[3713]542 (:method ((x number) (y integer)) (expt x y))
543 (:method ((x t) (y integer))
544 (declare (type fixnum y))
545 (cond
546 ((minusp y) (error "universal-expt: Negative exponent."))
547 ((universal-zerop x) (if (zerop y) 1))
548 (t
549 (do ((k 1 (ash k 1))
550 (q x (multiply q q)) ;keep squaring
551 (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
552 ((> k y) p)
[3778]553 (declare (fixnum k)))))))
554
555(defgeneric poly-p (object)
556 (:documentation "Checks if an object is a polynomial.")
[3779]557 (:method ((self poly)) t)
[3778]558 (:method ((self t)) nil))
[3830]559
560(defmethod ->infix ((self poly) &optional vars)
561 (cons '+ (mapcar #'(lambda (x) (->infix x vars))
562 (poly-termlist self))))
563
[3899]564
565(defun poly-eval (expr vars order)
566 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
567variables VARS. Return the resulting polynomial or list of
568polynomials. Standard arithmetical operators in form EXPR are
569replaced with their analogues in the ring of polynomials, and the
570resulting expression is evaluated, resulting in a polynomial or a list
571of polynomials in internal form. A similar operation in another computer
572algebra system could be called 'expand' or so."
573 (labels ((p-eval (p) (poly-eval p vars order))
574 (p-eval-scalar (p) (poly-eval p '() order))
575 (p-eval-list (plist) (mapcar #'p-eval plist)))
576 (cond
577 ((eq expr 0)
578 (make-instance 'poly :dimension (length vars)))
579 ((member expr vars :test #'equalp)
580 (let ((pos (position expr vars :test #'equalp)))
581 (make-poly-variable (length vars) pos)))
582 ((atom expr)
583 expr)
584 ((eq (car expr) +list-marker+)
585 (cons +list-marker+ (p-eval-list (cdr expr))))
586 (t
587 (case (car expr)
588 (+ (reduce #'add (p-eval-list (cdr expr))))
589 (- (apply #'subtract (p-eval-list (cdr expr))))
590 (*
591 (if (endp (cddr expr)) ;unary
592 (p-eval (cadr expr))
593 (reduce #'multiply (p-eval-list (cdr expr)))))
594 (/
595 ;; A polynomial can be divided by a scalar
596 (cond
597 ((endp (cddr expr))
598 ;; A special case (/ ?), the inverse
599 (divide (cadr expr)))
600 (t
601 (let ((num (p-eval (cadr expr)))
602 (denom-inverse (apply #'divide (mapcar #'p-eval-scalar (cddr expr)))))
603 (multiply denom-inverse num)))))
604 (expt
605 (cond
606 ((member (cadr expr) vars :test #'equalp)
607 ;;Special handling of (expt var pow)
608 (let ((pos (position (cadr expr) vars :test #'equalp)))
609 (make-poly-variable (length vars) pos (caddr expr))))
610 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
611 ;; Negative power means division in coefficient ring
612 ;; Non-integer power means non-polynomial coefficient
613 expr)
614 (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
615 (otherwise
616 expr))))))
Note: See TracBrowser for help on using the repository browser.