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 :ring-and-order :monom :order :term :termlist :infix)
|
---|
30 | (:export "POLY"
|
---|
31 | "POLY-TERMLIST"
|
---|
32 | "POLY-SUGAR"
|
---|
33 | "POLY-RESET-SUGAR"
|
---|
34 | "POLY-LT"
|
---|
35 | "MAKE-POLY-FROM-TERMLIST"
|
---|
36 | "MAKE-POLY-ZERO"
|
---|
37 | "MAKE-POLY-VARIABLE"
|
---|
38 | "POLY-UNIT"
|
---|
39 | "POLY-LM"
|
---|
40 | "POLY-SECOND-LM"
|
---|
41 | "POLY-SECOND-LT"
|
---|
42 | "POLY-LC"
|
---|
43 | "POLY-SECOND-LC"
|
---|
44 | "POLY-ZEROP"
|
---|
45 | "POLY-LENGTH"
|
---|
46 | "SCALAR-TIMES-POLY"
|
---|
47 | "SCALAR-TIMES-POLY-1"
|
---|
48 | "MONOM-TIMES-POLY"
|
---|
49 | "TERM-TIMES-POLY"
|
---|
50 | "POLY-ADD"
|
---|
51 | "POLY-SUB"
|
---|
52 | "POLY-UMINUS"
|
---|
53 | "POLY-MUL"
|
---|
54 | "POLY-EXPT"
|
---|
55 | "POLY-APPEND"
|
---|
56 | "POLY-NREVERSE"
|
---|
57 | "POLY-REVERSE"
|
---|
58 | "POLY-CONTRACT"
|
---|
59 | "POLY-EXTEND"
|
---|
60 | "POLY-ADD-VARIABLES"
|
---|
61 | "POLY-LIST-ADD-VARIABLES"
|
---|
62 | "POLY-STANDARD-EXTENSION"
|
---|
63 | "SATURATION-EXTENSION"
|
---|
64 | "POLYSATURATION-EXTENSION"
|
---|
65 | "SATURATION-EXTENSION-1"
|
---|
66 | "COERCE-COEFF"
|
---|
67 | "POLY-EVAL"
|
---|
68 | "POLY-EVAL-SCALAR"
|
---|
69 | "SPOLY"
|
---|
70 | "POLY-PRIMITIVE-PART"
|
---|
71 | "POLY-CONTENT"
|
---|
72 | "READ-INFIX-FORM"
|
---|
73 | "READ-POLY"
|
---|
74 | "STRING->POLY"
|
---|
75 | "POLY->ALIST"
|
---|
76 | "STRING->ALIST"
|
---|
77 | "POLY-EQUAL-NO-SUGAR-P"
|
---|
78 | "POLY-SET-EQUAL-NO-SUGAR-P"
|
---|
79 | "POLY-LIST-EQUAL-NO-SUGAR-P"
|
---|
80 | ))
|
---|
81 |
|
---|
82 | (in-package :polynomial)
|
---|
83 |
|
---|
84 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
85 |
|
---|
86 | (defstruct (poly
|
---|
87 | ;;
|
---|
88 | ;; BOA constructor, by default constructs zero polynomial
|
---|
89 | (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
|
---|
90 | (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
|
---|
91 | ;; Constructor of polynomials representing a variable
|
---|
92 | (:constructor make-poly-variable (ring nvars pos &optional (power 1)
|
---|
93 | &aux
|
---|
94 | (termlist (list
|
---|
95 | (make-term-variable ring nvars pos power)))
|
---|
96 | (sugar power)))
|
---|
97 | (:constructor poly-unit (ring dimension
|
---|
98 | &aux
|
---|
99 | (termlist (termlist-unit ring dimension))
|
---|
100 | (sugar 0))))
|
---|
101 | (termlist nil :type list)
|
---|
102 | (sugar -1 :type fixnum))
|
---|
103 |
|
---|
104 | ;; Leading term
|
---|
105 | (defmacro poly-lt (p) `(car (poly-termlist ,p)))
|
---|
106 |
|
---|
107 | ;; Second term
|
---|
108 | (defmacro poly-second-lt (p) `(cadar (poly-termlist ,p)))
|
---|
109 |
|
---|
110 | ;; Leading monomial
|
---|
111 | (defun poly-lm (p)
|
---|
112 | (declare (type poly p))
|
---|
113 | (term-monom (poly-lt p)))
|
---|
114 |
|
---|
115 | ;; Second monomial
|
---|
116 | (defun poly-second-lm (p)
|
---|
117 | (declare (type poly p))
|
---|
118 | (term-monom (poly-second-lt p)))
|
---|
119 |
|
---|
120 | ;; Leading coefficient
|
---|
121 | (defun poly-lc (p)
|
---|
122 | (declare (type poly p))
|
---|
123 | (term-coeff (poly-lt p)))
|
---|
124 |
|
---|
125 | ;; Second coefficient
|
---|
126 | (defun poly-second-lc (p)
|
---|
127 | (declare (type poly p))
|
---|
128 | (term-coeff (poly-second-lt p)))
|
---|
129 |
|
---|
130 | ;; Testing for a zero polynomial
|
---|
131 | (defun poly-zerop (p)
|
---|
132 | (declare (type poly p))
|
---|
133 | (null (poly-termlist p)))
|
---|
134 |
|
---|
135 | ;; The number of terms
|
---|
136 | (defun poly-length (p)
|
---|
137 | (declare (type poly p))
|
---|
138 | (length (poly-termlist p)))
|
---|
139 |
|
---|
140 | (defun poly-reset-sugar (p)
|
---|
141 | "(Re)sets the sugar of a polynomial P to the sugar of (POLY-TERMLIST P).
|
---|
142 | Thus, the sugar is set to the maximum sugar of all monomials of P, or -1
|
---|
143 | if P is a zero polynomial."
|
---|
144 | (declare (type poly p))
|
---|
145 | (setf (poly-sugar p) (termlist-sugar (poly-termlist p)))
|
---|
146 | p)
|
---|
147 |
|
---|
148 | (defun scalar-times-poly (ring c p)
|
---|
149 | "The scalar product of scalar C by a polynomial P. The sugar of the
|
---|
150 | original polynomial becomes the sugar of the result."
|
---|
151 | (declare (type ring ring) (type poly p))
|
---|
152 | (make-poly-from-termlist (scalar-times-termlist ring c (poly-termlist p)) (poly-sugar p)))
|
---|
153 |
|
---|
154 | (defun scalar-times-poly-1 (ring c p)
|
---|
155 | "The scalar product of scalar C by a polynomial P, omitting the head term. The sugar of the
|
---|
156 | original polynomial becomes the sugar of the result."
|
---|
157 | (declare (type ring ring) (type poly p))
|
---|
158 | (make-poly-from-termlist (scalar-times-termlist ring c (cdr (poly-termlist p))) (poly-sugar p)))
|
---|
159 |
|
---|
160 | (defun monom-times-poly (m p)
|
---|
161 | (declare (type monom m) (type poly p))
|
---|
162 | (make-poly-from-termlist
|
---|
163 | (monom-times-termlist m (poly-termlist p))
|
---|
164 | (+ (poly-sugar p) (monom-sugar m))))
|
---|
165 |
|
---|
166 | (defun term-times-poly (ring term p)
|
---|
167 | (declare (type ring ring) (type term term) (type poly p))
|
---|
168 | (make-poly-from-termlist
|
---|
169 | (term-times-termlist ring term (poly-termlist p))
|
---|
170 | (+ (poly-sugar p) (term-sugar term))))
|
---|
171 |
|
---|
172 | (defun poly-add (ring-and-order p q)
|
---|
173 | (declare (type ring-and-order ring-and-order) (type poly p q))
|
---|
174 | (make-poly-from-termlist
|
---|
175 | (termlist-add ring-and-order
|
---|
176 | (poly-termlist p)
|
---|
177 | (poly-termlist q))
|
---|
178 | (max (poly-sugar p) (poly-sugar q))))
|
---|
179 |
|
---|
180 | (defun poly-sub (ring-and-order p q)
|
---|
181 | (declare (type ring-and-order ring-and-order) (type poly p q))
|
---|
182 | (make-poly-from-termlist
|
---|
183 | (termlist-sub ring-and-order (poly-termlist p) (poly-termlist q))
|
---|
184 | (max (poly-sugar p) (poly-sugar q))))
|
---|
185 |
|
---|
186 | (defun poly-uminus (ring p)
|
---|
187 | (declare (type ring ring) (type poly p))
|
---|
188 | (make-poly-from-termlist
|
---|
189 | (termlist-uminus ring (poly-termlist p))
|
---|
190 | (poly-sugar p)))
|
---|
191 |
|
---|
192 | (defun poly-mul (ring-and-order p q)
|
---|
193 | (declare (type ring-and-order ring-and-order) (type poly p q))
|
---|
194 | (make-poly-from-termlist
|
---|
195 | (termlist-mul ring-and-order (poly-termlist p) (poly-termlist q))
|
---|
196 | (+ (poly-sugar p) (poly-sugar q))))
|
---|
197 |
|
---|
198 | (defun poly-expt (ring-and-order p n)
|
---|
199 | (declare (type ring-and-order ring-and-order) (type poly p))
|
---|
200 | (make-poly-from-termlist (termlist-expt ring-and-order (poly-termlist p) n) (* n (poly-sugar p))))
|
---|
201 |
|
---|
202 | (defun poly-append (&rest plist)
|
---|
203 | (make-poly-from-termlist (apply #'append (mapcar #'poly-termlist plist))
|
---|
204 | (apply #'max (mapcar #'poly-sugar plist))))
|
---|
205 |
|
---|
206 | (defun poly-nreverse (p)
|
---|
207 | "Destructively reverse the order of terms in polynomial P. Returns P"
|
---|
208 | (declare (type poly p))
|
---|
209 | (setf (poly-termlist p) (nreverse (poly-termlist p)))
|
---|
210 | p)
|
---|
211 |
|
---|
212 | (defun poly-reverse (p)
|
---|
213 | "Returns a copy of the polynomial P with terms in reverse order."
|
---|
214 | (declare (type poly p))
|
---|
215 | (make-poly-from-termlist (reverse (poly-termlist p))
|
---|
216 | (poly-sugar p)))
|
---|
217 |
|
---|
218 |
|
---|
219 | (defun poly-contract (p &optional (k 1))
|
---|
220 | (declare (type poly p))
|
---|
221 | (make-poly-from-termlist (termlist-contract (poly-termlist p) k)
|
---|
222 | (poly-sugar p)))
|
---|
223 |
|
---|
224 | (defun poly-extend (p &optional (m (make-monom :dimension 1)))
|
---|
225 | (declare (type poly p))
|
---|
226 | (make-poly-from-termlist
|
---|
227 | (termlist-extend (poly-termlist p) m)
|
---|
228 | (+ (poly-sugar p) (monom-sugar m))))
|
---|
229 |
|
---|
230 | (defun poly-add-variables (p k)
|
---|
231 | (declare (type poly p))
|
---|
232 | (setf (poly-termlist p) (termlist-add-variables (poly-termlist p) k))
|
---|
233 | p)
|
---|
234 |
|
---|
235 | (defun poly-list-add-variables (plist k)
|
---|
236 | (mapcar #'(lambda (p) (poly-add-variables p k)) plist))
|
---|
237 |
|
---|
238 | (defun poly-standard-extension (plist &aux (k (length plist)))
|
---|
239 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]."
|
---|
240 | (declare (list plist) (fixnum k))
|
---|
241 | (labels ((incf-power (g i)
|
---|
242 | (dolist (x (poly-termlist g))
|
---|
243 | (incf (monom-elt (term-monom x) i)))
|
---|
244 | (incf (poly-sugar g))))
|
---|
245 | (setf plist (poly-list-add-variables plist k))
|
---|
246 | (dotimes (i k plist)
|
---|
247 | (incf-power (nth i plist) i))))
|
---|
248 |
|
---|
249 | (defun saturation-extension (ring f plist
|
---|
250 | &aux
|
---|
251 | (k (length plist))
|
---|
252 | (d (monom-dimension (poly-lm (car plist))))
|
---|
253 | f-x plist-x)
|
---|
254 | "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
|
---|
255 | (declare (type ring ring))
|
---|
256 | (setf f-x (poly-list-add-variables f k)
|
---|
257 | plist-x (mapcar #'(lambda (x)
|
---|
258 | (setf (poly-termlist x)
|
---|
259 | (nconc (poly-termlist x)
|
---|
260 | (list (make-term :monom (make-monom :dimension d)
|
---|
261 | :coeff (funcall (ring-uminus ring)
|
---|
262 | (funcall (ring-unit ring)))))))
|
---|
263 | x)
|
---|
264 | (poly-standard-extension plist)))
|
---|
265 | (append f-x plist-x))
|
---|
266 |
|
---|
267 |
|
---|
268 | (defun polysaturation-extension (ring f plist
|
---|
269 | &aux
|
---|
270 | (k (length plist))
|
---|
271 | (d (+ k (monom-dimension (poly-lm (car plist)))))
|
---|
272 | ;; Add k variables to f
|
---|
273 | (f (poly-list-add-variables f k))
|
---|
274 | ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
|
---|
275 | (plist (apply #'poly-append (poly-standard-extension plist))))
|
---|
276 | "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
|
---|
277 | ;; Add -1 as the last term
|
---|
278 | (declare (type ring ring))
|
---|
279 | (setf (cdr (last (poly-termlist plist)))
|
---|
280 | (list (make-term :monom (make-monom :dimension d)
|
---|
281 | :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
|
---|
282 | (append f (list plist)))
|
---|
283 |
|
---|
284 | (defun saturation-extension-1 (ring f p)
|
---|
285 | "Calculate [F, U*P-1]. It destructively modifies F."
|
---|
286 | (declare (type ring ring))
|
---|
287 | (polysaturation-extension ring f (list p)))
|
---|
288 |
|
---|
289 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
290 | ;;
|
---|
291 | ;; Evaluation of polynomial (prefix) expressions
|
---|
292 | ;;
|
---|
293 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
294 |
|
---|
295 | (defun coerce-coeff (ring expr vars)
|
---|
296 | "Coerce an element of the coefficient ring to a constant polynomial."
|
---|
297 | ;; Modular arithmetic handler by rat
|
---|
298 | (declare (type ring ring))
|
---|
299 | (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
|
---|
300 | :coeff (funcall (ring-parse ring) expr)))
|
---|
301 | 0))
|
---|
302 |
|
---|
303 | (defun poly-eval (expr vars
|
---|
304 | &optional
|
---|
305 | (ring +ring-of-integers+)
|
---|
306 | (order #'lex>)
|
---|
307 | (list-marker :[)
|
---|
308 | &aux
|
---|
309 | (ring-and-order (make-ring-and-order :ring ring :order order)))
|
---|
310 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
311 | variables VARS. Return the resulting polynomial or list of
|
---|
312 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
313 | replaced with their analogues in the ring of polynomials, and the
|
---|
314 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
315 | of polynomials in internal form. A similar operation in another computer
|
---|
316 | algebra system could be called 'expand' or so."
|
---|
317 | (declare (type ring ring))
|
---|
318 | (labels ((p-eval (arg) (poly-eval arg vars ring order))
|
---|
319 | (p-eval-scalar (arg) (poly-eval-scalar arg))
|
---|
320 | (p-eval-list (args) (mapcar #'p-eval args))
|
---|
321 | (p-add (x y) (poly-add ring-and-order x y)))
|
---|
322 | (cond
|
---|
323 | ((null expr) (error "Empty expression"))
|
---|
324 | ((eql expr 0) (make-poly-zero))
|
---|
325 | ((member expr vars :test #'equalp)
|
---|
326 | (let ((pos (position expr vars :test #'equalp)))
|
---|
327 | (make-poly-variable ring (length vars) pos)))
|
---|
328 | ((atom expr)
|
---|
329 | (coerce-coeff ring expr vars))
|
---|
330 | ((eq (car expr) list-marker)
|
---|
331 | (cons list-marker (p-eval-list (cdr expr))))
|
---|
332 | (t
|
---|
333 | (case (car expr)
|
---|
334 | (+ (reduce #'p-add (p-eval-list (cdr expr))))
|
---|
335 | (- (case (length expr)
|
---|
336 | (1 (make-poly-zero))
|
---|
337 | (2 (poly-uminus ring (p-eval (cadr expr))))
|
---|
338 | (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
|
---|
339 | (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
|
---|
340 | (reduce #'p-add (p-eval-list (cddr expr)))))))
|
---|
341 | (*
|
---|
342 | (if (endp (cddr expr)) ;unary
|
---|
343 | (p-eval (cdr expr))
|
---|
344 | (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
|
---|
345 | (/
|
---|
346 | ;; A polynomial can be divided by a scalar
|
---|
347 | (cond
|
---|
348 | ((endp (cddr expr))
|
---|
349 | ;; A special case (/ ?), the inverse
|
---|
350 | (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
|
---|
351 | (t
|
---|
352 | (let ((num (p-eval (cadr expr)))
|
---|
353 | (denom-inverse (apply (ring-div ring)
|
---|
354 | (cons (funcall (ring-unit ring))
|
---|
355 | (mapcar #'p-eval-scalar (cddr expr))))))
|
---|
356 | (scalar-times-poly ring denom-inverse num)))))
|
---|
357 | (expt
|
---|
358 | (cond
|
---|
359 | ((member (cadr expr) vars :test #'equalp)
|
---|
360 | ;;Special handling of (expt var pow)
|
---|
361 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
362 | (make-poly-variable ring (length vars) pos (caddr expr))))
|
---|
363 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
364 | ;; Negative power means division in coefficient ring
|
---|
365 | ;; Non-integer power means non-polynomial coefficient
|
---|
366 | (coerce-coeff ring expr vars))
|
---|
367 | (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
|
---|
368 | (otherwise
|
---|
369 | (coerce-coeff ring expr vars)))))))
|
---|
370 |
|
---|
371 | (defun poly-eval-scalar (expr
|
---|
372 | &optional
|
---|
373 | (ring +ring-of-integers+)
|
---|
374 | &aux
|
---|
375 | (order #'lex>))
|
---|
376 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
377 | (declare (type ring ring))
|
---|
378 | (poly-lc (poly-eval expr nil ring order)))
|
---|
379 |
|
---|
380 | (defun spoly (ring-and-order f g
|
---|
381 | &aux
|
---|
382 | (ring (ro-ring ring-and-order)))
|
---|
383 | "It yields the S-polynomial of polynomials F and G."
|
---|
384 | (declare (type ring-and-order ring-and-order) (type poly f g))
|
---|
385 | (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
|
---|
386 | (mf (monom-div lcm (poly-lm f)))
|
---|
387 | (mg (monom-div lcm (poly-lm g))))
|
---|
388 | (declare (type monom mf mg))
|
---|
389 | (multiple-value-bind (c cf cg)
|
---|
390 | (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
|
---|
391 | (declare (ignore c))
|
---|
392 | (poly-sub
|
---|
393 | ring-and-order
|
---|
394 | (scalar-times-poly ring cg (monom-times-poly mf f))
|
---|
395 | (scalar-times-poly ring cf (monom-times-poly mg g))))))
|
---|
396 |
|
---|
397 |
|
---|
398 | (defun poly-primitive-part (ring p)
|
---|
399 | "Divide polynomial P with integer coefficients by gcd of its
|
---|
400 | coefficients and return the result."
|
---|
401 | (declare (type ring ring) (type poly p))
|
---|
402 | (if (poly-zerop p)
|
---|
403 | (values p 1)
|
---|
404 | (let ((c (poly-content ring p)))
|
---|
405 | (values (make-poly-from-termlist
|
---|
406 | (mapcar
|
---|
407 | #'(lambda (x)
|
---|
408 | (make-term :monom (term-monom x)
|
---|
409 | :coeff (funcall (ring-div ring) (term-coeff x) c)))
|
---|
410 | (poly-termlist p))
|
---|
411 | (poly-sugar p))
|
---|
412 | c))))
|
---|
413 |
|
---|
414 | (defun poly-content (ring p)
|
---|
415 | "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
|
---|
416 | to compute the greatest common divisor."
|
---|
417 | (declare (type ring ring) (type poly p))
|
---|
418 | (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
|
---|
419 |
|
---|
420 | (defun read-infix-form (&key (stream t))
|
---|
421 | "Parser of infix expressions with integer/rational coefficients
|
---|
422 | The parser will recognize two kinds of polynomial expressions:
|
---|
423 |
|
---|
424 | - polynomials in fully expanded forms with coefficients
|
---|
425 | written in front of symbolic expressions; constants can be optionally
|
---|
426 | enclosed in (); for example, the infix form
|
---|
427 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
428 | parses to
|
---|
429 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
430 |
|
---|
431 | - lists of polynomials; for example
|
---|
432 | [X-Y, X^2+3*Z]
|
---|
433 | parses to
|
---|
434 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
435 | where the first symbol [ marks a list of polynomials.
|
---|
436 |
|
---|
437 | -other infix expressions, for example
|
---|
438 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
439 | parses to:
|
---|
440 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
441 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
442 | (read-from-string
|
---|
443 | (concatenate 'string
|
---|
444 | "#I("
|
---|
445 | (with-output-to-string (s)
|
---|
446 | (loop
|
---|
447 | (multiple-value-bind (line eof)
|
---|
448 | (read-line stream t)
|
---|
449 | (format s "~A" line)
|
---|
450 | (when eof (return)))))
|
---|
451 | ")")))
|
---|
452 |
|
---|
453 | (defun read-poly (vars &key
|
---|
454 | (stream t)
|
---|
455 | (ring +ring-of-integers+)
|
---|
456 | (order #'lex>))
|
---|
457 | "Reads an expression in prefix form from a stream STREAM.
|
---|
458 | The expression read from the strem should represent a polynomial or a
|
---|
459 | list of polynomials in variables VARS, over the ring RING. The
|
---|
460 | polynomial or list of polynomials is returned, with terms in each
|
---|
461 | polynomial ordered according to monomial order ORDER."
|
---|
462 | (poly-eval (read-infix-form :stream stream) vars ring order))
|
---|
463 |
|
---|
464 | (defun string->poly (str vars
|
---|
465 | &optional
|
---|
466 | (ring +ring-of-integers+)
|
---|
467 | (order #'lex>))
|
---|
468 | "Converts a string STR to a polynomial in variables VARS."
|
---|
469 | (with-input-from-string (s str)
|
---|
470 | (read-poly vars :stream s :ring ring :order order)))
|
---|
471 |
|
---|
472 | (defun poly->alist (p)
|
---|
473 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
474 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
475 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
476 | corresponding coefficient in the ring."
|
---|
477 | (cond
|
---|
478 | ((poly-p p)
|
---|
479 | (mapcar #'term->cons (poly-termlist p)))
|
---|
480 | ((and (consp p) (eq (car p) :[))
|
---|
481 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
482 |
|
---|
483 | (defun string->alist (str vars
|
---|
484 | &optional
|
---|
485 | (ring +ring-of-integers+)
|
---|
486 | (order #'lex>))
|
---|
487 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
488 | an association list (... (MONOM . COEFF) ...)."
|
---|
489 | (poly->alist (string->poly str vars ring order)))
|
---|
490 |
|
---|
491 | (defun poly-equal-no-sugar-p (p q)
|
---|
492 | "Compare polynomials for equality, ignoring sugar."
|
---|
493 | (declare (type poly p q))
|
---|
494 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
495 |
|
---|
496 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
497 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
498 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
499 |
|
---|
500 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
501 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
502 | (every #'poly-equal-no-sugar-p p q))
|
---|