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