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