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