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