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

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

* empty log message *

File size: 23.0 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 "+LIST-MARKER+"
59 "POLY-EVAL")
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."))
63
64(in-package :polynomial)
65
66(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
67
68(defclass poly ()
69 ((dimension :initform nil
70 :initarg :dimension
71 :accessor poly-dimension
72 :documentation "Shared dimension of all terms, the number of variables")
73 (termlist :initform nil :initarg :termlist :accessor poly-termlist
74 :documentation "List of terms.")
75 (order :initform #'lex> :initarg :order :accessor poly-term-order
76 :documentation "Monomial/term order."))
77 (:default-initargs :dimension nil :termlist nil :order #'lex>)
78 (:documentation "A polynomial with a list of terms TERMLIST, ordered
79according to term order ORDER, which defaults to LEX>."))
80
81(defmethod print-object ((self poly) stream)
82 (print-unreadable-object (self stream :type t :identity t)
83 (with-accessors ((dimension poly-dimension)
84 (termlist poly-termlist)
85 (order poly-term-order))
86 self
87 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
88 dimension termlist order))))
89
90(defgeneric change-term-order (self other)
91 (:documentation "Change term order of SELF to the term order of OTHER.")
92 (:method ((self poly) (other poly))
93 (unless (eq (poly-term-order self) (poly-term-order other))
94 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
95 (poly-term-order self) (poly-term-order other)))
96 self))
97
98(defgeneric poly-insert-term (self term)
99 (:documentation "Insert a term TERM into SELF before all other
100 terms. Order is not enforced.")
101 (:method ((self poly) (term term))
102 (cond ((null (poly-dimension self))
103 (setf (poly-dimension self) (monom-dimension term)))
104 (t (assert (= (poly-dimension self) (monom-dimension term)))))
105 (push term (poly-termlist self))
106 self))
107
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))
111 (cond ((null (poly-dimension self))
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))
115 self))
116
117(defun alist->poly (alist &aux (poly (make-instance 'poly)))
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."
128 (dolist (x alist poly)
129 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
130
131(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
132 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
133 (reinitialize-instance new
134 :dimension (monom-dimension old)
135 :termlist (list old)))
136
137(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
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)
141 :termlist (list (change-class old 'term))))
142
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))))
148
149(defgeneric leading-term (object)
150 (:method ((self poly))
151 (car (poly-termlist self)))
152 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
153
154(defgeneric second-leading-term (object)
155 (:method ((self poly))
156 (cadar (poly-termlist self)))
157 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
158
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
169(defgeneric leading-coefficient (object)
170 (:method ((self poly))
171 (term-coeff (leading-term self)))
172 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
173
174(defgeneric second-leading-coefficient (object)
175 (:method ((self poly))
176 (term-coeff (second-leading-term self)))
177 (:documentation "The second leading coefficient of a polynomial. It
178 signals error for a polynomial with at most one term."))
179
180(defmethod universal-zerop ((self poly))
181 "Return T iff SELF is a zero polynomial."
182 (null (poly-termlist self)))
183
184(defgeneric poly-length (self)
185 (:documentation "Return the number of terms.")
186 (:method ((self poly))
187 (length (poly-termlist self))))
188
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
203(defmethod multiply-by ((self poly) (other monom))
204 "Multiply a polynomial SELF by OTHER."
205 (mapc #'(lambda (term) (multiply-by term other))
206 (poly-termlist self))
207 self)
208
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
215(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
216 "Return an expression which will efficiently adds/subtracts two
217polynomials, P and Q. The addition/subtraction of coefficients is
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."
223 `(macrolet ((lc (x) `(term-coeff (car ,x))))
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?
230 (unless (endp q)
231 ;; Upon subtraction, we must change the sign of
232 ;; all coefficients in q
233 ,@(when uminus-fn
234 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
235 (setf r (nreconc r q)))
236 (unless (endp p)
237 (setf r (nreconc r p)))
238 r)
239 (multiple-value-bind
240 (greater-p equal-p)
241 (funcall ,order-fn (car p) (car q))
242 (cond
243 (greater-p
244 (rotatef (cdr p) r p)
245 )
246 (equal-p
247 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
248 (cond
249 ((universal-zerop s)
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
258 ;;Negate the term of Q if UMINUS provided, signallig
259 ;;that we are doing subtraction
260 ,(when uminus-fn
261 `(setf (lc q) (funcall ,uminus-fn (lc q))))
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 )))
267
268
269
270(defgeneric add-to (self other)
271 (:documentation "Add OTHER to SELF.")
272 (:method ((self number) (other number))
273 (+ self other))
274 (:method ((self poly) (other number))
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)))
278
279
280(defgeneric subtract-from (self other)
281 (:documentation "Subtract OTHER from SELF.")
282 (:method ((self number) (other number))
283 (- self other))
284 (:method ((self poly) (other number))
285 (subtract-from self (make-poly-constant (poly-dimension self) other))))
286
287#|
288(defmacro def-add/subtract-method (add/subtract-method-name
289 uminus-method-name
290 &optional
291 (doc-string nil doc-string-supplied-p))
292 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
293 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
294 ,@(when doc-string-supplied-p `(,doc-string))
295 ;; Ensure orders are compatible
296 (change-term-order other self)
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))))
302 self))
303|#
304
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
315(defun subtract-termlists (p q order-fn)
316 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
317 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
318
319(defmethod add-to ((self poly) (other poly))
320 "Adds to polynomial SELF another polynomial OTHER.
321This operation destructively modifies both polynomials.
322The result is stored in SELF. This implementation does
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)
327 (poly-term-order self)))
328 self)
329
330
331(defmethod subtract-from ((self poly) (other poly))
332 "Subtracts from polynomial SELF another polynomial OTHER.
333This operation destructively modifies both polynomials.
334The result is stored in SELF. This implementation does
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)
339 (poly-term-order self)))
340 self)
341
342(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
343 &optional (reverse-arg-order-P nil))
344 "Multiplies term TERM by a list of term, TERMLIST.
345Takes into accound divisors of zero in the ring, by
346deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
347is T, change the order of arguments; this may be important
348if we extend the package to non-commutative rings."
349 `(mapcan #'(lambda (other-term)
350 (let ((prod (multiply
351 ,@(cond
352 (reverse-arg-order-p
353 `(other-term ,term))
354 (t
355 `(,term other-term))))))
356 (cond
357 ((universal-zerop prod) nil)
358 (t (list prod)))))
359 ,termlist))
360
361(defun multiply-termlists (p q order-fn)
362 "A version of polynomial multiplication, operating
363directly on termlists."
364 (cond
365 ((or (endp p) (endp q))
366 ;;p or q is 0 (represented by NIL)
367 nil)
368 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
369 ((endp (cdr p))
370 (multiply-term-by-termlist-dropping-zeros (car p) q))
371 ((endp (cdr q))
372 (multiply-term-by-termlist-dropping-zeros (car q) p t))
373 (t
374 (cons (multiply (car p) (car q))
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)))))
379
380(defmethod multiply-by ((self poly) (other poly))
381 (change-term-order other self)
382 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
383 (poly-termlist other)
384 (poly-term-order self)))
385 self)
386
387(defgeneric add-2 (object1 object2)
388 (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
389 (:method ((object1 t) (object2 t))
390 (add-to (copy-instance object1) (copy-instance object2))))
391
392(defun add (&rest summands)
393 "Non-destructively adds list SUMMANDS."
394 (cond ((endp summands) 0)
395 (t (reduce #'add-2 summands))))
396
397(defun subtract (minuend &rest subtrahends)
398 "Non-destructively subtract MINUEND and SUBTRAHENDS."
399 (cond ((endp subtrahends) (unary-minus minuend))
400 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
401
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
407 ((universal-zerop prod) nil)
408 (t (list prod)))))
409 (poly-termlist self)))
410 (incf (poly-dimension self) (monom-dimension other))
411 self)
412
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
418 ((universal-zerop prod) nil)
419 (t (list prod)))))
420 (poly-termlist self)))
421 (incf (poly-dimension self) (monom-dimension other))
422 self)
423
424
425(defun standard-extension (plist &aux (k (length plist)) (i 0))
426 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
427is a list of polynomials. Destructively modifies PLIST elements."
428 (mapc #'(lambda (poly)
429 (left-tensor-product-by
430 poly
431 (prog1
432 (make-monom-variable k i)
433 (incf i))))
434 plist))
435
436(defun standard-extension-1 (plist
437 &aux
438 (plist (standard-extension plist))
439 (nvars (poly-dimension (car plist))))
440 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
441Firstly, new K variables U1, U2, ..., UK, are inserted into each
442polynomial. Subsequently, P1, P2, ..., PK are destructively modified
443tantamount to replacing PI with UI*PI-1. It assumes that all
444polynomials have the same dimension, and only the first polynomial
445is examined to determine this dimension."
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.
450 (flet ((subtract-1 (p)
451 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
452 (setf plist (mapc #'subtract-1 plist)))
453 plist)
454
455
456(defun standard-sum (plist
457 &aux
458 (plist (standard-extension plist))
459 (nvars (poly-dimension (car plist))))
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
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."
467 (flet ((subtract-1 (p)
468 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
469 (subtract-1
470 (make-instance
471 'poly
472 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
473
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
483(defgeneric s-polynomial (object1 object2)
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)
490 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
491 (declare (ignore c))
492 (subtract
493 (multiply f (change-class mf 'term :coeff cg))
494 (multiply g (change-class mg 'term :coeff cf)))))))
495
496(defgeneric poly-content (object)
497 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
498 (:method ((self poly))
499 (reduce #'universal-gcd
500 (mapcar #'term-coeff (rest (poly-termlist self)))
501 :initial-value (leading-coefficient self))))
502
503(defun poly-primitive-part (object)
504 "Divide polynomial OBJECT by gcd of its
505coefficients. Return the resulting polynomial."
506 (scalar-divide-by object (poly-content object)))
507
508(defun poly-insert-variables (self k)
509 (left-tensor-product-by self (make-instance 'monom :dimension k)))
510
511(defun saturation-extension (f plist &aux (k (length plist)))
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
514as first K variables. It destructively modifies F and PLIST."
515 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
516 (standard-extension-1 plist)))
517
518(defun polysaturation-extension (f plist &aux (k (length plist)))
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
521variables. It destructively modifies F and PLIST."
522 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
523 (list (standard-sum plist))))
524
525(defun saturation-extension-1 (f p)
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."
529 (polysaturation-extension f (list p)))
530
531(defmethod multiply-by ((object1 number) (object2 poly))
532 (scalar-multiply-by (copy-instance object2) object1))
533
534(defun make-poly-variable (nvars pos &optional (power 1))
535 (change-class (make-monom-variable nvars pos power) 'poly))
536
537(defun make-poly-constant (nvars coeff)
538 (change-class (make-term-constant nvars coeff) 'poly))
539
540(defgeneric universal-expt (x y)
541 (:documentation "Raises X to power Y.")
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)
553 (declare (fixnum k)))))))
554
555(defgeneric poly-p (object)
556 (:documentation "Checks if an object is a polynomial.")
557 (:method ((self poly)) t)
558 (:method ((self t)) nil))
559
560(defmethod ->infix :before ((self poly) &optional vars)
561 "Ensures that the number of variables in VARS maches the polynomial dimension of the
562polynomial SELF."
563 (assert (= (length vars) (poly-dimension self))))
564
565(defmethod ->infix ((self poly) &optional vars)
566 "Converts a polynomial SELF to a sexp."
567 (cons '+ (mapcar #'(lambda (x) (->infix x vars))
568 (poly-termlist self))))
569
570(defparameter +list-marker+ :[
571 "A sexp with this head is considered a list of polynomials.")
572
573(defmethod ->infix ((self cons) &optional vars)
574 (assert (eql (car self) +list-marker+))
575 (cons '+ (mapcar #'(lambda (p) (->infix p vars)) (cdr self))))
576
577
578(defun poly-eval (expr vars order)
579 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
580variables VARS. Return the resulting polynomial or list of
581polynomials. Standard arithmetical operators in form EXPR are
582replaced with their analogues in the ring of polynomials, and the
583resulting expression is evaluated, resulting in a polynomial or a list
584of polynomials in internal form. A similar operation in another computer
585algebra system could be called 'expand' or so."
586 (labels ((p-eval (p) (poly-eval p vars order))
587 (p-eval-scalar (p) (poly-eval p '() order))
588 (p-eval-list (plist) (mapcar #'p-eval plist)))
589 (cond
590 ((eq expr 0)
591 (make-instance 'poly :dimension (length vars)))
592 ((member expr vars :test #'equalp)
593 (let ((pos (position expr vars :test #'equalp)))
594 (make-poly-variable (length vars) pos)))
595 ((atom expr)
596 expr)
597 ((eq (car expr) +list-marker+)
598 (cons +list-marker+ (p-eval-list (cdr expr))))
599 (t
600 (case (car expr)
601 (+ (reduce #'add (p-eval-list (cdr expr))))
602 (- (apply #'subtract (p-eval-list (cdr expr))))
603 (*
604 (if (endp (cddr expr)) ;unary
605 (p-eval (cadr expr))
606 (reduce #'multiply (p-eval-list (cdr expr)))))
607 (/
608 ;; A polynomial can be divided by a scalar
609 (cond
610 ((endp (cddr expr))
611 ;; A special case (/ ?), the inverse
612 (divide (cadr expr)))
613 (t
614 (let ((num (p-eval (cadr expr)))
615 (denom-inverse (apply #'divide (mapcar #'p-eval-scalar (cddr expr)))))
616 (multiply denom-inverse num)))))
617 (expt
618 (cond
619 ((member (cadr expr) vars :test #'equalp)
620 ;;Special handling of (expt var pow)
621 (let ((pos (position (cadr expr) vars :test #'equalp)))
622 (make-poly-variable (length vars) pos (caddr expr))))
623 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
624 ;; Negative power means division in coefficient ring
625 ;; Non-integer power means non-polynomial coefficient
626 expr)
627 (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
628 (otherwise
629 expr))))))
Note: See TracBrowser for help on using the repository browser.