| 1 | ;;----------------------------------------------------------------
 | 
|---|
| 2 | ;;; -*-  Mode: Lisp -*- 
 | 
|---|
| 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 | 
 | 
|---|
| 23 | (defpackage "POLYNOMIAL"
 | 
|---|
| 24 |   (:use :cl :utils :monom :copy :ring)
 | 
|---|
| 25 |   (:export "POLY"
 | 
|---|
| 26 |            "POLY-DIMENSION"
 | 
|---|
| 27 |            "POLY-TERMLIST"
 | 
|---|
| 28 |            "POLY-TERM-ORDER"
 | 
|---|
| 29 |            "POLY-INSERT-TERM"
 | 
|---|
| 30 |            "POLY-REMOVE-TERM"
 | 
|---|
| 31 |            "SCALAR-MULTIPLY-BY"
 | 
|---|
| 32 |            "SCALAR-DIVIDE-BY"
 | 
|---|
| 33 |            "LEADING-TERM"
 | 
|---|
| 34 |            "LEADING-MONOMIAL"
 | 
|---|
| 35 |            "LEADING-COEFFICIENT"
 | 
|---|
| 36 |            "SECOND-LEADING-TERM"
 | 
|---|
| 37 |            "SECOND-LEADING-MONOMIAL"
 | 
|---|
| 38 |            "SECOND-LEADING-COEFFICIENT"
 | 
|---|
| 39 |            "ADD-TO"
 | 
|---|
| 40 |            "ADD"
 | 
|---|
| 41 |            "SUBTRACT-FROM"
 | 
|---|
| 42 |            "SUBTRACT"
 | 
|---|
| 43 |            "CHANGE-TERM-ORDER"
 | 
|---|
| 44 |            "STANDARD-EXTENSION"
 | 
|---|
| 45 |            "STANDARD-EXTENSION-1"
 | 
|---|
| 46 |            "STANDARD-SUM"
 | 
|---|
| 47 |            "SATURATION-EXTENSION"
 | 
|---|
| 48 |            "ALIST->POLY"
 | 
|---|
| 49 |            "POLY->ALIST"
 | 
|---|
| 50 |            "->INFIX"
 | 
|---|
| 51 |            "UNIVERSAL-EZGCD"
 | 
|---|
| 52 |            "S-POLYNOMIAL"
 | 
|---|
| 53 |            "POLY-CONTENT"
 | 
|---|
| 54 |            "POLY-PRIMITIVE-PART"
 | 
|---|
| 55 |            "SATURATION-EXTENSION-1"
 | 
|---|
| 56 |            "MAKE-POLY-VARIABLE"
 | 
|---|
| 57 |            "MAKE-POLY-CONSTANT"
 | 
|---|
| 58 |            "MAKE-ZERO-FOR"
 | 
|---|
| 59 |            "MAKE-UNIT-FOR"
 | 
|---|
| 60 |            "UNIVERSAL-EXPT"
 | 
|---|
| 61 |            "UNIVERSAL-EQUALP"
 | 
|---|
| 62 |            "UNIVERSAL-ZEROP"
 | 
|---|
| 63 |            "POLY-LENGTH"
 | 
|---|
| 64 |            "POLY-REVERSE"
 | 
|---|
| 65 |            "POLY-P"
 | 
|---|
| 66 |            "+LIST-MARKER+"
 | 
|---|
| 67 |            "POLY-EVAL"
 | 
|---|
| 68 |            "*COEFFICIENT-CLASS*")
 | 
|---|
| 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."))
 | 
|---|
| 72 | 
 | 
|---|
| 73 | (in-package :polynomial)
 | 
|---|
| 74 | 
 | 
|---|
| 75 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
 | 
|---|
| 76 | 
 | 
|---|
| 77 | (defclass poly (ring)
 | 
|---|
| 78 |   ((termlist :initform nil :initarg :termlist :accessor poly-termlist
 | 
|---|
| 79 |              :documentation "List of terms.")
 | 
|---|
| 80 |    (order :initform #'lex> :initarg :order :accessor poly-term-order
 | 
|---|
| 81 |           :documentation "Monomial/term order."))
 | 
|---|
| 82 |   (:default-initargs :termlist nil :order #'lex>)
 | 
|---|
| 83 |   (:documentation "A polynomial with a list of terms TERMLIST, ordered
 | 
|---|
| 84 | according to term order ORDER, which defaults to LEX>."))
 | 
|---|
| 85 | 
 | 
|---|
| 86 | (defmethod print-object ((self poly) stream)
 | 
|---|
| 87 |   (print-unreadable-object (self stream :type t :identity t)
 | 
|---|
| 88 |     (with-accessors ((termlist poly-termlist)
 | 
|---|
| 89 |                      (order poly-term-order))
 | 
|---|
| 90 |         self
 | 
|---|
| 91 |       (format stream "TERMLIST=~A ORDER=~A" 
 | 
|---|
| 92 |               termlist order))))
 | 
|---|
| 93 | 
 | 
|---|
| 94 | (defmethod copy-instance :around ((object poly)  &rest initargs &key &allow-other-keys)
 | 
|---|
| 95 |   "Returns a deep copy of the polynomial POLY, by copying the TERMLIST and its terms."
 | 
|---|
| 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 | 
 | 
|---|
| 104 | (defgeneric change-term-order (self other)
 | 
|---|
| 105 |   (:documentation "Change term order of SELF to the term order of OTHER.")
 | 
|---|
| 106 |   (:method ((self poly) (other poly))
 | 
|---|
| 107 |     (unless (eq (poly-term-order self) (poly-term-order other))
 | 
|---|
| 108 |       (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
 | 
|---|
| 109 |             (poly-term-order self) (poly-term-order other)))
 | 
|---|
| 110 |     self))
 | 
|---|
| 111 | 
 | 
|---|
| 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 | 
 | 
|---|
| 117 | (defgeneric poly-insert-term (self term)
 | 
|---|
| 118 |   (:documentation "Insert a term TERM into SELF before all other
 | 
|---|
| 119 | terms. Order is not enforced.")
 | 
|---|
| 120 |   (:method ((self poly) (term term))
 | 
|---|
| 121 |     (with-slots (termlist)
 | 
|---|
| 122 |         self
 | 
|---|
| 123 |       (unless (endp termlist)
 | 
|---|
| 124 |         (assert (= (monom-dimension (car termlist)) (monom-dimension term)))))
 | 
|---|
| 125 |     (push term (poly-termlist self))
 | 
|---|
| 126 |     self))
 | 
|---|
| 127 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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))
 | 
|---|
| 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)))
 | 
|---|
| 141 |     self))
 | 
