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