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