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