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