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

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

* empty log message *

File size: 25.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 "MAKE-ZERO-FOR"
57 "MAKE-UNIT-FOR"
58 "UNIVERSAL-EXPT"
59 "UNIVERSAL-EQUALP"
60 "POLY-LENGTH"
61 "POLY-REVERSE"
62 "POLY-P"
63 "+LIST-MARKER+"
64 "POLY-EVAL")
65 (:documentation "Implements polynomials. A polynomial is essentially
66a mapping of monomials of the same degree to coefficients. The
67momomials are ordered according to a monomial order."))
68
69(in-package :polynomial)
70
71(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
72
73(defclass poly ()
74 ((dimension :initform nil
75 :initarg :dimension
76 :accessor poly-dimension
77 :documentation "Shared dimension of all terms, the number of variables")
78 (termlist :initform nil :initarg :termlist :accessor poly-termlist
79 :documentation "List of terms.")
80 (order :initform #'lex> :initarg :order :accessor poly-term-order
81 :documentation "Monomial/term order."))
82 (:default-initargs :dimension nil :termlist nil :order #'lex>)
83 (:documentation "A polynomial with a list of terms TERMLIST, ordered
84according to term order ORDER, which defaults to LEX>."))
85
86(defmethod print-object ((self poly) stream)
87 (print-unreadable-object (self stream :type t :identity t)
88 (with-accessors ((dimension poly-dimension)
89 (termlist poly-termlist)
90 (order poly-term-order))
91 self
92 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
93 dimension termlist order))))
94
95(defgeneric change-term-order (self other)
96 (:documentation "Change term order of SELF to the term order of OTHER.")
97 (:method ((self poly) (other poly))
98 (unless (eq (poly-term-order self) (poly-term-order other))
99 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
100 (poly-term-order self) (poly-term-order other)))
101 self))
102
103(defgeneric poly-insert-term (self term)
104 (:documentation "Insert a term TERM into SELF before all other
105 terms. Order is not enforced.")
106 (:method ((self poly) (term term))
107 (cond ((null (poly-dimension self))
108 (setf (poly-dimension self) (monom-dimension term)))
109 (t (assert (= (poly-dimension self) (monom-dimension term)))))
110 (push term (poly-termlist self))
111 self))
112
113(defgeneric poly-append-term (self term)
114 (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
115 (:method ((self poly) (term term))
116 (cond ((null (poly-dimension self))
117 (setf (poly-dimension self) (monom-dimension term)))
118 (t (assert (= (poly-dimension self) (monom-dimension term)))))
119 (setf (cdr (last (poly-termlist self))) (list term))
120 self))
121
122(defun alist->poly (alist &aux (poly (make-instance 'poly)))
123 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
124It can be used to enter simple polynomials by hand, e.g the polynomial
125in two variables, X and Y, given in standard notation as:
126
127 3*X^2*Y^3+2*Y+7
128
129can be entered as
130(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
131
132NOTE: The primary use is for low-level debugging of the package."
133 (dolist (x alist poly)
134 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
135
136(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
137 "Converts OLD of class TERM 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 old)))
141
142(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
143 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
144 (reinitialize-instance new
145 :dimension (monom-dimension old)
146 :termlist (list (change-class old 'term))))
147
148(defmethod universal-equalp ((self poly) (other poly))
149 "Implements equality of polynomials."
150 (and (eql (poly-dimension self) (poly-dimension other))
151 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
152 (eq (poly-term-order self) (poly-term-order other))))
153
154(defgeneric leading-term (object)
155 (:method ((self poly))
156 (car (poly-termlist self)))
157 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
158
159(defgeneric second-leading-term (object)
160 (:method ((self poly))
161 (cadar (poly-termlist self)))
162 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
163
164(defgeneric leading-monomial (object)
165 (:method ((self poly))
166 (change-class (copy-instance (leading-term self)) 'monom))
167 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
168
169(defgeneric second-leading-monomial (object)
170 (:method ((self poly))
171 (change-class (copy-instance (second-leading-term self)) 'monom))
172 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
173
174(defgeneric leading-coefficient (object)
175 (:method ((self poly))
176 (term-coeff (leading-term self)))
177 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
178
179(defgeneric second-leading-coefficient (object)
180 (:method ((self poly))
181 (term-coeff (second-leading-term self)))
182 (:documentation "The second leading coefficient of a polynomial. It
183 signals error for a polynomial with at most one term."))
184
185(defmethod universal-zerop ((self poly))
186 "Return T iff SELF is a zero polynomial."
187 (null (poly-termlist self)))
188
189(defgeneric poly-length (self)
190 (:documentation "Return the number of terms.")
191 (:method ((self poly))
192 (length (poly-termlist self))))
193
194(defgeneric scalar-multiply-by (self other)
195 (:documentation "Multiply vector SELF by a scalar OTHER.")
196 (:method ((self poly) other)
197 (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
198 (poly-termlist self))
199 self))
200
201(defgeneric scalar-divide-by (self other)
202 (:documentation "Divide vector SELF by a scalar OTHER.")
203 (:method ((self poly) other)
204 (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
205 (poly-termlist self))
206 self))
207
208(defmethod unary-inverse :before ((self poly))
209 "Checks invertibility of a polynomial SELF. To be invertable, the
210polynomial must be an invertible, constant polynomial."
211 (with-slots (termlist)
212 self
213 (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
214 nil
215 "To be invertible, the polynomial must have 1 term of total degree 0.")))
216
217(defmethod unary-inverse ((self poly))
218 "Returns the unary inverse of a polynomial SELF."
219 (with-slots (termlist)
220 self
221 (setf (car termlist) (unary-inverse (car termlist)))
222 self))
223
224(defmethod multiply-by ((self poly) (other monom))
225 "Multiply a polynomial SELF by OTHER."
226 (mapc #'(lambda (term) (multiply-by term other))
227 (poly-termlist self))
228 self)
229
230(defmethod multiply-by ((self poly) (other term))
231 "Multiply a polynomial SELF by OTHER."
232 (mapc #'(lambda (term) (multiply-by term other))
233 (poly-termlist self))
234 self)
235
236(defmethod multiply-by ((self monom) (other poly))
237 "Multiply a monomial SELF by polynomial OTHER."
238 (multiply-by other self))
239
240(defmethod multiply-by ((self term) (other poly))
241 "Multiply a term SELF by polynomial OTHER."
242 (multiply-by other self))
243
244(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
245 "Return an expression which will efficiently adds/subtracts two
246polynomials, P and Q. The addition/subtraction of coefficients is
247performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
248used to negate the coefficients of Q which do not have a corresponding
249coefficient in P. The code implements an efficient algorithm to add
250two polynomials represented as sorted lists of terms. The code
251destroys both arguments, reusing the terms to build the result."
252 `(macrolet ((lc (x) `(term-coeff (car ,x))))
253 (do ((p ,p)
254 (q ,q)
255 r)
256 ((or (endp p) (endp q))
257 ;; NOTE: R contains the result in reverse order. Can it
258 ;; be more efficient to produce the terms in correct order?
259 (unless (endp q)
260 ;; Upon subtraction, we must change the sign of
261 ;; all coefficients in q
262 ,@(when uminus-fn
263 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
264 (setf r (nreconc r q)))
265 (unless (endp p)
266 (setf r (nreconc r p)))
267 r)
268 (multiple-value-bind
269 (greater-p equal-p)
270 (funcall ,order-fn (car p) (car q))
271 (cond
272 (greater-p
273 (rotatef (cdr p) r p)
274 )
275 (equal-p
276 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
277 (cond
278 ((universal-zerop s)
279 (setf p (cdr p))
280 )
281 (t
282 (setf (lc p) s)
283 (rotatef (cdr p) r p))))
284 (setf q (cdr q))
285 )
286 (t
287 ;;Negate the term of Q if UMINUS provided, signallig
288 ;;that we are doing subtraction
289 ,(when uminus-fn
290 `(setf (lc q) (funcall ,uminus-fn (lc q))))
291 (rotatef (cdr q) r q))))
292 ;;(format t "P:~A~%" p)
293 ;;(format t "Q:~A~%" q)
294 ;;(format t "R:~A~%" r)
295 )))
296
297
298
299(defgeneric add-to (self other)
300 (:documentation "Add OTHER to SELF.")
301 (:method ((self number) (other number))
302 (+ self other))
303 (:method ((self poly) (other number))
304 (add-to self (make-poly-constant (poly-dimension self) other)))
305 (:method ((self number) (other poly))
306 (add-to (make-poly-constant (poly-dimension other) self) other)))
307
308
309(defgeneric subtract-from (self other)
310 (:documentation "Subtract OTHER from SELF.")
311 (:method ((self number) (other number))
312 (- self other))
313 (:method ((self poly) (other number))
314 (subtract-from self (make-poly-constant (poly-dimension self) other))))
315
316
317#|
318(defmacro def-add/subtract-method (add/subtract-method-name
319 uminus-method-name
320 &optional
321 (doc-string nil doc-string-supplied-p))
322 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
323 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
324 ,@(when doc-string-supplied-p `(,doc-string))
325 ;; Ensure orders are compatible
326 (change-term-order other self)
327 (setf (poly-termlist self) (fast-add/subtract
328 (poly-termlist self) (poly-termlist other)
329 (poly-term-order self)
330 #',add/subtract-method-name
331 ,(when uminus-method-name `(function ,uminus-method-name))))
332 self))
333
334(eval-when (:load-toplevel :execute)
335
336 (def-add/subtract-method add-to nil
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
342 (def-add/subtract-method subtract-from unary-minus
343 "Subtracts from polynomial SELF another polynomial OTHER.
344This operation destructively modifies both polynomials.
345The result is stored in SELF. This implementation does
346no consing, entirely reusing the sells of SELF and OTHER.")
347 )
348
349|#
350
351(defmethod unary-minus ((self poly))
352 "Destructively modifies the coefficients of the polynomial SELF,
353by changing their sign."
354 (mapc #'unary-minus (poly-termlist self))
355 self)
356
357(defun add-termlists (p q order-fn)
358 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
359 (fast-add/subtract p q order-fn #'add-to nil))
360
361(defun subtract-termlists (p q order-fn)
362 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
363 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
364
365(defmethod add-to ((self poly) (other poly))
366 "Adds to polynomial SELF another polynomial OTHER.
367This operation destructively modifies both polynomials.
368The result is stored in SELF. This implementation does
369no consing, entirely reusing the sells of SELF and OTHER."
370 (change-term-order other self)
371 (setf (poly-termlist self) (add-termlists
372 (poly-termlist self) (poly-termlist other)
373 (poly-term-order self)))
374 self)
375
376
377(defmethod subtract-from ((self poly) (other poly))
378 "Subtracts from polynomial SELF another polynomial OTHER.
379This operation destructively modifies both polynomials.
380The result is stored in SELF. This implementation does
381no consing, entirely reusing the sells of SELF and OTHER."
382 (change-term-order other self)
383 (setf (poly-termlist self) (subtract-termlists
384 (poly-termlist self) (poly-termlist other)
385 (poly-term-order self)))
386 self)
387
388
389(defmethod add-to ((self poly) (other term))
390 "Adds to a polynomial SELF a term OTHER."
391 (add-to self (change-class (copy-instance other) 'poly)))
392
393(defmethod subtract-from ((self poly) (other term))
394 "Subtracts from a polynomial SELF a term OTHER."
395 (subtract-from self (change-class (copy-instance other) 'poly)))
396
397
398(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
399 &optional (reverse-arg-order-P nil))
400 "Multiplies term TERM by a list of term, TERMLIST.
401Takes into accound divisors of zero in the ring, by
402deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
403is T, change the order of arguments; this may be important
404if we extend the package to non-commutative rings."
405 `(mapcan #'(lambda (other-term)
406 (let ((prod (multiply
407 ,@(cond
408 (reverse-arg-order-p
409 `(other-term ,term))
410 (t
411 `(,term other-term))))))
412 (cond
413 ((universal-zerop prod) nil)
414 (t (list prod)))))
415 ,termlist))
416
417(defun multiply-termlists (p q order-fn)
418 "A version of polynomial multiplication, operating
419directly on termlists."
420 (cond
421 ((or (endp p) (endp q))
422 ;;p or q is 0 (represented by NIL)
423 nil)
424 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
425 ((endp (cdr p))
426 (multiply-term-by-termlist-dropping-zeros (car p) q))
427 ((endp (cdr q))
428 (multiply-term-by-termlist-dropping-zeros (car q) p t))
429 (t
430 (cons (multiply (car p) (car q))
431 (add-termlists
432 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
433 (multiply-termlists (cdr p) q order-fn)
434 order-fn)))))
435
436(defmethod multiply-by ((self poly) (other poly))
437 (change-term-order other self)
438 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
439 (poly-termlist other)
440 (poly-term-order self)))
441 self)
442
443(defgeneric add-2 (object1 object2)
444 (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
445 (:method ((object1 t) (object2 t))
446 (add-to (copy-instance object1) (copy-instance object2))))
447
448(defun add (&rest summands)
449 "Non-destructively adds list SUMMANDS."
450 (cond ((endp summands) 0)
451 (t (reduce #'add-2 summands))))
452
453(defun subtract (minuend &rest subtrahends)
454 "Non-destructively subtract MINUEND and SUBTRAHENDS."
455 (cond ((endp subtrahends) (unary-minus minuend))
456 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
457
458(defmethod left-tensor-product-by ((self poly) (other monom))
459 (setf (poly-termlist self)
460 (mapcan #'(lambda (term)
461 (let ((prod (left-tensor-product-by term other)))
462 (cond
463 ((universal-zerop prod) nil)
464 (t (list prod)))))
465 (poly-termlist self)))
466 (incf (poly-dimension self) (monom-dimension other))
467 self)
468
469(defmethod right-tensor-product-by ((self poly) (other monom))
470 (setf (poly-termlist self)
471 (mapcan #'(lambda (term)
472 (let ((prod (right-tensor-product-by term other)))
473 (cond
474 ((universal-zerop prod) nil)
475 (t (list prod)))))
476 (poly-termlist self)))
477 (incf (poly-dimension self) (monom-dimension other))
478 self)
479
480
481(defun standard-extension (plist &aux (k (length plist)) (i 0))
482 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
483is a list of polynomials. Destructively modifies PLIST elements."
484 (mapc #'(lambda (poly)
485 (left-tensor-product-by
486 poly
487 (prog1
488 (make-monom-variable k i)
489 (incf i))))
490 plist))
491
492(defun standard-extension-1 (plist
493 &aux
494 (plist (standard-extension plist))
495 (nvars (poly-dimension (car plist))))
496 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
497Firstly, new K variables U1, U2, ..., UK, are inserted into each
498polynomial. Subsequently, P1, P2, ..., PK are destructively modified
499tantamount to replacing PI with UI*PI-1. It assumes that all
500polynomials have the same dimension, and only the first polynomial
501is examined to determine this dimension."
502 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
503 ;; 1 from each polynomial; since UI*PI has no constant term,
504 ;; we just need to append the constant term at the end
505 ;; of each termlist.
506 (flet ((subtract-1 (p)
507 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
508 (setf plist (mapc #'subtract-1 plist)))
509 plist)
510
511
512(defun standard-sum (plist
513 &aux
514 (plist (standard-extension plist))
515 (nvars (poly-dimension (car plist))))
516 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
517Firstly, new K variables, U1, U2, ..., UK, are inserted into each
518polynomial. Subsequently, P1, P2, ..., PK are destructively modified
519tantamount to replacing PI with UI*PI, and the resulting polynomials
520are added. Finally, 1 is subtracted. It should be noted that the term
521order is not modified, which is equivalent to using a lexicographic
522order on the first K variables."
523 (flet ((subtract-1 (p)
524 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
525 (subtract-1
526 (make-instance
527 'poly
528 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
529
530(defgeneric universal-ezgcd (x y)
531 (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
532C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
533the Euclidean algorithm.")
534 (:method ((x integer) (y integer)
535 &aux (c (gcd x y)))
536 (values c (/ x c) (/ y c)))
537 )
538
539(defgeneric s-polynomial (object1 object2)
540 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
541 (:method ((f poly) (g poly))
542 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
543 (mf (divide lcm (leading-monomial f)))
544 (mg (divide lcm (leading-monomial g))))
545 (multiple-value-bind (c cf cg)
546 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
547 (declare (ignore c))
548 (subtract
549 (multiply-by f (change-class mf 'term :coeff cg))
550 (multiply-by g (change-class mg 'term :coeff cf)))))))
551
552(defgeneric poly-content (object)
553 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
554 (:method ((self poly))
555 (reduce #'universal-gcd
556 (mapcar #'term-coeff (rest (poly-termlist self)))
557 :initial-value (leading-coefficient self))))
558
559(defun poly-primitive-part (object)
560 "Divide polynomial OBJECT by gcd of its
561coefficients. Return the resulting polynomial."
562 (scalar-divide-by object (poly-content object)))
563
564(defun poly-insert-variables (self k)
565 (left-tensor-product-by self (make-instance 'monom :dimension k)))
566
567(defun saturation-extension (f plist &aux (k (length plist)))
568 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
569PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
570as first K variables. It destructively modifies F and PLIST."
571 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
572 (standard-extension-1 plist)))
573
574(defun polysaturation-extension (f plist &aux (k (length plist)))
575 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
576and F' is F with variables U1,U2,...,UK inserted as first K
577variables. It destructively modifies F and PLIST."
578 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
579 (list (standard-sum plist))))
580
581(defun saturation-extension-1 (f p)
582 "Given family of polynomials F and a polynomial P, calculate [F',
583U*P-1], where F' is F with variable inserted as the first variable. It
584destructively modifies F and P."
585 (polysaturation-extension f (list p)))
586
587(defmethod multiply-by ((object1 number) (object2 poly))
588 (scalar-multiply-by (copy-instance object2) object1))
589
590(defmethod multiply-by ((object1 poly) (object2 number))
591 (scalar-multiply-by (copy-instance object1) object2))
592
593(defun make-poly-variable (nvars pos &optional (power 1))
594 (change-class (make-monom-variable nvars pos power) 'poly))
595
596(defun make-poly-constant (nvars coeff)
597 (change-class (make-term-constant nvars coeff) 'poly))
598
599(defgeneric universal-expt (x y)
600 (:documentation "Raises X to power Y.")
601 (:method ((x number) (y integer)) (expt x y))
602 (:method ((x t) (y integer))
603 (declare (type fixnum y))
604 (cond
605 ((minusp y) (error "universal-expt: Negative exponent."))
606 ((universal-zerop x) (if (zerop y) 1))
607 (t
608 (do ((k 1 (ash k 1))
609 (q x (multiply q q)) ;keep squaring
610 (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
611 ((> k y) p)
612 (declare (fixnum k)))))))
613
614(defgeneric poly-p (object)
615 (:documentation "Checks if an object is a polynomial.")
616 (:method ((self poly)) t)
617 (:method ((self t)) nil))
618
619(defmethod ->sexp :before ((self poly) &optional vars)
620 "Ensures that the number of variables in VARS maches the polynomial dimension of the
621polynomial SELF."
622 (with-slots (dimension)
623 self
624 (assert (= (length vars) dimension)
625 nil
626 "Number of variables ~S does not match the dimension ~S"
627 vars dimension)))
628
629(defmethod ->sexp ((self poly) &optional vars)
630 "Converts a polynomial SELF to a sexp."
631 (let ((m (mapcar #'(lambda (x) (->sexp x vars))
632 (poly-termlist self))))
633 (cond ((endp m) 0)
634 ((endp (cdr m)) (car m))
635 (t (cons '+ m)))))
636
637(defparameter +list-marker+ :[
638 "A sexp with this head is considered a list of polynomials.")
639
640(defmethod ->sexp ((self cons) &optional vars)
641 (assert (eql (car self) +list-marker+))
642 (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
643
644
645(defun poly-eval (expr vars order)
646 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
647variables VARS. Return the resulting polynomial or list of
648polynomials. Standard arithmetical operators in form EXPR are
649replaced with their analogues in the ring of polynomials, and the
650resulting expression is evaluated, resulting in a polynomial or a list
651of polynomials in internal form. A similar operation in another computer
652algebra system could be called 'expand' or so."
653 (labels ((p-eval (p) (poly-eval p vars order))
654 (p-eval-list (plist) (mapcar #'p-eval plist)))
655 (cond
656 ((eq expr 0)
657 (make-instance 'poly :dimension (length vars)))
658 ((member expr vars :test #'equalp)
659 (let ((pos (position expr vars :test #'equalp)))
660 (make-poly-variable (length vars) pos)))
661 ((atom expr)
662 (make-poly-constant (length vars) expr))
663 ((eq (car expr) +list-marker+)
664 (cons +list-marker+ (p-eval-list (cdr expr))))
665 (t
666 (case (car expr)
667 (+ (reduce #'add (p-eval-list (cdr expr))))
668 (- (apply #'subtract (p-eval-list (cdr expr))))
669 (*
670 (if (endp (cddr expr)) ;unary
671 (p-eval (cadr expr))
672 (apply #'multiply (p-eval-list (cdr expr)))))
673 (/
674 ;; A polynomial can be divided by a scalar
675 (cond
676 ((endp (cddr expr))
677 ;; A special case (/ ?), the inverse
678 (divide (cadr expr)))
679 (t
680 (let ((num (p-eval (cadr expr)))
681 (denom-inverse (apply #'divide (mapcar #'p-eval (cddr expr)))))
682 (multiply denom-inverse num)))))
683 (expt
684 (cond
685 ((member (cadr expr) vars :test #'equalp)
686 ;;Special handling of (expt var pow)
687 (let ((pos (position (cadr expr) vars :test #'equalp)))
688 (make-poly-variable (length vars) pos (caddr expr))))
689 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
690 ;; Negative power means division in coefficient ring
691 ;; Non-integer power means non-polynomial coefficient
692 expr)
693 (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
694 (otherwise
695 (error "Cannot evaluate as polynomial: ~A" expr)))))))
696
697(defgeneric make-zero-for (self)
698 (:method ((self poly))
699 (make-instance 'poly :dimension (poly-dimension self))))
700
701(defgeneric make-unit-for (self)
702 (:method ((self poly))
703 (make-poly-constant (poly-dimension self) 1)))
704
705(defgeneric poly-reverse (self)
706 (:documentation "Reverse the order of terms in a polynomial SELF.")
707 (:method ((self poly))
708 (with-slots (termlist)
709 self
710 (setf termlist (nreverse termlist)))
711 self))
712
713
714
Note: See TracBrowser for help on using the repository browser.