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