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