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