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