[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 |
|
---|
[431] | 22 | (defpackage "POLYNOMIAL"
|
---|
[3055] | 23 | (:use :cl :utils :ring :monom :order :term #| :infix |# )
|
---|
[2596] | 24 | (:export "POLY"
|
---|
| 25 | "POLY-TERMLIST"
|
---|
[3016] | 26 | "POLY-TERM-ORDER"
|
---|
| 27 | "CHANGE-TERM-ORDER")
|
---|
[2522] | 28 | (:documentation "Implements polynomials"))
|
---|
[143] | 29 |
|
---|
[431] | 30 | (in-package :polynomial)
|
---|
| 31 |
|
---|
[1927] | 32 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[52] | 33 |
|
---|
[2442] | 34 | (defclass poly ()
|
---|
[2697] | 35 | ((termlist :initarg :termlist :accessor poly-termlist
|
---|
| 36 | :documentation "List of terms.")
|
---|
| 37 | (order :initarg :order :accessor poly-term-order
|
---|
| 38 | :documentation "Monomial/term order."))
|
---|
[2695] | 39 | (:default-initargs :termlist nil :order #'lex>)
|
---|
| 40 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
---|
[2696] | 41 | according to term order ORDER, which defaults to LEX>."))
|
---|
[2442] | 42 |
|
---|
[2471] | 43 | (defmethod print-object ((self poly) stream)
|
---|
[2600] | 44 | (format stream "#<POLY TERMLIST=~A ORDER=~A>"
|
---|
[2595] | 45 | (poly-termlist self)
|
---|
| 46 | (poly-term-order self)))
|
---|
[2469] | 47 |
|
---|
[3015] | 48 | (defgeneric change-term-order (self other)
|
---|
[3012] | 49 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
---|
[3010] | 50 | (:method ((self poly) (other poly))
|
---|
| 51 | (unless (eq (poly-term-order self) (poly-term-order other))
|
---|
| 52 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
---|
| 53 | (poly-term-order self) (poly-term-order other)))
|
---|
[3012] | 54 | self))
|
---|
[3010] | 55 |
|
---|
[2650] | 56 | (defmethod r-equalp ((self poly) (other poly))
|
---|
[2680] | 57 | "POLY instances are R-EQUALP if they have the same
|
---|
| 58 | order and if all terms are R-EQUALP."
|
---|
[2651] | 59 | (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
|
---|
| 60 | (eq (poly-term-order self) (poly-term-order other))))
|
---|
[2650] | 61 |
|
---|
[2513] | 62 | (defmethod insert-item ((self poly) (item term))
|
---|
| 63 | (push item (poly-termlist self))
|
---|
[2514] | 64 | self)
|
---|
[2464] | 65 |
|
---|
[2513] | 66 | (defmethod append-item ((self poly) (item term))
|
---|
| 67 | (setf (cdr (last (poly-termlist self))) (list item))
|
---|
| 68 | self)
|
---|
[2466] | 69 |
|
---|
[52] | 70 | ;; Leading term
|
---|
[2442] | 71 | (defgeneric leading-term (object)
|
---|
| 72 | (:method ((self poly))
|
---|
[2525] | 73 | (car (poly-termlist self)))
|
---|
| 74 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
[52] | 75 |
|
---|
| 76 | ;; Second term
|
---|
[2442] | 77 | (defgeneric second-leading-term (object)
|
---|
| 78 | (:method ((self poly))
|
---|
[2525] | 79 | (cadar (poly-termlist self)))
|
---|
| 80 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
[52] | 81 |
|
---|
| 82 | ;; Leading coefficient
|
---|
[2442] | 83 | (defgeneric leading-coefficient (object)
|
---|
| 84 | (:method ((self poly))
|
---|
[2526] | 85 | (r-coeff (leading-term self)))
|
---|
[2545] | 86 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
[52] | 87 |
|
---|
| 88 | ;; Second coefficient
|
---|
[2442] | 89 | (defgeneric second-leading-coefficient (object)
|
---|
| 90 | (:method ((self poly))
|
---|
[2526] | 91 | (r-coeff (second-leading-term self)))
|
---|
[2906] | 92 | (:documentation "The second leading coefficient of a polynomial. It
|
---|
| 93 | signals error for a polynomial with at most one term."))
|
---|
[52] | 94 |
|
---|
| 95 | ;; Testing for a zero polynomial
|
---|
[2445] | 96 | (defmethod r-zerop ((self poly))
|
---|
| 97 | (null (poly-termlist self)))
|
---|
[52] | 98 |
|
---|
| 99 | ;; The number of terms
|
---|
[2445] | 100 | (defmethod r-length ((self poly))
|
---|
| 101 | (length (poly-termlist self)))
|
---|
[52] | 102 |
|
---|
[2483] | 103 | (defmethod multiply-by ((self poly) (other monom))
|
---|
[2501] | 104 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
| 105 | (poly-termlist self))
|
---|
[2483] | 106 | self)
|
---|
[2469] | 107 |
|
---|
[2501] | 108 | (defmethod multiply-by ((self poly) (other scalar))
|
---|
[2502] | 109 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
[2501] | 110 | (poly-termlist self))
|
---|
[2487] | 111 | self)
|
---|
| 112 |
|
---|
[2607] | 113 |
|
---|
[2761] | 114 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
---|
[2755] | 115 | "Return an expression which will efficiently adds/subtracts two
|
---|
| 116 | polynomials, P and Q. The addition/subtraction of coefficients is
|
---|
| 117 | performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
|
---|
| 118 | is supplied, it is used to negate the coefficients of Q which do not
|
---|
[2756] | 119 | have a corresponding coefficient in P. The code implements an
|
---|
| 120 | efficient algorithm to add two polynomials represented as sorted lists
|
---|
| 121 | of terms. The code destroys both arguments, reusing the terms to build
|
---|
| 122 | the result."
|
---|
[2742] | 123 | `(macrolet ((lc (x) `(r-coeff (car ,x))))
|
---|
| 124 | (do ((p ,p)
|
---|
| 125 | (q ,q)
|
---|
| 126 | r)
|
---|
| 127 | ((or (endp p) (endp q))
|
---|
| 128 | ;; NOTE: R contains the result in reverse order. Can it
|
---|
| 129 | ;; be more efficient to produce the terms in correct order?
|
---|
[2774] | 130 | (unless (endp q)
|
---|
[2776] | 131 | ;; Upon subtraction, we must change the sign of
|
---|
| 132 | ;; all coefficients in q
|
---|
[2774] | 133 | ,@(when uminus-fn
|
---|
[2775] | 134 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
---|
[2774] | 135 | (setf r (nreconc r q)))
|
---|
[2742] | 136 | r)
|
---|
| 137 | (multiple-value-bind
|
---|
| 138 | (greater-p equal-p)
|
---|
[2766] | 139 | (funcall ,order-fn (car p) (car q))
|
---|
[2742] | 140 | (cond
|
---|
| 141 | (greater-p
|
---|
| 142 | (rotatef (cdr p) r p)
|
---|
| 143 | )
|
---|
| 144 | (equal-p
|
---|
[2766] | 145 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
---|
[2742] | 146 | (cond
|
---|
| 147 | ((r-zerop s)
|
---|
| 148 | (setf p (cdr p))
|
---|
| 149 | )
|
---|
| 150 | (t
|
---|
| 151 | (setf (lc p) s)
|
---|
| 152 | (rotatef (cdr p) r p))))
|
---|
| 153 | (setf q (cdr q))
|
---|
| 154 | )
|
---|
| 155 | (t
|
---|
[2743] | 156 | ;;Negate the term of Q if UMINUS provided, signallig
|
---|
| 157 | ;;that we are doing subtraction
|
---|
[2908] | 158 | ,(when uminus-fn
|
---|
| 159 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
---|
[2743] | 160 | (rotatef (cdr q) r q)))))))
|
---|
[2585] | 161 |
|
---|
[2655] | 162 |
|
---|
[2763] | 163 | (defmacro def-add/subtract-method (add/subtract-method-name
|
---|
[2752] | 164 | uminus-method-name
|
---|
| 165 | &optional
|
---|
[2913] | 166 | (doc-string nil doc-string-supplied-p))
|
---|
[2615] | 167 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
[2749] | 168 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
---|
[2615] | 169 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
[2769] | 170 | ;; Ensure orders are compatible
|
---|
[3015] | 171 | (change-term-order other self)
|
---|
[2772] | 172 | (setf (poly-termlist self) (fast-add/subtract
|
---|
| 173 | (poly-termlist self) (poly-termlist other)
|
---|
| 174 | (poly-term-order self)
|
---|
| 175 | #',add/subtract-method-name
|
---|
| 176 | ,(when uminus-method-name `(function ,uminus-method-name))))
|
---|
[2609] | 177 | self))
|
---|
[2487] | 178 |
|
---|
[2916] | 179 | (eval-when (:compile-toplevel :load-toplevel :execute)
|
---|
[2777] | 180 |
|
---|
| 181 | (def-add/subtract-method add-to nil
|
---|
| 182 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
[2610] | 183 | This operation destructively modifies both polynomials.
|
---|
| 184 | The result is stored in SELF. This implementation does
|
---|
[2752] | 185 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
[2609] | 186 |
|
---|
[2777] | 187 | (def-add/subtract-method subtract-from unary-minus
|
---|
[2753] | 188 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
[2610] | 189 | This operation destructively modifies both polynomials.
|
---|
| 190 | The result is stored in SELF. This implementation does
|
---|
[2752] | 191 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
[2610] | 192 |
|
---|
[2916] | 193 | )
|
---|
[2777] | 194 |
|
---|
[2916] | 195 |
|
---|
| 196 |
|
---|
[2691] | 197 | (defmethod unary-minus ((self poly))
|
---|
[2694] | 198 | "Destructively modifies the coefficients of the polynomial SELF,
|
---|
| 199 | by changing their sign."
|
---|
[2692] | 200 | (mapc #'unary-minus (poly-termlist self))
|
---|
[2683] | 201 | self)
|
---|
[52] | 202 |
|
---|
[2795] | 203 | (defun add-termlists (p q order-fn)
|
---|
[2794] | 204 | "Destructively adds two termlists P and Q ordered according to ORDER-FN."
|
---|
[2917] | 205 | (fast-add/subtract p q order-fn #'add-to nil))
|
---|
[2794] | 206 |
|
---|
[2800] | 207 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
---|
[2927] | 208 | &optional (reverse-arg-order-P nil))
|
---|
[2799] | 209 | "Multiplies term TERM by a list of term, TERMLIST.
|
---|
[2792] | 210 | Takes into accound divisors of zero in the ring, by
|
---|
[2927] | 211 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
---|
[2928] | 212 | is T, change the order of arguments; this may be important
|
---|
[2927] | 213 | if we extend the package to non-commutative rings."
|
---|
[2800] | 214 | `(mapcan #'(lambda (other-term)
|
---|
[2907] | 215 | (let ((prod (r*
|
---|
[2923] | 216 | ,@(cond
|
---|
[2930] | 217 | (reverse-arg-order-p
|
---|
[2925] | 218 | `(other-term ,term))
|
---|
| 219 | (t
|
---|
| 220 | `(,term other-term))))))
|
---|
[2800] | 221 | (cond
|
---|
| 222 | ((r-zerop prod) nil)
|
---|
| 223 | (t (list prod)))))
|
---|
| 224 | ,termlist))
|
---|
[2790] | 225 |
|
---|
[2796] | 226 | (defun multiply-termlists (p q order-fn)
|
---|
[2787] | 227 | (cond
|
---|
[2917] | 228 | ((or (endp p) (endp q))
|
---|
| 229 | ;;p or q is 0 (represented by NIL)
|
---|
| 230 | nil)
|
---|
[2789] | 231 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
---|
[2787] | 232 | ((endp (cdr p))
|
---|
[2918] | 233 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
---|
| 234 | ((endp (cdr q))
|
---|
[2919] | 235 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
---|
| 236 | (t
|
---|
[2948] | 237 | (cons (r* (car p) (car q))
|
---|
[2949] | 238 | (add-termlists
|
---|
| 239 | (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
|
---|
| 240 | (multiply-termlists (cdr p) q order-fn)
|
---|
| 241 | order-fn)))))
|
---|
[2793] | 242 |
|
---|
[2803] | 243 | (defmethod multiply-by ((self poly) (other poly))
|
---|
[3014] | 244 | (change-term-order other self)
|
---|
[2803] | 245 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
---|
| 246 | (poly-termlist other)
|
---|
| 247 | (poly-term-order self)))
|
---|
| 248 | self)
|
---|
| 249 |
|
---|
[2939] | 250 | (defmethod r* ((poly1 poly) (poly2 poly))
|
---|
| 251 | "Non-destructively multiply POLY1 by POLY2."
|
---|
| 252 | (multiply-by (copy-instance POLY1) (copy-instance POLY2)))
|
---|
[2916] | 253 |
|
---|
[3044] | 254 | (defmethod left-tensor-product-by ((self poly) (other term))
|
---|
| 255 | (setf (poly-termlist self)
|
---|
| 256 | (mapcan #'(lambda (term)
|
---|
[3047] | 257 | (let ((prod (left-tensor-product-by term other)))
|
---|
[3044] | 258 | (cond
|
---|
| 259 | ((r-zerop prod) nil)
|
---|
| 260 | (t (list prod)))))
|
---|
[3048] | 261 | (poly-termlist self)))
|
---|
[3044] | 262 | self)
|
---|
| 263 |
|
---|
| 264 | (defmethod right-tensor-product-by ((self poly) (other term))
|
---|
[3045] | 265 | (setf (poly-termlist self)
|
---|
| 266 | (mapcan #'(lambda (term)
|
---|
[3046] | 267 | (let ((prod (right-tensor-product-by term other)))
|
---|
[3045] | 268 | (cond
|
---|
| 269 | ((r-zerop prod) nil)
|
---|
| 270 | (t (list prod)))))
|
---|
[3048] | 271 | (poly-termlist self)))
|
---|
[3045] | 272 | self)
|
---|
[3044] | 273 |
|
---|
| 274 |
|
---|
[52] | 275 | (defun poly-standard-extension (plist &aux (k (length plist)))
|
---|
[2716] | 276 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
---|
| 277 | is a list of polynomials."
|
---|
[3058] | 278 | (makelist (left-tensor-product-by (nth i plist) (make-monom-variable k i)) (i 0 k)))
|
---|
[52] | 279 |
|
---|
[3050] | 280 | #|
|
---|
[2716] | 281 |
|
---|
[1473] | 282 | (defun saturation-extension (ring f plist
|
---|
| 283 | &aux
|
---|
| 284 | (k (length plist))
|
---|
[1474] | 285 | (d (monom-dimension (poly-lm (car plist))))
|
---|
| 286 | f-x plist-x)
|
---|
[52] | 287 | "Calculate [F, U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK]."
|
---|
[1907] | 288 | (declare (type ring ring))
|
---|
[1474] | 289 | (setf f-x (poly-list-add-variables f k)
|
---|
| 290 | plist-x (mapcar #'(lambda (x)
|
---|
[1843] | 291 | (setf (poly-termlist x)
|
---|
| 292 | (nconc (poly-termlist x)
|
---|
| 293 | (list (make-term :monom (make-monom :dimension d)
|
---|
[1844] | 294 | :coeff (funcall (ring-uminus ring)
|
---|
| 295 | (funcall (ring-unit ring)))))))
|
---|
[1474] | 296 | x)
|
---|
| 297 | (poly-standard-extension plist)))
|
---|
| 298 | (append f-x plist-x))
|
---|
[52] | 299 |
|
---|
| 300 |
|
---|
[1475] | 301 | (defun polysaturation-extension (ring f plist
|
---|
| 302 | &aux
|
---|
| 303 | (k (length plist))
|
---|
[1476] | 304 | (d (+ k (monom-dimension (poly-lm (car plist)))))
|
---|
[1494] | 305 | ;; Add k variables to f
|
---|
[1493] | 306 | (f (poly-list-add-variables f k))
|
---|
[1495] | 307 | ;; Set PLIST to [U1*P1,U2*P2,...,UK*PK]
|
---|
[1493] | 308 | (plist (apply #'poly-append (poly-standard-extension plist))))
|
---|
[1497] | 309 | "Calculate [F, U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]. It destructively modifies F."
|
---|
[1493] | 310 | ;; Add -1 as the last term
|
---|
[1908] | 311 | (declare (type ring ring))
|
---|
[1493] | 312 | (setf (cdr (last (poly-termlist plist)))
|
---|
[1845] | 313 | (list (make-term :monom (make-monom :dimension d)
|
---|
| 314 | :coeff (funcall (ring-uminus ring) (funcall (ring-unit ring))))))
|
---|
[1493] | 315 | (append f (list plist)))
|
---|
[52] | 316 |
|
---|
[1477] | 317 | (defun saturation-extension-1 (ring f p)
|
---|
[1497] | 318 | "Calculate [F, U*P-1]. It destructively modifies F."
|
---|
[1908] | 319 | (declare (type ring ring))
|
---|
[1477] | 320 | (polysaturation-extension ring f (list p)))
|
---|
[53] | 321 |
|
---|
| 322 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 323 | ;;
|
---|
| 324 | ;; Evaluation of polynomial (prefix) expressions
|
---|
| 325 | ;;
|
---|
| 326 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 327 |
|
---|
| 328 | (defun coerce-coeff (ring expr vars)
|
---|
| 329 | "Coerce an element of the coefficient ring to a constant polynomial."
|
---|
| 330 | ;; Modular arithmetic handler by rat
|
---|
[1908] | 331 | (declare (type ring ring))
|
---|
[1846] | 332 | (make-poly-from-termlist (list (make-term :monom (make-monom :dimension (length vars))
|
---|
| 333 | :coeff (funcall (ring-parse ring) expr)))
|
---|
[53] | 334 | 0))
|
---|
| 335 |
|
---|
[1046] | 336 | (defun poly-eval (expr vars
|
---|
| 337 | &optional
|
---|
[1668] | 338 | (ring +ring-of-integers+)
|
---|
[1048] | 339 | (order #'lex>)
|
---|
[1170] | 340 | (list-marker :[)
|
---|
[1047] | 341 | &aux
|
---|
| 342 | (ring-and-order (make-ring-and-order :ring ring :order order)))
|
---|
[1168] | 343 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
[1208] | 344 | variables VARS. Return the resulting polynomial or list of
|
---|
| 345 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
| 346 | replaced with their analogues in the ring of polynomials, and the
|
---|
| 347 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
[1209] | 348 | of polynomials in internal form. A similar operation in another computer
|
---|
| 349 | algebra system could be called 'expand' or so."
|
---|
[1909] | 350 | (declare (type ring ring))
|
---|
[1050] | 351 | (labels ((p-eval (arg) (poly-eval arg vars ring order))
|
---|
[1140] | 352 | (p-eval-scalar (arg) (poly-eval-scalar arg))
|
---|
[53] | 353 | (p-eval-list (args) (mapcar #'p-eval args))
|
---|
[989] | 354 | (p-add (x y) (poly-add ring-and-order x y)))
|
---|
[53] | 355 | (cond
|
---|
[1128] | 356 | ((null expr) (error "Empty expression"))
|
---|
[53] | 357 | ((eql expr 0) (make-poly-zero))
|
---|
| 358 | ((member expr vars :test #'equalp)
|
---|
| 359 | (let ((pos (position expr vars :test #'equalp)))
|
---|
[1657] | 360 | (make-poly-variable ring (length vars) pos)))
|
---|
[53] | 361 | ((atom expr)
|
---|
| 362 | (coerce-coeff ring expr vars))
|
---|
| 363 | ((eq (car expr) list-marker)
|
---|
| 364 | (cons list-marker (p-eval-list (cdr expr))))
|
---|
| 365 | (t
|
---|
| 366 | (case (car expr)
|
---|
| 367 | (+ (reduce #'p-add (p-eval-list (cdr expr))))
|
---|
| 368 | (- (case (length expr)
|
---|
| 369 | (1 (make-poly-zero))
|
---|
| 370 | (2 (poly-uminus ring (p-eval (cadr expr))))
|
---|
[989] | 371 | (3 (poly-sub ring-and-order (p-eval (cadr expr)) (p-eval (caddr expr))))
|
---|
| 372 | (otherwise (poly-sub ring-and-order (p-eval (cadr expr))
|
---|
[53] | 373 | (reduce #'p-add (p-eval-list (cddr expr)))))))
|
---|
| 374 | (*
|
---|
| 375 | (if (endp (cddr expr)) ;unary
|
---|
| 376 | (p-eval (cdr expr))
|
---|
[989] | 377 | (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (p-eval-list (cdr expr)))))
|
---|
[1106] | 378 | (/
|
---|
| 379 | ;; A polynomial can be divided by a scalar
|
---|
[1115] | 380 | (cond
|
---|
| 381 | ((endp (cddr expr))
|
---|
[1117] | 382 | ;; A special case (/ ?), the inverse
|
---|
[1119] | 383 | (coerce-coeff ring (apply (ring-div ring) (cdr expr)) vars))
|
---|
[1128] | 384 | (t
|
---|
[1115] | 385 | (let ((num (p-eval (cadr expr)))
|
---|
[1142] | 386 | (denom-inverse (apply (ring-div ring)
|
---|
| 387 | (cons (funcall (ring-unit ring))
|
---|
| 388 | (mapcar #'p-eval-scalar (cddr expr))))))
|
---|
[1118] | 389 | (scalar-times-poly ring denom-inverse num)))))
|
---|
[53] | 390 | (expt
|
---|
| 391 | (cond
|
---|
| 392 | ((member (cadr expr) vars :test #'equalp)
|
---|
| 393 | ;;Special handling of (expt var pow)
|
---|
| 394 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
[1657] | 395 | (make-poly-variable ring (length vars) pos (caddr expr))))
|
---|
[53] | 396 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
| 397 | ;; Negative power means division in coefficient ring
|
---|
| 398 | ;; Non-integer power means non-polynomial coefficient
|
---|
| 399 | (coerce-coeff ring expr vars))
|
---|
[989] | 400 | (t (poly-expt ring-and-order (p-eval (cadr expr)) (caddr expr)))))
|
---|
[53] | 401 | (otherwise
|
---|
| 402 | (coerce-coeff ring expr vars)))))))
|
---|
| 403 |
|
---|
[1133] | 404 | (defun poly-eval-scalar (expr
|
---|
| 405 | &optional
|
---|
[1668] | 406 | (ring +ring-of-integers+)
|
---|
[1133] | 407 | &aux
|
---|
| 408 | (order #'lex>))
|
---|
| 409 | "Evaluate a scalar expression EXPR in ring RING."
|
---|
[1910] | 410 | (declare (type ring ring))
|
---|
[1133] | 411 | (poly-lc (poly-eval expr nil ring order)))
|
---|
| 412 |
|
---|
[1189] | 413 | (defun spoly (ring-and-order f g
|
---|
| 414 | &aux
|
---|
| 415 | (ring (ro-ring ring-and-order)))
|
---|
[55] | 416 | "It yields the S-polynomial of polynomials F and G."
|
---|
[1911] | 417 | (declare (type ring-and-order ring-and-order) (type poly f g))
|
---|
[55] | 418 | (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
|
---|
[2913] | 419 | (mf (monom-div lcm (poly-lm f)))
|
---|
| 420 | (mg (monom-div lcm (poly-lm g))))
|
---|
[55] | 421 | (declare (type monom mf mg))
|
---|
| 422 | (multiple-value-bind (c cf cg)
|
---|
| 423 | (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
|
---|
| 424 | (declare (ignore c))
|
---|
| 425 | (poly-sub
|
---|
[1189] | 426 | ring-and-order
|
---|
[55] | 427 | (scalar-times-poly ring cg (monom-times-poly mf f))
|
---|
| 428 | (scalar-times-poly ring cf (monom-times-poly mg g))))))
|
---|
[53] | 429 |
|
---|
| 430 |
|
---|
[55] | 431 | (defun poly-primitive-part (ring p)
|
---|
| 432 | "Divide polynomial P with integer coefficients by gcd of its
|
---|
| 433 | coefficients and return the result."
|
---|
[1912] | 434 | (declare (type ring ring) (type poly p))
|
---|
[55] | 435 | (if (poly-zerop p)
|
---|
| 436 | (values p 1)
|
---|
[2913] | 437 | (let ((c (poly-content ring p)))
|
---|
| 438 | (values (make-poly-from-termlist
|
---|
| 439 | (mapcar
|
---|
| 440 | #'(lambda (x)
|
---|
| 441 | (make-term :monom (term-monom x)
|
---|
| 442 | :coeff (funcall (ring-div ring) (term-coeff x) c)))
|
---|
| 443 | (poly-termlist p))
|
---|
| 444 | (poly-sugar p))
|
---|
| 445 | c))))
|
---|
[55] | 446 |
|
---|
| 447 | (defun poly-content (ring p)
|
---|
| 448 | "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
|
---|
| 449 | to compute the greatest common divisor."
|
---|
[1913] | 450 | (declare (type ring ring) (type poly p))
|
---|
[55] | 451 | (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
|
---|
[1066] | 452 |
|
---|
[1091] | 453 | (defun read-infix-form (&key (stream t))
|
---|
[1066] | 454 | "Parser of infix expressions with integer/rational coefficients
|
---|
| 455 | The parser will recognize two kinds of polynomial expressions:
|
---|
| 456 |
|
---|
| 457 | - polynomials in fully expanded forms with coefficients
|
---|
| 458 | written in front of symbolic expressions; constants can be optionally
|
---|
| 459 | enclosed in (); for example, the infix form
|
---|
| 460 | X^2-Y^2+(-4/3)*U^2*W^3-5
|
---|
| 461 | parses to
|
---|
| 462 | (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))
|
---|
| 463 |
|
---|
| 464 | - lists of polynomials; for example
|
---|
| 465 | [X-Y, X^2+3*Z]
|
---|
| 466 | parses to
|
---|
| 467 | (:[ (- X Y) (+ (EXPT X 2) (* 3 Z)))
|
---|
| 468 | where the first symbol [ marks a list of polynomials.
|
---|
| 469 |
|
---|
| 470 | -other infix expressions, for example
|
---|
| 471 | [(X-Y)*(X+Y)/Z,(X+1)^2]
|
---|
| 472 | parses to:
|
---|
| 473 | (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2))
|
---|
| 474 | Currently this function is implemented using M. Kantrowitz's INFIX package."
|
---|
| 475 | (read-from-string
|
---|
| 476 | (concatenate 'string
|
---|
[2913] | 477 | "#I("
|
---|
| 478 | (with-output-to-string (s)
|
---|
| 479 | (loop
|
---|
| 480 | (multiple-value-bind (line eof)
|
---|
| 481 | (read-line stream t)
|
---|
| 482 | (format s "~A" line)
|
---|
| 483 | (when eof (return)))))
|
---|
| 484 | ")")))
|
---|
| 485 |
|
---|
[1145] | 486 | (defun read-poly (vars &key
|
---|
| 487 | (stream t)
|
---|
[1668] | 488 | (ring +ring-of-integers+)
|
---|
[1145] | 489 | (order #'lex>))
|
---|
[1067] | 490 | "Reads an expression in prefix form from a stream STREAM.
|
---|
[1144] | 491 | The expression read from the strem should represent a polynomial or a
|
---|
| 492 | list of polynomials in variables VARS, over the ring RING. The
|
---|
| 493 | polynomial or list of polynomials is returned, with terms in each
|
---|
| 494 | polynomial ordered according to monomial order ORDER."
|
---|
[1146] | 495 | (poly-eval (read-infix-form :stream stream) vars ring order))
|
---|
[1092] | 496 |
|
---|
[1146] | 497 | (defun string->poly (str vars
|
---|
[1164] | 498 | &optional
|
---|
[1668] | 499 | (ring +ring-of-integers+)
|
---|
[1146] | 500 | (order #'lex>))
|
---|
| 501 | "Converts a string STR to a polynomial in variables VARS."
|
---|
[1097] | 502 | (with-input-from-string (s str)
|
---|
[1165] | 503 | (read-poly vars :stream s :ring ring :order order)))
|
---|
[1095] | 504 |
|
---|
[1143] | 505 | (defun poly->alist (p)
|
---|
| 506 | "Convert a polynomial P to an association list. Thus, the format of the
|
---|
| 507 | returned value is ((MONOM[0] . COEFF[0]) (MONOM[1] . COEFF[1]) ...), where
|
---|
| 508 | MONOM[I] is a list of exponents in the monomial and COEFF[I] is the
|
---|
| 509 | corresponding coefficient in the ring."
|
---|
[1171] | 510 | (cond
|
---|
| 511 | ((poly-p p)
|
---|
| 512 | (mapcar #'term->cons (poly-termlist p)))
|
---|
| 513 | ((and (consp p) (eq (car p) :[))
|
---|
[1172] | 514 | (cons :[ (mapcar #'poly->alist (cdr p))))))
|
---|
[1143] | 515 |
|
---|
[1164] | 516 | (defun string->alist (str vars
|
---|
[2913] | 517 | &optional
|
---|
| 518 | (ring +ring-of-integers+)
|
---|
| 519 | (order #'lex>))
|
---|
[1143] | 520 | "Convert a string STR representing a polynomial or polynomial list to
|
---|
[1158] | 521 | an association list (... (MONOM . COEFF) ...)."
|
---|
[1166] | 522 | (poly->alist (string->poly str vars ring order)))
|
---|
[1440] | 523 |
|
---|
| 524 | (defun poly-equal-no-sugar-p (p q)
|
---|
| 525 | "Compare polynomials for equality, ignoring sugar."
|
---|
[1914] | 526 | (declare (type poly p q))
|
---|
[1440] | 527 | (equalp (poly-termlist p) (poly-termlist q)))
|
---|
[1559] | 528 |
|
---|
| 529 | (defun poly-set-equal-no-sugar-p (p q)
|
---|
| 530 | "Compare polynomial sets P and Q for equality, ignoring sugar."
|
---|
| 531 | (null (set-exclusive-or p q :test #'poly-equal-no-sugar-p )))
|
---|
[1560] | 532 |
|
---|
| 533 | (defun poly-list-equal-no-sugar-p (p q)
|
---|
| 534 | "Compare polynomial lists P and Q for equality, ignoring sugar."
|
---|
| 535 | (every #'poly-equal-no-sugar-p p q))
|
---|
[2456] | 536 | |#
|
---|