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