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