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, 8 years ago

* empty log message *

File size: 22.5 KB
Line 
1;;----------------------------------------------------------------
2;;; -*- Mode: Lisp -*-
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
23(defpackage "POLYNOMIAL"
24 (:use :cl :utils :monom :copy)
25 (:export "POLY"
26 "POLY-DIMENSION"
27 "POLY-TERMLIST"
28 "POLY-TERM-ORDER"
29 "POLY-INSERT-TERM"
30 "SCALAR-MULTIPLY-BY"
31 "SCALAR-DIVIDE-BY"
32 "LEADING-TERM"
33 "LEADING-MONOMIAL"
34 "LEADING-COEFFICIENT"
35 "SECOND-LEADING-TERM"
36 "SECOND-LEADING-MONOMIAL"
37 "SECOND-LEADING-COEFFICIENT"
38 "ADD-TO"
39 "ADD"
40 "SUBTRACT-FROM"
41 "SUBTRACT"
42 "CHANGE-TERM-ORDER"
43 "STANDARD-EXTENSION"
44 "STANDARD-EXTENSION-1"
45 "STANDARD-SUM"
46 "SATURATION-EXTENSION"
47 "ALIST->POLY"
48 "->INFIX"
49 "UNIVERSAL-EZGCD"
50 "S-POLYNOMIAL"
51 "POLY-CONTENT"
52 "POLY-PRIMITIVE-PART"
53 "SATURATION-EXTENSION-1"
54 "MAKE-POLY-VARIABLE"
55 "MAKE-POLY-CONSTANT"
56 "UNIVERSAL-EXPT"
57 "POLY-P"
58 "POLY-EVAL")
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."))
62
63(in-package :polynomial)
64
65(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
66
67(defclass poly ()
68 ((dimension :initform nil
69 :initarg :dimension
70 :accessor poly-dimension
71 :documentation "Shared dimension of all terms, the number of variables")
72 (termlist :initform nil :initarg :termlist :accessor poly-termlist
73 :documentation "List of terms.")
74 (order :initform #'lex> :initarg :order :accessor poly-term-order
75 :documentation "Monomial/term order."))
76 (:default-initargs :dimension nil :termlist nil :order #'lex>)
77 (:documentation "A polynomial with a list of terms TERMLIST, ordered
78according to term order ORDER, which defaults to LEX>."))
79
80(defmethod print-object ((self poly) stream)
81 (print-unreadable-object (self stream :type t :identity t)
82 (with-accessors ((dimension poly-dimension)
83 (termlist poly-termlist)
84 (order poly-term-order))
85 self
86 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
87 dimension termlist order))))
88
89(defgeneric change-term-order (self other)
90 (:documentation "Change term order of SELF to the term order of OTHER.")
91 (:method ((self poly) (other poly))
92 (unless (eq (poly-term-order self) (poly-term-order other))
93 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
94 (poly-term-order self) (poly-term-order other)))
95 self))
96
97(defgeneric poly-insert-term (self term)
98 (:documentation "Insert a term TERM into SELF before all other
99 terms. Order is not enforced.")
100 (:method ((self poly) (term term))
101 (cond ((null (poly-dimension self))
102 (setf (poly-dimension self) (monom-dimension term)))
103 (t (assert (= (poly-dimension self) (monom-dimension term)))))
104 (push term (poly-termlist self))
105 self))
106
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))
110 (cond ((null (poly-dimension self))
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))
114 self))
115
116(defun alist->poly (alist &aux (poly (make-instance 'poly)))
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."
127 (dolist (x alist poly)
128 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
129
130(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
131 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
132 (reinitialize-instance new
133 :dimension (monom-dimension old)
134 :termlist (list old)))
135
136(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
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)
140 :termlist (list (change-class old 'term))))
141
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))))
147
148(defgeneric leading-term (object)
149 (:method ((self poly))
150 (car (poly-termlist self)))
151 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
152
153(defgeneric second-leading-term (object)
154 (:method ((self poly))
155 (cadar (poly-termlist self)))
156 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
157
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
168(defgeneric leading-coefficient (object)
169 (:method ((self poly))
170 (term-coeff (leading-term self)))
171 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
172
173(defgeneric second-leading-coefficient (object)
174 (:method ((self poly))
175 (term-coeff (second-leading-term self)))
176 (:documentation "The second leading coefficient of a polynomial. It
177 signals error for a polynomial with at most one term."))
178
179(defmethod universal-zerop ((self poly))
180 "Return T iff SELF is a zero polynomial."
181 (null (poly-termlist self)))
182
183(defgeneric poly-length (self)
184 (:documentation "Return the number of terms.")
185 (:method ((self poly))
186 (length (poly-termlist self))))
187
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
202(defmethod multiply-by ((self poly) (other monom))
203 "Multiply a polynomial SELF by OTHER."
204 (mapc #'(lambda (term) (multiply-by term other))
205 (poly-termlist self))
206 self)
207
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
214(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
215 "Return an expression which will efficiently adds/subtracts two
216polynomials, P and Q. The addition/subtraction of coefficients is
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."
222 `(macrolet ((lc (x) `(term-coeff (car ,x))))
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?
229 (unless (endp q)
230 ;; Upon subtraction, we must change the sign of
231 ;; all coefficients in q
232 ,@(when uminus-fn
233 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
234 (setf r (nreconc r q)))
235 (unless (endp p)
236 (setf r (nreconc r p)))
237 r)
238 (multiple-value-bind
239 (greater-p equal-p)
240 (funcall ,order-fn (car p) (car q))
241 (cond
242 (greater-p
243 (rotatef (cdr p) r p)
244 )
245 (equal-p
246 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
247 (cond
248 ((universal-zerop s)
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
257 ;;Negate the term of Q if UMINUS provided, signallig
258 ;;that we are doing subtraction
259 ,(when uminus-fn
260 `(setf (lc q) (funcall ,uminus-fn (lc q))))
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 )))
266
267
268
269(defgeneric add-to (self other)
270 (:documentation "Add OTHER to SELF.")
271 (:method ((self number) (other number))
272 (+ self other))
273 (:method ((self poly) (other number))
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)))
277
278
279(defgeneric subtract-from (self other)
280 (:documentation "Subtract OTHER from SELF.")
281 (:method ((self number) (other number))
282 (- self other))
283 (:method ((self poly) (other number))
284 (subtract-from self (make-poly-constant (poly-dimension self) other))))
285
286#|
287(defmacro def-add/subtract-method (add/subtract-method-name
288 uminus-method-name
289 &optional
290 (doc-string nil doc-string-supplied-p))
291 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
292 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
293 ,@(when doc-string-supplied-p `(,doc-string))
294 ;; Ensure orders are compatible
295 (change-term-order other self)
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))))
301 self))
302|#
303
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
314(defun subtract-termlists (p q order-fn)
315 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
316 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
317
318(defmethod add-to ((self poly) (other poly))
319 "Adds to polynomial SELF another polynomial OTHER.
320This operation destructively modifies both polynomials.
321The result is stored in SELF. This implementation does
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)
326 (poly-term-order self)))
327 self)
328
329
330(defmethod subtract-from ((self poly) (other poly))
331 "Subtracts from polynomial SELF another polynomial OTHER.
332This operation destructively modifies both polynomials.
333The result is stored in SELF. This implementation does
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)
338 (poly-term-order self)))
339 self)
340
341(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
342 &optional (reverse-arg-order-P nil))
343 "Multiplies term TERM by a list of term, TERMLIST.
344Takes into accound divisors of zero in the ring, by
345deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
346is T, change the order of arguments; this may be important
347if we extend the package to non-commutative rings."
348 `(mapcan #'(lambda (other-term)
349 (let ((prod (multiply
350 ,@(cond
351 (reverse-arg-order-p
352 `(other-term ,term))
353 (t
354 `(,term other-term))))))
355 (cond
356 ((universal-zerop prod) nil)
357 (t (list prod)))))
358 ,termlist))
359
360(defun multiply-termlists (p q order-fn)
361 "A version of polynomial multiplication, operating
362directly on termlists."
363 (cond
364 ((or (endp p) (endp q))
365 ;;p or q is 0 (represented by NIL)
366 nil)
367 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
368 ((endp (cdr p))
369 (multiply-term-by-termlist-dropping-zeros (car p) q))
370 ((endp (cdr q))
371 (multiply-term-by-termlist-dropping-zeros (car q) p t))
372 (t
373 (cons (multiply (car p) (car q))
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)))))
378
379(defmethod multiply-by ((self poly) (other poly))
380 (change-term-order other self)
381 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
382 (poly-termlist other)
383 (poly-term-order self)))
384 self)
385
386(defgeneric add-2 (object1 object2)
387 (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
388 (:method ((object1 t) (object2 t))
389 (add-to (copy-instance object1) (copy-instance object2))))
390
391(defun add (&rest summands)
392 "Non-destructively adds list SUMMANDS."
393 (cond ((endp summands) 0)
394 (t (reduce #'add-2 summands))))
395
396(defun subtract (minuend &rest subtrahends)
397 "Non-destructively subtract MINUEND and SUBTRAHENDS."
398 (cond ((endp subtrahends) (unary-minus minuend))
399 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
400
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
406 ((universal-zerop prod) nil)
407 (t (list prod)))))
408 (poly-termlist self)))
409 (incf (poly-dimension self) (monom-dimension other))
410 self)
411
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
417 ((universal-zerop prod) nil)
418 (t (list prod)))))
419 (poly-termlist self)))
420 (incf (poly-dimension self) (monom-dimension other))
421 self)
422
423
424(defun standard-extension (plist &aux (k (length plist)) (i 0))
425 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
426is a list of polynomials. Destructively modifies PLIST elements."
427 (mapc #'(lambda (poly)
428 (left-tensor-product-by
429 poly
430 (prog1
431 (make-monom-variable k i)
432 (incf i))))
433 plist))
434
435(defun standard-extension-1 (plist
436 &aux
437 (plist (standard-extension plist))
438 (nvars (poly-dimension (car plist))))
439 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
440Firstly, new K variables U1, U2, ..., UK, are inserted into each
441polynomial. Subsequently, P1, P2, ..., PK are destructively modified
442tantamount to replacing PI with UI*PI-1. It assumes that all
443polynomials have the same dimension, and only the first polynomial
444is examined to determine this dimension."
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.
449 (flet ((subtract-1 (p)
450 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
451 (setf plist (mapc #'subtract-1 plist)))
452 plist)
453
454
455(defun standard-sum (plist
456 &aux
457 (plist (standard-extension plist))
458 (nvars (poly-dimension (car plist))))
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
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."
466 (flet ((subtract-1 (p)
467 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
468 (subtract-1
469 (make-instance
470 'poly
471 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
472
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
482(defgeneric s-polynomial (object1 object2)
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)
489 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
490 (declare (ignore c))
491 (subtract
492 (multiply f (change-class mf 'term :coeff cg))
493 (multiply g (change-class mg 'term :coeff cf)))))))
494
495(defgeneric poly-content (object)
496 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
497 (:method ((self poly))
498 (reduce #'universal-gcd
499 (mapcar #'term-coeff (rest (poly-termlist self)))
500 :initial-value (leading-coefficient self))))
501
502(defun poly-primitive-part (object)
503 "Divide polynomial OBJECT by gcd of its
504coefficients. Return the resulting polynomial."
505 (scalar-divide-by object (poly-content object)))
506
507(defun poly-insert-variables (self k)
508 (left-tensor-product-by self (make-instance 'monom :dimension k)))
509
510(defun saturation-extension (f plist &aux (k (length plist)))
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
513as first K variables. It destructively modifies F and PLIST."
514 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
515 (standard-extension-1 plist)))
516
517(defun polysaturation-extension (f plist &aux (k (length plist)))
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
520variables. It destructively modifies F and PLIST."
521 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
522 (list (standard-sum plist))))
523
524(defun saturation-extension-1 (f p)
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."
528 (polysaturation-extension f (list p)))
529
530(defmethod multiply-by ((object1 number) (object2 poly))
531 (scalar-multiply-by (copy-instance object2) object1))
532
533(defun make-poly-variable (nvars pos &optional (power 1))
534 (change-class (make-monom-variable nvars pos power) 'poly))
535
536(defun make-poly-constant (nvars coeff)
537 (change-class (make-term-constant nvars coeff) 'poly))
538
539(defgeneric universal-expt (x y)
540 (:documentation "Raises X to power Y.")
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)
552 (declare (fixnum k)))))))
553
554(defgeneric poly-p (object)
555 (:documentation "Checks if an object is a polynomial.")
556 (:method ((self poly)) t)
557 (:method ((self t)) nil))
558
559(defmethod ->infix ((self poly) &optional vars)
560 (cons '+ (mapcar #'(lambda (x) (->infix x vars))
561 (poly-termlist self))))
562
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.