1 | ;;----------------------------------------------------------------
|
---|
2 | ;; File: polynomial.lisp
|
---|
3 | ;;----------------------------------------------------------------
|
---|
4 | ;;
|
---|
5 | ;; Author: Marek Rychlik (rychlik@u.arizona.edu)
|
---|
6 | ;; Date: Thu Aug 27 09:41:24 2015
|
---|
7 | ;; Copying: (C) Marek Rychlik, 2010. All rights reserved.
|
---|
8 | ;;
|
---|
9 | ;;----------------------------------------------------------------
|
---|
10 | ;;; -*- Mode: Lisp -*-
|
---|
11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
12 | ;;;
|
---|
13 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
14 | ;;;
|
---|
15 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
16 | ;;; it under the terms of the GNU General Public License as published by
|
---|
17 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
18 | ;;; (at your option) any later version.
|
---|
19 | ;;;
|
---|
20 | ;;; This program is distributed in the hope that it will be useful,
|
---|
21 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
22 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
23 | ;;; GNU General Public License for more details.
|
---|
24 | ;;;
|
---|
25 | ;;; You should have received a copy of the GNU General Public License
|
---|
26 | ;;; along with this program; if not, write to the Free Software
|
---|
27 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
28 | ;;;
|
---|
29 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
30 |
|
---|
31 | (defpackage "POLYNOMIAL"
|
---|
32 | (:use :cl :utils :monom :copy)
|
---|
33 | (:export "POLY"
|
---|
34 | "POLY-DIMENSION"
|
---|
35 | "POLY-TERMLIST"
|
---|
36 | "POLY-TERM-ORDER"
|
---|
37 | "POLY-INSERT-TERM"
|
---|
38 | "SCALAR-MULTIPLY-BY"
|
---|
39 | "SCALAR-DIVIDE-BY"
|
---|
40 | "LEADING-TERM"
|
---|
41 | "LEADING-MONOMIAL"
|
---|
42 | "LEADING-COEFFICIENT"
|
---|
43 | "SECOND-LEADING-TERM"
|
---|
44 | "SECOND-LEADING-MONOMIAL"
|
---|
45 | "SECOND-LEADING-COEFFICIENT"
|
---|
46 | "ADD-TO"
|
---|
47 | "ADD"
|
---|
48 | "SUBTRACT-FROM"
|
---|
49 | "SUBTRACT"
|
---|
50 | "CHANGE-TERM-ORDER"
|
---|
51 | "STANDARD-EXTENSION"
|
---|
52 | "STANDARD-EXTENSION-1"
|
---|
53 | "STANDARD-SUM"
|
---|
54 | "SATURATION-EXTENSION"
|
---|
55 | "ALIST->POLY"
|
---|
56 | "UNIVERSAL-EZGCD"
|
---|
57 | "S-POLYNOMIAL"
|
---|
58 | "POLY-CONTENT"
|
---|
59 | "POLY-PRIMITIVE-PART"
|
---|
60 | "SATURATION-EXTENSION-1")
|
---|
61 | (:documentation "Implements polynomials. A polynomial is essentially
|
---|
62 | a mapping of monomials of the same degree to coefficients. The
|
---|
63 | momomials are ordered according to a monomial order."))
|
---|
64 |
|
---|
65 | (in-package :polynomial)
|
---|
66 |
|
---|
67 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
68 |
|
---|
69 | (defclass poly ()
|
---|
70 | ((dimension :initform nil
|
---|
71 | :initarg :dimension
|
---|
72 | :accessor poly-dimension
|
---|
73 | :documentation "Shared dimension of all terms, the number of variables")
|
---|
74 | (termlist :initform nil :initarg :termlist :accessor poly-termlist
|
---|
75 | :documentation "List of terms.")
|
---|
76 | (order :initform #'lex> :initarg :order :accessor poly-term-order
|
---|
77 | :documentation "Monomial/term order."))
|
---|
78 | (:default-initargs :dimension nil :termlist nil :order #'lex>)
|
---|
79 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
---|
80 | according to term order ORDER, which defaults to LEX>."))
|
---|
81 |
|
---|
82 | (defmethod print-object ((self poly) stream)
|
---|
83 | (print-unreadable-object (self stream :type t :identity t)
|
---|
84 | (with-accessors ((dimension poly-dimension)
|
---|
85 | (termlist poly-termlist)
|
---|
86 | (order poly-term-order))
|
---|
87 | self
|
---|
88 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
|
---|
89 | dimension termlist order))))
|
---|
90 |
|
---|
91 | (defgeneric change-term-order (self other)
|
---|
92 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
---|
93 | (:method ((self poly) (other poly))
|
---|
94 | (unless (eq (poly-term-order self) (poly-term-order other))
|
---|
95 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
---|
96 | (poly-term-order self) (poly-term-order other)))
|
---|
97 | self))
|
---|
98 |
|
---|
99 | (defgeneric poly-insert-term (self term)
|
---|
100 | (:documentation "Insert a term TERM into SELF before all other
|
---|
101 | terms. Order is not enforced.")
|
---|
102 | (:method ((self poly) (term term))
|
---|
103 | (cond ((null (poly-dimension self))
|
---|
104 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
105 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
106 | (push term (poly-termlist self))
|
---|
107 | self))
|
---|
108 |
|
---|
109 | (defgeneric poly-append-term (self term)
|
---|
110 | (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
|
---|
111 | (:method ((self poly) (term term))
|
---|
112 | (cond ((null (poly-dimension self))
|
---|
113 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
114 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
115 | (setf (cdr (last (poly-termlist self))) (list term))
|
---|
116 | self))
|
---|
117 |
|
---|
118 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
|
---|
119 | "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
|
---|
120 | It can be used to enter simple polynomials by hand, e.g the polynomial
|
---|
121 | in two variables, X and Y, given in standard notation as:
|
---|
122 |
|
---|
123 | 3*X^2*Y^3+2*Y+7
|
---|
124 |
|
---|
125 | can be entered as
|
---|
126 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
|
---|
127 |
|
---|
128 | NOTE: The primary use is for low-level debugging of the package."
|
---|
129 | (dolist (x alist poly)
|
---|
130 | (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
|
---|
131 |
|
---|
132 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
|
---|
133 | "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
134 | (reinitialize-instance new
|
---|
135 | :dimension (monom-dimension old)
|
---|
136 | :termlist (list (cons old 1))))
|
---|
137 |
|
---|
138 | (defmethod universal-equalp ((self poly) (other poly))
|
---|
139 | "Implements equality of polynomials."
|
---|
140 | (and (eql (poly-dimension self) (poly-dimension other))
|
---|
141 | (every #'universal-equalp (poly-termlist self) (poly-termlist other))
|
---|
142 | (eq (poly-term-order self) (poly-term-order other))))
|
---|
143 |
|
---|
144 | (defgeneric leading-term (object)
|
---|
145 | (:method ((self poly))
|
---|
146 | (car (poly-termlist self)))
|
---|
147 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
148 |
|
---|
149 | (defgeneric second-leading-term (object)
|
---|
150 | (:method ((self poly))
|
---|
151 | (cadar (poly-termlist self)))
|
---|
152 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
153 |
|
---|
154 | (defgeneric leading-monomial (object)
|
---|
155 | (:method ((self poly))
|
---|
156 | (change-class (copy-instance (leading-term self)) 'monom))
|
---|
157 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
158 |
|
---|
159 | (defgeneric second-leading-monomial (object)
|
---|
160 | (:method ((self poly))
|
---|
161 | (change-class (copy-instance (second-leading-term self)) 'monom))
|
---|
162 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
163 |
|
---|
164 | (defgeneric leading-coefficient (object)
|
---|
165 | (:method ((self poly))
|
---|
166 | (term-coeff (leading-term self)))
|
---|
167 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
168 |
|
---|
169 | (defgeneric second-leading-coefficient (object)
|
---|
170 | (:method ((self poly))
|
---|
171 | (term-coeff (second-leading-term self)))
|
---|
172 | (:documentation "The second leading coefficient of a polynomial. It
|
---|
173 | signals error for a polynomial with at most one term."))
|
---|
174 |
|
---|
175 | (defmethod universal-zerop ((self poly))
|
---|
176 | "Return T iff SELF is a zero polynomial."
|
---|
177 | (null (poly-termlist self)))
|
---|
178 |
|
---|
179 | (defgeneric poly-length (self)
|
---|
180 | (:documentation "Return the number of terms.")
|
---|
181 | (:method ((self poly))
|
---|
182 | (length (poly-termlist self))))
|
---|
183 |
|
---|
184 | (defgeneric scalar-multiply-by (self other)
|
---|
185 | (:documentation "Multiply vector SELF by a scalar OTHER.")
|
---|
186 | (:method ((self poly) other)
|
---|
187 | (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
|
---|
188 | (poly-termlist self))
|
---|
189 | self))
|
---|
190 |
|
---|
191 | (defgeneric scalar-divide-by (self other)
|
---|
192 | (:documentation "Divide vector SELF by a scalar OTHER.")
|
---|
193 | (:method ((self poly) other)
|
---|
194 | (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
|
---|
195 | (poly-termlist self))
|
---|
196 | self))
|
---|
197 |
|
---|
198 | (defmethod multiply-by ((self poly) (other monom))
|
---|
199 | "Multiply a polynomial SELF by OTHER."
|
---|
200 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
201 | (poly-termlist self))
|
---|
202 | self)
|
---|
203 |
|
---|
204 | (defmethod multiply-by ((self poly) (other term))
|
---|
205 | "Multiply a polynomial SELF by OTHER."
|
---|
206 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
207 | (poly-termlist self))
|
---|
208 | self)
|
---|
209 |
|
---|
210 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
---|
211 | "Return an expression which will efficiently adds/subtracts two
|
---|
212 | polynomials, P and Q. The addition/subtraction of coefficients is
|
---|
213 | performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
|
---|
214 | is supplied, it is used to negate the coefficients of Q which do not
|
---|
215 | have a corresponding coefficient in P. The code implements an
|
---|
216 | efficient algorithm to add two polynomials represented as sorted lists
|
---|
217 | of terms. The code destroys both arguments, reusing the terms to build
|
---|
218 | the result."
|
---|
219 | `(macrolet ((lc (x) `(term-coeff (car ,x))))
|
---|
220 | (do ((p ,p)
|
---|
221 | (q ,q)
|
---|
222 | r)
|
---|
223 | ((or (endp p) (endp q))
|
---|
224 | ;; NOTE: R contains the result in reverse order. Can it
|
---|
225 | ;; be more efficient to produce the terms in correct order?
|
---|
226 | (unless (endp q)
|
---|
227 | ;; Upon subtraction, we must change the sign of
|
---|
228 | ;; all coefficients in q
|
---|
229 | ,@(when uminus-fn
|
---|
230 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
---|
231 | (setf r (nreconc r q)))
|
---|
232 | r)
|
---|
233 | (multiple-value-bind
|
---|
234 | (greater-p equal-p)
|
---|
235 | (funcall ,order-fn (car p) (car q))
|
---|
236 | (cond
|
---|
237 | (greater-p
|
---|
238 | (rotatef (cdr p) r p)
|
---|
239 | )
|
---|
240 | (equal-p
|
---|
241 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
---|
242 | (cond
|
---|
243 | ((universal-zerop s)
|
---|
244 | (setf p (cdr p))
|
---|
245 | )
|
---|
246 | (t
|
---|
247 | (setf (lc p) s)
|
---|
248 | (rotatef (cdr p) r p))))
|
---|
249 | (setf q (cdr q))
|
---|
250 | )
|
---|
251 | (t
|
---|
252 | ;;Negate the term of Q if UMINUS provided, signallig
|
---|
253 | ;;that we are doing subtraction
|
---|
254 | ,(when uminus-fn
|
---|
255 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
---|
256 | (rotatef (cdr q) r q)))))))
|
---|
257 |
|
---|
258 |
|
---|
259 | (defgeneric add-to (self other)
|
---|
260 | (:documentation "Add OTHER to SELF.")
|
---|
261 | (:method ((self number) (other number))
|
---|
262 | (+ self other)))
|
---|
263 |
|
---|
264 | (defgeneric subtract-from (self other)
|
---|
265 | (:documentation "Subtract OTHER from SELF.")
|
---|
266 | (:method ((self number) (other number))
|
---|
267 | (- self other)))
|
---|
268 |
|
---|
269 | (defmacro def-add/subtract-method (add/subtract-method-name
|
---|
270 | uminus-method-name
|
---|
271 | &optional
|
---|
272 | (doc-string nil doc-string-supplied-p))
|
---|
273 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
274 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
---|
275 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
276 | ;; Ensure orders are compatible
|
---|
277 | (change-term-order other self)
|
---|
278 | (setf (poly-termlist self) (fast-add/subtract
|
---|
279 | (poly-termlist self) (poly-termlist other)
|
---|
280 | (poly-term-order self)
|
---|
281 | #',add/subtract-method-name
|
---|
282 | ,(when uminus-method-name `(function ,uminus-method-name))))
|
---|
283 | self))
|
---|
284 |
|
---|
285 | (eval-when (:compile-toplevel :load-toplevel :execute)
|
---|
286 |
|
---|
287 | (def-add/subtract-method add-to nil
|
---|
288 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
289 | This operation destructively modifies both polynomials.
|
---|
290 | The result is stored in SELF. This implementation does
|
---|
291 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
292 |
|
---|
293 | (def-add/subtract-method subtract-from unary-minus
|
---|
294 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
295 | This operation destructively modifies both polynomials.
|
---|
296 | The result is stored in SELF. This implementation does
|
---|
297 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
298 | )
|
---|
299 |
|
---|
300 | (defmethod unary-minus ((self poly))
|
---|
301 | "Destructively modifies the coefficients of the polynomial SELF,
|
---|
302 | by changing their sign."
|
---|
303 | (mapc #'unary-minus (poly-termlist self))
|
---|
304 | self)
|
---|
305 |
|
---|
306 | (defun add-termlists (p q order-fn)
|
---|
307 | "Destructively adds two termlists P and Q ordered according to ORDER-FN."
|
---|
308 | (fast-add/subtract p q order-fn #'add-to nil))
|
---|
309 |
|
---|
310 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
---|
311 | &optional (reverse-arg-order-P nil))
|
---|
312 | "Multiplies term TERM by a list of term, TERMLIST.
|
---|
313 | Takes into accound divisors of zero in the ring, by
|
---|
314 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
---|
315 | is T, change the order of arguments; this may be important
|
---|
316 | if we extend the package to non-commutative rings."
|
---|
317 | `(mapcan #'(lambda (other-term)
|
---|
318 | (let ((prod (multiply
|
---|
319 | ,@(cond
|
---|
320 | (reverse-arg-order-p
|
---|
321 | `(other-term ,term))
|
---|
322 | (t
|
---|
323 | `(,term other-term))))))
|
---|
324 | (cond
|
---|
325 | ((universal-zerop prod) nil)
|
---|
326 | (t (list prod)))))
|
---|
327 | ,termlist))
|
---|
328 |
|
---|
329 | (defun multiply-termlists (p q order-fn)
|
---|
330 | "A version of polynomial multiplication, operating
|
---|
331 | directly on termlists."
|
---|
332 | (cond
|
---|
333 | ((or (endp p) (endp q))
|
---|
334 | ;;p or q is 0 (represented by NIL)
|
---|
335 | nil)
|
---|
336 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
---|
337 | ((endp (cdr p))
|
---|
338 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
---|
339 | ((endp (cdr q))
|
---|
340 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
---|
341 | (t
|
---|
342 | (cons (multiply (car p) (car q))
|
---|
343 | (add-termlists
|
---|
344 | (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
|
---|
345 | (multiply-termlists (cdr p) q order-fn)
|
---|
346 | order-fn)))))
|
---|
347 |
|
---|
348 | (defmethod multiply-by ((self poly) (other poly))
|
---|
349 | (change-term-order other self)
|
---|
350 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
---|
351 | (poly-termlist other)
|
---|
352 | (poly-term-order self)))
|
---|
353 | self)
|
---|
354 |
|
---|
355 | (defun add (object1 object2)
|
---|
356 | "Non-destructively add POLY1 by POLY2."
|
---|
357 | (add-to (copy-instance object1) (copy-instance object2)))
|
---|
358 |
|
---|
359 | (defun subtract (minuend &rest subtrahends)
|
---|
360 | "Non-destructively subtract MINUEND and SUBTRAHENDS."
|
---|
361 | (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))
|
---|
362 |
|
---|
363 | (defmethod left-tensor-product-by ((self poly) (other monom))
|
---|
364 | (setf (poly-termlist self)
|
---|
365 | (mapcan #'(lambda (term)
|
---|
366 | (let ((prod (left-tensor-product-by term other)))
|
---|
367 | (cond
|
---|
368 | ((universal-zerop prod) nil)
|
---|
369 | (t (list prod)))))
|
---|
370 | (poly-termlist self)))
|
---|
371 | (incf (poly-dimension self) (monom-dimension other))
|
---|
372 | self)
|
---|
373 |
|
---|
374 | (defmethod right-tensor-product-by ((self poly) (other monom))
|
---|
375 | (setf (poly-termlist self)
|
---|
376 | (mapcan #'(lambda (term)
|
---|
377 | (let ((prod (right-tensor-product-by term other)))
|
---|
378 | (cond
|
---|
379 | ((universal-zerop prod) nil)
|
---|
380 | (t (list prod)))))
|
---|
381 | (poly-termlist self)))
|
---|
382 | (incf (poly-dimension self) (monom-dimension other))
|
---|
383 | self)
|
---|
384 |
|
---|
385 |
|
---|
386 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
|
---|
387 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
---|
388 | is a list of polynomials. Destructively modifies PLIST elements."
|
---|
389 | (mapc #'(lambda (poly)
|
---|
390 | (left-tensor-product-by
|
---|
391 | poly
|
---|
392 | (prog1
|
---|
393 | (make-monom-variable k i)
|
---|
394 | (incf i))))
|
---|
395 | plist))
|
---|
396 |
|
---|
397 | (defun standard-extension-1 (plist
|
---|
398 | &aux
|
---|
399 | (plist (standard-extension plist))
|
---|
400 | (nvars (poly-dimension (car plist))))
|
---|
401 | "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
|
---|
402 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
|
---|
403 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
404 | tantamount to replacing PI with UI*PI-1. It assumes that all
|
---|
405 | polynomials have the same dimension, and only the first polynomial
|
---|
406 | is examined to determine this dimension."
|
---|
407 | ;; Implementation note: we use STANDARD-EXTENSION and then subtract
|
---|
408 | ;; 1 from each polynomial; since UI*PI has no constant term,
|
---|
409 | ;; we just need to append the constant term at the end
|
---|
410 | ;; of each termlist.
|
---|
411 | (flet ((subtract-1 (p)
|
---|
412 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
413 | (setf plist (mapc #'subtract-1 plist)))
|
---|
414 | plist)
|
---|
415 |
|
---|
416 |
|
---|
417 | (defun standard-sum (plist
|
---|
418 | &aux
|
---|
419 | (plist (standard-extension plist))
|
---|
420 | (nvars (poly-dimension (car plist))))
|
---|
421 | "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
|
---|
422 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
|
---|
423 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
424 | tantamount to replacing PI with UI*PI, and the resulting polynomials
|
---|
425 | are added. Finally, 1 is subtracted. It should be noted that the term
|
---|
426 | order is not modified, which is equivalent to using a lexicographic
|
---|
427 | order on the first K variables."
|
---|
428 | (flet ((subtract-1 (p)
|
---|
429 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
430 | (subtract-1
|
---|
431 | (make-instance
|
---|
432 | 'poly
|
---|
433 | :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
|
---|
434 |
|
---|
435 | (defgeneric universal-ezgcd (x y)
|
---|
436 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
437 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
---|
438 | the Euclidean algorithm.")
|
---|
439 | (:method ((x integer) (y integer)
|
---|
440 | &aux (c (gcd x y)))
|
---|
441 | (values c (/ x c) (/ y c)))
|
---|
442 | )
|
---|
443 |
|
---|
444 | (defgeneric s-polynomial (object1 object2)
|
---|
445 | (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
|
---|
446 | (:method ((f poly) (g poly))
|
---|
447 | (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
|
---|
448 | (mf (divide lcm (leading-monomial f)))
|
---|
449 | (mg (divide lcm (leading-monomial g))))
|
---|
450 | (multiple-value-bind (c cf cg)
|
---|
451 | (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
|
---|
452 | (declare (ignore c))
|
---|
453 | (subtract
|
---|
454 | (multiply f (change-class mf 'term :coeff cg))
|
---|
455 | (multiply g (change-class mg 'term :coeff cf)))))))
|
---|
456 |
|
---|
457 | (defgeneric poly-content (object)
|
---|
458 | (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
|
---|
459 | (:method ((self poly))
|
---|
460 | (reduce #'universal-gcd
|
---|
461 | (mapcar #'term-coeff (rest (poly-termlist self)))
|
---|
462 | :initial-value (leading-coefficient self))))
|
---|
463 |
|
---|
464 | (defun poly-primitive-part (object)
|
---|
465 | "Divide polynomial OBJECT by gcd of its
|
---|
466 | coefficients. Return the resulting polynomial."
|
---|
467 | (scalar-divide-by object (poly-content object)))
|
---|
468 |
|
---|
469 | (defun poly-add-variables (self k)
|
---|
470 | (left-tensor-product-by self (make-instance 'monom :dimension k)))
|
---|
471 |
|
---|
472 | (defun saturation-extension (f plist &aux (k (length plist)))
|
---|
473 | "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
|
---|
474 | (nconc (mapc #'(lambda (x) (poly-add-variables x k)) f)
|
---|
475 | (standard-extension-1 plist)))
|
---|
476 |
|
---|
477 |
|
---|
478 | (defun polysaturation-extension (f plist &aux (k (length plist)))
|
---|
479 | "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]."
|
---|
480 | (nconc (mapc #'(lambda (x) (poly-add-variables x k)) f)
|
---|
481 | (standard-sum plist)))
|
---|
482 |
|
---|
483 | (defun saturation-extension-1 (f p)
|
---|
484 | "Given family of polynomials F and a polynomial P, calculate [F', U*P-1], where
|
---|
485 | F' is F with variable U inserted as the first variable. It destructively modifies F."
|
---|
486 | (polysaturation-extension f (list p)))
|
---|