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