[3400] | 1 | ;;----------------------------------------------------------------
|
---|
[1201] | 2 | ;;; -*- Mode: Lisp -*-
|
---|
[77] | 3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 4 | ;;;
|
---|
| 5 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
| 6 | ;;;
|
---|
| 7 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 8 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 9 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | ;;; (at your option) any later version.
|
---|
| 11 | ;;;
|
---|
| 12 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 13 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | ;;; GNU General Public License for more details.
|
---|
| 16 | ;;;
|
---|
| 17 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 18 | ;;; along with this program; if not, write to the Free Software
|
---|
| 19 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
| 20 | ;;;
|
---|
| 21 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 22 |
|
---|
[431] | 23 | (defpackage "POLYNOMIAL"
|
---|
[3643] | 24 | (:use :cl :utils :monom :copy)
|
---|
[2596] | 25 | (:export "POLY"
|
---|
[3270] | 26 | "POLY-DIMENSION"
|
---|
[2596] | 27 | "POLY-TERMLIST"
|
---|
[3016] | 28 | "POLY-TERM-ORDER"
|
---|
[3509] | 29 | "POLY-INSERT-TERM"
|
---|
[3690] | 30 | "SCALAR-MULTIPLY-BY"
|
---|
| 31 | "SCALAR-DIVIDE-BY"
|
---|
[3642] | 32 | "LEADING-TERM"
|
---|
[3657] | 33 | "LEADING-MONOMIAL"
|
---|
[3642] | 34 | "LEADING-COEFFICIENT"
|
---|
[3657] | 35 | "SECOND-LEADING-TERM"
|
---|
| 36 | "SECOND-LEADING-MONOMIAL"
|
---|
| 37 | "SECOND-LEADING-COEFFICIENT"
|
---|
[3642] | 38 | "ADD-TO"
|
---|
[3646] | 39 | "ADD"
|
---|
[3642] | 40 | "SUBTRACT-FROM"
|
---|
[3646] | 41 | "SUBTRACT"
|
---|
[3071] | 42 | "CHANGE-TERM-ORDER"
|
---|
[3099] | 43 | "STANDARD-EXTENSION"
|
---|
[3101] | 44 | "STANDARD-EXTENSION-1"
|
---|
[3109] | 45 | "STANDARD-SUM"
|
---|
[3094] | 46 | "SATURATION-EXTENSION"
|
---|
[3655] | 47 | "ALIST->POLY"
|
---|
| 48 | "UNIVERSAL-EZGCD"
|
---|
[3678] | 49 | "S-POLYNOMIAL"
|
---|
[3686] | 50 | "POLY-CONTENT"
|
---|
[3692] | 51 | "POLY-PRIMITIVE-PART"
|
---|
[3714] | 52 | "SATURATION-EXTENSION-1"
|
---|
[3737] | 53 | "MAKE-POLY-VARIABLE"
|
---|
[3821] | 54 | "MAKE-POLY-CONSTANT"
|
---|
[3778] | 55 | "UNIVERSAL-EXPT"
|
---|
| 56 | "POLY-P")
|
---|
[3489] | 57 | (:documentation "Implements polynomials. A polynomial is essentially
|
---|
| 58 | a mapping of monomials of the same degree to coefficients. The
|
---|
| 59 | momomials are ordered according to a monomial order."))
|
---|
[143] | 60 |
|
---|
[431] | 61 | (in-package :polynomial)
|
---|
| 62 |
|
---|
[1927] | 63 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[52] | 64 |
|
---|
[2442] | 65 | (defclass poly ()
|
---|
[3253] | 66 | ((dimension :initform nil
|
---|
[3250] | 67 | :initarg :dimension
|
---|
| 68 | :accessor poly-dimension
|
---|
[3242] | 69 | :documentation "Shared dimension of all terms, the number of variables")
|
---|
[3250] | 70 | (termlist :initform nil :initarg :termlist :accessor poly-termlist
|
---|
[3619] | 71 | :documentation "List of terms.")
|
---|
[3250] | 72 | (order :initform #'lex> :initarg :order :accessor poly-term-order
|
---|
[2697] | 73 | :documentation "Monomial/term order."))
|
---|
[3262] | 74 | (:default-initargs :dimension nil :termlist nil :order #'lex>)
|
---|
[2695] | 75 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
---|
[2696] | 76 | according to term order ORDER, which defaults to LEX>."))
|
---|
[2442] | 77 |
|
---|
[2471] | 78 | (defmethod print-object ((self poly) stream)
|
---|
[3241] | 79 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3243] | 80 | (with-accessors ((dimension poly-dimension)
|
---|
| 81 | (termlist poly-termlist)
|
---|
| 82 | (order poly-term-order))
|
---|
[3237] | 83 | self
|
---|
[3244] | 84 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
|
---|
| 85 | dimension termlist order))))
|
---|
[2469] | 86 |
|
---|
[3015] | 87 | (defgeneric change-term-order (self other)
|
---|
[3012] | 88 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
---|
[3010] | 89 | (:method ((self poly) (other poly))
|
---|
| 90 | (unless (eq (poly-term-order self) (poly-term-order other))
|
---|
[3620] | 91 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
---|
[3010] | 92 | (poly-term-order self) (poly-term-order other)))
|
---|
[3012] | 93 | self))
|
---|
[3010] | 94 |
|
---|
[3621] | 95 | (defgeneric poly-insert-term (self term)
|
---|
[3622] | 96 | (:documentation "Insert a term TERM into SELF before all other
|
---|
[3621] | 97 | terms. Order is not enforced.")
|
---|
| 98 | (:method ((self poly) (term term))
|
---|
[3510] | 99 | (cond ((null (poly-dimension self))
|
---|
[3621] | 100 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
| 101 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
| 102 | (push term (poly-termlist self))
|
---|
[3510] | 103 | self))
|
---|
| 104 |
|
---|
[3622] | 105 | (defgeneric poly-append-term (self term)
|
---|
| 106 | (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
|
---|
| 107 | (:method ((self poly) (term term))
|
---|
[3510] | 108 | (cond ((null (poly-dimension self))
|
---|
[3622] | 109 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
| 110 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
| 111 | (setf (cdr (last (poly-termlist self))) (list term))
|
---|
[3510] | 112 | self))
|
---|
| 113 |
|
---|
[3095] | 114 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
|
---|
[3126] | 115 | "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
|
---|
| 116 | It can be used to enter simple polynomials by hand, e.g the polynomial
|
---|
| 117 | in two variables, X and Y, given in standard notation as:
|
---|
| 118 |
|
---|
| 119 | 3*X^2*Y^3+2*Y+7
|
---|
| 120 |
|
---|
| 121 | can be entered as
|
---|
| 122 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
|
---|
| 123 |
|
---|
| 124 | NOTE: The primary use is for low-level debugging of the package."
|
---|
[3099] | 125 | (dolist (x alist poly)
|
---|
[3705] | 126 | (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
|
---|
[3092] | 127 |
|
---|
[3795] | 128 | (defmethod update-instance-for-different-class :before ((old term) (new poly) &key)
|
---|
[3786] | 129 | "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
[3401] | 130 | (reinitialize-instance new
|
---|
| 131 | :dimension (monom-dimension old)
|
---|
[3786] | 132 | :termlist (list old)))
|
---|
[3796] | 133 |
|
---|
| 134 | (defmethod update-instance-for-different-class :before ((old monom) (new poly) &key)
|
---|
| 135 | "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
| 136 | (reinitialize-instance new
|
---|
| 137 | :dimension (monom-dimension old)
|
---|
[3797] | 138 | :termlist (list (change-class old 'term))))
|
---|
[3403] | 139 |
|
---|
[3624] | 140 | (defmethod universal-equalp ((self poly) (other poly))
|
---|
| 141 | "Implements equality of polynomials."
|
---|
| 142 | (and (eql (poly-dimension self) (poly-dimension other))
|
---|
| 143 | (every #'universal-equalp (poly-termlist self) (poly-termlist other))
|
---|
| 144 | (eq (poly-term-order self) (poly-term-order other))))
|
---|
[2650] | 145 |
|
---|
[3624] | 146 | (defgeneric leading-term (object)
|
---|
[2442] | 147 | (:method ((self poly))
|
---|
[2525] | 148 | (car (poly-termlist self)))
|
---|
| 149 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
[52] | 150 |
|
---|
[3625] | 151 | (defgeneric second-leading-term (object)
|
---|
[2442] | 152 | (:method ((self poly))
|
---|
[2525] | 153 | (cadar (poly-termlist self)))
|
---|
| 154 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
[52] | 155 |
|
---|
[3656] | 156 | (defgeneric leading-monomial (object)
|
---|
| 157 | (:method ((self poly))
|
---|
| 158 | (change-class (copy-instance (leading-term self)) 'monom))
|
---|
| 159 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
| 160 |
|
---|
| 161 | (defgeneric second-leading-monomial (object)
|
---|
| 162 | (:method ((self poly))
|
---|
| 163 | (change-class (copy-instance (second-leading-term self)) 'monom))
|
---|
| 164 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
| 165 |
|
---|
[3625] | 166 | (defgeneric leading-coefficient (object)
|
---|
[2442] | 167 | (:method ((self poly))
|
---|
[3642] | 168 | (term-coeff (leading-term self)))
|
---|
[2545] | 169 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
[52] | 170 |
|
---|
[2442] | 171 | (defgeneric second-leading-coefficient (object)
|
---|
| 172 | (:method ((self poly))
|
---|
[3645] | 173 | (term-coeff (second-leading-term self)))
|
---|
[2906] | 174 | (:documentation "The second leading coefficient of a polynomial. It
|
---|
| 175 | signals error for a polynomial with at most one term."))
|
---|
[52] | 176 |
|
---|
[3629] | 177 | (defmethod universal-zerop ((self poly))
|
---|
| 178 | "Return T iff SELF is a zero polynomial."
|
---|
[3639] | 179 | (null (poly-termlist self)))
|
---|
[52] | 180 |
|
---|
[3518] | 181 | (defgeneric poly-length (self)
|
---|
[3630] | 182 | (:documentation "Return the number of terms.")
|
---|
[3518] | 183 | (:method ((self poly))
|
---|
| 184 | (length (poly-termlist self))))
|
---|
[52] | 185 |
|
---|
[3689] | 186 | (defgeneric scalar-multiply-by (self other)
|
---|
| 187 | (:documentation "Multiply vector SELF by a scalar OTHER.")
|
---|
| 188 | (:method ((self poly) other)
|
---|
| 189 | (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
|
---|
| 190 | (poly-termlist self))
|
---|
| 191 | self))
|
---|
| 192 |
|
---|
| 193 | (defgeneric scalar-divide-by (self other)
|
---|
| 194 | (:documentation "Divide vector SELF by a scalar OTHER.")
|
---|
| 195 | (:method ((self poly) other)
|
---|
| 196 | (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
|
---|
| 197 | (poly-termlist self))
|
---|
| 198 | self))
|
---|
| 199 |
|
---|
[3663] | 200 | (defmethod multiply-by ((self poly) (other monom))
|
---|
[3630] | 201 | "Multiply a polynomial SELF by OTHER."
|
---|
| 202 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
| 203 | (poly-termlist self))
|
---|
| 204 | self)
|
---|
[2469] | 205 |
|
---|
[3672] | 206 | (defmethod multiply-by ((self poly) (other term))
|
---|
| 207 | "Multiply a polynomial SELF by OTHER."
|
---|
| 208 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
| 209 | (poly-termlist self))
|
---|
| 210 | self)
|
---|
| 211 |
|
---|
[2761] | 212 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
---|
[2755] | 213 | "Return an expression which will efficiently adds/subtracts two
|
---|
| 214 | polynomials, P and Q. The addition/subtraction of coefficients is
|
---|
| 215 | performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
|
---|
| 216 | is supplied, it is used to negate the coefficients of Q which do not
|
---|
[2756] | 217 | have a corresponding coefficient in P. The code implements an
|
---|
| 218 | efficient algorithm to add two polynomials represented as sorted lists
|
---|
| 219 | of terms. The code destroys both arguments, reusing the terms to build
|
---|
| 220 | the result."
|
---|
[3631] | 221 | `(macrolet ((lc (x) `(term-coeff (car ,x))))
|
---|
[2742] | 222 | (do ((p ,p)
|
---|
| 223 | (q ,q)
|
---|
| 224 | r)
|
---|
| 225 | ((or (endp p) (endp q))
|
---|
| 226 | ;; NOTE: R contains the result in reverse order. Can it
|
---|
| 227 | ;; be more efficient to produce the terms in correct order?
|
---|
[2774] | 228 | (unless (endp q)
|
---|
[2776] | 229 | ;; Upon subtraction, we must change the sign of
|
---|
| 230 | ;; all coefficients in q
|
---|
[2774] | 231 | ,@(when uminus-fn
|
---|
[2775] | 232 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
---|
[2774] | 233 | (setf r (nreconc r q)))
|
---|
[2742] | 234 | r)
|
---|
| 235 | (multiple-value-bind
|
---|
| 236 | (greater-p equal-p)
|
---|
[3632] | 237 | (funcall ,order-fn (car p) (car q))
|
---|
[2742] | 238 | (cond
|
---|
| 239 | (greater-p
|
---|
| 240 | (rotatef (cdr p) r p)
|
---|
| 241 | )
|
---|
| 242 | (equal-p
|
---|
[2766] | 243 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
---|
[2742] | 244 | (cond
|
---|
[3640] | 245 | ((universal-zerop s)
|
---|
[2742] | 246 | (setf p (cdr p))
|
---|
| 247 | )
|
---|
| 248 | (t
|
---|
| 249 | (setf (lc p) s)
|
---|
| 250 | (rotatef (cdr p) r p))))
|
---|
| 251 | (setf q (cdr q))
|
---|
| 252 | )
|
---|
| 253 | (t
|
---|
[2743] | 254 | ;;Negate the term of Q if UMINUS provided, signallig
|
---|
| 255 | ;;that we are doing subtraction
|
---|
[2908] | 256 | ,(when uminus-fn
|
---|
| 257 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
---|
[2743] | 258 | (rotatef (cdr q) r q)))))))
|
---|
[2585] | 259 |
|
---|
[2655] | 260 |
|
---|
[3647] | 261 | (defgeneric add-to (self other)
|
---|
| 262 | (:documentation "Add OTHER to SELF.")
|
---|
| 263 | (:method ((self number) (other number))
|
---|
[3819] | 264 | (+ self other))
|
---|
| 265 | (:method ((self poly) (other number))
|
---|
| 266 | (add-to self (make-poly-constant :dimension (poly-dimension self)
|
---|
[3820] | 267 | :coeff other))))
|
---|
[3819] | 268 |
|
---|
[3647] | 269 |
|
---|
| 270 | (defgeneric subtract-from (self other)
|
---|
[3648] | 271 | (:documentation "Subtract OTHER from SELF.")
|
---|
| 272 | (:method ((self number) (other number))
|
---|
| 273 | (- self other)))
|
---|
[3647] | 274 |
|
---|
[3750] | 275 | (defmacro def-add/subtract-method (add/subtract-method-name
|
---|
[3749] | 276 | uminus-method-name
|
---|
| 277 | &optional
|
---|
| 278 | (doc-string nil doc-string-supplied-p))
|
---|
[3647] | 279 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
[2749] | 280 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
---|
[2615] | 281 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
[2769] | 282 | ;; Ensure orders are compatible
|
---|
[3015] | 283 | (change-term-order other self)
|
---|
[2772] | 284 | (setf (poly-termlist self) (fast-add/subtract
|
---|
| 285 | (poly-termlist self) (poly-termlist other)
|
---|
| 286 | (poly-term-order self)
|
---|
| 287 | #',add/subtract-method-name
|
---|
| 288 | ,(when uminus-method-name `(function ,uminus-method-name))))
|
---|
[3748] | 289 | self))
|
---|
[2487] | 290 |
|
---|
[3787] | 291 | (eval-when (:load-toplevel :execute)
|
---|
[2777] | 292 |
|
---|
[3639] | 293 | (def-add/subtract-method add-to nil
|
---|
[2777] | 294 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
[2610] | 295 | This operation destructively modifies both polynomials.
|
---|
| 296 | The result is stored in SELF. This implementation does
|
---|
[2752] | 297 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
[2609] | 298 |
|
---|
[3639] | 299 | (def-add/subtract-method subtract-from unary-minus
|
---|
[2753] | 300 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
[2610] | 301 | This operation destructively modifies both polynomials.
|
---|
| 302 | The result is stored in SELF. This implementation does
|
---|
[2752] | 303 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
[2916] | 304 | )
|
---|
[2777] | 305 |
|
---|
[2691] | 306 | (defmethod unary-minus ((self poly))
|
---|
[2694] | 307 | "Destructively modifies the coefficients of the polynomial SELF,
|
---|
| 308 | by changing their sign."
|
---|
[2692] | 309 | (mapc #'unary-minus (poly-termlist self))
|
---|
[2683] | 310 | self)
|
---|
[52] | 311 |
|
---|
[2795] | 312 | (defun add-termlists (p q order-fn)
|
---|
[2794] | 313 | "Destructively adds two termlists P and Q ordered according to ORDER-FN."
|
---|
[3639] | 314 | (fast-add/subtract p q order-fn #'add-to nil))
|
---|
[2794] | 315 |
|
---|
[2800] | 316 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
---|
[2927] | 317 | &optional (reverse-arg-order-P nil))
|
---|
[2799] | 318 | "Multiplies term TERM by a list of term, TERMLIST.
|
---|
[2792] | 319 | Takes into accound divisors of zero in the ring, by
|
---|
[2927] | 320 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
---|
[2928] | 321 | is T, change the order of arguments; this may be important
|
---|
[2927] | 322 | if we extend the package to non-commutative rings."
|
---|
[2800] | 323 | `(mapcan #'(lambda (other-term)
|
---|
[3633] | 324 | (let ((prod (multiply
|
---|
[2923] | 325 | ,@(cond
|
---|
[2930] | 326 | (reverse-arg-order-p
|
---|
[2925] | 327 | `(other-term ,term))
|
---|
| 328 | (t
|
---|
| 329 | `(,term other-term))))))
|
---|
[2800] | 330 | (cond
|
---|
[3633] | 331 | ((universal-zerop prod) nil)
|
---|
[2800] | 332 | (t (list prod)))))
|
---|
| 333 | ,termlist))
|
---|
[2790] | 334 |
|
---|
[2796] | 335 | (defun multiply-termlists (p q order-fn)
|
---|
[3127] | 336 | "A version of polynomial multiplication, operating
|
---|
| 337 | directly on termlists."
|
---|
[2787] | 338 | (cond
|
---|
[2917] | 339 | ((or (endp p) (endp q))
|
---|
| 340 | ;;p or q is 0 (represented by NIL)
|
---|
| 341 | nil)
|
---|
[2789] | 342 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
---|
[2787] | 343 | ((endp (cdr p))
|
---|
[2918] | 344 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
---|
| 345 | ((endp (cdr q))
|
---|
[2919] | 346 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
---|
| 347 | (t
|
---|
[3633] | 348 | (cons (multiply (car p) (car q))
|
---|
[2949] | 349 | (add-termlists
|
---|
| 350 | (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
|
---|
| 351 | (multiply-termlists (cdr p) q order-fn)
|
---|
| 352 | order-fn)))))
|
---|
[2793] | 353 |
|
---|
[2803] | 354 | (defmethod multiply-by ((self poly) (other poly))
|
---|
[3014] | 355 | (change-term-order other self)
|
---|
[2803] | 356 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
---|
| 357 | (poly-termlist other)
|
---|
| 358 | (poly-term-order self)))
|
---|
| 359 | self)
|
---|
| 360 |
|
---|
[3804] | 361 | (defgeneric add-2 (object1 object2)
|
---|
| 362 | (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
|
---|
[3813] | 363 | (:method ((object1 t) (object2 t))
|
---|
[3804] | 364 | (add-to (copy-instance object1) (copy-instance object2))))
|
---|
[3374] | 365 |
|
---|
[3803] | 366 | (defun add (&rest summands)
|
---|
| 367 | "Non-destructively adds list SUMMANDS."
|
---|
| 368 | (cond ((endp summands) 0)
|
---|
[3818] | 369 | (t (reduce #'add-2 summands))))
|
---|
[3803] | 370 |
|
---|
[3634] | 371 | (defun subtract (minuend &rest subtrahends)
|
---|
[3427] | 372 | "Non-destructively subtract MINUEND and SUBTRAHENDS."
|
---|
[3634] | 373 | (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))
|
---|
[3374] | 374 |
|
---|
[3062] | 375 | (defmethod left-tensor-product-by ((self poly) (other monom))
|
---|
| 376 | (setf (poly-termlist self)
|
---|
| 377 | (mapcan #'(lambda (term)
|
---|
| 378 | (let ((prod (left-tensor-product-by term other)))
|
---|
| 379 | (cond
|
---|
[3640] | 380 | ((universal-zerop prod) nil)
|
---|
[3062] | 381 | (t (list prod)))))
|
---|
| 382 | (poly-termlist self)))
|
---|
[3249] | 383 | (incf (poly-dimension self) (monom-dimension other))
|
---|
[3062] | 384 | self)
|
---|
[3044] | 385 |
|
---|
[3062] | 386 | (defmethod right-tensor-product-by ((self poly) (other monom))
|
---|
| 387 | (setf (poly-termlist self)
|
---|
| 388 | (mapcan #'(lambda (term)
|
---|
| 389 | (let ((prod (right-tensor-product-by term other)))
|
---|
| 390 | (cond
|
---|
[3640] | 391 | ((universal-zerop prod) nil)
|
---|
[3062] | 392 | (t (list prod)))))
|
---|
| 393 | (poly-termlist self)))
|
---|
[3249] | 394 | (incf (poly-dimension self) (monom-dimension other))
|
---|
[3062] | 395 | self)
|
---|
| 396 |
|
---|
| 397 |
|
---|
[3084] | 398 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
|
---|
[2716] | 399 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
---|
[3060] | 400 | is a list of polynomials. Destructively modifies PLIST elements."
|
---|
[3061] | 401 | (mapc #'(lambda (poly)
|
---|
[3085] | 402 | (left-tensor-product-by
|
---|
| 403 | poly
|
---|
| 404 | (prog1
|
---|
| 405 | (make-monom-variable k i)
|
---|
| 406 | (incf i))))
|
---|
[3061] | 407 | plist))
|
---|
[52] | 408 |
|
---|
[3087] | 409 | (defun standard-extension-1 (plist
|
---|
| 410 | &aux
|
---|
[3096] | 411 | (plist (standard-extension plist))
|
---|
[3087] | 412 | (nvars (poly-dimension (car plist))))
|
---|
[3081] | 413 | "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
|
---|
[3087] | 414 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
|
---|
| 415 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
[3105] | 416 | tantamount to replacing PI with UI*PI-1. It assumes that all
|
---|
[3106] | 417 | polynomials have the same dimension, and only the first polynomial
|
---|
| 418 | is examined to determine this dimension."
|
---|
[3089] | 419 | ;; Implementation note: we use STANDARD-EXTENSION and then subtract
|
---|
| 420 | ;; 1 from each polynomial; since UI*PI has no constant term,
|
---|
| 421 | ;; we just need to append the constant term at the end
|
---|
| 422 | ;; of each termlist.
|
---|
[3064] | 423 | (flet ((subtract-1 (p)
|
---|
[3641] | 424 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
[3083] | 425 | (setf plist (mapc #'subtract-1 plist)))
|
---|
[3077] | 426 | plist)
|
---|
[52] | 427 |
|
---|
| 428 |
|
---|
[3107] | 429 | (defun standard-sum (plist
|
---|
| 430 | &aux
|
---|
| 431 | (plist (standard-extension plist))
|
---|
| 432 | (nvars (poly-dimension (car plist))))
|
---|
[3087] | 433 | "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
|
---|
| 434 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
|
---|
| 435 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
| 436 | tantamount to replacing PI with UI*PI, and the resulting polynomials
|
---|
[3117] | 437 | are added. Finally, 1 is subtracted. It should be noted that the term
|
---|
| 438 | order is not modified, which is equivalent to using a lexicographic
|
---|
| 439 | order on the first K variables."
|
---|
[3107] | 440 | (flet ((subtract-1 (p)
|
---|
[3641] | 441 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
[3108] | 442 | (subtract-1
|
---|
| 443 | (make-instance
|
---|
| 444 | 'poly
|
---|
[3115] | 445 | :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
|
---|
[52] | 446 |
|
---|
[3653] | 447 | (defgeneric universal-ezgcd (x y)
|
---|
| 448 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
| 449 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
---|
| 450 | the Euclidean algorithm.")
|
---|
| 451 | (:method ((x integer) (y integer)
|
---|
| 452 | &aux (c (gcd x y)))
|
---|
| 453 | (values c (/ x c) (/ y c)))
|
---|
| 454 | )
|
---|
| 455 |
|
---|
[3655] | 456 | (defgeneric s-polynomial (object1 object2)
|
---|
[3651] | 457 | (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
|
---|
| 458 | (:method ((f poly) (g poly))
|
---|
| 459 | (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
|
---|
| 460 | (mf (divide lcm (leading-monomial f)))
|
---|
| 461 | (mg (divide lcm (leading-monomial g))))
|
---|
| 462 | (multiple-value-bind (c cf cg)
|
---|
[3652] | 463 | (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
|
---|
[3651] | 464 | (declare (ignore c))
|
---|
| 465 | (subtract
|
---|
[3673] | 466 | (multiply f (change-class mf 'term :coeff cg))
|
---|
| 467 | (multiply g (change-class mg 'term :coeff cf)))))))
|
---|
[3651] | 468 |
|
---|
[3676] | 469 | (defgeneric poly-content (object)
|
---|
| 470 | (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
|
---|
[3677] | 471 | (:method ((self poly))
|
---|
| 472 | (reduce #'universal-gcd
|
---|
[3679] | 473 | (mapcar #'term-coeff (rest (poly-termlist self)))
|
---|
| 474 | :initial-value (leading-coefficient self))))
|
---|
[3676] | 475 |
|
---|
[3684] | 476 | (defun poly-primitive-part (object)
|
---|
[3685] | 477 | "Divide polynomial OBJECT by gcd of its
|
---|
[3684] | 478 | coefficients. Return the resulting polynomial."
|
---|
[3688] | 479 | (scalar-divide-by object (poly-content object)))
|
---|
[3682] | 480 |
|
---|
[3700] | 481 | (defun poly-insert-variables (self k)
|
---|
[3697] | 482 | (left-tensor-product-by self (make-instance 'monom :dimension k)))
|
---|
| 483 |
|
---|
[3698] | 484 | (defun saturation-extension (f plist &aux (k (length plist)))
|
---|
[3708] | 485 | "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
|
---|
| 486 | PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
|
---|
[3711] | 487 | as first K variables. It destructively modifies F and PLIST."
|
---|
[3700] | 488 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
[3699] | 489 | (standard-extension-1 plist)))
|
---|
[3694] | 490 |
|
---|
[3699] | 491 | (defun polysaturation-extension (f plist &aux (k (length plist)))
|
---|
[3708] | 492 | "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
|
---|
| 493 | and F' is F with variables U1,U2,...,UK inserted as first K
|
---|
[3711] | 494 | variables. It destructively modifies F and PLIST."
|
---|
[3700] | 495 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
[3703] | 496 | (list (standard-sum plist))))
|
---|
[3694] | 497 |
|
---|
[3691] | 498 | (defun saturation-extension-1 (f p)
|
---|
[3712] | 499 | "Given family of polynomials F and a polynomial P, calculate [F',
|
---|
| 500 | U*P-1], where F' is F with variable inserted as the first variable. It
|
---|
| 501 | destructively modifies F and P."
|
---|
[3693] | 502 | (polysaturation-extension f (list p)))
|
---|
[3713] | 503 |
|
---|
[3717] | 504 | (defmethod multiply-by ((object1 number) (object2 poly))
|
---|
[3720] | 505 | (scalar-multiply-by (copy-instance object2) object1))
|
---|
[3716] | 506 |
|
---|
[3781] | 507 | (defun make-poly-variable (nvars pos &optional (power 1))
|
---|
| 508 | (change-class (make-monom-variable nvars pos power) 'poly))
|
---|
[3736] | 509 |
|
---|
[3821] | 510 | (defun make-poly-constant (nvars coeff)
|
---|
| 511 | (change-class (make-term-constant nvars coeff) 'poly))
|
---|
| 512 |
|
---|
[3713] | 513 | (defgeneric universal-expt (x y)
|
---|
[3721] | 514 | (:documentation "Raises X to power Y.")
|
---|
[3713] | 515 | (:method ((x number) (y integer)) (expt x y))
|
---|
| 516 | (:method ((x t) (y integer))
|
---|
| 517 | (declare (type fixnum y))
|
---|
| 518 | (cond
|
---|
| 519 | ((minusp y) (error "universal-expt: Negative exponent."))
|
---|
| 520 | ((universal-zerop x) (if (zerop y) 1))
|
---|
| 521 | (t
|
---|
| 522 | (do ((k 1 (ash k 1))
|
---|
| 523 | (q x (multiply q q)) ;keep squaring
|
---|
| 524 | (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
|
---|
| 525 | ((> k y) p)
|
---|
[3778] | 526 | (declare (fixnum k)))))))
|
---|
| 527 |
|
---|
| 528 | (defgeneric poly-p (object)
|
---|
| 529 | (:documentation "Checks if an object is a polynomial.")
|
---|
[3779] | 530 | (:method ((self poly)) t)
|
---|
[3778] | 531 | (:method ((self t)) nil))
|
---|