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 | "MAKE-ZERO-FOR"
|
---|
57 | "MAKE-UNIT-FOR"
|
---|
58 | "UNIVERSAL-EXPT"
|
---|
59 | "UNIVERSAL-EQUALP"
|
---|
60 | "POLY-LENGTH"
|
---|
61 | "POLY-REVERSE"
|
---|
62 | "POLY-P"
|
---|
63 | "+LIST-MARKER+"
|
---|
64 | "POLY-EVAL")
|
---|
65 | (:documentation "Implements polynomials. A polynomial is essentially
|
---|
66 | a mapping of monomials of the same degree to coefficients. The
|
---|
67 | momomials are ordered according to a monomial order."))
|
---|
68 |
|
---|
69 | (in-package :polynomial)
|
---|
70 |
|
---|
71 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
72 |
|
---|
73 | (defclass poly ()
|
---|
74 | ((dimension :initform nil
|
---|
75 | :initarg :dimension
|
---|
76 | :accessor poly-dimension
|
---|
77 | :documentation "Shared dimension of all terms, the number of variables")
|
---|
78 | (termlist :initform nil :initarg :termlist :accessor poly-termlist
|
---|
79 | :documentation "List of terms.")
|
---|
80 | (order :initform #'lex> :initarg :order :accessor poly-term-order
|
---|
81 | :documentation "Monomial/term order."))
|
---|
82 | (:default-initargs :dimension nil :termlist nil :order #'lex>)
|
---|
83 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
---|
84 | according to term order ORDER, which defaults to LEX>."))
|
---|
85 |
|
---|
86 | (defmethod print-object ((self poly) stream)
|
---|
87 | (print-unreadable-object (self stream :type t :identity t)
|
---|
88 | (with-accessors ((dimension poly-dimension)
|
---|
89 | (termlist poly-termlist)
|
---|
90 | (order poly-term-order))
|
---|
91 | self
|
---|
92 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
|
---|
93 | dimension termlist order))))
|
---|
94 |
|
---|
95 | (defmethod copy-instance :around ((object poly) &rest initargs &key &allow-other-keys)
|
---|
96 | "An :AROUND method of COPY-INSTANCE. It replaces term with a fresh copy of the TERMLIST."
|
---|
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 |
|
---|
105 | (defgeneric change-term-order (self other)
|
---|
106 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
---|
107 | (:method ((self poly) (other poly))
|
---|
108 | (unless (eq (poly-term-order self) (poly-term-order other))
|
---|
109 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
---|
110 | (poly-term-order self) (poly-term-order other)))
|
---|
111 | self))
|
---|
112 |
|
---|
113 | (defgeneric poly-insert-term (self term)
|
---|
114 | (:documentation "Insert a term TERM into SELF before all other
|
---|
115 | terms. Order is not enforced.")
|
---|
116 | (:method ((self poly) (term term))
|
---|
117 | (cond ((null (poly-dimension self))
|
---|
118 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
119 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
120 | (push term (poly-termlist self))
|
---|
121 | self))
|
---|
122 |
|
---|
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))
|
---|
126 | (cond ((null (poly-dimension self))
|
---|
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))
|
---|
130 | self))
|
---|
131 |
|
---|
132 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
|
---|
133 | "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
|
---|
134 | It can be used to enter simple polynomials by hand, e.g the polynomial
|
---|
135 | in two variables, X and Y, given in standard notation as:
|
---|
136 |
|
---|
137 | 3*X^2*Y^3+2*Y+7
|
---|
138 |
|
---|
139 | can be entered as
|
---|
140 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
|
---|
141 |
|
---|
142 | NOTE: The primary use is for low-level debugging of the package."
|
---|
143 | (dolist (x alist poly)
|
---|
144 | (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
|
---|
145 |
|
---|
146 | (defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
|
---|
147 | "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
148 | (reinitialize-instance new
|
---|
149 | :dimension (monom-dimension old)
|
---|
150 | :termlist (list old)))
|
---|
151 |
|
---|
152 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
|
---|
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)
|
---|
156 | :termlist (list (change-class old 'term))))
|
---|
157 |
|
---|
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))))
|
---|
163 |
|
---|
164 | (defgeneric leading-term (object)
|
---|
165 | (:method ((self poly))
|
---|
166 | (car (poly-termlist self)))
|
---|
167 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
168 |
|
---|
169 | (defgeneric second-leading-term (object)
|
---|
170 | (:method ((self poly))
|
---|
171 | (cadar (poly-termlist self)))
|
---|
172 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
173 |
|
---|
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 |
|
---|
184 | (defgeneric leading-coefficient (object)
|
---|
185 | (:method ((self poly))
|
---|
186 | (term-coeff (leading-term self)))
|
---|
187 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
188 |
|
---|
189 | (defgeneric second-leading-coefficient (object)
|
---|
190 | (:method ((self poly))
|
---|
191 | (term-coeff (second-leading-term self)))
|
---|
192 | (:documentation "The second leading coefficient of a polynomial. It
|
---|
193 | signals error for a polynomial with at most one term."))
|
---|
194 |
|
---|
195 | (defmethod universal-zerop ((self poly))
|
---|
196 | "Return T iff SELF is a zero polynomial."
|
---|
197 | (null (poly-termlist self)))
|
---|
198 |
|
---|
199 | (defgeneric poly-length (self)
|
---|
200 | (:documentation "Return the number of terms.")
|
---|
201 | (:method ((self poly))
|
---|
202 | (length (poly-termlist self))))
|
---|
203 |
|
---|
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 |
|
---|
218 | (defmethod unary-inverse :before ((self poly))
|
---|
219 | "Checks invertibility of a polynomial SELF. To be invertable, the
|
---|
220 | polynomial must be an invertible, constant polynomial."
|
---|
221 | (with-slots (termlist)
|
---|
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.")))
|
---|
226 |
|
---|
227 | (defmethod unary-inverse ((self poly))
|
---|
228 | "Returns the unary inverse of a polynomial SELF."
|
---|
229 | (with-slots (termlist)
|
---|
230 | self
|
---|
231 | (setf (car termlist) (unary-inverse (car termlist)))
|
---|
232 | self))
|
---|
233 |
|
---|
234 | (defmethod multiply-by ((self poly) (other monom))
|
---|
235 | "Multiply a polynomial SELF by OTHER."
|
---|
236 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
237 | (poly-termlist self))
|
---|
238 | self)
|
---|
239 |
|
---|
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 |
|
---|
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 |
|
---|
254 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
---|
255 | "Return an expression which will efficiently adds/subtracts two
|
---|
256 | polynomials, P and Q. The addition/subtraction of coefficients is
|
---|
257 | performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
|
---|
258 | used to negate the coefficients of Q which do not have a corresponding
|
---|
259 | coefficient in P. The code implements an efficient algorithm to add
|
---|
260 | two polynomials represented as sorted lists of terms. The code
|
---|
261 | destroys both arguments, reusing the terms to build the result."
|
---|
262 | `(macrolet ((lc (x) `(term-coeff (car ,x))))
|
---|
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?
|
---|
269 | (unless (endp q)
|
---|
270 | ;; Upon subtraction, we must change the sign of
|
---|
271 | ;; all coefficients in q
|
---|
272 | ,@(when uminus-fn
|
---|
273 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
---|
274 | (setf r (nreconc r q)))
|
---|
275 | (unless (endp p)
|
---|
276 | (setf r (nreconc r p)))
|
---|
277 | r)
|
---|
278 | (multiple-value-bind
|
---|
279 | (greater-p equal-p)
|
---|
280 | (funcall ,order-fn (car p) (car q))
|
---|
281 | (cond
|
---|
282 | (greater-p
|
---|
283 | (rotatef (cdr p) r p)
|
---|
284 | )
|
---|
285 | (equal-p
|
---|
286 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
---|
287 | (cond
|
---|
288 | ((universal-zerop s)
|
---|
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
|
---|
297 | ;;Negate the term of Q if UMINUS provided, signallig
|
---|
298 | ;;that we are doing subtraction
|
---|
299 | ,(when uminus-fn
|
---|
300 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
---|
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 | )))
|
---|
306 |
|
---|
307 |
|
---|
308 |
|
---|
309 | (defgeneric add-to (self other)
|
---|
310 | (:documentation "Add OTHER to SELF.")
|
---|
311 | (:method ((self number) (other number))
|
---|
312 | (+ self other))
|
---|
313 | (:method ((self poly) (other number))
|
---|
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)))
|
---|
317 |
|
---|
318 |
|
---|
319 | (defgeneric subtract-from (self other)
|
---|
320 | (:documentation "Subtract OTHER from SELF.")
|
---|
321 | (:method ((self number) (other number))
|
---|
322 | (- self other))
|
---|
323 | (:method ((self poly) (other number))
|
---|
324 | (subtract-from self (make-poly-constant (poly-dimension self) other))))
|
---|
325 |
|
---|
326 |
|
---|
327 | #|
|
---|
328 | (defmacro def-add/subtract-method (add/subtract-method-name
|
---|
329 | uminus-method-name
|
---|
330 | &optional
|
---|
331 | (doc-string nil doc-string-supplied-p))
|
---|
332 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
333 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
---|
334 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
335 | ;; Ensure orders are compatible
|
---|
336 | (change-term-order other self)
|
---|
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))))
|
---|
342 | self))
|
---|
343 |
|
---|
344 | (eval-when (:load-toplevel :execute)
|
---|
345 |
|
---|
346 | (def-add/subtract-method add-to nil
|
---|
347 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
348 | This operation destructively modifies both polynomials.
|
---|
349 | The result is stored in SELF. This implementation does
|
---|
350 | no 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.
|
---|
354 | This operation destructively modifies both polynomials.
|
---|
355 | The result is stored in SELF. This implementation does
|
---|
356 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
357 | )
|
---|
358 |
|
---|
359 | |#
|
---|
360 |
|
---|
361 | (defmethod unary-minus ((self poly))
|
---|
362 | "Destructively modifies the coefficients of the polynomial SELF,
|
---|
363 | by 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 |
|
---|
371 | (defun subtract-termlists (p q order-fn)
|
---|
372 | "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
|
---|
373 | (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
|
---|
374 |
|
---|
375 | (defmethod add-to ((self poly) (other poly))
|
---|
376 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
377 | This operation destructively modifies both polynomials.
|
---|
378 | The result is stored in SELF. This implementation does
|
---|
379 | no 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)
|
---|
383 | (poly-term-order self)))
|
---|
384 | self)
|
---|
385 |
|
---|
386 |
|
---|
387 | (defmethod subtract-from ((self poly) (other poly))
|
---|
388 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
389 | This operation destructively modifies both polynomials.
|
---|
390 | The result is stored in SELF. This implementation does
|
---|
391 | no 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)
|
---|
395 | (poly-term-order self)))
|
---|
396 | self)
|
---|
397 |
|
---|
398 |
|
---|
399 | (defmethod add-to ((self poly) (other term))
|
---|
400 | "Adds to a polynomial SELF a term OTHER. The term OTHER is not
|
---|
401 | modified."
|
---|
402 | (add-to self (change-class (copy-instance other) 'poly)))
|
---|
403 |
|
---|
404 | (defmethod subtract-from ((self poly) (other term))
|
---|
405 | "Subtracts from a polynomial SELF a term OTHER. The term OTHER is not
|
---|
406 | modified."
|
---|
407 | (subtract-from self (change-class (copy-instance other) 'poly)))
|
---|
408 |
|
---|
409 |
|
---|
410 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
---|
411 | &optional (reverse-arg-order-P nil))
|
---|
412 | "Multiplies term TERM by a list of term, TERMLIST.
|
---|
413 | Takes into accound divisors of zero in the ring, by
|
---|
414 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
---|
415 | is T, change the order of arguments; this may be important
|
---|
416 | if we extend the package to non-commutative rings."
|
---|
417 | `(mapcan #'(lambda (other-term)
|
---|
418 | (let ((prod (multiply
|
---|
419 | ,@(cond
|
---|
420 | (reverse-arg-order-p
|
---|
421 | `(other-term ,term))
|
---|
422 | (t
|
---|
423 | `(,term other-term))))))
|
---|
424 | (cond
|
---|
425 | ((universal-zerop prod) nil)
|
---|
426 | (t (list prod)))))
|
---|
427 | ,termlist))
|
---|
428 |
|
---|
429 | (defun multiply-termlists (p q order-fn)
|
---|
430 | "A version of polynomial multiplication, operating
|
---|
431 | directly on termlists."
|
---|
432 | (cond
|
---|
433 | ((or (endp p) (endp q))
|
---|
434 | ;;p or q is 0 (represented by NIL)
|
---|
435 | nil)
|
---|
436 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
---|
437 | ((endp (cdr p))
|
---|
438 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
---|
439 | ((endp (cdr q))
|
---|
440 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
---|
441 | (t
|
---|
442 | (cons (multiply (car p) (car q))
|
---|
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)))))
|
---|
447 |
|
---|
448 | (defmethod multiply-by ((self poly) (other poly))
|
---|
449 | (change-term-order other self)
|
---|
450 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
---|
451 | (poly-termlist other)
|
---|
452 | (poly-term-order self)))
|
---|
453 | self)
|
---|
454 |
|
---|
455 | (defgeneric add-2 (object1 object2)
|
---|
456 | (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
|
---|
457 | (:method ((object1 t) (object2 t))
|
---|
458 | (add-to (copy-instance object1) (copy-instance object2))))
|
---|
459 |
|
---|
460 | (defun add (&rest summands)
|
---|
461 | "Non-destructively adds list SUMMANDS."
|
---|
462 | (cond ((endp summands) 0)
|
---|
463 | (t (reduce #'add-2 summands))))
|
---|
464 |
|
---|
465 | (defun subtract (minuend &rest subtrahends)
|
---|
466 | "Non-destructively subtract MINUEND and SUBTRAHENDS."
|
---|
467 | (cond ((endp subtrahends) (unary-minus minuend))
|
---|
468 | (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
|
---|
469 |
|
---|
470 | (defmethod left-tensor-product-by ((self poly) (other monom))
|
---|
471 | (setf (poly-termlist self)
|
---|
472 | (mapcan #'(lambda (term)
|
---|
473 | (let ((prod (left-tensor-product-by term other)))
|
---|
474 | (cond
|
---|
475 | ((universal-zerop prod) nil)
|
---|
476 | (t (list prod)))))
|
---|
477 | (poly-termlist self)))
|
---|
478 | (incf (poly-dimension self) (monom-dimension other))
|
---|
479 | self)
|
---|
480 |
|
---|
481 | (defmethod right-tensor-product-by ((self poly) (other monom))
|
---|
482 | (setf (poly-termlist self)
|
---|
483 | (mapcan #'(lambda (term)
|
---|
484 | (let ((prod (right-tensor-product-by term other)))
|
---|
485 | (cond
|
---|
486 | ((universal-zerop prod) nil)
|
---|
487 | (t (list prod)))))
|
---|
488 | (poly-termlist self)))
|
---|
489 | (incf (poly-dimension self) (monom-dimension other))
|
---|
490 | self)
|
---|
491 |
|
---|
492 |
|
---|
493 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
|
---|
494 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
---|
495 | is a list of polynomials. Destructively modifies PLIST elements."
|
---|
496 | (mapc #'(lambda (poly)
|
---|
497 | (left-tensor-product-by
|
---|
498 | poly
|
---|
499 | (prog1
|
---|
500 | (make-monom-variable k i)
|
---|
501 | (incf i))))
|
---|
502 | plist))
|
---|
503 |
|
---|
504 | (defun standard-extension-1 (plist
|
---|
505 | &aux
|
---|
506 | (plist (standard-extension plist))
|
---|
507 | (nvars (poly-dimension (car plist))))
|
---|
508 | "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
|
---|
509 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
|
---|
510 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
511 | tantamount to replacing PI with UI*PI-1. It assumes that all
|
---|
512 | polynomials have the same dimension, and only the first polynomial
|
---|
513 | is examined to determine this dimension."
|
---|
514 | ;; Implementation note: we use STANDARD-EXTENSION and then subtract
|
---|
515 | ;; 1 from each polynomial; since UI*PI has no constant term,
|
---|
516 | ;; we just need to append the constant term at the end
|
---|
517 | ;; of each termlist.
|
---|
518 | (flet ((subtract-1 (p)
|
---|
519 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
520 | (setf plist (mapc #'subtract-1 plist)))
|
---|
521 | plist)
|
---|
522 |
|
---|
523 |
|
---|
524 | (defun standard-sum (plist
|
---|
525 | &aux
|
---|
526 | (plist (standard-extension plist))
|
---|
527 | (nvars (poly-dimension (car plist))))
|
---|
528 | "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
|
---|
529 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
|
---|
530 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
531 | tantamount to replacing PI with UI*PI, and the resulting polynomials
|
---|
532 | are added. Finally, 1 is subtracted. It should be noted that the term
|
---|
533 | order is not modified, which is equivalent to using a lexicographic
|
---|
534 | order on the first K variables."
|
---|
535 | (flet ((subtract-1 (p)
|
---|
536 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
537 | (subtract-1
|
---|
538 | (make-instance
|
---|
539 | 'poly
|
---|
540 | :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
|
---|
541 |
|
---|
542 | (defgeneric universal-ezgcd (x y)
|
---|
543 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
544 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
---|
545 | the Euclidean algorithm.")
|
---|
546 | (:method ((x integer) (y integer)
|
---|
547 | &aux (c (gcd x y)))
|
---|
548 | (values c (/ x c) (/ y c)))
|
---|
549 | )
|
---|
550 |
|
---|
551 | (defgeneric s-polynomial (object1 object2)
|
---|
552 | (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
|
---|
553 | (:method ((f poly) (g poly))
|
---|
554 | (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
|
---|
555 | (mf (divide lcm (leading-monomial f)))
|
---|
556 | (mg (divide lcm (leading-monomial g))))
|
---|
557 | (multiple-value-bind (c cf cg)
|
---|
558 | (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
|
---|
559 | (declare (ignore c))
|
---|
560 | (subtract
|
---|
561 | (multiply f (change-class mf 'term :coeff cg))
|
---|
562 | (multiply g (change-class mg 'term :coeff cf)))))))
|
---|
563 |
|
---|
564 | (defgeneric poly-content (object)
|
---|
565 | (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
|
---|
566 | (:method ((self poly))
|
---|
567 | (reduce #'universal-gcd
|
---|
568 | (mapcar #'term-coeff (rest (poly-termlist self)))
|
---|
569 | :initial-value (leading-coefficient self))))
|
---|
570 |
|
---|
571 | (defun poly-primitive-part (object)
|
---|
572 | "Divide polynomial OBJECT by gcd of its
|
---|
573 | coefficients. Return the resulting polynomial."
|
---|
574 | (scalar-divide-by object (poly-content object)))
|
---|
575 |
|
---|
576 | (defun poly-insert-variables (self k)
|
---|
577 | (left-tensor-product-by self (make-instance 'monom :dimension k)))
|
---|
578 |
|
---|
579 | (defun saturation-extension (f plist &aux (k (length plist)))
|
---|
580 | "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
|
---|
581 | PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
|
---|
582 | as first K variables. It destructively modifies F and PLIST."
|
---|
583 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
584 | (standard-extension-1 plist)))
|
---|
585 |
|
---|
586 | (defun polysaturation-extension (f plist &aux (k (length plist)))
|
---|
587 | "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
|
---|
588 | and F' is F with variables U1,U2,...,UK inserted as first K
|
---|
589 | variables. It destructively modifies F and PLIST."
|
---|
590 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
591 | (list (standard-sum plist))))
|
---|
592 |
|
---|
593 | (defun saturation-extension-1 (f p)
|
---|
594 | "Given family of polynomials F and a polynomial P, calculate [F',
|
---|
595 | U*P-1], where F' is F with variable inserted as the first variable. It
|
---|
596 | destructively modifies F and P."
|
---|
597 | (polysaturation-extension f (list p)))
|
---|
598 |
|
---|
599 | (defmethod multiply-by ((object1 number) (object2 poly))
|
---|
600 | (scalar-multiply-by (copy-instance object2) object1))
|
---|
601 |
|
---|
602 | (defmethod multiply-by ((object1 poly) (object2 number))
|
---|
603 | (scalar-multiply-by (copy-instance object1) object2))
|
---|
604 |
|
---|
605 | (defun make-poly-variable (nvars pos &optional (power 1))
|
---|
606 | (change-class (make-monom-variable nvars pos power) 'poly))
|
---|
607 |
|
---|
608 | (defun make-poly-constant (nvars coeff)
|
---|
609 | (change-class (make-term-constant nvars coeff) 'poly))
|
---|
610 |
|
---|
611 | (defgeneric universal-expt (x y)
|
---|
612 | (:documentation "Raises X to power Y.")
|
---|
613 | (:method ((x number) (y integer)) (expt x y))
|
---|
614 | (:method ((x t) (y integer))
|
---|
615 | (declare (type fixnum y))
|
---|
616 | (cond
|
---|
617 | ((minusp y) (error "universal-expt: Negative exponent."))
|
---|
618 | ((universal-zerop x) (if (zerop y) 1))
|
---|
619 | (t
|
---|
620 | (do ((k 1 (ash k 1))
|
---|
621 | (q x (multiply q q)) ;keep squaring
|
---|
622 | (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
|
---|
623 | ((> k y) p)
|
---|
624 | (declare (fixnum k)))))))
|
---|
625 |
|
---|
626 | (defgeneric poly-p (object)
|
---|
627 | (:documentation "Checks if an object is a polynomial.")
|
---|
628 | (:method ((self poly)) t)
|
---|
629 | (:method ((self t)) nil))
|
---|
630 |
|
---|
631 | (defmethod ->sexp :before ((self poly) &optional vars)
|
---|
632 | "Ensures that the number of variables in VARS maches the polynomial dimension of the
|
---|
633 | polynomial SELF."
|
---|
634 | (with-slots (dimension)
|
---|
635 | self
|
---|
636 | (assert (= (length vars) dimension)
|
---|
637 | nil
|
---|
638 | "Number of variables ~S does not match the dimension ~S"
|
---|
639 | vars dimension)))
|
---|
640 |
|
---|
641 | (defmethod ->sexp ((self poly) &optional vars)
|
---|
642 | "Converts a polynomial SELF to a sexp."
|
---|
643 | (let ((m (mapcar #'(lambda (x) (->sexp x vars))
|
---|
644 | (poly-termlist self))))
|
---|
645 | (cond ((endp m) 0)
|
---|
646 | ((endp (cdr m)) (car m))
|
---|
647 | (t (cons '+ m)))))
|
---|
648 |
|
---|
649 | (defparameter +list-marker+ :[
|
---|
650 | "A sexp with this head is considered a list of polynomials.")
|
---|
651 |
|
---|
652 | (defmethod ->sexp ((self cons) &optional vars)
|
---|
653 | (assert (eql (car self) +list-marker+))
|
---|
654 | (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
|
---|
655 |
|
---|
656 |
|
---|
657 | (defun poly-eval (expr vars order)
|
---|
658 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
659 | variables VARS. Return the resulting polynomial or list of
|
---|
660 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
661 | replaced with their analogues in the ring of polynomials, and the
|
---|
662 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
663 | of polynomials in internal form. A similar operation in another computer
|
---|
664 | algebra system could be called 'expand' or so."
|
---|
665 | (labels ((p-eval (p) (poly-eval p vars order))
|
---|
666 | (p-eval-list (plist) (mapcar #'p-eval plist)))
|
---|
667 | (cond
|
---|
668 | ((eq expr 0)
|
---|
669 | (make-instance 'poly :dimension (length vars)))
|
---|
670 | ((member expr vars :test #'equalp)
|
---|
671 | (let ((pos (position expr vars :test #'equalp)))
|
---|
672 | (make-poly-variable (length vars) pos)))
|
---|
673 | ((atom expr)
|
---|
674 | (make-poly-constant (length vars) expr))
|
---|
675 | ((eq (car expr) +list-marker+)
|
---|
676 | (cons +list-marker+ (p-eval-list (cdr expr))))
|
---|
677 | (t
|
---|
678 | (case (car expr)
|
---|
679 | (+ (reduce #'add (p-eval-list (cdr expr))))
|
---|
680 | (- (apply #'subtract (p-eval-list (cdr expr))))
|
---|
681 | (*
|
---|
682 | (if (endp (cddr expr)) ;unary
|
---|
683 | (p-eval (cadr expr))
|
---|
684 | (apply #'multiply (p-eval-list (cdr expr)))))
|
---|
685 | (/
|
---|
686 | ;; A polynomial can be divided by a scalar
|
---|
687 | (cond
|
---|
688 | ((endp (cddr expr))
|
---|
689 | ;; A special case (/ ?), the inverse
|
---|
690 | (divide (cadr expr)))
|
---|
691 | (t
|
---|
692 | (let ((num (p-eval (cadr expr)))
|
---|
693 | (denom-inverse (apply #'divide (mapcar #'p-eval (cddr expr)))))
|
---|
694 | (multiply denom-inverse num)))))
|
---|
695 | (expt
|
---|
696 | (cond
|
---|
697 | ((member (cadr expr) vars :test #'equalp)
|
---|
698 | ;;Special handling of (expt var pow)
|
---|
699 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
700 | (make-poly-variable (length vars) pos (caddr expr))))
|
---|
701 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
702 | ;; Negative power means division in coefficient ring
|
---|
703 | ;; Non-integer power means non-polynomial coefficient
|
---|
704 | expr)
|
---|
705 | (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
|
---|
706 | (otherwise
|
---|
707 | (error "Cannot evaluate as polynomial: ~A" expr)))))))
|
---|
708 |
|
---|
709 | (defgeneric make-zero-for (self)
|
---|
710 | (:method ((self poly))
|
---|
711 | (make-instance 'poly :dimension (poly-dimension self))))
|
---|
712 |
|
---|
713 | (defgeneric make-unit-for (self)
|
---|
714 | (:method ((self poly))
|
---|
715 | (make-poly-constant (poly-dimension self) 1)))
|
---|
716 |
|
---|
717 | (defgeneric poly-reverse (self)
|
---|
718 | (:documentation "Reverse the order of terms in a polynomial SELF.")
|
---|
719 | (:method ((self poly))
|
---|
720 | (with-slots (termlist)
|
---|
721 | self
|
---|
722 | (setf termlist (nreverse termlist)))
|
---|
723 | self))
|
---|
724 |
|
---|
725 |
|
---|
726 |
|
---|