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