|---|
| 142 | 
 | 
|---|
| 143 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
 | 
|---|
| 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
 | 
|---|
| 151 | (ALIST->POLY '(((0 0) . 7) ((0 1) . 2) ((2 3) . 3) )). NOTE: the
 | 
|---|
| 152 | terms are entered in the increasing order.
 | 
|---|
| 153 | 
 | 
|---|
| 154 | NOTE: The primary use is for low-level debugging of the package."
 | 
|---|
| 155 |   (dolist (x alist poly)
 | 
|---|
| 156 |     (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
 | 
|---|
| 157 | 
 | 
|---|
| 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 | 
 | 
|---|
| 170 | #+nil
 | 
|---|
| 171 | (defmethod shared-initialize :after ((self poly) slot-names 
 | 
|---|
| 172 |                                      &rest initargs 
 | 
|---|
| 173 |                                      &key)
 | 
|---|
| 174 |   "If TERMLIST is supplied and non-empty, and DIMENSION is NIL, set
 | 
|---|
| 175 | the dimension to the dimension of the first term in TERMLIST."
 | 
|---|
| 176 |   (declare (ignore initargs))
 | 
|---|
| 177 |   (let ((dims (mapcar #'monom-dimension (slot-value self 'termlist))))
 | 
|---|
| 178 |     (format t "Dimensions: ~A~%" dims)
 | 
|---|
| 179 |     (assert (apply #'= dims))
 | 
|---|
| 180 |     (unless (endp dims)
 | 
|---|
| 181 |       (setf (slot-value self 'dimension) (car dims))))
 | 
|---|
| 182 |   self)
 | 
|---|
| 183 | 
 | 
|---|
| 184 | (defmethod update-instance-for-different-class :after ((old term) (new poly) &key)
 | 
|---|
| 185 |   "Converts OLD of class TERM to a NEW of class POLY, by making it into a 1-element TERMLIST."
 | 
|---|
| 186 |   (reinitialize-instance new :termlist (list old)))
 | 
|---|
| 187 | 
 | 
|---|
| 188 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
 | 
|---|
| 189 |   "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
 | 
|---|
| 190 |   (reinitialize-instance new :termlist (list (change-class old 'term))))
 | 
|---|
| 191 |   
 | 
|---|
| 192 | (defmethod universal-equalp ((self poly) (other poly))
 | 
|---|
| 193 |   "Implements equality of polynomials."
 | 
|---|
| 194 |   (and 
 | 
|---|
| 195 |    ;(eql (poly-dimension self) (poly-dimension other))
 | 
|---|
| 196 |    (every #'universal-equalp (poly-termlist self) (poly-termlist other))
 | 
|---|
| 197 |    (eq (poly-term-order self) (poly-term-order other))))
 | 
|---|
| 198 | 
 | 
|---|
| 199 | (defgeneric leading-term (object)
 | 
|---|
| 200 |   (:method ((self poly)) 
 | 
|---|
| 201 |     (car (poly-termlist self)))
 | 
|---|
| 202 |   (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
 | 
|---|
| 203 | 
 | 
|---|
| 204 | (defgeneric second-leading-term (object) 
 | 
|---|
| 205 |   (:method ((self poly))
 | 
|---|
| 206 |     (cadar (poly-termlist self)))
 | 
|---|
| 207 |   (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
 | 
|---|
| 208 | 
 | 
|---|
| 209 | (defgeneric leading-monomial (object)
 | 
|---|
| 210 |   (:method ((self poly)) 
 | 
|---|
| 211 |     (change-class (copy-instance (leading-term self)) 'monom))
 | 
|---|
| 212 |   (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
 | 
|---|
| 213 | 
 | 
|---|
| 214 | (defgeneric second-leading-monomial (object)
 | 
|---|
| 215 |   (:method ((self poly)) 
 | 
|---|
| 216 |     (change-class (copy-instance (second-leading-term self)) 'monom))
 | 
|---|
| 217 |   (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
 | 
|---|
| 218 | 
 | 
|---|
| 219 | (defgeneric leading-coefficient (object)
 | 
|---|
| 220 |   (:method ((self poly))
 | 
|---|
| 221 |     (term-coeff (leading-term self)))
 | 
|---|
| 222 |   (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
 | 
|---|
| 223 | 
 | 
|---|
| 224 | (defgeneric second-leading-coefficient (object)
 | 
|---|
| 225 |   (:method ((self poly)) 
 | 
|---|
| 226 |     (term-coeff (second-leading-term self)))
 | 
|---|
| 227 |   (:documentation "The second leading coefficient of a polynomial. It
 | 
|---|
| 228 |   signals error for a polynomial with at most one term."))
 | 
|---|
| 229 | 
 | 
|---|
| 230 | (defmethod universal-zerop ((self poly))
 | 
|---|
| 231 |   "Return T iff SELF is a zero polynomial."
 | 
|---|
| 232 |   (null (poly-termlist self)))
 | 
|---|
| 233 | 
 | 
|---|
| 234 | (defgeneric poly-length (self)
 | 
|---|
| 235 |   (:documentation "Return the number of terms.")
 | 
|---|
| 236 |   (:method ((self poly))
 | 
|---|
| 237 |     (length (poly-termlist self))))
 | 
|---|
| 238 | 
 | 
|---|
| 239 | (defgeneric scalar-multiply-by (self other)
 | 
|---|
| 240 |   (:documentation "Multiply vector SELF by a scalar OTHER.")
 | 
|---|
| 241 |   (:method ((self poly) other)
 | 
|---|
| 242 |     (mapc #'(lambda (term) (setf (term-coeff term) (multiply-by (term-coeff term) other)))
 | 
|---|
| 243 |           (poly-termlist self))
 | 
|---|
| 244 |     self))
 | 
|---|
| 245 | 
 | 
|---|
| 246 | (defgeneric scalar-divide-by (self other)
 | 
|---|
| 247 |   (:documentation "Divide vector SELF by a scalar OTHER.")
 | 
|---|
| 248 |   (:method ((self poly) other)
 | 
|---|
| 249 |     (mapc #'(lambda (term) (setf (term-coeff term) (divide-by (term-coeff term) other)))
 | 
|---|
| 250 |           (poly-termlist self))
 | 
|---|
| 251 |     self))
 | 
|---|
| 252 | 
 | 
|---|
| 253 | (defmethod unary-inverse :before ((self poly))
 | 
|---|
| 254 |   "Checks invertibility of a polynomial SELF. To be invertable, the
 | 
|---|
| 255 | polynomial must be an invertible, constant polynomial."
 | 
|---|
| 256 |   (with-slots (termlist)
 | 
|---|
| 257 |       self
 | 
|---|
| 258 |     (assert (and (= (length termlist) 1) (zerop (total-degree (car termlist))))
 | 
|---|
| 259 |             nil
 | 
|---|
| 260 |             "To be invertible, the polynomial must have 1 term of total degree 0.")))
 | 
|---|
| 261 | 
 | 
|---|
| 262 | (defmethod unary-inverse ((self poly))
 | 
|---|
| 263 |   "Returns the unary inverse of a polynomial SELF."
 | 
|---|
| 264 |   (with-slots (termlist)
 | 
|---|
| 265 |       self
 | 
|---|
| 266 |     (setf (car termlist) (unary-inverse (car termlist)))
 | 
|---|
| 267 |     self))
 | 
|---|
| 268 | 
 | 
|---|
| 269 | (defmethod multiply-by ((self poly) (other monom))
 | 
|---|
| 270 |   "Multiply a polynomial SELF by OTHER."
 | 
|---|
| 271 |   (mapc #'(lambda (term) (multiply-by term other))
 | 
|---|
| 272 |         (poly-termlist self))
 | 
|---|
| 273 |   self)
 | 
|---|
| 274 | 
 | 
|---|
| 275 | (defmethod multiply-by ((self poly) (other term))
 | 
|---|
| 276 |   "Multiply a polynomial SELF by OTHER."
 | 
|---|
| 277 |   (mapc #'(lambda (term) (multiply-by term other))
 | 
|---|
| 278 |         (poly-termlist self))
 | 
|---|
| 279 |   self)
 | 
|---|
| 280 | 
 | 
|---|
| 281 | #|
 | 
|---|
| 282 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
 | 
|---|
| 283 |   "Return an expression which will efficiently adds/subtracts two
 | 
|---|
| 284 | polynomials, P and Q.  The addition/subtraction of coefficients is
 | 
|---|
| 285 | performed by calling ADD/SUBTRACT-FN.  If UMINUS-FN is supplied, it is
 | 
|---|
| 286 | used to negate the coefficients of Q which do not have a corresponding
 | 
|---|
| 287 | coefficient in P. The code implements an efficient algorithm to add
 | 
|---|
| 288 | two polynomials represented as sorted lists of terms. The code
 | 
|---|
| 289 | destroys both arguments, reusing the terms to build the result."
 | 
|---|
| 290 |   `(macrolet ((lc (x) `(term-coeff (car ,x))))
 | 
|---|
| 291 |      (do ((p ,p)
 | 
|---|
| 292 |           (q ,q)
 | 
|---|
| 293 |           r)
 | 
|---|
| 294 |          ((or (endp p) (endp q))
 | 
|---|
| 295 |           ;; NOTE: R contains the result in reverse order. Can it
 | 
|---|
| 296 |           ;; be more efficient to produce the terms in correct order?
 | 
|---|
| 297 |           (unless (endp q) 
 | 
|---|
| 298 |             ;; Upon subtraction, we must change the sign of
 | 
|---|
| 299 |             ;; all coefficients in q
 | 
|---|
| 300 |             ,@(when uminus-fn
 | 
|---|
| 301 |                     `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
 | 
|---|
| 302 |             (setf r (nreconc r q)))
 | 
|---|
| 303 |           (unless (endp p)
 | 
|---|
| 304 |             (setf r (nreconc r p)))
 | 
|---|
| 305 |           r)
 | 
|---|
| 306 |        (multiple-value-bind 
 | 
|---|
| 307 |              (greater-p equal-p)
 | 
|---|
| 308 |            (funcall ,order-fn (car p) (car q))
 | 
|---|
| 309 |          (cond
 | 
|---|
| 310 |            (greater-p
 | 
|---|
| 311 |             (rotatef (cdr p) r p)
 | 
|---|
| 312 |             )
 | 
|---|
| 313 |            (equal-p
 | 
|---|
| 314 |             (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
 | 
|---|
| 315 |               (cond 
 | 
|---|
| 316 |                 ((universal-zerop s)
 | 
|---|
| 317 |                  (setf p (cdr p))
 | 
|---|
| 318 |                  )
 | 
|---|
| 319 |                 (t 
 | 
|---|
| 320 |                  (setf (lc p) s)
 | 
|---|
| 321 |                  (rotatef (cdr p) r p))))
 | 
|---|
| 322 |             (setf q (cdr q))
 | 
|---|
| 323 |             )
 | 
|---|
| 324 |            (t 
 | 
|---|
| 325 |             ;;Negate the term of Q if UMINUS provided, signallig
 | 
|---|
| 326 |             ;;that we are doing subtraction
 | 
|---|
| 327 |             ,(when uminus-fn
 | 
|---|
| 328 |                    `(setf (lc q) (funcall ,uminus-fn (lc q))))
 | 
|---|
| 329 |             (rotatef (cdr q) r q))))
 | 
|---|
| 330 |        ;;(format t "P:~A~%" p)
 | 
|---|
| 331 |        ;;(format t "Q:~A~%" q)
 | 
|---|
| 332 |        ;;(format t "R:~A~%" r)
 | 
|---|
| 333 |        )))
 | 
|---|
| 334 | |#
 | 
|---|
| 335 | 
 | 
|---|
| 336 | ;; Shorthand for leading coefficient of a termlist
 | 
|---|
| 337 | (defmacro lc (x) `(term-coeff (car ,x)))
 | 
|---|
| 338 | 
 | 
|---|
| 339 | (defun slow-add (p q order-fn add-fn)
 | 
|---|
| 340 |   (cond
 | 
|---|
| 341 |     ((endp p) q)
 | 
|---|
| 342 |     ((endp q) p)
 | 
|---|
| 343 |     (t 
 | 
|---|
| 344 |      (multiple-value-bind 
 | 
|---|
| 345 |            (greater-p equal-p)
 | 
|---|
| 346 |          (funcall order-fn (car p) (car q))
 | 
|---|
| 347 |        (cond
 | 
|---|
| 348 |          (greater-p                     ; (> (car p) (car q))
 | 
|---|
| 349 |           (cons (car p) (slow-add (cdr p) q order-fn add-fn))
 | 
|---|
| 350 |           )
 | 
|---|
| 351 |          (equal-p                       ; (= (car p)) (car q))
 | 
|---|
| 352 |           (let ((s (funcall add-fn (lc p) (lc q))))
 | 
|---|
| 353 |             (cond 
 | 
|---|
| 354 |               ((universal-zerop s)
 | 
|---|
| 355 |                (slow-add (cdr p) (cdr q) order-fn add-fn))
 | 
|---|
| 356 |               (t 
 | 
|---|
| 357 |                ;; Adjust the lc of p
 | 
|---|
| 358 |                (setf (lc p) s)
 | 
|---|
| 359 |                (cons (car p) (slow-add (cdr p) (cdr q) order-fn add-fn))
 | 
|---|
| 360 |                ))))
 | 
|---|
| 361 |          (t                    ;(< (car p) (car q))                     
 | 
|---|
| 362 |           (cons (car q) (slow-add p (cdr q) order-fn add-fn))
 | 
|---|
| 363 |           ))))))
 | 
|---|
| 364 | 
 | 
|---|
| 365 | 
 | 
|---|
| 366 | (defun fast-and-risky-add (p q order-fn add-fn &aux result result-last)
 | 
|---|
| 367 |   (when (and p q (eq p q)) (warn "FAST-AND-RISKY-ADD: ~S is EQ to ~S" p q))
 | 
|---|
| 368 |   (flet ((add-to-result (x)
 | 
|---|
| 369 |            (assert (consp x))
 | 
|---|
| 370 |            (setf (cdr x) nil)
 | 
|---|
| 371 |            (if (endp result)
 | 
|---|
| 372 |                (setf result x
 | 
|---|
| 373 |                      result-last x)
 | 
|---|
| 374 |                (setf (cdr result-last) x
 | 
|---|
| 375 |                      result-last (cdr result-last)))))
 | 
|---|
| 376 |     (loop 
 | 
|---|
| 377 |        (cond
 | 
|---|
| 378 |          ((endp p) (unless (endp q) (add-to-result q)) (return result))
 | 
|---|
| 379 |          ((endp q) (unless (endp p) (add-to-result p)) (return result))
 | 
|---|
| 380 |          (t 
 | 
|---|
| 381 |           (multiple-value-bind 
 | 
|---|
| 382 |                 (greater-p equal-p)
 | 
|---|
| 383 |               (funcall order-fn (car p) (car q))
 | 
|---|
| 384 |             (cond
 | 
|---|
| 385 |               (greater-p                ; (> (car p) (car q))
 | 
|---|
| 386 |                (let ((tmp (cdr p)))
 | 
|---|
| 387 |                  (add-to-result p)
 | 
|---|
| 388 |                  (setf p tmp)))
 | 
|---|
| 389 |               (equal-p                  ; (= (car p)) (car q))
 | 
|---|
| 390 |                (let ((s (funcall add-fn (lc p) (lc q))))
 | 
|---|
| 391 |                  (cond 
 | 
|---|
| 392 |                    ((universal-zerop s)
 | 
|---|
| 393 |                     ;; Terms cancel, discard both
 | 
|---|
| 394 |                     (setf p (cdr p)
 | 
|---|
| 395 |                           q (cdr q)))
 | 
|---|
| 396 |                    (t 
 | 
|---|
| 397 |                     ;; Terms do not cancel, store the
 | 
|---|
| 398 |                     ;; sum of coefficients in (lc p)
 | 
|---|
| 399 |                     (setf (lc p) s)
 | 
|---|
| 400 |                     (let ((tmp (cdr p)))
 | 
|---|
| 401 |                       (add-to-result p)
 | 
|---|
| 402 |                       (setf p tmp
 | 
|---|
| 403 |                             q (cdr q)))))))
 | 
|---|
| 404 |               (t                 ;(< (car p) (car q))                   
 | 
|---|
| 405 |                (let ((tmp (cdr q)))
 | 
|---|
| 406 |                  (add-to-result q)
 | 
|---|
| 407 |                  (setf q tmp))
 | 
|---|
| 408 |                ))))))))
 | 
|---|
| 409 | 
 | 
|---|
| 410 | (defun fast-add (p q order-fn add-fn)
 | 
|---|
| 411 |   "This version calls SLOW-ADD and is bullet-proof."
 | 
|---|
| 412 |   (slow-add p q order-fn add-fn)
 | 
|---|
| 413 |   ;;(fast-and-risky-add p q order-fn add-fn)
 | 
|---|
| 414 |   ;;(f-add p q order-fn add-fn)
 | 
|---|
| 415 |   ;;(s-add p q order-fn add-fn)
 | 
|---|
| 416 |   )
 | 
|---|
| 417 | 
 | 
|---|
| 418 | #|
 | 
|---|
| 419 | ;; NOTE: The stuff below works, but may not be worth the trouble.
 | 
|---|
| 420 | 
 | 
|---|
| 421 | (defmacro def-add/subtract-method (add/subtract-method-name
 | 
|---|
| 422 |                                    uminus-method-name
 | 
|---|
| 423 |                                    &optional
 | 
|---|
| 424 |                                      (doc-string nil doc-string-supplied-p))
 | 
|---|
| 425 |   "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
 | 
|---|
| 426 |   `(defmethod ,add/subtract-method-name ((self poly) (other poly))
 | 
|---|
| 427 |      ,@(when doc-string-supplied-p `(,doc-string))
 | 
|---|
| 428 |      ;; Ensure orders are compatible
 | 
|---|
| 429 |      (change-term-order other self)
 | 
|---|
| 430 |      (setf (poly-termlist self) (fast-add/subtract 
 | 
|---|
| 431 |                                  (poly-termlist self) (poly-termlist other)
 | 
|---|
| 432 |                                  (poly-term-order self)
 | 
|---|
| 433 |                                  #',add/subtract-method-name
 | 
|---|
| 434 |                                  ,(when uminus-method-name `(function ,uminus-method-name))))
 | 
|---|
| 435 |      self))
 | 
|---|
| 436 | 
 | 
|---|
| 437 | (eval-when (:load-toplevel :execute)
 | 
|---|
| 438 | 
 | 
|---|
| 439 |   (def-add/subtract-method add-to nil
 | 
|---|
| 440 |     "Adds to polynomial SELF another polynomial OTHER.
 | 
|---|
| 441 | This operation destructively modifies both polynomials.
 | 
|---|
| 442 | The result is stored in SELF. This implementation does
 | 
|---|
| 443 | no consing, entirely reusing the sells of SELF and OTHER.")
 | 
|---|
| 444 | 
 | 
|---|
| 445 |   (def-add/subtract-method subtract-from unary-minus
 | 
|---|
| 446 |     "Subtracts from polynomial SELF another polynomial OTHER.
 | 
|---|
| 447 | This operation destructively modifies both polynomials.
 | 
|---|
| 448 | The result is stored in SELF. This implementation does
 | 
|---|
| 449 | no consing, entirely reusing the sells of SELF and OTHER.")
 | 
|---|
| 450 |   )
 | 
|---|
| 451 | 
 | 
|---|
| 452 | |#
 | 
|---|
| 453 | 
 | 
|---|
| 454 | (defmethod unary-minus ((self poly))
 | 
|---|
| 455 |   "Destructively modifies the coefficients of the polynomial SELF,
 | 
|---|
| 456 | by changing their sign."
 | 
|---|
| 457 |   (mapc #'unary-minus (poly-termlist self))
 | 
|---|
| 458 |   self)
 | 
|---|
| 459 | 
 | 
|---|
| 460 | (defun add-termlists (p q order-fn)
 | 
|---|
| 461 |   "Destructively adds two termlists P and Q ordered according to ORDER-FN."
 | 
|---|
| 462 |   (fast-add p q order-fn #'add-to))
 | 
|---|
| 463 | 
 | 
|---|
| 464 | (defun subtract-termlists (p q order-fn)
 | 
|---|
| 465 |   "Destructively subtracts two termlists P and Q ordered according to ORDER-FN."
 | 
|---|
| 466 |   (setf q (mapc #'unary-minus q))
 | 
|---|
| 467 |   (add-termlists p q order-fn))
 | 
|---|
| 468 | 
 | 
|---|
| 469 | (defmethod add-to ((self poly) (other poly))
 | 
|---|
| 470 |   "Adds to polynomial SELF another polynomial OTHER.
 | 
|---|
| 471 | This operation destructively modifies both polynomials.
 | 
|---|
| 472 | The result is stored in SELF. This implementation does
 | 
|---|
| 473 | no consing, entirely reusing the sells of SELF and OTHER."
 | 
|---|
| 474 |   (change-term-order other self)
 | 
|---|
| 475 |   (setf (poly-termlist self) (add-termlists
 | 
|---|
| 476 |                               (poly-termlist self) (poly-termlist other)
 | 
|---|
| 477 |                               (poly-term-order self)))
 | 
|---|
| 478 |   self)
 | 
|---|
| 479 |   
 | 
|---|
| 480 | 
 | 
|---|
| 481 | (defmethod subtract-from ((self poly) (other poly))
 | 
|---|
| 482 |   "Subtracts from 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 |   (change-term-order other self)
 | 
|---|
| 487 |   (setf (poly-termlist self) (subtract-termlists
 | 
|---|
| 488 |                               (poly-termlist self) (poly-termlist other)
 | 
|---|
| 489 |                               (poly-term-order self)))
 | 
|---|
| 490 |   self)
 | 
|---|
| 491 | 
 | 
|---|
| 492 | 
 | 
|---|
| 493 | (defmethod add-to ((self poly) (other term))
 | 
|---|
| 494 |   "Adds to a polynomial SELF a term OTHER. The term OTHER is not
 | 
|---|
| 495 | modified."
 | 
|---|
| 496 |   (add-to self (change-class other 'poly)))
 | 
|---|
| 497 | 
 | 
|---|
| 498 | (defmethod subtract-from ((self poly) (other term))
 | 
|---|
| 499 |   "Subtracts from a polynomial SELF a term OTHER. The term OTHER is not
 | 
|---|
| 500 | modified."
 | 
|---|
| 501 |   (subtract-from self (change-class other 'poly)))
 | 
|---|
| 502 | 
 | 
|---|
| 503 | 
 | 
|---|
| 504 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist 
 | 
|---|
| 505 |                                                     &optional (reverse-arg-order-P nil))
 | 
|---|
| 506 |   "Multiplies term TERM by a list of term, TERMLIST.
 | 
|---|
| 507 | Takes into accound divisors of zero in the ring, by
 | 
|---|
| 508 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
 | 
|---|
| 509 | is T, change the order of arguments; this may be important
 | 
|---|
| 510 | if we extend the package to non-commutative rings."
 | 
|---|
| 511 |   `(mapcan #'(lambda (other-term) 
 | 
|---|
| 512 |                (let ((prod (multiply
 | 
|---|
| 513 |                             ,@(cond 
 | 
|---|
| 514 |                                (reverse-arg-order-p
 | 
|---|
| 515 |                                 `(other-term ,term))
 | 
|---|
| 516 |                                (t 
 | 
|---|
| 517 |                                 `(,term other-term))))))
 | 
|---|
| 518 |                  (cond 
 | 
|---|
| 519 |                    ((universal-zerop prod) nil)
 | 
|---|
| 520 |                    (t (list prod)))))
 | 
|---|
| 521 |            ,termlist))
 | 
|---|
| 522 | 
 | 
|---|
| 523 | (defun multiply-termlists (p q order-fn)
 | 
|---|
| 524 |   "A version of polynomial multiplication, operating
 | 
|---|
| 525 | directly on termlists."
 | 
|---|
| 526 |   (cond 
 | 
|---|
| 527 |     ((or (endp p) (endp q))         
 | 
|---|
| 528 |      ;;p or q is 0 (represented by NIL)
 | 
|---|
| 529 |      nil)       
 | 
|---|
| 530 |     ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
 | 
|---|
| 531 |     ((endp (cdr p))
 | 
|---|
| 532 |      (multiply-term-by-termlist-dropping-zeros (car p) q))
 | 
|---|
| 533 |     ((endp (cdr q))
 | 
|---|
| 534 |      (multiply-term-by-termlist-dropping-zeros (car q) p t))
 | 
|---|
| 535 |     (t
 | 
|---|
| 536 |      (cons (multiply (car p) (car q))
 | 
|---|
| 537 |            (add-termlists 
 | 
|---|
| 538 |             (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
 | 
|---|
| 539 |             (multiply-termlists (cdr p) q order-fn)
 | 
|---|
| 540 |             order-fn)))))
 | 
|---|
| 541 | 
 | 
|---|
| 542 | (defmethod multiply-by ((self poly) (other poly) &aux (other-copy (copy-instance other)))
 | 
|---|
| 543 |   (change-term-order other-copy self)
 | 
|---|
| 544 |   (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
 | 
|---|
| 545 |                                                  (poly-termlist other-copy)
 | 
|---|
| 546 |                                                  (poly-term-order self)))
 | 
|---|
| 547 |   self)
 | 
|---|
| 548 | 
 | 
|---|
| 549 | (defmethod left-tensor-product-by ((self poly) (other monom))
 | 
|---|
| 550 |   (setf (poly-termlist self) 
 | 
|---|
| 551 |         (mapcan #'(lambda (term) 
 | 
|---|
| 552 |                     (let ((prod (left-tensor-product-by term other)))
 | 
|---|
| 553 |                       (cond 
 | 
|---|
| 554 |                         ((universal-zerop prod) nil)
 | 
|---|
| 555 |                         (t (list prod)))))
 | 
|---|
| 556 |                 (poly-termlist self)))
 | 
|---|
| 557 |   self)
 | 
|---|
| 558 | 
 | 
|---|
| 559 | (defmethod right-tensor-product-by ((self poly) (other monom))
 | 
|---|
| 560 |   (setf (poly-termlist self) 
 | 
|---|
| 561 |         (mapcan #'(lambda (term) 
 | 
|---|
| 562 |                     (let ((prod (right-tensor-product-by term other)))
 | 
|---|
| 563 |                       (cond 
 | 
|---|
| 564 |                         ((universal-zerop prod) nil)
 | 
|---|
| 565 |                         (t (list prod)))))
 | 
|---|
| 566 |                 (poly-termlist self)))
 | 
|---|
| 567 |   self)
 | 
|---|
| 568 | 
 | 
|---|
| 569 | 
 | 
|---|
| 570 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
 | 
|---|
| 571 |   "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
 | 
|---|
| 572 | is a list of polynomials. Destructively modifies PLIST elements."
 | 
|---|
| 573 |   (mapc #'(lambda (poly)
 | 
|---|
| 574 |             (left-tensor-product-by 
 | 
|---|
| 575 |              poly 
 | 
|---|
| 576 |              (prog1 
 | 
|---|
| 577 |                  (make-monom-variable k i) 
 | 
|---|
| 578 |                (incf i))))
 | 
|---|
| 579 |         plist))
 | 
|---|
| 580 | 
 | 
|---|
| 581 | (defun standard-extension-1 (plist 
 | 
|---|
| 582 |                              &aux 
 | 
|---|
| 583 |                                (plist (standard-extension plist))
 | 
|---|
| 584 |                                (nvars (poly-dimension (car plist))))
 | 
|---|
| 585 |   "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
 | 
|---|
| 586 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
 | 
|---|
| 587 | polynomial.  Subsequently, P1, P2, ..., PK are destructively modified
 | 
|---|
| 588 | tantamount to replacing PI with UI*PI-1. It assumes that all
 | 
|---|
| 589 | polynomials have the same dimension, and only the first polynomial
 | 
|---|
| 590 | is examined to determine this dimension."
 | 
|---|
| 591 |   ;; Implementation note: we use STANDARD-EXTENSION and then subtract
 | 
|---|
| 592 |   ;; 1 from each polynomial; since UI*PI has no constant term,
 | 
|---|
| 593 |   ;; we just need to append the constant term at the end
 | 
|---|
| 594 |   ;; of each termlist.
 | 
|---|
| 595 |   (flet ((subtract-1 (p) 
 | 
|---|
| 596 |            (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
 | 
|---|
| 597 |     (setf plist (mapc #'subtract-1 plist)))
 | 
|---|
| 598 |   plist)
 | 
|---|
| 599 | 
 | 
|---|
| 600 | 
 | 
|---|
| 601 | (defun standard-sum (plist
 | 
|---|
| 602 |                      &aux 
 | 
|---|
| 603 |                        (plist (standard-extension plist))
 | 
|---|
| 604 |                        (nvars (poly-dimension (car plist))))
 | 
|---|
| 605 |   "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
 | 
|---|
| 606 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
 | 
|---|
| 607 | polynomial.  Subsequently, P1, P2, ..., PK are destructively modified
 | 
|---|
| 608 | tantamount to replacing PI with UI*PI, and the resulting polynomials
 | 
|---|
| 609 | are added. Finally, 1 is subtracted.  It should be noted that the term
 | 
|---|
| 610 | order is not modified, which is equivalent to using a lexicographic
 | 
|---|
| 611 | order on the first K variables."
 | 
|---|
| 612 |   (flet ((subtract-1 (p) 
 | 
|---|
| 613 |            (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
 | 
|---|
| 614 |     (subtract-1 
 | 
|---|
| 615 |      (make-instance
 | 
|---|
| 616 |       'poly
 | 
|---|
| 617 |       :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
 | 
|---|
| 618 | 
 | 
|---|
| 619 | (defgeneric s-polynomial (object1 object2)
 | 
|---|
| 620 |   (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
 | 
|---|
| 621 |   (:method ((f poly) (g poly))
 | 
|---|
| 622 |     (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
 | 
|---|
| 623 |            (mf (divide lcm (leading-monomial f)))
 | 
|---|
| 624 |            (mg (divide lcm (leading-monomial g))))
 | 
|---|
| 625 |       (multiple-value-bind (c cf cg)
 | 
|---|
| 626 |           (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
 | 
|---|
| 627 |         (declare (ignore c))
 | 
|---|
| 628 |         (subtract
 | 
|---|
| 629 |          (multiply f mf cg)
 | 
|---|
| 630 |          (multiply g mg cf))))))
 | 
|---|
| 631 | 
 | 
|---|
| 632 | (defgeneric poly-content (object)
 | 
|---|
| 633 |   (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
 | 
|---|
| 634 |   (:method ((self poly))
 | 
|---|
| 635 |     (reduce #'universal-gcd 
 | 
|---|
| 636 |             (mapcar #'term-coeff (rest (poly-termlist self))) 
 | 
|---|
| 637 |             :initial-value (leading-coefficient self))))
 | 
|---|
| 638 | 
 | 
|---|
| 639 | (defun poly-primitive-part (self)
 | 
|---|
| 640 |   "Divide polynomial SELF by gcd of its
 | 
|---|
| 641 | coefficients. Return the resulting polynomial."
 | 
|---|
| 642 |   (scalar-divide-by self (poly-content self)))
 | 
|---|
| 643 | 
 | 
|---|
| 644 | (defun poly-insert-variables (self k)
 | 
|---|
| 645 |   (left-tensor-product-by self (make-instance 'monom :dimension k)))
 | 
|---|
| 646 | 
 | 
|---|
| 647 | (defun saturation-extension (f plist &aux (k (length plist)))
 | 
|---|
| 648 |   "Calculate [F', U1*P1-1,U2*P2-1,...,UK*PK-1], where
 | 
|---|
| 649 | PLIST=[P1,P2,...,PK] and F' is F with variables U1,U2,...,UK inserted
 | 
|---|
| 650 | as first K variables. It destructively modifies F and PLIST."
 | 
|---|
| 651 |   (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
 | 
|---|
| 652 |          (standard-extension-1 plist)))
 | 
|---|
| 653 | 
 | 
|---|
| 654 | (defun polysaturation-extension (f plist &aux (k (length plist)))
 | 
|---|
| 655 |   "Calculate [F', U1*P1+U2*P2+...+UK*PK-1], where PLIST=[P1,P2,...,PK]
 | 
|---|
| 656 | and F' is F with variables U1,U2,...,UK inserted as first K
 | 
|---|
| 657 | variables. It destructively modifies F and PLIST."
 | 
|---|
| 658 |   (nconc (mapc #'(lambda (x) (poly-insert-variables x k)) f)
 | 
|---|
| 659 |          (list (standard-sum plist))))
 | 
|---|
| 660 | 
 | 
|---|
| 661 | (defun saturation-extension-1 (f p) 
 | 
|---|
| 662 |   "Given family of polynomials F and a polynomial P, calculate [F',
 | 
|---|
| 663 | U*P-1], where F' is F with variable inserted as the first variable. It
 | 
|---|
| 664 | destructively modifies F and P."
 | 
|---|
| 665 |   (polysaturation-extension f (list p)))
 | 
|---|
| 666 | 
 | 
|---|
| 667 | (defmethod multiply-by ((self poly) (other ring))
 | 
|---|
| 668 |   (scalar-multiply-by self other))
 | 
|---|
| 669 | 
 | 
|---|
| 670 | (defun make-poly-variable (nvars pos &optional (power 1))
 | 
|---|
| 671 |   (change-class (make-monom-variable nvars pos power) 'poly))
 | 
|---|
| 672 | 
 | 
|---|
| 673 | (defun make-poly-constant (nvars coeff)
 | 
|---|
| 674 |   (change-class (make-term-constant nvars coeff) 'poly))
 | 
|---|
| 675 | 
 | 
|---|
| 676 | (defgeneric universal-expt (x y)
 | 
|---|
| 677 |   (:documentation "Raises X to power Y.")
 | 
|---|
| 678 |   (:method ((x number) (y integer)) (expt x y))
 | 
|---|
| 679 |   (:method ((x t) (y integer)) 
 | 
|---|
| 680 |     (declare (type fixnum y))
 | 
|---|
| 681 |     (cond
 | 
|---|
| 682 |       ((minusp y) (error "universal-expt: Negative exponent."))
 | 
|---|
| 683 |       ((universal-zerop x) (if (zerop y) 1))
 | 
|---|
| 684 |       (t
 | 
|---|
| 685 |        (do ((k 1 (ash k 1))
 | 
|---|
| 686 |             (q x (multiply q q))        ;keep squaring
 | 
|---|
| 687 |             (p (make-unit-for x) (if (not (zerop (logand k y))) (multiply p q) p)))
 | 
|---|
| 688 |            ((> k y) p)
 | 
|---|
| 689 |          (declare (fixnum k)))))))
 | 
|---|
| 690 | 
 | 
|---|
| 691 | (defgeneric poly-p (object)
 | 
|---|
| 692 |   (:documentation "Checks if an object is a polynomial.")
 | 
|---|
| 693 |   (:method ((self poly)) t)
 | 
|---|
| 694 |   (:method ((self t)) nil))
 | 
|---|
| 695 | 
 | 
|---|
| 696 | (defmethod ->sexp :before ((self poly) &optional vars)
 | 
|---|
| 697 |   "Ensures that the number of variables in VARS maches the polynomial dimension of the
 | 
|---|
| 698 | polynomial SELF."
 | 
|---|
| 699 |   (unless (endp (poly-termlist self))
 | 
|---|
| 700 |     (let ((dimension (poly-dimension self)))
 | 
|---|
| 701 |       (assert (= (length vars) dimension)
 | 
|---|
| 702 |               nil
 | 
|---|
| 703 |               "Number of variables ~S does not match the dimension ~S"
 | 
|---|
| 704 |               vars dimension))))
 | 
|---|
| 705 | 
 | 
|---|
| 706 | (defmethod ->sexp ((self poly) &optional vars)
 | 
|---|
| 707 |   "Converts a polynomial SELF to a sexp."
 | 
|---|
| 708 |   (let ((m (mapcar #'(lambda (trm) (->sexp trm vars)) 
 | 
|---|
| 709 |                    (poly-termlist self))))
 | 
|---|
| 710 |     (cond ((endp m) 0)
 | 
|---|
| 711 |           ((endp (cdr m)) (car m))
 | 
|---|
| 712 |           (t (cons '+ m)))))
 | 
|---|
| 713 | 
 | 
|---|
| 714 | (defconstant +list-marker+ :[
 | 
|---|
| 715 |   "A sexp with this head is considered a list of polynomials.")
 | 
|---|
| 716 | 
 | 
|---|
| 717 | (defmethod ->sexp ((self cons) &optional vars)
 | 
|---|
| 718 |   (assert (eql (car self) +list-marker+))
 | 
|---|
| 719 |   (cons +list-marker+ (mapcar #'(lambda (p) (->sexp p vars)) (cdr self))))
 | 
|---|
| 720 | 
 | 
|---|
| 721 | (defmethod make-zero-for ((self poly))
 | 
|---|
| 722 |   (make-instance 'poly))
 | 
|---|
| 723 | 
 | 
|---|
| 724 | (defmethod make-unit-for ((self poly))
 | 
|---|
| 725 |   (make-poly-constant (poly-dimension self) 1))
 | 
|---|
| 726 | 
 | 
|---|
| 727 | (defgeneric poly-reverse (self)
 | 
|---|
| 728 |   (:documentation "Reverse the order of terms in a polynomial SELF.")
 | 
|---|
| 729 |   (:method ((self poly))
 | 
|---|
| 730 |     (with-slots (termlist)
 | 
|---|
| 731 |         self
 | 
|---|
| 732 |       (setf termlist (nreverse termlist)))
 | 
|---|
| 733 |     self))
 | 
|---|
| 734 | 
 | 
|---|
| 735 | 
 | 
|---|
| 736 |     
 | 
|---|