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