close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/polynomial.lisp@ 3909

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

* empty log message *

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