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