[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"
|
---|
[4053] | 56 | "MAKE-ZERO-FOR"
|
---|
| 57 | "MAKE-UNIT-FOR"
|
---|
[3778] | 58 | "UNIVERSAL-EXPT"
|
---|
[3969] | 59 | "UNIVERSAL-EQUALP"
|
---|
| 60 | "POLY-LENGTH"
|
---|
[4062] | 61 | "POLY-REVERSE"
|
---|
[3900] | 62 | "POLY-P"
|
---|
[3901] | 63 | "+LIST-MARKER+"
|
---|
[3900] | 64 | "POLY-EVAL")
|
---|
[3489] | 65 | (:documentation "Implements polynomials. A polynomial is essentially
|
---|
| 66 | a mapping of monomials of the same degree to coefficients. The
|
---|
| 67 | momomials are ordered according to a monomial order."))
|
---|
[143] | 68 |
|
---|
[431] | 69 | (in-package :polynomial)
|
---|
| 70 |
|
---|
[1927] | 71 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
---|
[52] | 72 |
|
---|
[2442] | 73 | (defclass poly ()
|
---|
[3253] | 74 | ((dimension :initform nil
|
---|
[3250] | 75 | :initarg :dimension
|
---|
| 76 | :accessor poly-dimension
|
---|
[3242] | 77 | :documentation "Shared dimension of all terms, the number of variables")
|
---|
[3250] | 78 | (termlist :initform nil :initarg :termlist :accessor poly-termlist
|
---|
[3619] | 79 | :documentation "List of terms.")
|
---|
[3250] | 80 | (order :initform #'lex> :initarg :order :accessor poly-term-order
|
---|
[2697] | 81 | :documentation "Monomial/term order."))
|
---|
[3262] | 82 | (:default-initargs :dimension nil :termlist nil :order #'lex>)
|
---|
[2695] | 83 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
---|
[2696] | 84 | according to term order ORDER, which defaults to LEX>."))
|
---|
[2442] | 85 |
|
---|
[2471] | 86 | (defmethod print-object ((self poly) stream)
|
---|
[3241] | 87 | (print-unreadable-object (self stream :type t :identity t)
|
---|
[3243] | 88 | (with-accessors ((dimension poly-dimension)
|
---|
| 89 | (termlist poly-termlist)
|
---|
| 90 | (order poly-term-order))
|
---|
[3237] | 91 | self
|
---|
[3244] | 92 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
|
---|
| 93 | dimension termlist order))))
|
---|
[2469] | 94 |
|
---|
[3015] | 95 | (defgeneric change-term-order (self other)
|
---|
[3012] | 96 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
---|
[3010] | 97 | (:method ((self poly) (other poly))
|
---|
| 98 | (unless (eq (poly-term-order self) (poly-term-order other))
|
---|
[3620] | 99 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
---|
[3010] | 100 | (poly-term-order self) (poly-term-order other)))
|
---|
[3012] | 101 | self))
|
---|
[3010] | 102 |
|
---|
[3621] | 103 | (defgeneric poly-insert-term (self term)
|
---|
[3622] | 104 | (:documentation "Insert a term TERM into SELF before all other
|
---|
[3621] | 105 | terms. Order is not enforced.")
|
---|
| 106 | (:method ((self poly) (term term))
|
---|
[3510] | 107 | (cond ((null (poly-dimension self))
|
---|
[3621] | 108 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
| 109 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
| 110 | (push term (poly-termlist self))
|
---|
[3510] | 111 | self))
|
---|
| 112 |
|
---|
[3622] | 113 | (defgeneric poly-append-term (self term)
|
---|
| 114 | (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
|
---|
| 115 | (:method ((self poly) (term term))
|
---|
[3510] | 116 | (cond ((null (poly-dimension self))
|
---|
[3622] | 117 | (setf (poly-dimension self) (monom-dimension term)))
|
---|
| 118 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
---|
| 119 | (setf (cdr (last (poly-termlist self))) (list term))
|
---|
[3510] | 120 | self))
|
---|
| 121 |
|
---|
[3095] | 122 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
|
---|
[3126] | 123 | "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
|
---|
| 124 | It can be used to enter simple polynomials by hand, e.g the polynomial
|
---|
| 125 | in two variables, X and Y, given in standard notation as:
|
---|
| 126 |
|
---|
| 127 | 3*X^2*Y^3+2*Y+7
|
---|
| 128 |
|
---|
| 129 | can be entered as
|
---|
| 130 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
|
---|
| 131 |
|
---|
| 132 | NOTE: The primary use is for low-level debugging of the package."
|
---|
[3099] | 133 | (dolist (x alist poly)
|
---|
[3705] | 134 | (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
|
---|
[3092] | 135 |
|
---|
[3877] | 136 | (defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
|
---|
[3786] | 137 | "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
[3401] | 138 | (reinitialize-instance new
|
---|
| 139 | :dimension (monom-dimension old)
|
---|
[3786] | 140 | :termlist (list old)))
|
---|
[3796] | 141 |
|
---|
[3877] | 142 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
|
---|
[3796] | 143 | "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
---|
| 144 | (reinitialize-instance new
|
---|
| 145 | :dimension (monom-dimension old)
|
---|
[3797] | 146 | :termlist (list (change-class old 'term))))
|
---|
[3403] | 147 |
|
---|
[3624] | 148 | (defmethod universal-equalp ((self poly) (other poly))
|
---|
| 149 | "Implements equality of polynomials."
|
---|
| 150 | (and (eql (poly-dimension self) (poly-dimension other))
|
---|
| 151 | (every #'universal-equalp (poly-termlist self) (poly-termlist other))
|
---|
| 152 | (eq (poly-term-order self) (poly-term-order other))))
|
---|
[2650] | 153 |
|
---|
[3624] | 154 | (defgeneric leading-term (object)
|
---|
[2442] | 155 | (:method ((self poly))
|
---|
[2525] | 156 | (car (poly-termlist self)))
|
---|
| 157 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
---|
[52] | 158 |
|
---|
[3625] | 159 | (defgeneric second-leading-term (object)
|
---|
[2442] | 160 | (:method ((self poly))
|
---|
[2525] | 161 | (cadar (poly-termlist self)))
|
---|
| 162 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
---|
[52] | 163 |
|
---|
[3656] | 164 | (defgeneric leading-monomial (object)
|
---|
| 165 | (:method ((self poly))
|
---|
| 166 | (change-class (copy-instance (leading-term self)) 'monom))
|
---|
| 167 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
| 168 |
|
---|
| 169 | (defgeneric second-leading-monomial (object)
|
---|
| 170 | (:method ((self poly))
|
---|
| 171 | (change-class (copy-instance (second-leading-term self)) 'monom))
|
---|
| 172 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
---|
| 173 |
|
---|
[3625] | 174 | (defgeneric leading-coefficient (object)
|
---|
[2442] | 175 | (:method ((self poly))
|
---|
[3642] | 176 | (term-coeff (leading-term self)))
|
---|
[2545] | 177 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
---|
[52] | 178 |
|
---|
[2442] | 179 | (defgeneric second-leading-coefficient (object)
|
---|
| 180 | (:method ((self poly))
|
---|
[3645] | 181 | (term-coeff (second-leading-term self)))
|
---|
[2906] | 182 | (:documentation "The second leading coefficient of a polynomial. It
|
---|
| 183 | signals error for a polynomial with at most one term."))
|
---|
[52] | 184 |
|
---|
[3629] | 185 | (defmethod universal-zerop ((self poly))
|
---|
| 186 | "Return T iff SELF is a zero polynomial."
|
---|
[3639] | 187 | (null (poly-termlist self)))
|
---|
[52] | 188 |
|
---|
[3518] | 189 | (defgeneric poly-length (self)
|
---|
[3630] | 190 | (:documentation "Return the number of terms.")
|
---|
[3518] | 191 | (:method ((self poly))
|
---|
| 192 | (length (poly-termlist self))))
|
---|
[52] | 193 |
|
---|
[3689] | 194 | (defgeneric scalar-multiply-by (self other)
|
---|
| 195 | (:documentation "Multiply vector SELF by a scalar OTHER.")
|
---|
| 196 | (:method ((self poly) other)
|
---|
| 197 | (mapc #'(lambda (term) (setf (term-coeff term) (multiply (term-coeff term) other)))
|
---|
| 198 | (poly-termlist self))
|
---|
| 199 | self))
|
---|
| 200 |
|
---|
| 201 | (defgeneric scalar-divide-by (self other)
|
---|
| 202 | (:documentation "Divide vector SELF by a scalar OTHER.")
|
---|
| 203 | (:method ((self poly) other)
|
---|
| 204 | (mapc #'(lambda (term) (setf (term-coeff term) (divide (term-coeff term) other)))
|
---|
| 205 | (poly-termlist self))
|
---|
| 206 | self))
|
---|
| 207 |
|
---|
[4034] | 208 | (defmethod unary-inverse :before ((self poly))
|
---|
[4035] | 209 | "Checks invertibility of a polynomial SELF. To be invertable, the
|
---|
| 210 | polynomial must be an invertible, constant polynomial."
|
---|
[4034] | 211 | (with-slots (termlist)
|
---|
[4035] | 212 | self
|
---|
| 213 | (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
|
---|
| 214 | nil
|
---|
| 215 | "To be invertible, the polynomial must have 1 term of total degree 0.")))
|
---|
[4034] | 216 |
|
---|
| 217 | (defmethod unary-inverse ((self poly))
|
---|
[4035] | 218 | "Returns the unary inverse of a polynomial SELF."
|
---|
[4034] | 219 | (with-slots (termlist)
|
---|
| 220 | self
|
---|
[4035] | 221 | (setf (car termlist) (unary-inverse (car termlist)))
|
---|
| 222 | self))
|
---|
[4034] | 223 |
|
---|
[3663] | 224 | (defmethod multiply-by ((self poly) (other monom))
|
---|
[3630] | 225 | "Multiply a polynomial SELF by OTHER."
|
---|
| 226 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
| 227 | (poly-termlist self))
|
---|
| 228 | self)
|
---|
[2469] | 229 |
|
---|
[3672] | 230 | (defmethod multiply-by ((self poly) (other term))
|
---|
| 231 | "Multiply a polynomial SELF by OTHER."
|
---|
| 232 | (mapc #'(lambda (term) (multiply-by term other))
|
---|
| 233 | (poly-termlist self))
|
---|
| 234 | self)
|
---|
| 235 |
|
---|
[4071] | 236 | (defmethod multiply-by ((self monom) (other poly))
|
---|
| 237 | "Multiply a monomial SELF by polynomial OTHER."
|
---|
| 238 | (multiply-by other self))
|
---|
| 239 |
|
---|
| 240 | (defmethod multiply-by ((self term) (other poly))
|
---|
| 241 | "Multiply a term SELF by polynomial OTHER."
|
---|
| 242 | (multiply-by other self))
|
---|
| 243 |
|
---|
[2761] | 244 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
---|
[2755] | 245 | "Return an expression which will efficiently adds/subtracts two
|
---|
| 246 | polynomials, P and Q. The addition/subtraction of coefficients is
|
---|
[3878] | 247 | performed by calling ADD/SUBTRACT-FN. If UMINUS-FN is supplied, it is
|
---|
| 248 | used to negate the coefficients of Q which do not have a corresponding
|
---|
| 249 | coefficient in P. The code implements an efficient algorithm to add
|
---|
| 250 | two polynomials represented as sorted lists of terms. The code
|
---|
| 251 | destroys both arguments, reusing the terms to build the result."
|
---|
[3631] | 252 | `(macrolet ((lc (x) `(term-coeff (car ,x))))
|
---|
[2742] | 253 | (do ((p ,p)
|
---|
| 254 | (q ,q)
|
---|
| 255 | r)
|
---|
| 256 | ((or (endp p) (endp q))
|
---|
| 257 | ;; NOTE: R contains the result in reverse order. Can it
|
---|
| 258 | ;; be more efficient to produce the terms in correct order?
|
---|
[2774] | 259 | (unless (endp q)
|
---|
[2776] | 260 | ;; Upon subtraction, we must change the sign of
|
---|
| 261 | ;; all coefficients in q
|
---|
[2774] | 262 | ,@(when uminus-fn
|
---|
[2775] | 263 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
---|
[2774] | 264 | (setf r (nreconc r q)))
|
---|
[3887] | 265 | (unless (endp p)
|
---|
| 266 | (setf r (nreconc r p)))
|
---|
| 267 | r)
|
---|
[2742] | 268 | (multiple-value-bind
|
---|
| 269 | (greater-p equal-p)
|
---|
[3632] | 270 | (funcall ,order-fn (car p) (car q))
|
---|
[2742] | 271 | (cond
|
---|
| 272 | (greater-p
|
---|
| 273 | (rotatef (cdr p) r p)
|
---|
| 274 | )
|
---|
| 275 | (equal-p
|
---|
[2766] | 276 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
---|
[2742] | 277 | (cond
|
---|
[3640] | 278 | ((universal-zerop s)
|
---|
[2742] | 279 | (setf p (cdr p))
|
---|
| 280 | )
|
---|
| 281 | (t
|
---|
| 282 | (setf (lc p) s)
|
---|
| 283 | (rotatef (cdr p) r p))))
|
---|
| 284 | (setf q (cdr q))
|
---|
| 285 | )
|
---|
| 286 | (t
|
---|
[2743] | 287 | ;;Negate the term of Q if UMINUS provided, signallig
|
---|
| 288 | ;;that we are doing subtraction
|
---|
[2908] | 289 | ,(when uminus-fn
|
---|
| 290 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
---|
[3887] | 291 | (rotatef (cdr q) r q))))
|
---|
| 292 | ;;(format t "P:~A~%" p)
|
---|
| 293 | ;;(format t "Q:~A~%" q)
|
---|
| 294 | ;;(format t "R:~A~%" r)
|
---|
| 295 | )))
|
---|
[2585] | 296 |
|
---|
[2655] | 297 |
|
---|
[3887] | 298 |
|
---|
[3647] | 299 | (defgeneric add-to (self other)
|
---|
| 300 | (:documentation "Add OTHER to SELF.")
|
---|
| 301 | (:method ((self number) (other number))
|
---|
[3819] | 302 | (+ self other))
|
---|
| 303 | (:method ((self poly) (other number))
|
---|
[3865] | 304 | (add-to self (make-poly-constant (poly-dimension self) other)))
|
---|
| 305 | (:method ((self number) (other poly))
|
---|
| 306 | (add-to (make-poly-constant (poly-dimension other) self) other)))
|
---|
[3819] | 307 |
|
---|
[3647] | 308 |
|
---|
| 309 | (defgeneric subtract-from (self other)
|
---|
[3648] | 310 | (:documentation "Subtract OTHER from SELF.")
|
---|
| 311 | (:method ((self number) (other number))
|
---|
[3830] | 312 | (- self other))
|
---|
| 313 | (:method ((self poly) (other number))
|
---|
| 314 | (subtract-from self (make-poly-constant (poly-dimension self) other))))
|
---|
[3647] | 315 |
|
---|
[3969] | 316 |
|
---|
[3884] | 317 | #|
|
---|
[3750] | 318 | (defmacro def-add/subtract-method (add/subtract-method-name
|
---|
[3749] | 319 | uminus-method-name
|
---|
| 320 | &optional
|
---|
| 321 | (doc-string nil doc-string-supplied-p))
|
---|
[3647] | 322 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
---|
[2749] | 323 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
---|
[2615] | 324 | ,@(when doc-string-supplied-p `(,doc-string))
|
---|
[2769] | 325 | ;; Ensure orders are compatible
|
---|
[3015] | 326 | (change-term-order other self)
|
---|
[2772] | 327 | (setf (poly-termlist self) (fast-add/subtract
|
---|
| 328 | (poly-termlist self) (poly-termlist other)
|
---|
| 329 | (poly-term-order self)
|
---|
| 330 | #',add/subtract-method-name
|
---|
| 331 | ,(when uminus-method-name `(function ,uminus-method-name))))
|
---|
[3748] | 332 | self))
|
---|
[3908] | 333 |
|
---|
| 334 | (eval-when (:load-toplevel :execute)
|
---|
| 335 |
|
---|
| 336 | (def-add/subtract-method add-to nil
|
---|
| 337 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
| 338 | This operation destructively modifies both polynomials.
|
---|
| 339 | The result is stored in SELF. This implementation does
|
---|
| 340 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
| 341 |
|
---|
| 342 | (def-add/subtract-method subtract-from unary-minus
|
---|
| 343 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
| 344 | This operation destructively modifies both polynomials.
|
---|
| 345 | The result is stored in SELF. This implementation does
|
---|
| 346 | no consing, entirely reusing the sells of SELF and OTHER.")
|
---|
| 347 | )
|
---|
| 348 |
|
---|
[3884] | 349 | |#
|
---|
[2487] | 350 |
|
---|
[3880] | 351 | (defmethod unary-minus ((self poly))
|
---|
| 352 | "Destructively modifies the coefficients of the polynomial SELF,
|
---|
| 353 | by changing their sign."
|
---|
| 354 | (mapc #'unary-minus (poly-termlist self))
|
---|
| 355 | self)
|
---|
| 356 |
|
---|
| 357 | (defun add-termlists (p q order-fn)
|
---|
| 358 | "Destructively adds two termlists P and Q ordered according to ORDER-FN."
|
---|
| 359 | (fast-add/subtract p q order-fn #'add-to nil))
|
---|
| 360 |
|
---|
[3881] | 361 | (defun subtract-termlists (p q order-fn)
|
---|
[3885] | 362 | "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
|
---|
[3882] | 363 | (fast-add/subtract p q order-fn #'subtract-from #'unary-minus))
|
---|
[3881] | 364 |
|
---|
[3879] | 365 | (defmethod add-to ((self poly) (other poly))
|
---|
| 366 | "Adds to polynomial SELF another polynomial OTHER.
|
---|
[2610] | 367 | This operation destructively modifies both polynomials.
|
---|
| 368 | The result is stored in SELF. This implementation does
|
---|
[3879] | 369 | no consing, entirely reusing the sells of SELF and OTHER."
|
---|
| 370 | (change-term-order other self)
|
---|
| 371 | (setf (poly-termlist self) (add-termlists
|
---|
| 372 | (poly-termlist self) (poly-termlist other)
|
---|
[3883] | 373 | (poly-term-order self)))
|
---|
| 374 | self)
|
---|
[3879] | 375 |
|
---|
[4092] | 376 | (defmethod add-to ((self poly) (other term))
|
---|
| 377 | "Adds to polynomial SELF a term OTHER. This operation
|
---|
| 378 | destructively modifies OTHER."
|
---|
| 379 | (with-slots (termlist order)
|
---|
| 380 | self
|
---|
| 381 | (setf termlist (merge 'list termlist (list other) order))))
|
---|
[2609] | 382 |
|
---|
[3879] | 383 | (defmethod subtract-from ((self poly) (other poly))
|
---|
[2753] | 384 | "Subtracts from polynomial SELF another polynomial OTHER.
|
---|
[2610] | 385 | This operation destructively modifies both polynomials.
|
---|
| 386 | The result is stored in SELF. This implementation does
|
---|
[3879] | 387 | no consing, entirely reusing the sells of SELF and OTHER."
|
---|
| 388 | (change-term-order other self)
|
---|
| 389 | (setf (poly-termlist self) (subtract-termlists
|
---|
| 390 | (poly-termlist self) (poly-termlist other)
|
---|
[3883] | 391 | (poly-term-order self)))
|
---|
| 392 | self)
|
---|
[2777] | 393 |
|
---|
[2800] | 394 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
---|
[2927] | 395 | &optional (reverse-arg-order-P nil))
|
---|
[2799] | 396 | "Multiplies term TERM by a list of term, TERMLIST.
|
---|
[2792] | 397 | Takes into accound divisors of zero in the ring, by
|
---|
[2927] | 398 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
---|
[2928] | 399 | is T, change the order of arguments; this may be important
|
---|
[2927] | 400 | if we extend the package to non-commutative rings."
|
---|
[2800] | 401 | `(mapcan #'(lambda (other-term)
|
---|
[3633] | 402 | (let ((prod (multiply
|
---|
[2923] | 403 | ,@(cond
|
---|
[2930] | 404 | (reverse-arg-order-p
|
---|
[2925] | 405 | `(other-term ,term))
|
---|
| 406 | (t
|
---|
| 407 | `(,term other-term))))))
|
---|
[2800] | 408 | (cond
|
---|
[3633] | 409 | ((universal-zerop prod) nil)
|
---|
[2800] | 410 | (t (list prod)))))
|
---|
| 411 | ,termlist))
|
---|
[2790] | 412 |
|
---|
[2796] | 413 | (defun multiply-termlists (p q order-fn)
|
---|
[3127] | 414 | "A version of polynomial multiplication, operating
|
---|
| 415 | directly on termlists."
|
---|
[2787] | 416 | (cond
|
---|
[2917] | 417 | ((or (endp p) (endp q))
|
---|
| 418 | ;;p or q is 0 (represented by NIL)
|
---|
| 419 | nil)
|
---|
[2789] | 420 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
---|
[2787] | 421 | ((endp (cdr p))
|
---|
[2918] | 422 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
---|
| 423 | ((endp (cdr q))
|
---|
[2919] | 424 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
---|
| 425 | (t
|
---|
[4092] | 426 | (cons (multiply-by (car p) (car q))
|
---|
[2949] | 427 | (add-termlists
|
---|
| 428 | (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
|
---|
| 429 | (multiply-termlists (cdr p) q order-fn)
|
---|
| 430 | order-fn)))))
|
---|
[2793] | 431 |
|
---|
[2803] | 432 | (defmethod multiply-by ((self poly) (other poly))
|
---|
[3014] | 433 | (change-term-order other self)
|
---|
[2803] | 434 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
---|
| 435 | (poly-termlist other)
|
---|
| 436 | (poly-term-order self)))
|
---|
| 437 | self)
|
---|
| 438 |
|
---|
[3804] | 439 | (defgeneric add-2 (object1 object2)
|
---|
| 440 | (:documentation "Non-destructively add OBJECT1 to OBJECT2.")
|
---|
[3813] | 441 | (:method ((object1 t) (object2 t))
|
---|
[3804] | 442 | (add-to (copy-instance object1) (copy-instance object2))))
|
---|
[3374] | 443 |
|
---|
[3803] | 444 | (defun add (&rest summands)
|
---|
| 445 | "Non-destructively adds list SUMMANDS."
|
---|
| 446 | (cond ((endp summands) 0)
|
---|
[3818] | 447 | (t (reduce #'add-2 summands))))
|
---|
[3803] | 448 |
|
---|
[3634] | 449 | (defun subtract (minuend &rest subtrahends)
|
---|
[3427] | 450 | "Non-destructively subtract MINUEND and SUBTRAHENDS."
|
---|
[3851] | 451 | (cond ((endp subtrahends) (unary-minus minuend))
|
---|
| 452 | (t (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))))
|
---|
[3374] | 453 |
|
---|
[3062] | 454 | (defmethod left-tensor-product-by ((self poly) (other monom))
|
---|
| 455 | (setf (poly-termlist self)
|
---|
| 456 | (mapcan #'(lambda (term)
|
---|
| 457 | (let ((prod (left-tensor-product-by term other)))
|
---|
| 458 | (cond
|
---|
[3640] | 459 | ((universal-zerop prod) nil)
|
---|
[3062] | 460 | (t (list prod)))))
|
---|
| 461 | (poly-termlist self)))
|
---|
[3249] | 462 | (incf (poly-dimension self) (monom-dimension other))
|
---|
[3062] | 463 | self)
|
---|
[3044] | 464 |
|
---|
[3062] | 465 | (defmethod right-tensor-product-by ((self poly) (other monom))
|
---|
| 466 | (setf (poly-termlist self)
|
---|
| 467 | (mapcan #'(lambda (term)
|
---|
| 468 | (let ((prod (right-tensor-product-by term other)))
|
---|
| 469 | (cond
|
---|
[3640] | 470 | ((universal-zerop prod) nil)
|
---|
[3062] | 471 | (t (list prod)))))
|
---|
| 472 | (poly-termlist self)))
|
---|
[3249] | 473 | (incf (poly-dimension self) (monom-dimension other))
|
---|
[3062] | 474 | self)
|
---|
| 475 |
|
---|
| 476 |
|
---|
[3084] | 477 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
|
---|
[2716] | 478 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
---|
[3060] | 479 | is a list of polynomials. Destructively modifies PLIST elements."
|
---|
[3061] | 480 | (mapc #'(lambda (poly)
|
---|
[3085] | 481 | (left-tensor-product-by
|
---|
| 482 | poly
|
---|
| 483 | (prog1
|
---|
| 484 | (make-monom-variable k i)
|
---|
| 485 | (incf i))))
|
---|
[3061] | 486 | plist))
|
---|
[52] | 487 |
|
---|
[3087] | 488 | (defun standard-extension-1 (plist
|
---|
| 489 | &aux
|
---|
[3096] | 490 | (plist (standard-extension plist))
|
---|
[3087] | 491 | (nvars (poly-dimension (car plist))))
|
---|
[3081] | 492 | "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
|
---|
[3087] | 493 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
|
---|
| 494 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
[3105] | 495 | tantamount to replacing PI with UI*PI-1. It assumes that all
|
---|
[3106] | 496 | polynomials have the same dimension, and only the first polynomial
|
---|
| 497 | is examined to determine this dimension."
|
---|
[3089] | 498 | ;; Implementation note: we use STANDARD-EXTENSION and then subtract
|
---|
| 499 | ;; 1 from each polynomial; since UI*PI has no constant term,
|
---|
| 500 | ;; we just need to append the constant term at the end
|
---|
| 501 | ;; of each termlist.
|
---|
[3064] | 502 | (flet ((subtract-1 (p)
|
---|
[3641] | 503 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
[3083] | 504 | (setf plist (mapc #'subtract-1 plist)))
|
---|
[3077] | 505 | plist)
|
---|
[52] | 506 |
|
---|
| 507 |
|
---|
[3107] | 508 | (defun standard-sum (plist
|
---|
| 509 | &aux
|
---|
| 510 | (plist (standard-extension plist))
|
---|
| 511 | (nvars (poly-dimension (car plist))))
|
---|
[3087] | 512 | "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
|
---|
| 513 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
|
---|
| 514 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
---|
| 515 | tantamount to replacing PI with UI*PI, and the resulting polynomials
|
---|
[3117] | 516 | are added. Finally, 1 is subtracted. It should be noted that the term
|
---|
| 517 | order is not modified, which is equivalent to using a lexicographic
|
---|
| 518 | order on the first K variables."
|
---|
[3107] | 519 | (flet ((subtract-1 (p)
|
---|
[3641] | 520 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
---|
[3108] | 521 | (subtract-1
|
---|
| 522 | (make-instance
|
---|
| 523 | 'poly
|
---|
[3115] | 524 | :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
|
---|
[52] | 525 |
|
---|
[3653] | 526 | (defgeneric universal-ezgcd (x y)
|
---|
| 527 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
---|
| 528 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
---|
| 529 | the Euclidean algorithm.")
|
---|
| 530 | (:method ((x integer) (y integer)
|
---|
| 531 | &aux (c (gcd x y)))
|
---|
| 532 | (values c (/ x c) (/ y c)))
|
---|
| 533 | )
|
---|
| 534 |
|
---|
[3655] | 535 | (defgeneric s-polynomial (object1 object2)
|
---|
[3651] | 536 | (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
|
---|
| 537 | (:method ((f poly) (g poly))
|
---|
| 538 | (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
|
---|
| 539 | (mf (divide lcm (leading-monomial f)))
|
---|
| 540 | (mg (divide lcm (leading-monomial g))))
|
---|
| 541 | (multiple-value-bind (c cf cg)
|
---|
[3652] | 542 | (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
|
---|
[3651] | 543 | (declare (ignore c))
|
---|
| 544 | (subtract
|
---|
[4092] | 545 | (multiply-by f (change-class mf 'term :coeff cg))
|
---|
| 546 | (multiply-by g (change-class mg 'term :coeff cf)))))))
|
---|
[3651] | 547 |
|
---|
[3676] | 548 | (defgeneric poly-content (object)
|
---|
| 549 | (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
|
---|
[3677] | 550 | (:method ((self poly))
|
---|
| 551 | (reduce #'universal-gcd
|
---|
[3679] | 552 | (mapcar #'term-coeff (rest (poly-termlist self)))
|
---|
| 553 | :initial-value (leading-coefficient self))))
|
---|
[3676] | 554 |
|
---|
[3684] | 555 | (defun poly-primitive-part (object)
|
---|
[3685] | 556 | "Divide polynomial OBJECT by gcd of its
|
---|
[3684] | 557 | coefficients. Return the resulting polynomial."
|
---|
[3688] | 558 | (scalar-divide-by object (poly-content object)))
|
---|
[3682] | 559 |
|
---|
[3700] | 560 | (defun poly-insert-variables (self k)
|
---|
[3697] | 561 | (left-tensor-product-by self (make-instance 'monom :dimension k)))
|
---|
| 562 |
|
---|
[3698] | 563 | (defun saturation-extension (f plist &aux (k (length plist)))
|
---|
[3708] | 564 | "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
|
---|
| 565 | PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
|
---|
[3711] | 566 | as first K variables. It destructively modifies F and PLIST."
|
---|
[3700] | 567 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
[3699] | 568 | (standard-extension-1 plist)))
|
---|
[3694] | 569 |
|
---|
[3699] | 570 | (defun polysaturation-extension (f plist &aux (k (length plist)))
|
---|
[3708] | 571 | "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
|
---|
| 572 | and F' is F with variables U1,U2,...,UK inserted as first K
|
---|
[3711] | 573 | variables. It destructively modifies F and PLIST."
|
---|
[3700] | 574 | (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
|
---|
[3703] | 575 | (list (standard-sum plist))))
|
---|
[3694] | 576 |
|
---|
[3691] | 577 | (defun saturation-extension-1 (f p)
|
---|
[3712] | 578 | "Given family of polynomials F and a polynomial P, calculate [F',
|
---|
| 579 | U*P-1], where F' is F with variable inserted as the first variable. It
|
---|
| 580 | destructively modifies F and P."
|
---|
[3693] | 581 | (polysaturation-extension f (list p)))
|
---|
[3713] | 582 |
|
---|
[3717] | 583 | (defmethod multiply-by ((object1 number) (object2 poly))
|
---|
[3720] | 584 | (scalar-multiply-by (copy-instance object2) object1))
|
---|
[3716] | 585 |
|
---|
[4068] | 586 | (defmethod multiply-by ((object1 poly) (object2 number))
|
---|
| 587 | (scalar-multiply-by (copy-instance object1) object2))
|
---|
| 588 |
|
---|
[3781] | 589 | (defun make-poly-variable (nvars pos &optional (power 1))
|
---|
| 590 | (change-class (make-monom-variable nvars pos power) 'poly))
|
---|
[3736] | 591 |
|
---|
[3821] | 592 | (defun make-poly-constant (nvars coeff)
|
---|
| 593 | (change-class (make-term-constant nvars coeff) 'poly))
|
---|
| 594 |
|
---|
[3713] | 595 | (defgeneric universal-expt (x y)
|
---|
[3721] | 596 | (:documentation "Raises X to power Y.")
|
---|
[3713] | 597 | (:method ((x number) (y integer)) (expt x y))
|
---|
| 598 | (:method ((x t) (y integer))
|
---|
| 599 | (declare (type fixnum y))
|
---|
| 600 | (cond
|
---|
| 601 | ((minusp y) (error "universal-expt: Negative exponent."))
|
---|
| 602 | ((universal-zerop x) (if (zerop y) 1))
|
---|
| 603 | (t
|
---|
| 604 | (do ((k 1 (ash k 1))
|
---|
| 605 | (q x (multiply q q)) ;keep squaring
|
---|
| 606 | (p 1 (if (not (zerop (logand k y))) (multiply p q) p)))
|
---|
| 607 | ((> k y) p)
|
---|
[3778] | 608 | (declare (fixnum k)))))))
|
---|
| 609 |
|
---|
| 610 | (defgeneric poly-p (object)
|
---|
| 611 | (:documentation "Checks if an object is a polynomial.")
|
---|
[3779] | 612 | (:method ((self poly)) t)
|
---|
[3778] | 613 | (:method ((self t)) nil))
|
---|
[3830] | 614 |
|
---|
[4021] | 615 | (defmethod ->sexp :before ((self poly) &optional vars)
|
---|
[3905] | 616 | "Ensures that the number of variables in VARS maches the polynomial dimension of the
|
---|
| 617 | polynomial SELF."
|
---|
[4027] | 618 | (with-slots (dimension)
|
---|
| 619 | self
|
---|
| 620 | (assert (= (length vars) dimension)
|
---|
[4028] | 621 | nil
|
---|
[4027] | 622 | "Number of variables ~S does not match the dimension ~S"
|
---|
| 623 | vars dimension)))
|
---|
[3904] | 624 |
|
---|
[4021] | 625 | (defmethod ->sexp ((self poly) &optional vars)
|
---|
[3905] | 626 | "Converts a polynomial SELF to a sexp."
|
---|
[4036] | 627 | (let ((m (mapcar #'(lambda (x) (->sexp x vars))
|
---|
[3830] | 628 | (poly-termlist self))))
|
---|
[4053] | 629 | (cond ((endp m) 0)
|
---|
[4036] | 630 | ((endp (cdr m)) (car m))
|
---|
| 631 | (t (cons '+ m)))))
|
---|
[3899] | 632 |
|
---|
[3903] | 633 | (defparameter +list-marker+ :[
|
---|
| 634 | "A sexp with this head is considered a list of polynomials.")
|
---|
| 635 |
|
---|
[4021] | 636 | (defmethod ->sexp ((self cons) &optional vars)
|
---|
[3906] | 637 | (assert (eql (car self) +list-marker+))
|
---|
[4021] | 638 | (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
|
---|
[3906] | 639 |
|
---|
| 640 |
|
---|
[3899] | 641 | (defun poly-eval (expr vars order)
|
---|
| 642 | "Evaluate Lisp form EXPR to a polynomial or a list of polynomials in
|
---|
| 643 | variables VARS. Return the resulting polynomial or list of
|
---|
| 644 | polynomials. Standard arithmetical operators in form EXPR are
|
---|
| 645 | replaced with their analogues in the ring of polynomials, and the
|
---|
| 646 | resulting expression is evaluated, resulting in a polynomial or a list
|
---|
| 647 | of polynomials in internal form. A similar operation in another computer
|
---|
| 648 | algebra system could be called 'expand' or so."
|
---|
| 649 | (labels ((p-eval (p) (poly-eval p vars order))
|
---|
| 650 | (p-eval-list (plist) (mapcar #'p-eval plist)))
|
---|
| 651 | (cond
|
---|
| 652 | ((eq expr 0)
|
---|
| 653 | (make-instance 'poly :dimension (length vars)))
|
---|
| 654 | ((member expr vars :test #'equalp)
|
---|
| 655 | (let ((pos (position expr vars :test #'equalp)))
|
---|
| 656 | (make-poly-variable (length vars) pos)))
|
---|
| 657 | ((atom expr)
|
---|
[4015] | 658 | (make-poly-constant (length vars) expr))
|
---|
[3899] | 659 | ((eq (car expr) +list-marker+)
|
---|
| 660 | (cons +list-marker+ (p-eval-list (cdr expr))))
|
---|
| 661 | (t
|
---|
| 662 | (case (car expr)
|
---|
| 663 | (+ (reduce #'add (p-eval-list (cdr expr))))
|
---|
| 664 | (- (apply #'subtract (p-eval-list (cdr expr))))
|
---|
| 665 | (*
|
---|
| 666 | (if (endp (cddr expr)) ;unary
|
---|
| 667 | (p-eval (cadr expr))
|
---|
| 668 | (reduce #'multiply (p-eval-list (cdr expr)))))
|
---|
| 669 | (/
|
---|
| 670 | ;; A polynomial can be divided by a scalar
|
---|
| 671 | (cond
|
---|
| 672 | ((endp (cddr expr))
|
---|
| 673 | ;; A special case (/ ?), the inverse
|
---|
| 674 | (divide (cadr expr)))
|
---|
| 675 | (t
|
---|
| 676 | (let ((num (p-eval (cadr expr)))
|
---|
[4016] | 677 | (denom-inverse (apply #'divide (mapcar #'p-eval (cddr expr)))))
|
---|
[3899] | 678 | (multiply denom-inverse num)))))
|
---|
| 679 | (expt
|
---|
| 680 | (cond
|
---|
| 681 | ((member (cadr expr) vars :test #'equalp)
|
---|
| 682 | ;;Special handling of (expt var pow)
|
---|
| 683 | (let ((pos (position (cadr expr) vars :test #'equalp)))
|
---|
| 684 | (make-poly-variable (length vars) pos (caddr expr))))
|
---|
| 685 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
| 686 | ;; Negative power means division in coefficient ring
|
---|
| 687 | ;; Non-integer power means non-polynomial coefficient
|
---|
| 688 | expr)
|
---|
| 689 | (t (universal-expt (p-eval (cadr expr)) (caddr expr)))))
|
---|
| 690 | (otherwise
|
---|
[4017] | 691 | (error "Cannot evaluate as polynomial: ~A" expr)))))))
|
---|
[4053] | 692 |
|
---|
| 693 | (defgeneric make-zero-for (self)
|
---|
| 694 | (:method ((self poly))
|
---|
| 695 | (make-instance 'poly :dimension (poly-dimension self))))
|
---|
| 696 |
|
---|
| 697 | (defgeneric make-unit-for (self)
|
---|
| 698 | (:method ((self poly))
|
---|
| 699 | (make-poly-constant (poly-dimension self) 1)))
|
---|
[4057] | 700 |
|
---|
[4068] | 701 | (defgeneric poly-reverse (self)
|
---|
[4061] | 702 | (:documentation "Reverse the order of terms in a polynomial SELF.")
|
---|
[4057] | 703 | (:method ((self poly))
|
---|
| 704 | (with-slots (termlist)
|
---|
| 705 | self
|
---|
[4060] | 706 | (setf termlist (nreverse termlist)))
|
---|
[4057] | 707 | self))
|
---|
| 708 |
|
---|
| 709 |
|
---|
[4053] | 710 |
|
---|