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

Last change on this file since 4551 was 4551, checked in by Marek Rychlik, 9 years ago

* empty log message *

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