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