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

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

* empty log message *

File size: 25.8 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"
[3643]24 (:use :cl :utils :monom :copy)
[2596]25 (:export "POLY"
[3270]26 "POLY-DIMENSION"
[2596]27 "POLY-TERMLIST"
[3016]28 "POLY-TERM-ORDER"
[3509]29 "POLY-INSERT-TERM"
[3690]30 "SCALAR-MULTIPLY-BY"
31 "SCALAR-DIVIDE-BY"
[3642]32 "LEADING-TERM"
[3657]33 "LEADING-MONOMIAL"
[3642]34 "LEADING-COEFFICIENT"
[3657]35 "SECOND-LEADING-TERM"
36 "SECOND-LEADING-MONOMIAL"
37 "SECOND-LEADING-COEFFICIENT"
[3642]38 "ADD-TO"
[3646]39 "ADD"
[3642]40 "SUBTRACT-FROM"
[3646]41 "SUBTRACT"
[3071]42 "CHANGE-TERM-ORDER"
[3099]43 "STANDARD-EXTENSION"
[3101]44 "STANDARD-EXTENSION-1"
[3109]45 "STANDARD-SUM"
[3094]46 "SATURATION-EXTENSION"
[3655]47 "ALIST->POLY"
[3852]48 "->INFIX"
[3655]49 "UNIVERSAL-EZGCD"
[3678]50 "S-POLYNOMIAL"
[3686]51 "POLY-CONTENT"
[3692]52 "POLY-PRIMITIVE-PART"
[3714]53 "SATURATION-EXTENSION-1"
[3737]54 "MAKE-POLY-VARIABLE"
[3821]55 "MAKE-POLY-CONSTANT"
[4053]56 "MAKE-ZERO-FOR"
57 "MAKE-UNIT-FOR"
[3778]58 "UNIVERSAL-EXPT"
[3969]59 "UNIVERSAL-EQUALP"
60 "POLY-LENGTH"
[4062]61 "POLY-REVERSE"
[3900]62 "POLY-P"
[3901]63 "+LIST-MARKER+"
[3900]64 "POLY-EVAL")
[3489]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."))
[143]68
[431]69(in-package :polynomial)
70
[1927]71(proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
[52]72
[2442]73(defclass poly ()
[3253]74 ((dimension :initform nil
[3250]75 :initarg :dimension
76 :accessor poly-dimension
[3242]77 :documentation "Shared dimension of all terms, the number of variables")
[3250]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."))
[3262]82 (:default-initargs :dimension nil :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)
[3243]88 (with-accessors ((dimension poly-dimension)
89 (termlist poly-termlist)
90 (order poly-term-order))
[3237]91 self
[3244]92 (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
93 dimension termlist order))))
[2469]94
[4114]95(defmethod copy-instance :around ((object poly) &rest initargs &key &allow-other-keys)
[4115]96 "Returns a deep copy of the polynomial POLY, by copying the TERMLIST and its terms."
[4114]97 (declare (ignore object initargs))
98 (let ((copy (call-next-method)))
99 (with-slots (termlist)
100 copy
101 (setf termlist (mapcar #'copy-instance termlist)))
102 copy))
103
104
[3015]105(defgeneric change-term-order (self other)
[3012]106 (:documentation "Change term order of SELF to the term order of OTHER.")
[3010]107 (:method ((self poly) (other poly))
108 (unless (eq (poly-term-order self) (poly-term-order other))
[3620]109 (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
[3010]110 (poly-term-order self) (poly-term-order other)))
[3012]111 self))
[3010]112
[3621]113(defgeneric poly-insert-term (self term)
[3622]114 (:documentation "Insert a term TERM into SELF before all other
[3621]115 terms. Order is not enforced.")
116 (:method ((self poly) (term term))
[3510]117 (cond ((null (poly-dimension self))
[3621]118 (setf (poly-dimension self) (monom-dimension term)))
119 (t (assert (= (poly-dimension self) (monom-dimension term)))))
120 (push term (poly-termlist self))
[3510]121 self))
122
[3622]123(defgeneric poly-append-term (self term)
124 (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
125 (:method ((self poly) (term term))
[3510]126 (cond ((null (poly-dimension self))
[3622]127 (setf (poly-dimension self) (monom-dimension term)))
128 (t (assert (= (poly-dimension self) (monom-dimension term)))))
129 (setf (cdr (last (poly-termlist self))) (list term))
[3510]130 self))
131
[3095]132(defun alist->poly (alist &aux (poly (make-instance 'poly)))
[3126]133 "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
134It can be used to enter simple polynomials by hand, e.g the polynomial
135in two variables, X and Y, given in standard notation as:
136
137 3*X^2*Y^3+2*Y+7
138
139can be entered as
140(ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
141
142NOTE: The primary use is for low-level debugging of the package."
[3099]143 (dolist (x alist poly)
[3705]144 (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
[3092]145
[3877]146(defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
[3786]147 "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
[3401]148 (reinitialize-instance new
149 :dimension (monom-dimension old)
[3786]150 :termlist (list old)))
[3796]151
[3877]152(defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
[3796]153 "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
154 (reinitialize-instance new
155 :dimension (monom-dimension old)
[3797]156 :termlist (list (change-class old 'term))))
[3403]157
[3624]158(defmethod universal-equalp ((self poly) (other poly))
159 "Implements equality of polynomials."
160 (and (eql (poly-dimension self) (poly-dimension other))
161 (every #'universal-equalp (poly-termlist self) (poly-termlist other))
162 (eq (poly-term-order self) (poly-term-order other))))
[2650]163
[3624]164(defgeneric leading-term (object)
[2442]165 (:method ((self poly))
[2525]166 (car (poly-termlist self)))
167 (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
[52]168
[3625]169(defgeneric second-leading-term (object)
[2442]170 (:method ((self poly))
[2525]171 (cadar (poly-termlist self)))
172 (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
[52]173
[3656]174(defgeneric leading-monomial (object)
175 (:method ((self poly))
176 (change-class (copy-instance (leading-term self)) 'monom))
177 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
178
179(defgeneric second-leading-monomial (object)
180 (:method ((self poly))
181 (change-class (copy-instance (second-leading-term self)) 'monom))
182 (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
183
[3625]184(defgeneric leading-coefficient (object)
[2442]185 (:method ((self poly))
[3642]186 (term-coeff (leading-term self)))
[2545]187 (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
[52]188
[2442]189(defgeneric second-leading-coefficient (object)
190 (:method ((self poly))
[3645]191 (term-coeff (second-leading-term self)))
[2906]192 (:documentation "The second leading coefficient of a polynomial. It
193 signals error for a polynomial with at most one term."))
[52]194
[3629]195(defmethod universal-zerop ((self poly))
196 "Return T iff SELF is a zero polynomial."
[3639]197 (null (poly-termlist self)))
[52]198
[3518]199(defgeneric poly-length (self)
[3630]200 (:documentation "Return the number of terms.")
[3518]201 (:method ((self poly))
202 (length (poly-termlist self))))
[52]203
[3689]204(defgeneric scalar-multiply-by (self other)
205 (:documentation "Multiply vector SELF by a scalar OTHER.")
206 (:method ((self poly) other)
207 (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
208 (poly-termlist self))
209 self))
210
211(defgeneric scalar-divide-by (self other)
212 (:documentation "Divide vector SELF by a scalar OTHER.")
213 (:method ((self poly) other)
214 (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
215 (poly-termlist self))
216 self))
217
[4034]218(defmethod unary-inverse :before ((self poly))
[4035]219 "Checks invertibility of a polynomial SELF. To be invertable, the
220polynomial must be an invertible, constant polynomial."
[4034]221 (with-slots (termlist)
[4035]222 self
223 (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
224 nil
225 "To be invertible, the polynomial must have 1 term of total degree 0.")))
[4034]226
227(defmethod unary-inverse ((self poly))
[4035]228 "Returns the unary inverse of a polynomial SELF."
[4034]229 (with-slots (termlist)
230 self
[4035]231 (setf (car termlist) (unary-inverse (car termlist)))
232 self))
[4034]233
[3663]234(defmethod multiply-by ((self poly) (other monom))
[3630]235 "Multiply a polynomial SELF by OTHER."
236 (mapc #'(lambda (term) (multiply-by term other))
237 (poly-termlist self))
238 self)
[2469]239
[3672]240(defmethod multiply-by ((self poly) (other term))
241 "Multiply a polynomial SELF by OTHER."
242 (mapc #'(lambda (term) (multiply-by term other))
243 (poly-termlist self))
244 self)
245
[4071]246(defmethod multiply-by ((self monom) (other poly))
247 "Multiply a monomial SELF by polynomial OTHER."
248 (multiply-by other self))
249
250(defmethod multiply-by ((self term) (other poly))
251 "Multiply a term SELF by polynomial OTHER."
252 (multiply-by other self))
253
[2761]254(defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
[2755]255 "Return an expression which will efficiently adds/subtracts two
256polynomials, P and Q. The addition/subtraction of coefficients is
[3878]257performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
258used to negate the coefficients of Q which do not have a corresponding
259coefficient in P. The code implements an efficient algorithm to add
260two polynomials represented as sorted lists of terms. The code
261destroys both arguments, reusing the terms to build the result."
[3631]262 `(macrolet ((lc (x) `(term-coeff (car ,x))))
[2742]263 (do ((p ,p)
264 (q ,q)
265 r)
266 ((or (endp p) (endp q))
267 ;; NOTE: R contains the result in reverse order. Can it
268 ;; be more efficient to produce the terms in correct order?
[2774]269 (unless (endp q)
[2776]270 ;; Upon subtraction, we must change the sign of
271 ;; all coefficients in q
[2774]272 ,@(when uminus-fn
[2775]273 `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
[2774]274 (setf r (nreconc r q)))
[3887]275 (unless (endp p)
276 (setf r (nreconc r p)))
277 r)
[2742]278 (multiple-value-bind
279 (greater-p equal-p)
[3632]280 (funcall ,order-fn (car p) (car q))
[2742]281 (cond
282 (greater-p
283 (rotatef (cdr p) r p)
284 )
285 (equal-p
[2766]286 (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
[2742]287 (cond
[3640]288 ((universal-zerop s)
[2742]289 (setf p (cdr p))
290 )
291 (t
292 (setf (lc p) s)
293 (rotatef (cdr p) r p))))
294 (setf q (cdr q))
295 )
296 (t
[2743]297 ;;Negate the term of Q if UMINUS provided, signallig
298 ;;that we are doing subtraction
[2908]299 ,(when uminus-fn
300 `(setf (lc q) (funcall ,uminus-fn (lc q))))
[3887]301 (rotatef (cdr q) r q))))
302 ;;(format t "P:~A~%" p)
303 ;;(format t "Q:~A~%" q)
304 ;;(format t "R:~A~%" r)
305 )))
[2585]306
[2655]307
[3887]308
[3647]309(defgeneric add-to (self other)
310 (:documentation "Add OTHER to SELF.")
311 (:method ((self number) (other number))
[3819]312 (+ self other))
313 (:method ((self poly) (other number))
[3865]314 (add-to self (make-poly-constant (poly-dimension self) other)))
315 (:method ((self number) (other poly))
316 (add-to (make-poly-constant (poly-dimension other) self) other)))
[3819]317
[3647]318
319(defgeneric subtract-from (self other)
[3648]320 (:documentation "Subtract OTHER from SELF.")
321 (:method ((self number) (other number))
[3830]322 (- self other))
323 (:method ((self poly) (other number))
324 (subtract-from self (make-poly-constant (poly-dimension self) other))))
[3647]325
[3969]326
[3884]327#|
[3750]328(defmacro def-add/subtract-method (add/subtract-method-name
[3749]329 uminus-method-name
330 &optional
331 (doc-string nil doc-string-supplied-p))
[3647]332 "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
[2749]333 `(defmethod ,add/subtract-method-name ((self poly) (other poly))
[2615]334 ,@(when doc-string-supplied-p `(,doc-string))
[2769]335 ;; Ensure orders are compatible
[3015]336 (change-term-order other self)
[2772]337 (setf (poly-termlist self) (fast-add/subtract
338 (poly-termlist self) (poly-termlist other)
339 (poly-term-order self)
340 #',add/subtract-method-name
341 ,(when uminus-method-name `(function ,uminus-method-name))))
[3748]342 self))
[3908]343
344(eval-when (:load-toplevel :execute)
345
346 (def-add/subtract-method add-to nil
347 "Adds to polynomial SELF another polynomial OTHER.
348This operation destructively modifies both polynomials.
349The result is stored in SELF. This implementation does
350no consing, entirely reusing the sells of SELF and OTHER.")
351
352 (def-add/subtract-method subtract-from unary-minus
353 "Subtracts from polynomial SELF another polynomial OTHER.
354This operation destructively modifies both polynomials.
355The result is stored in SELF. This implementation does
356no consing, entirely reusing the sells of SELF and OTHER.")
357 )
358
[3884]359|#
[2487]360
[3880]361(defmethod unary-minus ((self poly))
362 "Destructively modifies the coefficients of the polynomial SELF,
363by changing their sign."
364 (mapc #'unary-minus (poly-termlist self))
365 self)
366
367(defun add-termlists (p q order-fn)
368 "Destructively adds two termlists P and Q ordered according to ORDER-FN."
369 (fast-add/subtract p q order-fn #'add-to nil))
370
[3881]371(defun subtract-termlists (p q order-fn)
[3885]372 "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
[3882]373 (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
[3881]374
[3879]375(defmethod add-to ((self poly) (other poly))
376 "Adds to polynomial SELF another polynomial OTHER.
[2610]377This operation destructively modifies both polynomials.
378The result is stored in SELF. This implementation does
[3879]379no consing, entirely reusing the sells of SELF and OTHER."
380 (change-term-order other self)
381 (setf (poly-termlist self) (add-termlists
382 (poly-termlist self) (poly-termlist other)
[3883]383 (poly-term-order self)))
384 self)
[3879]385
[2609]386
[3879]387(defmethod subtract-from ((self poly) (other poly))
[2753]388 "Subtracts from polynomial SELF another polynomial OTHER.
[2610]389This operation destructively modifies both polynomials.
390The result is stored in SELF. This implementation does
[3879]391no consing, entirely reusing the sells of SELF and OTHER."
392 (change-term-order other self)
393 (setf (poly-termlist self) (subtract-termlists
394 (poly-termlist self) (poly-termlist other)
[3883]395 (poly-term-order self)))
396 self)
[2777]397
[4103]398
399(defmethod add-to ((self poly) (other term))
[4105]400 "Adds to a polynomial SELF a term OTHER. The term OTHER is not
401modified."
[4104]402 (add-to self (change-class (copy-instance other) 'poly)))
[4103]403
404(defmethod subtract-from ((self poly) (other term))
[4105]405 "Subtracts from a polynomial SELF a term OTHER. The term OTHER is not
406modified."
[4104]407 (subtract-from self (change-class (copy-instance other) 'poly)))
[4103]408
409
[2800]410(defmacro multiply-term-by-termlist-dropping-zeros (term termlist
[2927]411 &optional (reverse-arg-order-P nil))
[2799]412 "Multiplies term TERM by a list of term, TERMLIST.
[2792]413Takes into accound divisors of zero in the ring, by
[2927]414deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
[2928]415is T, change the order of arguments; this may be important
[2927]416if we extend the package to non-commutative rings."
[2800]417 `(mapcan #'(lambda (other-term)
[3633]418 (let ((prod (multiply
[2923]419 ,@(cond
[2930]420 (reverse-arg-order-p
[2925]421 `(other-term ,term))
422 (t
423 `(,term other-term))))))
[2800]424 (cond
[3633]425 ((universal-zerop prod) nil)
[2800]426 (t (list prod)))))
427 ,termlist))
[2790]428
[2796]429(defun multiply-termlists (p q order-fn)
[3127]430 "A version of polynomial multiplication, operating
431directly on termlists."
[2787]432 (cond
[2917]433 ((or (endp p) (endp q))
434 ;;p or q is 0 (represented by NIL)
435 nil)
[2789]436 ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
[2787]437 ((endp (cdr p))
[2918]438 (multiply-term-by-termlist-dropping-zeros (car p) q))
439 ((endp (cdr q))
[2919]440 (multiply-term-by-termlist-dropping-zeros (car q) p t))
441 (t
[4101]442 (cons (multiply (car p) (car q))
[2949]443 (add-termlists
444 (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
445 (multiply-termlists (cdr p) q order-fn)
446 order-fn)))))
[2793]447
[2803]448(defmethod multiply-by ((self poly) (other poly))
[3014]449 (change-term-order other self)
[2803]450 (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
451 (poly-termlist other)
452 (poly-term-order self)))
453 self)
454
[3803]455(defun add (&rest summands)
456 "Non-destructively adds list SUMMANDS."
457 (cond ((endp summands) 0)
[3818]458 (t (reduce #'add-2 summands))))
[3803]459
[3634]460(defun subtract (minuend &rest subtrahends)
[3427]461 "Non-destructively subtract MINUEND and SUBTRAHENDS."
[3851]462 (cond ((endp subtrahends) (unary-minus minuend))
463 (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
[3374]464
[3062]465(defmethod left-tensor-product-by ((self poly) (other monom))
466 (setf (poly-termlist self)
467 (mapcan #'(lambda (term)
468 (let ((prod (left-tensor-product-by term other)))
469 (cond
[3640]470 ((universal-zerop prod) nil)
[3062]471 (t (list prod)))))
472 (poly-termlist self)))
[3249]473 (incf (poly-dimension self) (monom-dimension other))
[3062]474 self)
[3044]475
[3062]476(defmethod right-tensor-product-by ((self poly) (other monom))
477 (setf (poly-termlist self)
478 (mapcan #'(lambda (term)
479 (let ((prod (right-tensor-product-by term other)))
480 (cond
[3640]481 ((universal-zerop prod) nil)
[3062]482 (t (list prod)))))
483 (poly-termlist self)))
[3249]484 (incf (poly-dimension self) (monom-dimension other))
[3062]485 self)
486
487
[3084]488(defun standard-extension (plist &aux (k (length plist)) (i 0))
[2716]489 "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
[3060]490is a list of polynomials. Destructively modifies PLIST elements."
[3061]491 (mapc #'(lambda (poly)
[3085]492 (left-tensor-product-by
493 poly
494 (prog1
495 (make-monom-variable k i)
496 (incf i))))
[3061]497 plist))
[52]498
[3087]499(defun standard-extension-1 (plist
500 &aux
[3096]501 (plist (standard-extension plist))
[3087]502 (nvars (poly-dimension (car plist))))
[3081]503 "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
[3087]504Firstly, new K variables U1, U2, ..., UK, are inserted into each
505polynomial. Subsequently, P1, P2, ..., PK are destructively modified
[3105]506tantamount to replacing PI with UI*PI-1. It assumes that all
[3106]507polynomials have the same dimension, and only the first polynomial
508is examined to determine this dimension."
[3089]509 ;; Implementation note: we use STANDARD-EXTENSION and then subtract
510 ;; 1 from each polynomial; since UI*PI has no constant term,
511 ;; we just need to append the constant term at the end
512 ;; of each termlist.
[3064]513 (flet ((subtract-1 (p)
[3641]514 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3083]515 (setf plist (mapc #'subtract-1 plist)))
[3077]516 plist)
[52]517
518
[3107]519(defun standard-sum (plist
520 &aux
521 (plist (standard-extension plist))
522 (nvars (poly-dimension (car plist))))
[3087]523 "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
524Firstly, new K variables, U1, U2, ..., UK, are inserted into each
525polynomial. Subsequently, P1, P2, ..., PK are destructively modified
526tantamount to replacing PI with UI*PI, and the resulting polynomials
[3117]527are added. Finally, 1 is subtracted. It should be noted that the term
528order is not modified, which is equivalent to using a lexicographic
529order on the first K variables."
[3107]530 (flet ((subtract-1 (p)
[3641]531 (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
[3108]532 (subtract-1
533 (make-instance
534 'poly
[3115]535 :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
[52]536
[3653]537(defgeneric universal-ezgcd (x y)
538 (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
539C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
540the Euclidean algorithm.")
541 (:method ((x integer) (y integer)
542 &aux (c (gcd x y)))
543 (values c (/ x c) (/ y c)))
544 )
545
[3655]546(defgeneric s-polynomial (object1 object2)
[3651]547 (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
548 (:method ((f poly) (g poly))
549 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
550 (mf (divide lcm (leading-monomial f)))
551 (mg (divide lcm (leading-monomial g))))
552 (multiple-value-bind (c cf cg)
[3652]553 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
[3651]554 (declare (ignore c))
555 (subtract
[4111]556 (multiply f (change-class mf 'term :coeff cg))
557 (multiply g (change-class mg 'term :coeff cf)))))))
[3651]558
[3676]559(defgeneric poly-content (object)
560 (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
[3677]561 (:method ((self poly))
562 (reduce #'universal-gcd
[3679]563 (mapcar #'term-coeff (rest (poly-termlist self)))
564 :initial-value (leading-coefficient self))))
[3676]565
[3684]566(defun poly-primitive-part (object)
[3685]567 "Divide polynomial OBJECT by gcd of its
[3684]568coefficients. Return the resulting polynomial."
[3688]569 (scalar-divide-by object (poly-content object)))
[3682]570
[3700]571(defun poly-insert-variables (self k)
[3697]572 (left-tensor-product-by self (make-instance 'monom :dimension k)))
573
[3698]574(defun saturation-extension (f plist &aux (k (length plist)))
[3708]575 "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
576PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
[3711]577as first K variables. It destructively modifies F and PLIST."
[3700]578 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3699]579 (standard-extension-1 plist)))
[3694]580
[3699]581(defun polysaturation-extension (f plist &aux (k (length plist)))
[3708]582 "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
583and F' is F with variables U1,U2,...,UK inserted as first K
[3711]584variables. It destructively modifies F and PLIST."
[3700]585 (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
[3703]586 (list (standard-sum plist))))
[3694]587
[3691]588(defun saturation-extension-1 (f p)
[3712]589 "Given family of polynomials F and a polynomial P, calculate [F',
590U*P-1], where F' is F with variable inserted as the first variable. It
591destructively modifies F and P."
[3693]592 (polysaturation-extension f (list p)))
[3713]593
[3717]594(defmethod multiply-by ((object1 number) (object2 poly))
[3720]595 (scalar-multiply-by (copy-instance object2) object1))
[3716]596
[4068]597(defmethod multiply-by ((object1 poly) (object2 number))
598 (scalar-multiply-by (copy-instance object1) object2))
599
[3781]600(defun make-poly-variable (nvars pos &optional (power 1))
601 (change-class (make-monom-variable nvars pos power) 'poly))
[3736]602
[3821]603(defun make-poly-constant (nvars coeff)
604 (change-class (make-term-constant nvars coeff) 'poly))
605
[3713]606(defgeneric universal-expt (x y)
[3721]607 (:documentation "Raises X to power Y.")
[3713]608 (:method ((x number) (y integer)) (expt x y))
609 (:method ((x t) (y integer))
610 (declare (type fixnum y))
611 (cond
612 ((minusp y) (error "universal-expt: Negative exponent."))
613 ((universal-zerop x) (if (zerop y) 1))
614 (t
615 (do ((k 1 (ash k 1))
616 (q x (multiply q q)) ;keep squaring
617 (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
618 ((> k y) p)
[3778]619 (declare (fixnum k)))))))
620
621(defgeneric poly-p (object)
622 (:documentation "Checks if an object is a polynomial.")
[3779]623 (:method ((self poly)) t)
[3778]624 (:method ((self t)) nil))
[3830]625
[4021]626(defmethod ->sexp :before ((self poly) &optional vars)
[3905]627 "Ensures that the number of variables in VARS maches the polynomial dimension of the
628polynomial SELF."
[4027]629 (with-slots (dimension)
630 self
631 (assert (= (length vars) dimension)
[4028]632 nil
[4027]633 "Number of variables ~S does not match the dimension ~S"
634 vars dimension)))
[3904]635
[4021]636(defmethod ->sexp ((self poly) &optional vars)
[3905]637 "Converts a polynomial SELF to a sexp."
[4036]638 (let ((m (mapcar #'(lambda (x) (->sexp x vars))
[3830]639 (poly-termlist self))))
[4053]640 (cond ((endp m) 0)
[4036]641 ((endp (cdr m)) (car m))
642 (t (cons '+ m)))))
[3899]643
[3903]644(defparameter +list-marker+ :[
645 "A sexp with this head is considered a list of polynomials.")
646
[4021]647(defmethod ->sexp ((self cons) &optional vars)
[3906]648 (assert (eql (car self) +list-marker+))
[4021]649 (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
[3906]650
651
[3899]652(defun poly-eval (expr vars order)
653 "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
654variables VARS. Return the resulting polynomial or list of
655polynomials. Standard arithmetical operators in form EXPR are
656replaced with their analogues in the ring of polynomials, and the
657resulting expression is evaluated, resulting in a polynomial or a list
658of polynomials in internal form. A similar operation in another computer
659algebra system could be called 'expand' or so."
660 (labels ((p-eval (p) (poly-eval p vars order))
661 (p-eval-list (plist) (mapcar #'p-eval plist)))
662 (cond
663 ((eq expr 0)
664 (make-instance 'poly :dimension (length vars)))
665 ((member expr vars :test #'equalp)
666 (let ((pos (position expr vars :test #'equalp)))
667 (make-poly-variable (length vars) pos)))
668 ((atom expr)
[4015]669 (make-poly-constant (length vars) expr))
[3899]670 ((eq (car expr) +list-marker+)
671 (cons +list-marker+ (p-eval-list (cdr expr))))
672 (t
673 (case (car expr)
674 (+ (reduce #'add (p-eval-list (cdr expr))))
675 (- (apply #'subtract (p-eval-list (cdr expr))))
676 (*
677 (if (endp (cddr expr)) ;unary
678 (p-eval (cadr expr))
[4101]679 (apply #'multiply (p-eval-list (cdr expr)))))
[3899]680 (/
681 ;; A polynomial can be divided by a scalar
682 (cond
683 ((endp (cddr expr))
684 ;; A special case (/ ?), the inverse
685 (divide (cadr expr)))
686 (t
687 (let ((num (p-eval (cadr expr)))
[4016]688 (denom-inverse (apply #'divide (mapcar #'p-eval (cddr expr)))))
[3899]689 (multiply denom-inverse num)))))
690 (expt
691 (cond
692 ((member (cadr expr) vars :test #'equalp)
693 ;;Special handling of (expt var pow)
694 (let ((pos (position (cadr expr) vars :test #'equalp)))
695 (make-poly-variable (length vars) pos (caddr expr))))
696 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
697 ;; Negative power means division in coefficient ring
698 ;; Non-integer power means non-polynomial coefficient
699 expr)
700 (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
701 (otherwise
[4017]702 (error "Cannot evaluate as polynomial: ~A" expr)))))))
[4053]703
704(defgeneric make-zero-for (self)
705 (:method ((self poly))
706 (make-instance 'poly :dimension (poly-dimension self))))
707
708(defgeneric make-unit-for (self)
709 (:method ((self poly))
710 (make-poly-constant (poly-dimension self) 1)))
[4057]711
[4068]712(defgeneric poly-reverse (self)
[4061]713 (:documentation "Reverse the order of terms in a polynomial SELF.")
[4057]714 (:method ((self poly))
715 (with-slots (termlist)
716 self
[4060]717 (setf termlist (nreverse termlist)))
[4057]718 self))
719
720
[4053]721
Note: See TracBrowser for help on using the repository browser.