[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 |
|
---|
[1927] | 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 23 | ;;
|
---|
| 24 | ;; Polynomials
|
---|
| 25 | ;;
|
---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
[77] | 27 |
|
---|
[431] | 28 | (defpackage "POLYNOMIAL"
|
---|
[1606] | 29 | (:use :cl :ring :ring-and-order :monom :order :term :termlist :infix)
|
---|
[432] | 30 | (:export "POLY"
|
---|
| 31 | "POLY-TERMLIST"
|
---|
| 32 | "POLY-SUGAR"
|
---|
[1218] | 33 | "POLY-RESET-SUGAR"
|
---|
[432] | 34 | "POLY-LT"
|
---|
[433] | 35 | "MAKE-POLY-FROM-TERMLIST"
|
---|
| 36 | "MAKE-POLY-ZERO"
|
---|
[1657] | 37 | "MAKE-POLY-VARIABLE"
|
---|
[433] | 38 | "POLY-UNIT"
|
---|
| 39 | "POLY-LM"
|
---|
| 40 | "POLY-SECOND-LM"
|
---|
| 41 | "POLY-SECOND-LT"
|
---|
| 42 | "POLY-LC"
|
---|
| 43 | "POLY-SECOND-LC"
|
---|
| 44 | "POLY-ZEROP"
|
---|
[458] | 45 | "POLY-LENGTH"
|
---|
[433] | 46 | "SCALAR-TIMES-POLY"
|
---|
| 47 | "SCALAR-TIMES-POLY-1"
|
---|
| 48 | "MONOM-TIMES-POLY"
|
---|
| 49 | "TERM-TIMES-POLY"
|
---|
| 50 | "POLY-ADD"
|
---|
| 51 | "POLY-SUB"
|
---|
| 52 | "POLY-UMINUS"
|
---|
| 53 | "POLY-MUL"
|
---|
| 54 | "POLY-EXPT"
|
---|
| 55 | "POLY-APPEND"
|
---|
| 56 | "POLY-NREVERSE"
|
---|
[1266] | 57 | "POLY-REVERSE"
|
---|
[433] | 58 | "POLY-CONTRACT"
|
---|
| 59 | "POLY-EXTEND"
|
---|
| 60 | "POLY-ADD-VARIABLES"
|
---|
| 61 | "POLY-LIST-ADD-VARIABLES"
|
---|
| 62 | "POLY-STANDARD-EXTENSION"
|
---|
| 63 | "SATURATION-EXTENSION"
|
---|
| 64 | "POLYSATURATION-EXTENSION"
|
---|
| 65 | "SATURATION-EXTENSION-1"
|
---|
| 66 | "COERCE-COEFF"
|
---|
| 67 | "POLY-EVAL"
|
---|
[1134] | 68 | "POLY-EVAL-SCALAR"
|
---|
[433] | 69 | "SPOLY"
|
---|
| 70 | "POLY-PRIMITIVE-PART"
|
---|
| 71 | "POLY-CONTENT"
|
---|
[1085] | 72 | "READ-INFIX-FORM"
|
---|
[1093] | 73 | "READ-POLY"
|
---|
[1104] | 74 | "STRING->POLY"
|
---|
[1159] | 75 | "POLY->ALIST"
|
---|
| 76 | "STRING->ALIST"
|
---|
[1441] | 77 | "POLY-EQUAL-NO-SUGAR-P"
|
---|
[1561] | 78 | "POLY-SET-EQUAL-NO-SUGAR-P"
|
---|
| 79 | "POLY-LIST-EQUAL-NO-SUGAR-P"
|
---|
[432] | 80 | ))
|
---|
[143] | 81 |
|
---|
[431] | 82 | (in-package :polynomial)
|
---|
| 83 |
|
---|
[1927] | 84 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[52] | 85 |
|
---|
[2442] | 86 | #|
|
---|
[52] | 87 | ;;
|
---|
| 88 | ;; BOA constructor, by default constructs zero polynomial
|
---|
| 89 | (:constructor make-poly-from-termlist (termlist &optional (sugar (termlist-sugar termlist))))
|
---|
| 90 | (:constructor make-poly-zero (&aux (termlist nil) (sugar -1)))
|
---|
| 91 | ;; Constructor of polynomials representing a variable
|
---|
[1657] | 92 | (:constructor make-poly-variable (ring nvars pos &optional (power 1)
|
---|
[53] | 93 | &aux
|
---|
| 94 | (termlist (list
|
---|
| 95 | (make-term-variable ring nvars pos power)))
|
---|
| 96 | (sugar power)))
|
---|
| 97 | (:constructor poly-unit (ring dimension
|
---|
| 98 | &aux
|
---|
| 99 | (termlist (termlist-unit ring dimension))
|
---|
| 100 | (sugar 0))))
|
---|
[52] | 101 |
|
---|
[2442] | 102 | |#
|
---|
| 103 |
|
---|
| 104 | (defclass poly ()
|
---|
| 105 | ((termlist :initarg :terms :accessor poly-termlist))
|
---|
| 106 | (:default-initargs :termlist nil))
|
---|
| 107 |
|
---|
[52] | 108 | ;; Leading term
|
---|
[2442] | 109 | (defgeneric leading-term (object)
|
---|
| 110 | (:method ((self poly))
|
---|
| 111 | (car (poly-termlist self))))
|
---|
[52] | 112 |
|
---|
| 113 | ;; Second term
|
---|
[2442] | 114 | (defgeneric second-leading-term (object)
|
---|
| 115 | (:method ((self poly))
|
---|
| 116 | (cadar (poly-termlist self))))
|
---|
[52] | 117 |
|
---|
| 118 | ;; Leading coefficient
|
---|
[2442] | 119 | (defgeneric leading-coefficient (object)
|
---|
| 120 | (:method ((self poly))
|
---|
| 121 | (r-coeff (leading-term self))))
|
---|
[52] | 122 |
|
---|
| 123 | ;; Second coefficient
|
---|
[2442] | 124 | (defgeneric second-leading-coefficient (object)
|
---|
| 125 | (:method ((self poly))
|
---|
| 126 | (term-coeff (second-leading-term self))))
|
---|
[52] | 127 |
|
---|
| 128 | ;; Testing for a zero polynomial
|
---|
[2445] | 129 | (defmethod r-zerop ((self poly))
|
---|
| 130 | (null (poly-termlist self)))
|
---|
[52] | 131 |
|
---|
| 132 | ;; The number of terms
|
---|
[2445] | 133 | (defmethod r-length ((self poly))
|
---|
| 134 | (length (poly-termlist self)))
|
---|
[52] | 135 |
|
---|
[2446] | 136 | (defmethod scalar-multiply ((self poly) scalar)
|
---|
| 137 | "The scalar product of a polynomial SELF by a scalar SCALAR."
|
---|
| 138 | (mapc #'(lambda (term) (scalar-multiply term scalar)) (poly-termlist self))
|
---|
| 139 | self)
|
---|
[1215] | 140 |
|
---|
[52] | 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)
|
---|
[1906] | 148 | (declare (type monom m) (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]."
|
---|
[1907] | 242 | (declare (type ring ring))
|
---|
[1474] | 243 | (setf f-x (poly-list-add-variables f k)
|
---|
| 244 | plist-x (mapcar #'(lambda (x)
|
---|
[1843] | 245 | (setf (poly-termlist x)
|
---|
| 246 | (nconc (poly-termlist x)
|
---|
| 247 | (list (make-term :monom (make-monom :dimension d)
|
---|
[1844] | 248 | :coeff (funcall (ring-uminus ring)
|
---|
| 249 | (funcall (ring-unit ring)))))))
|
---|
[1474] | 250 | x)
|
---|
| 251 | (poly-standard-extension plist)))
|
---|
| 252 | (append f-x plist-x))
|
---|
[52] | 253 |
|
---|
| 254 |
|
---|
[1475] | 255 | (defun polysaturation-extension (ring f plist
|
---|
| 256 | &aux
|
---|
| 257 | (k (length plist))
|
---|
[1476] | 258 | (d (+ k (monom-dimension (poly-lm (car plist)))))
|
---|
[1494] | 259 | ;; Add k variables to f
|
---|
[1493] | 260 | (f (poly-list-add-variables f k))
|
---|
[1495] | 261 | ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
|
---|
[1493] | 262 | (plist (apply #'poly-append (poly-standard-extension plist))))
|
---|
[1497] | 263 | "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
|
---|
[1493] | 264 | ;; Add -1 as the last term
|
---|
[1908] | 265 | (declare (type ring ring))
|
---|
[1493] | 266 | (setf (cdr (last (poly-termlist plist)))
|
---|
[1845] | 267 | (list (make-term :monom (make-monom :dimension d)
|
---|
| 268 | :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
|
---|
[1493] | 269 | (append f (list plist)))
|
---|
[52] | 270 |
|
---|
[1477] | 271 | (defun saturation-extension-1 (ring f p)
|
---|
[1497] | 272 | "Calculate [F, U*P-1]. It destructively modifies F."
|
---|
[1908] | 273 | (declare (type ring ring))
|
---|
[1477] | 274 | (polysaturation-extension ring f (list p)))
|
---|
[53] | 275 |
|
---|
| 276 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 277 | ;;
|
---|
| 278 | ;; Evaluation of polynomial (prefix) expressions
|
---|
| 279 | ;;
|
---|
| 280 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 281 |
|
---|
| 282 | (defun coerce-coeff (ring expr vars)
|
---|
| 283 | "Coerce an element of the coefficient ring to a constant polynomial."
|
---|
| 284 | ;; Modular arithmetic handler by rat
|
---|
[1908] | 285 | (declare (type ring ring))
|
---|
[1846] | 286 | (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
|
---|
| 287 | :coeff (funcall (ring-parse ring) expr)))
|
---|
[53] | 288 | 0))
|
---|
| 289 |
|
---|
[1046] | 290 | (defun poly-eval (expr vars
|
---|
| 291 | &optional
|
---|
[1668] | 292 | (ring +ring-of-integers+)
|
---|
[1048] | 293 | (order #'lex>)
|
---|
[1170] | 294 | (list-marker :[)
|
---|
[1047] | 295 | &aux
|
---|
| 296 | (ring-and-order (make-ring-and-order :ring ring :order order)))
|
---|
[1168] | 297 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
[1208] | 298 | variables VARS. Return the resulting polynomial or list of
|
---|
| 299 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
| 300 | replaced with their analogues in the ring of polynomials, and the
|
---|
| 301 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
[1209] | 302 | of polynomials in internal form. A similar operation in another computer
|
---|
| 303 | algebra system could be called 'expand' or so."
|
---|
[1909] | 304 | (declare (type ring ring))
|
---|
[1050] | 305 | (labels ((p-eval (arg) (poly-eval arg vars ring order))
|
---|
[1140] | 306 | (p-eval-scalar (arg) (poly-eval-scalar arg))
|
---|
[53] | 307 | (p-eval-list (args) (mapcar #'p-eval args))
|
---|
[989] | 308 | (p-add (x y) (poly-add ring-and-order x y)))
|
---|
[53] | 309 | (cond
|
---|
[1128] | 310 | ((null expr) (error "Empty expression"))
|
---|
[53] | 311 | ((eql expr 0) (make-poly-zero))
|
---|
| 312 | ((member expr vars :test #'equalp)
|
---|
| 313 | (let ((pos (position expr vars :test #'equalp)))
|
---|
[1657] | 314 | (make-poly-variable ring (length vars) pos)))
|
---|
[53] | 315 | ((atom expr)
|
---|
| 316 | (coerce-coeff ring expr vars))
|
---|
| 317 | ((eq (car expr) list-marker)
|
---|
| 318 | (cons list-marker (p-eval-list (cdr expr))))
|
---|
| 319 | (t
|
---|
| 320 | (case (car expr)
|
---|
| 321 | (+ (reduce #'p-add (p-eval-list (cdr expr))))
|
---|
| 322 | (- (case (length expr)
|
---|
| 323 | (1 (make-poly-zero))
|
---|
| 324 | (2 (poly-uminus ring (p-eval (cadr expr))))
|
---|
[989] | 325 | (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
|
---|
| 326 | (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
|
---|
[53] | 327 | (reduce #'p-add (p-eval-list (cddr expr)))))))
|
---|
| 328 | (*
|
---|
| 329 | (if (endp (cddr expr)) ;unary
|
---|
| 330 | (p-eval (cdr expr))
|
---|
[989] | 331 | (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
|
---|
[1106] | 332 | (/
|
---|
| 333 | ;; A polynomial can be divided by a scalar
|
---|
[1115] | 334 | (cond
|
---|
| 335 | ((endp (cddr expr))
|
---|
[1117] | 336 | ;; A special case (/ ?), the inverse
|
---|
[1119] | 337 | (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
|
---|
[1128] | 338 | (t
|
---|
[1115] | 339 | (let ((num (p-eval (cadr expr)))
|
---|
[1142] | 340 | (denom-inverse (apply (ring-div ring)
|
---|
| 341 | (cons (funcall (ring-unit ring))
|
---|
| 342 | (mapcar #'p-eval-scalar (cddr expr))))))
|
---|
[1118] | 343 | (scalar-times-poly ring denom-inverse num)))))
|
---|
[53] | 344 | (expt
|
---|
| 345 | (cond
|
---|
| 346 | ((member (cadr expr) vars :test #'equalp)
|
---|
| 347 | ;;Special handling of (expt var pow)
|
---|
| 348 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
[1657] | 349 | (make-poly-variable ring (length vars) pos (caddr expr))))
|
---|
[53] | 350 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
| 351 | ;; Negative power means division in coefficient ring
|
---|
| 352 | ;; Non-integer power means non-polynomial coefficient
|
---|
| 353 | (coerce-coeff ring expr vars))
|
---|
[989] | 354 | (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
|
---|
[53] | 355 | (otherwise
|
---|
| 356 | (coerce-coeff ring expr vars)))))))
|
---|
| 357 |
|
---|
[1133] | 358 | (defun poly-eval-scalar (expr
|
---|
| 359 | &optional
|
---|
[1668] | 360 | (ring +ring-of-integers+)
|
---|
[1133] | 361 | &aux
|
---|
| 362 | (order #'lex>))
|
---|
| 363 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
[1910] | 364 | (declare (type ring ring))
|
---|
[1133] | 365 | (poly-lc (poly-eval expr nil ring order)))
|
---|
| 366 |
|
---|
[1189] | 367 | (defun spoly (ring-and-order f g
|
---|
| 368 | &aux
|
---|
| 369 | (ring (ro-ring ring-and-order)))
|
---|
[55] | 370 | "It yields the S-polynomial of polynomials F and G."
|
---|
[1911] | 371 | (declare (type ring-and-order ring-and-order) (type poly f g))
|
---|
[55] | 372 | (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
|
---|
| 373 | (mf (monom-div lcm (poly-lm f)))
|
---|
| 374 | (mg (monom-div lcm (poly-lm g))))
|
---|
| 375 | (declare (type monom mf mg))
|
---|
| 376 | (multiple-value-bind (c cf cg)
|
---|
| 377 | (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
|
---|
| 378 | (declare (ignore c))
|
---|
| 379 | (poly-sub
|
---|
[1189] | 380 | ring-and-order
|
---|
[55] | 381 | (scalar-times-poly ring cg (monom-times-poly mf f))
|
---|
| 382 | (scalar-times-poly ring cf (monom-times-poly mg g))))))
|
---|
[53] | 383 |
|
---|
| 384 |
|
---|
[55] | 385 | (defun poly-primitive-part (ring p)
|
---|
| 386 | "Divide polynomial P with integer coefficients by gcd of its
|
---|
| 387 | coefficients and return the result."
|
---|
[1912] | 388 | (declare (type ring ring) (type poly p))
|
---|
[55] | 389 | (if (poly-zerop p)
|
---|
| 390 | (values p 1)
|
---|
| 391 | (let ((c (poly-content ring p)))
|
---|
[1203] | 392 | (values (make-poly-from-termlist
|
---|
| 393 | (mapcar
|
---|
| 394 | #'(lambda (x)
|
---|
[1847] | 395 | (make-term :monom (term-monom x)
|
---|
| 396 | :coeff (funcall (ring-div ring) (term-coeff x) c)))
|
---|
[1203] | 397 | (poly-termlist p))
|
---|
| 398 | (poly-sugar p))
|
---|
| 399 | c))))
|
---|
[55] | 400 |
|
---|
| 401 | (defun poly-content (ring p)
|
---|
| 402 | "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
|
---|
| 403 | to compute the greatest common divisor."
|
---|
[1913] | 404 | (declare (type ring ring) (type poly p))
|
---|
[55] | 405 | (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
|
---|
[1066] | 406 |
|
---|
[1091] | 407 | (defun read-infix-form (&key (stream t))
|
---|
[1066] | 408 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 409 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 410 |
|
---|
| 411 | - polynomials in fully expanded forms with coefficients
|
---|
| 412 | written in front of symbolic expressions; constants can be optionally
|
---|
| 413 | enclosed in (); for example, the infix form
|
---|
| 414 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 415 | parses to
|
---|
| 416 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 417 |
|
---|
| 418 | - lists of polynomials; for example
|
---|
| 419 | [X-Y, X^2+3*Z]
|
---|
| 420 | parses to
|
---|
| 421 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 422 | where the first symbol [ marks a list of polynomials.
|
---|
| 423 |
|
---|
| 424 | -other infix expressions, for example
|
---|
| 425 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 426 | parses to:
|
---|
| 427 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 428 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 429 | (read-from-string
|
---|
| 430 | (concatenate 'string
|
---|
| 431 | "#I("
|
---|
| 432 | (with-output-to-string (s)
|
---|
| 433 | (loop
|
---|
| 434 | (multiple-value-bind (line eof)
|
---|
| 435 | (read-line stream t)
|
---|
| 436 | (format s "~A" line)
|
---|
| 437 | (when eof (return)))))
|
---|
| 438 | ")")))
|
---|
| 439 |
|
---|
[1145] | 440 | (defun read-poly (vars &key
|
---|
| 441 | (stream t)
|
---|
[1668] | 442 | (ring +ring-of-integers+)
|
---|
[1145] | 443 | (order #'lex>))
|
---|
[1067] | 444 | "Reads an expression in prefix form from a stream STREAM.
|
---|
[1144] | 445 | The expression read from the strem should represent a polynomial or a
|
---|
| 446 | list of polynomials in variables VARS, over the ring RING. The
|
---|
| 447 | polynomial or list of polynomials is returned, with terms in each
|
---|
| 448 | polynomial ordered according to monomial order ORDER."
|
---|
[1146] | 449 | (poly-eval (read-infix-form :stream stream) vars ring order))
|
---|
[1092] | 450 |
|
---|
[1146] | 451 | (defun string->poly (str vars
|
---|
[1164] | 452 | &optional
|
---|
[1668] | 453 | (ring +ring-of-integers+)
|
---|
[1146] | 454 | (order #'lex>))
|
---|
| 455 | "Converts a string STR to a polynomial in variables VARS."
|
---|
[1097] | 456 | (with-input-from-string (s str)
|
---|
[1165] | 457 | (read-poly vars :stream s :ring ring :order order)))
|
---|
[1095] | 458 |
|
---|
[1143] | 459 | (defun poly->alist (p)
|
---|
| 460 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
| 461 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
| 462 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
| 463 | corresponding coefficient in the ring."
|
---|
[1171] | 464 | (cond
|
---|
| 465 | ((poly-p p)
|
---|
| 466 | (mapcar #'term->cons (poly-termlist p)))
|
---|
| 467 | ((and (consp p) (eq (car p) :[))
|
---|
[1172] | 468 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
[1143] | 469 |
|
---|
[1164] | 470 | (defun string->alist (str vars
|
---|
| 471 | &optional
|
---|
[1668] | 472 | (ring +ring-of-integers+)
|
---|
[1164] | 473 | (order #'lex>))
|
---|
[1143] | 474 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
[1158] | 475 | an association list (... (MONOM . COEFF) ...)."
|
---|
[1166] | 476 | (poly->alist (string->poly str vars ring order)))
|
---|
[1440] | 477 |
|
---|
| 478 | (defun poly-equal-no-sugar-p (p q)
|
---|
| 479 | "Compare polynomials for equality, ignoring sugar."
|
---|
[1914] | 480 | (declare (type poly p q))
|
---|
[1440] | 481 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
[1559] | 482 |
|
---|
| 483 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
| 484 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
| 485 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
[1560] | 486 |
|
---|
| 487 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
| 488 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
| 489 | (every #'poly-equal-no-sugar-p p q))
|
---|