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