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