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

Last change on this file since 4469 was 4463, checked in by Marek Rychlik, 9 years ago
File size: 26.2 KB
RevLine 
[3400]1;;----------------------------------------------------------------
[1201]2;;; -*- Mode: Lisp -*-
[77]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
[431]23(defpackage "POLYNOMIAL"
[4325]24 (:use :cl :utils :monom :copy :ring)
[2596]25 (:export "POLY"
[3270]26 "POLY-DIMENSION"
[2596]27 "POLY-TERMLIST"
[3016]28 "POLY-TERM-ORDER"
[3509]29 "POLY-INSERT-TERM"
[4456]30 "POLY-REMOVE-TERM"
[3690]31 "SCALAR-MULTIPLY-BY"
32 "SCALAR-DIVIDE-BY"
[3642]33 "LEADING-TERM"
[3657]34 "LEADING-MONOMIAL"
[3642]35 "LEADING-COEFFICIENT"
[3657]36 "SECOND-LEADING-TERM"
37 "SECOND-LEADING-MONOMIAL"
38 "SECOND-LEADING-COEFFICIENT"
[3642]39 "ADD-TO"
[3646]40 "ADD"
[3642]41 "SUBTRACT-FROM"
[3646]42 "SUBTRACT"
[3071]43 "CHANGE-TERM-ORDER"
[3099]44 "STANDARD-EXTENSION"
[3101]45 "STANDARD-EXTENSION-1"
[3109]46 "STANDARD-SUM"
[3094]47 "SATURATION-EXTENSION"
[3655]48 "ALIST->POLY"
[4442]49 "POLY->ALIST"
[3852]50 "->INFIX"
[3655]51 "UNIVERSAL-EZGCD"
[3678]52 "S-POLYNOMIAL"
[3686]53 "POLY-CONTENT"
[3692]54 "POLY-PRIMITIVE-PART"
[3714]55 "SATURATION-EXTENSION-1"
[3737]56 "MAKE-POLY-VARIABLE"
[3821]57 "MAKE-POLY-CONSTANT"
[4053]58 "MAKE-ZERO-FOR"
59 "MAKE-UNIT-FOR"
[3778]60 "UNIVERSAL-EXPT"
[3969]61 "UNIVERSAL-EQUALP"
[4191]62 "UNIVERSAL-ZEROP"
[3969]63 "POLY-LENGTH"
[4062]64 "POLY-REVERSE"
[3900]65 "POLY-P"
[3901]66 "+LIST-MARKER+"
[4366]67 "POLY-EVAL"
68 "*COEFFICIENT-CLASS*")
[3489]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."))
[143]72
[431]73(in-package :polynomial)
74
[1927]75(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]76
[4347]77(defclass poly (ring)
[4457]78 ((termlist :initform nil :initarg :termlist :accessor poly-termlist
[3619]79 :documentation "List of terms.")
[3250]80 (order :initform #'lex> :initarg :order :accessor poly-term-order
[2697]81 :documentation "Monomial/term order."))
[4457]82 (:default-initargs :termlist nil :order #'lex>)
[2695]83 (:documentation "A polynomial with a list of terms TERMLIST, ordered
[2696]84according to term order ORDER, which defaults to LEX>."))
[2442]85
[2471]86(defmethod print-object ((self poly) stream)
[3241]87 (print-unreadable-object (self stream :type t :identity t)
[4457]88 (with-accessors ((termlist poly-termlist)
[3243]89 (order poly-term-order))
[3237]90 self
[4457]91 (format stream "TERMLIST=~A ORDER=~A"
92 termlist order))))
[2469]93
[4114]94(defmethod copy-instance :around ((object poly) &rest initargs &key &allow-other-keys)
[4115]95 "Returns a deep copy of the polynomial POLY, by copying the TERMLIST and its terms."
[4114]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
[3015]104(defgeneric change-term-order (self other)
[3012]105 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]106 (:method ((self poly) (other poly))
107 (unless (eq (poly-term-order self) (poly-term-order other))
[3620]108 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
[3010]109 (poly-term-order self) (poly-term-order other)))
[3012]110 self))
[3010]111
[4457]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
[3621]117(defgeneric poly-insert-term (self term)
[3622]118 (:documentation "Insert a term TERM into SELF before all other
[4329]119terms. Order is not enforced.")
[3621]120 (:method ((self poly) (term term))
[4457]121 (with-slots (termlist)
122 self
123 (unless (endp termlist)
124 (assert (= (monom-dimension (car termlist)) (monom-dimension term)))))
[3621]125 (push term (poly-termlist self))
[3510]126 self))
127
[4456]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
[3622]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))
[4457]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)))
[3510]141 self))
142
[3095]143(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3126]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
[4442]151(ALIST->POLY '(((0 0) . 7) ((0 1) . 2) ((2 3) . 3) )). NOTE: the
152terms are entered in the increasing order.
[3126]153
154NOTE: The primary use is for low-level debugging of the package."
[3099]155 (dolist (x alist poly)
[3705]156 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
[3092]157
[4442]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
[4456]170#+nil
171(defmethod shared-initialize :after ((self poly) slot-names
172 &rest initargs
173 &key)
174 "If TERMLIST is supplied and non-empty, and DIMENSION is NIL, set
175the dimension to the dimension of the first term in TERMLIST."
176 (declare (ignore initargs))
177 (let ((dims (mapcar #'monom-dimension (slot-value self 'termlist))))
178 (format t "Dimensions: ~A~%" dims)
179 (assert (apply #'= dims))
180 (unless (endp dims)
181 (setf (slot-value self 'dimension) (car dims))))
182 self)
[4442]183
[3877]184(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
[3786]185 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
[4457]186 (reinitialize-instance new :termlist (list old)))
[3796]187
[3877]188(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
[3796]189 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
[4457]190 (reinitialize-instance new :termlist (list (change-class old 'term))))
[3403]191
[3624]192(defmethod universal-equalp ((self poly) (other poly))
193 "Implements equality of polynomials."
[4457]194 (and
195 ;(eql (poly-dimension self) (poly-dimension other))
196 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
197 (eq (poly-term-order self) (poly-term-order other))))
[2650]198
[3624]199(defgeneric leading-term (object)
[2442]200 (:method ((self poly))
[2525]201 (car (poly-termlist self)))
202 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]203
[3625]204(defgeneric second-leading-term (object)
[2442]205 (:method ((self poly))
[2525]206 (cadar (poly-termlist self)))
207 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]208
[3656]209(defgeneric leading-monomial (object)
210 (:method ((self poly))
211 (change-class (copy-instance (leading-term self)) 'monom))
212 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
213
214(defgeneric second-leading-monomial (object)
215 (:method ((self poly))
216 (change-class (copy-instance (second-leading-term self)) 'monom))
217 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
218
[3625]219(defgeneric leading-coefficient (object)
[2442]220 (:method ((self poly))
[3642]221 (term-coeff (leading-term self)))
[2545]222 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]223
[2442]224(defgeneric second-leading-coefficient (object)
225 (:method ((self poly))
[3645]226 (term-coeff (second-leading-term self)))
[2906]227 (:documentation "The second leading coefficient of a polynomial. It
228 signals error for a polynomial with at most one term."))
[52]229
[3629]230(defmethod universal-zerop ((self poly))
231 "Return T iff SELF is a zero polynomial."
[3639]232 (null (poly-termlist self)))
[52]233
[3518]234(defgeneric poly-length (self)
[3630]235 (:documentation "Return the number of terms.")
[3518]236 (:method ((self poly))
237 (length (poly-termlist self))))
[52]238
[3689]239(defgeneric scalar-multiply-by (self other)
240 (:documentation "Multiply vector SELF by a scalar OTHER.")
241 (:method ((self poly) other)
[4333]242 (mapc #'(lambda (term) (setf (term-coeff term) (multiply-by (term-coeff term) other)))
[3689]243 (poly-termlist self))
244 self))
245
246(defgeneric scalar-divide-by (self other)
247 (:documentation "Divide vector SELF by a scalar OTHER.")
248 (:method ((self poly) other)
[4333]249 (mapc #'(lambda (term) (setf (term-coeff term) (divide-by (term-coeff term) other)))
[3689]250 (poly-termlist self))
251 self))
252
[4034]253(defmethod unary-inverse :before ((self poly))
[4035]254 "Checks invertibility of a polynomial SELF. To be invertable, the
255polynomial must be an invertible, constant polynomial."
[4034]256 (with-slots (termlist)
[4035]257 self
258 (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
259 nil
260 "To be invertible, the polynomial must have 1 term of total degree 0.")))
[4034]261
262(defmethod unary-inverse ((self poly))
[4035]263 "Returns the unary inverse of a polynomial SELF."
[4034]264 (with-slots (termlist)
265 self
[4035]266 (setf (car termlist) (unary-inverse (car termlist)))
267 self))
[4034]268
[3663]269(defmethod multiply-by ((self poly) (other monom))
[3630]270 "Multiply a polynomial SELF by OTHER."
271 (mapc #'(lambda (term) (multiply-by term other))
272 (poly-termlist self))
273 self)
[2469]274
[3672]275(defmethod multiply-by ((self poly) (other term))
276 "Multiply a polynomial SELF by OTHER."
277 (mapc #'(lambda (term) (multiply-by term other))
278 (poly-termlist self))
279 self)
280
[4427]281#|
[2761]282(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]283 "Return an expression which will efficiently adds/subtracts two
284polynomials, P and Q. The addition/subtraction of coefficients is
[3878]285performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
286used to negate the coefficients of Q which do not have a corresponding
287coefficient in P. The code implements an efficient algorithm to add
288two polynomials represented as sorted lists of terms. The code
289destroys both arguments, reusing the terms to build the result."
[3631]290 `(macrolet ((lc (x) `(term-coeff (car ,x))))
[2742]291 (do ((p ,p)
292 (q ,q)
293 r)
294 ((or (endp p) (endp q))
295 ;; NOTE: R contains the result in reverse order. Can it
296 ;; be more efficient to produce the terms in correct order?
[2774]297 (unless (endp q)
[2776]298 ;; Upon subtraction, we must change the sign of
299 ;; all coefficients in q
[2774]300 ,@(when uminus-fn
[2775]301 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]302 (setf r (nreconc r q)))
[3887]303 (unless (endp p)
304 (setf r (nreconc r p)))
305 r)
[2742]306 (multiple-value-bind
307 (greater-p equal-p)
[3632]308 (funcall ,order-fn (car p) (car q))
[2742]309 (cond
310 (greater-p
311 (rotatef (cdr p) r p)
312 )
313 (equal-p
[2766]314 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]315 (cond
[3640]316 ((universal-zerop s)
[2742]317 (setf p (cdr p))
318 )
319 (t
320 (setf (lc p) s)
321 (rotatef (cdr p) r p))))
322 (setf q (cdr q))
323 )
324 (t
[2743]325 ;;Negate the term of Q if UMINUS provided, signallig
326 ;;that we are doing subtraction
[2908]327 ,(when uminus-fn
328 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[3887]329 (rotatef (cdr q) r q))))
330 ;;(format t "P:~A~%" p)
331 ;;(format t "Q:~A~%" q)
332 ;;(format t "R:~A~%" r)
333 )))
[4427]334|#
[3647]335
[4434]336
337
338
[4432]339#|
[4395]340(defun fast-add (p q order-fn add-fn)
[4427]341 "Add two polynomials, P and Q, represented as lists of terms.
342The operation is destructive to both polynomials, as the terms
[4428]343of both lists are combined into the result. The operation does not
344create any new instance of TERM."
[4395]345 (macrolet ((lc (x) `(term-coeff (car ,x))))
346 (do (r)
347 ((or (endp p) (endp q))
348 ;; NOTE: R contains the result in reverse order. Can it
349 ;; be more efficient to produce the terms in correct order?
350 (unless (endp q)
351 (setf r (nreconc r q)))
352 (unless (endp p)
353 (setf r (nreconc r p)))
354 r)
355 (multiple-value-bind
356 (greater-p equal-p)
357 (funcall order-fn (car p) (car q))
358 (cond
359 (greater-p
360 (rotatef (cdr p) r p)
361 )
362 (equal-p
363 (let ((s (funcall add-fn (lc p) (lc q))))
364 (cond
365 ((universal-zerop s)
366 (setf p (cdr p))
367 )
368 (t
369 (setf (lc p) s)
370 (rotatef (cdr p) r p))))
371 (setf q (cdr q))
372 )
373 (t
374 (rotatef (cdr q) r q)))))))
[4434]375|#
[4432]376
377;; Getter/setter of leading coefficient
378(defun lc (x) (term-coeff (car x)))
379(defun (setf lc) (new-value x) (setf (term-coeff (car x)) new-value))
380
[4442]381
[4447]382(defun slow-add (p q order-fn add-fn)
[4434]383 (cond
[4436]384 ((endp p) q)
385 ((endp q) p)
[4434]386 (t
387 (multiple-value-bind
388 (greater-p equal-p)
389 (funcall order-fn (car p) (car q))
390 (cond
[4442]391 (greater-p ; (> (car p) (car q))
[4447]392 (cons (car p) (slow-add (cdr p) q order-fn add-fn))
[4434]393 )
[4442]394 (equal-p ; (= (car p)) (car q))
[4434]395 (let ((s (funcall add-fn (lc p) (lc q))))
396 (cond
397 ((universal-zerop s)
[4447]398 (slow-add (cdr p) (cdr q) order-fn add-fn))
[4434]399 (t
400 ;; Adjust the lc of p
401 (setf (lc p) s)
[4447]402 (cons (car p) (slow-add (cdr p) (cdr q) order-fn add-fn))
[4434]403 ))))
[4442]404 (t ;(< (car p) (car q))
[4447]405 (cons (car q) (slow-add p (cdr q) order-fn add-fn))
[4434]406 ))))))
[4432]407
408
[4451]409(defun fast-and-risky-add (p q order-fn add-fn &aux result result-last)
410 (when (and p q (eq p q)) (warn "FAST-AND-RISKY-ADD: ~S is EQ to ~S" p q))
411 (flet ((add-to-result (x)
412 (assert (consp x))
413 (setf (cdr x) nil)
414 (if (endp result)
415 (setf result x
416 result-last x)
417 (setf (cdr result-last) x
418 result-last (cdr result-last)))))
419 (loop
420 (cond
421 ((endp p) (unless (endp q) (add-to-result q)) (return result))
422 ((endp q) (unless (endp p) (add-to-result p)) (return result))
423 (t
424 (multiple-value-bind
425 (greater-p equal-p)
426 (funcall order-fn (car p) (car q))
427 (cond
428 (greater-p ; (> (car p) (car q))
429 (let ((tmp (cdr p)))
430 (add-to-result p)
431 (setf p tmp)))
432 (equal-p ; (= (car p)) (car q))
433 (let ((s (funcall add-fn (lc p) (lc q))))
434 (cond
435 ((universal-zerop s)
436 ;; Terms cancel, discard both
437 (setf p (cdr p)
438 q (cdr q)))
439 (t
440 ;; Terms do not cancel, store the
441 ;; sum of coefficients in (lc p)
442 (setf (lc p) s)
443 (let ((tmp (cdr p)))
444 (add-to-result p)
445 (setf p tmp
446 q (cdr q)))))))
447 (t ;(< (car p) (car q))
448 (let ((tmp (cdr q)))
449 (add-to-result q)
450 (setf q tmp))
451 ))))))))
452
[4447]453(defun fast-add (p q order-fn add-fn)
454 "This version calls SLOW-ADD and is bullet-proof."
[4455]455 (slow-add p q order-fn add-fn)
456 ;;(fast-and-risky-add p q order-fn add-fn)
[4451]457 )
[4447]458
[3884]459#|
[4385]460;; NOTE: The stuff below works, but may not be worth the trouble.
461
[3750]462(defmacro def-add/subtract-method (add/subtract-method-name
[4452]463 uminus-method-name
464 &optional
465 (doc-string nil doc-string-supplied-p))
[3647]466 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]467 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]468 ,@(when doc-string-supplied-p `(,doc-string))
[2769]469 ;; Ensure orders are compatible
[3015]470 (change-term-order other self)
[2772]471 (setf (poly-termlist self) (fast-add/subtract
472 (poly-termlist self) (poly-termlist other)
473 (poly-term-order self)
474 #',add/subtract-method-name
475 ,(when uminus-method-name `(function ,uminus-method-name))))
[3748]476 self))
[3908]477
478(eval-when (:load-toplevel :execute)
479
480 (def-add/subtract-method add-to nil
481 "Adds to polynomial SELF another polynomial OTHER.
482This operation destructively modifies both polynomials.
483The result is stored in SELF. This implementation does
484no consing, entirely reusing the sells of SELF and OTHER.")
485
486 (def-add/subtract-method subtract-from unary-minus
487 "Subtracts from polynomial SELF another polynomial OTHER.
488This operation destructively modifies both polynomials.
489The result is stored in SELF. This implementation does
490no consing, entirely reusing the sells of SELF and OTHER.")
491 )
492
[3884]493|#
[2487]494
[3880]495(defmethod unary-minus ((self poly))
496 "Destructively modifies the coefficients of the polynomial SELF,
497by changing their sign."
498 (mapc #'unary-minus (poly-termlist self))
499 self)
500
501(defun add-termlists (p q order-fn)
502 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
[4395]503 (fast-add p q order-fn #'add-to))
[3880]504
[3881]505(defun subtract-termlists (p q order-fn)
[3885]506 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
[4395]507 (setf q (mapc #'unary-minus q))
508 (add-termlists p q order-fn))
[3881]509
[4452]510(defmethod add-to ((self poly) (other poly))
[3879]511 "Adds to polynomial SELF another polynomial OTHER.
[2610]512This operation destructively modifies both polynomials.
513The result is stored in SELF. This implementation does
[3879]514no consing, entirely reusing the sells of SELF and OTHER."
[4452]515 (change-term-order other self)
[3879]516 (setf (poly-termlist self) (add-termlists
[4452]517 (poly-termlist self) (poly-termlist other)
[3883]518 (poly-term-order self)))
519 self)
[3879]520
[2609]521
[4451]522(defmethod subtract-from ((self poly) (other poly))
[4215]523 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]524This operation destructively modifies both polynomials.
525The result is stored in SELF. This implementation does
[3879]526no consing, entirely reusing the sells of SELF and OTHER."
[4451]527 (change-term-order other self)
[3879]528 (setf (poly-termlist self) (subtract-termlists
[4451]529 (poly-termlist self) (poly-termlist other)
[3883]530 (poly-term-order self)))
531 self)
[2777]532
[4103]533
[4452]534(defmethod add-to ((self poly) (other term))
[4105]535 "Adds to a polynomial SELF a term OTHER. The term OTHER is not
536modified."
[4452]537 (add-to self (change-class other 'poly)))
[4103]538
[4452]539(defmethod subtract-from ((self poly) (other term))
[4105]540 "Subtracts from a polynomial SELF a term OTHER. The term OTHER is not
541modified."
[4452]542 (subtract-from self (change-class other 'poly)))
[4103]543
544
[2800]545(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]546 &optional (reverse-arg-order-P nil))
[2799]547 "Multiplies term TERM by a list of term, TERMLIST.
[2792]548Takes into accound divisors of zero in the ring, by
[2927]549deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]550is T, change the order of arguments; this may be important
[2927]551if we extend the package to non-commutative rings."
[2800]552 `(mapcan #'(lambda (other-term)
[3633]553 (let ((prod (multiply
[2923]554 ,@(cond
[2930]555 (reverse-arg-order-p
[2925]556 `(other-term ,term))
557 (t
558 `(,term other-term))))))
[2800]559 (cond
[3633]560 ((universal-zerop prod) nil)
[2800]561 (t (list prod)))))
562 ,termlist))
[2790]563
[2796]564(defun multiply-termlists (p q order-fn)
[3127]565 "A version of polynomial multiplication, operating
566directly on termlists."
[2787]567 (cond
[2917]568 ((or (endp p) (endp q))
569 ;;p or q is 0 (represented by NIL)
570 nil)
[2789]571 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]572 ((endp (cdr p))
[2918]573 (multiply-term-by-termlist-dropping-zeros (car p) q))
574 ((endp (cdr q))
[2919]575 (multiply-term-by-termlist-dropping-zeros (car q) p t))
576 (t
[4101]577 (cons (multiply (car p) (car q))
[2949]578 (add-termlists
579 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
580 (multiply-termlists (cdr p) q order-fn)
581 order-fn)))))
[2793]582
[4331]583(defmethod multiply-by ((self poly) (other poly) &aux (other-copy (copy-instance other)))
584 (change-term-order other-copy self)
[2803]585 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
[4331]586 (poly-termlist other-copy)
[2803]587 (poly-term-order self)))
588 self)
589
[3062]590(defmethod left-tensor-product-by ((self poly) (other monom))
591 (setf (poly-termlist self)
592 (mapcan #'(lambda (term)
593 (let ((prod (left-tensor-product-by term other)))
594 (cond
[3640]595 ((universal-zerop prod) nil)
[3062]596 (t (list prod)))))
597 (poly-termlist self)))
598 self)
[3044]599
[3062]600(defmethod right-tensor-product-by ((self poly) (other monom))
601 (setf (poly-termlist self)
602 (mapcan #'(lambda (term)
603 (let ((prod (right-tensor-product-by term other)))
604 (cond
[3640]605 ((universal-zerop prod) nil)
[3062]606 (t (list prod)))))
607 (poly-termlist self)))
608 self)
609
610
[3084]611(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]612 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]613is a list of polynomials. Destructively modifies PLIST elements."
[3061]614 (mapc #'(lambda (poly)
[3085]615 (left-tensor-product-by
616 poly
617 (prog1
618 (make-monom-variable k i)
619 (incf i))))
[3061]620 plist))
[52]621
[3087]622(defun standard-extension-1 (plist
623 &aux
[3096]624 (plist (standard-extension plist))
[3087]625 (nvars (poly-dimension (car plist))))
[3081]626 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]627Firstly, new K variables U1, U2, ..., UK, are inserted into each
628polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]629tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]630polynomials have the same dimension, and only the first polynomial
631is examined to determine this dimension."
[3089]632 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
633 ;; 1 from each polynomial; since UI*PI has no constant term,
634 ;; we just need to append the constant term at the end
635 ;; of each termlist.
[3064]636 (flet ((subtract-1 (p)
[3641]637 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3083]638 (setf plist (mapc #'subtract-1 plist)))
[3077]639 plist)
[52]640
641
[3107]642(defun standard-sum (plist
643 &aux
644 (plist (standard-extension plist))
645 (nvars (poly-dimension (car plist))))
[3087]646 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
647Firstly, new K variables, U1, U2, ..., UK, are inserted into each
648polynomial. Subsequently, P1, P2, ..., PK are destructively modified
649tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]650are added. Finally, 1 is subtracted. It should be noted that the term
651order is not modified, which is equivalent to using a lexicographic
652order on the first K variables."
[3107]653 (flet ((subtract-1 (p)
[3641]654 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3108]655 (subtract-1
656 (make-instance
657 'poly
[3115]658 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]659
[3655]660(defgeneric s-polynomial (object1 object2)
[3651]661 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
662 (:method ((f poly) (g poly))
663 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
664 (mf (divide lcm (leading-monomial f)))
665 (mg (divide lcm (leading-monomial g))))
666 (multiple-value-bind (c cf cg)
[3652]667 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
[3651]668 (declare (ignore c))
669 (subtract
[4444]670 (multiply f mf cg)
671 (multiply g mg cf))))))
[3651]672
[3676]673(defgeneric poly-content (object)
674 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
[3677]675 (:method ((self poly))
676 (reduce #'universal-gcd
[3679]677 (mapcar #'term-coeff (rest (poly-termlist self)))
678 :initial-value (leading-coefficient self))))
[3676]679
[4334]680(defun poly-primitive-part (self)
681 "Divide polynomial SELF by gcd of its
[3684]682coefficients. Return the resulting polynomial."
[4334]683 (scalar-divide-by self (poly-content self)))
[3682]684
[3700]685(defun poly-insert-variables (self k)
[3697]686 (left-tensor-product-by self (make-instance 'monom :dimension k)))
687
[3698]688(defun saturation-extension (f plist &aux (k (length plist)))
[3708]689 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
690PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
[3711]691as first K variables. It destructively modifies F and PLIST."
[3700]692 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3699]693 (standard-extension-1 plist)))
[3694]694
[3699]695(defun polysaturation-extension (f plist &aux (k (length plist)))
[3708]696 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
697and F' is F with variables U1,U2,...,UK inserted as first K
[3711]698variables. It destructively modifies F and PLIST."
[3700]699 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3703]700 (list (standard-sum plist))))
[3694]701
[3691]702(defun saturation-extension-1 (f p)
[3712]703 "Given family of polynomials F and a polynomial P, calculate [F',
704U*P-1], where F' is F with variable inserted as the first variable. It
705destructively modifies F and P."
[3693]706 (polysaturation-extension f (list p)))
[3713]707
[4305]708(defmethod multiply-by ((self poly) (other ring))
[4306]709 (scalar-multiply-by self other))
[4068]710
[3781]711(defun make-poly-variable (nvars pos &optional (power 1))
712 (change-class (make-monom-variable nvars pos power) 'poly))
[3736]713
[3821]714(defun make-poly-constant (nvars coeff)
715 (change-class (make-term-constant nvars coeff) 'poly))
716
[3713]717(defgeneric universal-expt (x y)
[3721]718 (:documentation "Raises X to power Y.")
[3713]719 (:method ((x number) (y integer)) (expt x y))
720 (:method ((x t) (y integer))
721 (declare (type fixnum y))
722 (cond
723 ((minusp y) (error "universal-expt: Negative exponent."))
724 ((universal-zerop x) (if (zerop y) 1))
725 (t
726 (do ((k 1 (ash k 1))
727 (q x (multiply q q)) ;keep squaring
[4119]728 (p (make-unit-for x) (if (not (zerop (logand k y))) (multiply p q) p)))
[3713]729 ((> k y) p)
[3778]730 (declare (fixnum k)))))))
731
732(defgeneric poly-p (object)
733 (:documentation "Checks if an object is a polynomial.")
[3779]734 (:method ((self poly)) t)
[3778]735 (:method ((self t)) nil))
[3830]736
[4021]737(defmethod ->sexp :before ((self poly) &optional vars)
[3905]738 "Ensures that the number of variables in VARS maches the polynomial dimension of the
739polynomial SELF."
[4463]740 (unless (endp (poly-termlist self))
741 (let ((dimension (poly-dimension self)))
742 (assert (= (length vars) dimension)
743 nil
744 "Number of variables ~S does not match the dimension ~S"
745 vars dimension))))
[3904]746
[4021]747(defmethod ->sexp ((self poly) &optional vars)
[3905]748 "Converts a polynomial SELF to a sexp."
[4396]749 (let ((m (mapcar #'(lambda (trm) (->sexp trm vars))
[3830]750 (poly-termlist self))))
[4053]751 (cond ((endp m) 0)
[4036]752 ((endp (cdr m)) (car m))
753 (t (cons '+ m)))))
[3899]754
[4363]755(defconstant +list-marker+ :[
[3903]756 "A sexp with this head is considered a list of polynomials.")
757
[4021]758(defmethod ->sexp ((self cons) &optional vars)
[3906]759 (assert (eql (car self) +list-marker+))
[4021]760 (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
[3906]761
[4277]762(defmethod make-zero-for ((self poly))
[4457]763 (make-instance 'poly))
[4053]764
[4277]765(defmethod make-unit-for ((self poly))
766 (make-poly-constant (poly-dimension self) 1))
[4057]767
[4068]768(defgeneric poly-reverse (self)
[4061]769 (:documentation "Reverse the order of terms in a polynomial SELF.")
[4057]770 (:method ((self poly))
771 (with-slots (termlist)
772 self
[4060]773 (setf termlist (nreverse termlist)))
[4057]774 self))
775
776
[4053]777
Note: See TracBrowser for help on using the repository browser.