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