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