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 :ring :monom :order :term #| :infix |# )
|
---|
24 | (:export "POLY"
|
---|
25 | "POLY-TERMLIST"
|
---|
26 | "POLY-TERM-ORDER")
|
---|
27 | (:documentation "Implements polynomials"))
|
---|
28 |
|
---|
29 | (in-package :polynomial)
|
---|
30 |
|
---|
31 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
32 |
|
---|
33 | (defclass poly ()
|
---|
34 | ((termlist :initarg :termlist :accessor poly-termlist)
|
---|
35 | (order :initarg :order :accessor poly-term-order))
|
---|
36 | (:default-initargs :termlist nil :order #'lex>))
|
---|
37 |
|
---|
38 | (defmethod print-object ((self poly) stream)
|
---|
39 | (format stream "#<POLY TERMLIST=~A ORDER=~A>"
|
---|
40 | (poly-termlist self)
|
---|
41 | (poly-term-order self)))
|
---|
42 |
|
---|
43 | (defmethod r-equalp ((self poly) (other poly))
|
---|
44 | (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
|
---|
45 | (eq (poly-term-order self) (poly-term-order other))))
|
---|
46 |
|
---|
47 | (defmethod insert-item ((self poly) (item term))
|
---|
48 | (push item (poly-termlist self))
|
---|
49 | self)
|
---|
50 |
|
---|
51 | (defmethod append-item ((self poly) (item term))
|
---|
52 | (setf (cdr (last (poly-termlist self))) (list item))
|
---|
53 | self)
|
---|
54 |
|
---|
55 | ;; Leading term
|
---|
56 | (defgeneric leading-term (object)
|
---|
57 | (:method ((self poly))
|
---|
58 | (car (poly-termlist self)))
|
---|
59 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
60 |
|
---|
61 | ;; Second term
|
---|
62 | (defgeneric second-leading-term (object)
|
---|
63 | (:method ((self poly))
|
---|
64 | (cadar (poly-termlist self)))
|
---|
65 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
66 |
|
---|
67 | ;; Leading coefficient
|
---|
68 | (defgeneric leading-coefficient (object)
|
---|
69 | (:method ((self poly))
|
---|
70 | (r-coeff (leading-term self)))
|
---|
71 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
72 |
|
---|
73 | ;; Second coefficient
|
---|
74 | (defgeneric second-leading-coefficient (object)
|
---|
75 | (:method ((self poly))
|
---|
76 | (r-coeff (second-leading-term self)))
|
---|
77 | (:documentation "The second leading coefficient of a polynomial. It signals error for a polynomial with at most one term."))
|
---|
78 |
|
---|
79 | ;; Testing for a zero polynomial
|
---|
80 | (defmethod r-zerop ((self poly))
|
---|
81 | (null (poly-termlist self)))
|
---|
82 |
|
---|
83 | ;; The number of terms
|
---|
84 | (defmethod r-length ((self poly))
|
---|
85 | (length (poly-termlist self)))
|
---|
86 |
|
---|
87 | (defmethod multiply-by ((self poly) (other monom))
|
---|
88 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
89 | (poly-termlist self))
|
---|
90 | self)
|
---|
91 |
|
---|
92 | (defmethod multiply-by ((self poly) (other scalar))
|
---|
93 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
94 | (poly-termlist self))
|
---|
95 | self)
|
---|
96 |
|
---|
97 |
|
---|
98 | (defun fast-addition (p q order-fn add-fun)
|
---|
99 | (macrolet ((lc (x) `(r-coeff (car ,x))))
|
---|
100 | (do ((p p)
|
---|
101 | (q q)
|
---|
102 | r)
|
---|
103 | ((or (endp p) (endp q))
|
---|
104 | (unless (endp q) (setf r (nreconc r q)))
|
---|
105 | r)
|
---|
106 | (multiple-value-bind
|
---|
107 | (greater-p equal-p)
|
---|
108 | (funcall order-fn (car p) (car q))
|
---|
109 | (cond
|
---|
110 | (greater-p
|
---|
111 | (rotatef (cdr p) r p)
|
---|
112 | )
|
---|
113 | (equal-p
|
---|
114 | (let ((s (funcall add-fun (lc p) (lc q))))
|
---|
115 | (print s)
|
---|
116 | (cond
|
---|
117 | ((r-zerop s)
|
---|
118 | (setf p (cdr p))
|
---|
119 | )
|
---|
120 | (t
|
---|
121 | (setf (lc p) s)
|
---|
122 | (rotatef (cdr p) r p))))
|
---|
123 | (setf q (cdr q))
|
---|
124 | )
|
---|
125 | (t
|
---|
126 | (rotatef (cdr q) r q)))))))
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | (defmacro def-additive-operation-method (method-name &optional (doc-string nil doc-string-supplied-p))
|
---|
131 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
132 | `(defmethod ,method-name ((self poly) (other poly))
|
---|
133 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
134 | (with-slots ((termlist1 termlist) (order1 order))
|
---|
135 | self
|
---|
136 | (with-slots ((termlist2 termlist) (order2 order))
|
---|
137 | other
|
---|
138 | ;; Ensure orders are compatible
|
---|
139 | (unless (eq order1 order2)
|
---|
140 | (setf termlist2 (sort termlist2 order1)
|
---|
141 | order2 order1))
|
---|
142 | (setf termlist1 (fast-addition termlist1 termlist2 order1 #',method-name))))
|
---|
143 | self))
|
---|
144 |
|
---|
145 | (def-additive-operation-method add-to
|
---|
146 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
147 | This operation destructively modifies both polynomials.
|
---|
148 | The result is stored in SELF. This implementation does
|
---|
149 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
150 |
|
---|
151 | (def-additive-operation-method subtract-from
|
---|
152 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
153 | This operation destructively modifies both polynomials.
|
---|
154 | The result is stored in SELF. This implementation does
|
---|
155 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
156 |
|
---|
157 | (defmethod unary-uminus ((self poly)))
|
---|
158 |
|
---|
159 | #|
|
---|
160 |
|
---|
161 | (defun poly-standard-extension (plist &aux (k (length plist)))
|
---|
162 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
|
---|
163 | (declare (list plist) (fixnum k))
|
---|
164 | (labels ((incf-power (g i)
|
---|
165 | (dolist (x (poly-termlist g))
|
---|
166 | (incf (monom-elt (term-monom x) i)))
|
---|
167 | (incf (poly-sugar g))))
|
---|
168 | (setf plist (poly-list-add-variables plist k))
|
---|
169 | (dotimes (i k plist)
|
---|
170 | (incf-power (nth i plist) i))))
|
---|
171 |
|
---|
172 | (defun saturation-extension (ring f plist
|
---|
173 | &aux
|
---|
174 | (k (length plist))
|
---|
175 | (d (monom-dimension (poly-lm (car plist))))
|
---|
176 | f-x plist-x)
|
---|
177 | "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
|
---|
178 | (declare (type ring ring))
|
---|
179 | (setf f-x (poly-list-add-variables f k)
|
---|
180 | plist-x (mapcar #'(lambda (x)
|
---|
181 | (setf (poly-termlist x)
|
---|
182 | (nconc (poly-termlist x)
|
---|
183 | (list (make-term :monom (make-monom :dimension d)
|
---|
184 | :coeff (funcall (ring-uminus ring)
|
---|
185 | (funcall (ring-unit ring)))))))
|
---|
186 | x)
|
---|
187 | (poly-standard-extension plist)))
|
---|
188 | (append f-x plist-x))
|
---|
189 |
|
---|
190 |
|
---|
191 | (defun polysaturation-extension (ring f plist
|
---|
192 | &aux
|
---|
193 | (k (length plist))
|
---|
194 | (d (+ k (monom-dimension (poly-lm (car plist)))))
|
---|
195 | ;; Add k variables to f
|
---|
196 | (f (poly-list-add-variables f k))
|
---|
197 | ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
|
---|
198 | (plist (apply #'poly-append (poly-standard-extension plist))))
|
---|
199 | "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
|
---|
200 | ;; Add -1 as the last term
|
---|
201 | (declare (type ring ring))
|
---|
202 | (setf (cdr (last (poly-termlist plist)))
|
---|
203 | (list (make-term :monom (make-monom :dimension d)
|
---|
204 | :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
|
---|
205 | (append f (list plist)))
|
---|
206 |
|
---|
207 | (defun saturation-extension-1 (ring f p)
|
---|
208 | "Calculate [F, U*P-1]. It destructively modifies F."
|
---|
209 | (declare (type ring ring))
|
---|
210 | (polysaturation-extension ring f (list p)))
|
---|
211 |
|
---|
212 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
213 | ;;
|
---|
214 | ;; Evaluation of polynomial (prefix) expressions
|
---|
215 | ;;
|
---|
216 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
217 |
|
---|
218 | (defun coerce-coeff (ring expr vars)
|
---|
219 | "Coerce an element of the coefficient ring to a constant polynomial."
|
---|
220 | ;; Modular arithmetic handler by rat
|
---|
221 | (declare (type ring ring))
|
---|
222 | (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
|
---|
223 | :coeff (funcall (ring-parse ring) expr)))
|
---|
224 | 0))
|
---|
225 |
|
---|
226 | (defun poly-eval (expr vars
|
---|
227 | &optional
|
---|
228 | (ring +ring-of-integers+)
|
---|
229 | (order #'lex>)
|
---|
230 | (list-marker :[)
|
---|
231 | &aux
|
---|
232 | (ring-and-order (make-ring-and-order :ring ring :order order)))
|
---|
233 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
234 | variables VARS. Return the resulting polynomial or list of
|
---|
235 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
236 | replaced with their analogues in the ring of polynomials, and the
|
---|
237 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
238 | of polynomials in internal form. A similar operation in another computer
|
---|
239 | algebra system could be called 'expand' or so."
|
---|
240 | (declare (type ring ring))
|
---|
241 | (labels ((p-eval (arg) (poly-eval arg vars ring order))
|
---|
242 | (p-eval-scalar (arg) (poly-eval-scalar arg))
|
---|
243 | (p-eval-list (args) (mapcar #'p-eval args))
|
---|
244 | (p-add (x y) (poly-add ring-and-order x y)))
|
---|
245 | (cond
|
---|
246 | ((null expr) (error "Empty expression"))
|
---|
247 | ((eql expr 0) (make-poly-zero))
|
---|
248 | ((member expr vars :test #'equalp)
|
---|
249 | (let ((pos (position expr vars :test #'equalp)))
|
---|
250 | (make-poly-variable ring (length vars) pos)))
|
---|
251 | ((atom expr)
|
---|
252 | (coerce-coeff ring expr vars))
|
---|
253 | ((eq (car expr) list-marker)
|
---|
254 | (cons list-marker (p-eval-list (cdr expr))))
|
---|
255 | (t
|
---|
256 | (case (car expr)
|
---|
257 | (+ (reduce #'p-add (p-eval-list (cdr expr))))
|
---|
258 | (- (case (length expr)
|
---|
259 | (1 (make-poly-zero))
|
---|
260 | (2 (poly-uminus ring (p-eval (cadr expr))))
|
---|
261 | (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
|
---|
262 | (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
|
---|
263 | (reduce #'p-add (p-eval-list (cddr expr)))))))
|
---|
264 | (*
|
---|
265 | (if (endp (cddr expr)) ;unary
|
---|
266 | (p-eval (cdr expr))
|
---|
267 | (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
|
---|
268 | (/
|
---|
269 | ;; A polynomial can be divided by a scalar
|
---|
270 | (cond
|
---|
271 | ((endp (cddr expr))
|
---|
272 | ;; A special case (/ ?), the inverse
|
---|
273 | (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
|
---|
274 | (t
|
---|
275 | (let ((num (p-eval (cadr expr)))
|
---|
276 | (denom-inverse (apply (ring-div ring)
|
---|
277 | (cons (funcall (ring-unit ring))
|
---|
278 | (mapcar #'p-eval-scalar (cddr expr))))))
|
---|
279 | (scalar-times-poly ring denom-inverse num)))))
|
---|
280 | (expt
|
---|
281 | (cond
|
---|
282 | ((member (cadr expr) vars :test #'equalp)
|
---|
283 | ;;Special handling of (expt var pow)
|
---|
284 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
285 | (make-poly-variable ring (length vars) pos (caddr expr))))
|
---|
286 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
287 | ;; Negative power means division in coefficient ring
|
---|
288 | ;; Non-integer power means non-polynomial coefficient
|
---|
289 | (coerce-coeff ring expr vars))
|
---|
290 | (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
|
---|
291 | (otherwise
|
---|
292 | (coerce-coeff ring expr vars)))))))
|
---|
293 |
|
---|
294 | (defun poly-eval-scalar (expr
|
---|
295 | &optional
|
---|
296 | (ring +ring-of-integers+)
|
---|
297 | &aux
|
---|
298 | (order #'lex>))
|
---|
299 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
300 | (declare (type ring ring))
|
---|
301 | (poly-lc (poly-eval expr nil ring order)))
|
---|
302 |
|
---|
303 | (defun spoly (ring-and-order f g
|
---|
304 | &aux
|
---|
305 | (ring (ro-ring ring-and-order)))
|
---|
306 | "It yields the S-polynomial of polynomials F and G."
|
---|
307 | (declare (type ring-and-order ring-and-order) (type poly f g))
|
---|
308 | (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
|
---|
309 | (mf (monom-div lcm (poly-lm f)))
|
---|
310 | (mg (monom-div lcm (poly-lm g))))
|
---|
311 | (declare (type monom mf mg))
|
---|
312 | (multiple-value-bind (c cf cg)
|
---|
313 | (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
|
---|
314 | (declare (ignore c))
|
---|
315 | (poly-sub
|
---|
316 | ring-and-order
|
---|
317 | (scalar-times-poly ring cg (monom-times-poly mf f))
|
---|
318 | (scalar-times-poly ring cf (monom-times-poly mg g))))))
|
---|
319 |
|
---|
320 |
|
---|
321 | (defun poly-primitive-part (ring p)
|
---|
322 | "Divide polynomial P with integer coefficients by gcd of its
|
---|
323 | coefficients and return the result."
|
---|
324 | (declare (type ring ring) (type poly p))
|
---|
325 | (if (poly-zerop p)
|
---|
326 | (values p 1)
|
---|
327 | (let ((c (poly-content ring p)))
|
---|
328 | (values (make-poly-from-termlist
|
---|
329 | (mapcar
|
---|
330 | #'(lambda (x)
|
---|
331 | (make-term :monom (term-monom x)
|
---|
332 | :coeff (funcall (ring-div ring) (term-coeff x) c)))
|
---|
333 | (poly-termlist p))
|
---|
334 | (poly-sugar p))
|
---|
335 | c))))
|
---|
336 |
|
---|
337 | (defun poly-content (ring p)
|
---|
338 | "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
|
---|
339 | to compute the greatest common divisor."
|
---|
340 | (declare (type ring ring) (type poly p))
|
---|
341 | (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
|
---|
342 |
|
---|
343 | (defun read-infix-form (&key (stream t))
|
---|
344 | "Parser of infix expressions with integer/rational coefficients
|
---|
345 | The parser will recognize two kinds of polynomial expressions:
|
---|
346 |
|
---|
347 | - polynomials in fully expanded forms with coefficients
|
---|
348 | written in front of symbolic expressions; constants can be optionally
|
---|
349 | enclosed in (); for example, the infix form
|
---|
350 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
351 | parses to
|
---|
352 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
353 |
|
---|
354 | - lists of polynomials; for example
|
---|
355 | [X-Y, X^2+3*Z]
|
---|
356 | parses to
|
---|
357 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
358 | where the first symbol [ marks a list of polynomials.
|
---|
359 |
|
---|
360 | -other infix expressions, for example
|
---|
361 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
362 | parses to:
|
---|
363 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
364 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
365 | (read-from-string
|
---|
366 | (concatenate 'string
|
---|
367 | "#I("
|
---|
368 | (with-output-to-string (s)
|
---|
369 | (loop
|
---|
370 | (multiple-value-bind (line eof)
|
---|
371 | (read-line stream t)
|
---|
372 | (format s "~A" line)
|
---|
373 | (when eof (return)))))
|
---|
374 | ")")))
|
---|
375 |
|
---|
376 | (defun read-poly (vars &key
|
---|
377 | (stream t)
|
---|
378 | (ring +ring-of-integers+)
|
---|
379 | (order #'lex>))
|
---|
380 | "Reads an expression in prefix form from a stream STREAM.
|
---|
381 | The expression read from the strem should represent a polynomial or a
|
---|
382 | list of polynomials in variables VARS, over the ring RING. The
|
---|
383 | polynomial or list of polynomials is returned, with terms in each
|
---|
384 | polynomial ordered according to monomial order ORDER."
|
---|
385 | (poly-eval (read-infix-form :stream stream) vars ring order))
|
---|
386 |
|
---|
387 | (defun string->poly (str vars
|
---|
388 | &optional
|
---|
389 | (ring +ring-of-integers+)
|
---|
390 | (order #'lex>))
|
---|
391 | "Converts a string STR to a polynomial in variables VARS."
|
---|
392 | (with-input-from-string (s str)
|
---|
393 | (read-poly vars :stream s :ring ring :order order)))
|
---|
394 |
|
---|
395 | (defun poly->alist (p)
|
---|
396 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
397 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
398 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
399 | corresponding coefficient in the ring."
|
---|
400 | (cond
|
---|
401 | ((poly-p p)
|
---|
402 | (mapcar #'term->cons (poly-termlist p)))
|
---|
403 | ((and (consp p) (eq (car p) :[))
|
---|
404 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
405 |
|
---|
406 | (defun string->alist (str vars
|
---|
407 | &optional
|
---|
408 | (ring +ring-of-integers+)
|
---|
409 | (order #'lex>))
|
---|
410 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
411 | an association list (... (MONOM . COEFF) ...)."
|
---|
412 | (poly->alist (string->poly str vars ring order)))
|
---|
413 |
|
---|
414 | (defun poly-equal-no-sugar-p (p q)
|
---|
415 | "Compare polynomials for equality, ignoring sugar."
|
---|
416 | (declare (type poly p q))
|
---|
417 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
418 |
|
---|
419 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
420 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
421 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
422 |
|
---|
423 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
424 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
425 | (every #'poly-equal-no-sugar-p p q))
|
---|
426 | |#
|
---|