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