| 1 | ;;----------------------------------------------------------------
 | 
|---|
| 2 | ;; File:     polynomial.lisp
 | 
|---|
| 3 | ;;----------------------------------------------------------------
 | 
|---|
| 4 | ;;
 | 
|---|
| 5 | ;; Author:   Marek Rychlik (rychlik@u.arizona.edu)
 | 
|---|
| 6 | ;; Date:     Thu Aug 27 09:41:24 2015
 | 
|---|
| 7 | ;; Copying:  (C) Marek Rychlik, 2010. All rights reserved.
 | 
|---|
| 8 | ;;
 | 
|---|
| 9 | ;;----------------------------------------------------------------
 | 
|---|
| 10 | ;;; -*-  Mode: Lisp -*- 
 | 
|---|
| 11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
|---|
| 12 | ;;;                                                                              
 | 
|---|
| 13 | ;;;  Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>          
 | 
|---|
| 14 | ;;;                                                                              
 | 
|---|
| 15 | ;;;  This program is free software; you can redistribute it and/or modify        
 | 
|---|
| 16 | ;;;  it under the terms of the GNU General Public License as published by        
 | 
|---|
| 17 | ;;;  the Free Software Foundation; either version 2 of the License, or           
 | 
|---|
| 18 | ;;;  (at your option) any later version.                                         
 | 
|---|
| 19 | ;;;                                                                              
 | 
|---|
| 20 | ;;;  This program is distributed in the hope that it will be useful,             
 | 
|---|
| 21 | ;;;  but WITHOUT ANY WARRANTY; without even the implied warranty of              
 | 
|---|
| 22 | ;;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
 | 
|---|
| 23 | ;;;  GNU General Public License for more details.                                
 | 
|---|
| 24 | ;;;                                                                              
 | 
|---|
| 25 | ;;;  You should have received a copy of the GNU General Public License           
 | 
|---|
| 26 | ;;;  along with this program; if not, write to the Free Software                 
 | 
|---|
| 27 | ;;;  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
 | 
|---|
| 28 | ;;;                                                                              
 | 
|---|
| 29 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
|---|
| 30 | 
 | 
|---|
| 31 | (defpackage "POLYNOMIAL"
 | 
|---|
| 32 |   (:use :cl :utils :monom)
 | 
|---|
| 33 |   (:export "POLY"
 | 
|---|
| 34 |            "POLY-DIMENSION"
 | 
|---|
| 35 |            "POLY-TERMLIST"
 | 
|---|
| 36 |            "POLY-TERM-ORDER"
 | 
|---|
| 37 |            "CHANGE-TERM-ORDER"
 | 
|---|
| 38 |            "STANDARD-EXTENSION"
 | 
|---|
| 39 |            "STANDARD-EXTENSION-1"
 | 
|---|
| 40 |            "STANDARD-SUM"
 | 
|---|
| 41 |            "SATURATION-EXTENSION"
 | 
|---|
| 42 |            "ALIST->POLY")
 | 
|---|
| 43 |   (:documentation "Implements polynomials. A polynomial is essentially
 | 
|---|
| 44 | a mapping of monomials of the same degree to coefficients. The
 | 
|---|
| 45 | momomials are ordered according to a monomial order."))
 | 
|---|
| 46 | 
 | 
|---|
| 47 | (in-package :polynomial)
 | 
|---|
| 48 | 
 | 
|---|
| 49 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
 | 
|---|
| 50 | 
 | 
|---|
| 51 | (defclass poly ()
 | 
|---|
| 52 |   ((dimension :initform nil 
 | 
|---|
| 53 |               :initarg :dimension 
 | 
|---|
| 54 |               :accessor poly-dimension
 | 
|---|
| 55 |               :documentation "Shared dimension of all terms, the number of variables")
 | 
|---|
| 56 |    (termlist :initform nil :initarg :termlist :accessor poly-termlist
 | 
|---|
| 57 |              :documentation "List of terms.")
 | 
|---|
| 58 |    (order :initform #'lex> :initarg :order :accessor poly-term-order
 | 
|---|
| 59 |           :documentation "Monomial/term order."))
 | 
|---|
| 60 |   (:default-initargs :dimension nil :termlist nil :order #'lex>)
 | 
|---|
| 61 |   (:documentation "A polynomial with a list of terms TERMLIST, ordered
 | 
|---|
| 62 | according to term order ORDER, which defaults to LEX>."))
 | 
|---|
| 63 | 
 | 
|---|
| 64 | (defmethod print-object ((self poly) stream)
 | 
|---|
| 65 |   (print-unreadable-object (self stream :type t :identity t)
 | 
|---|
| 66 |     (with-accessors ((dimension poly-dimension)
 | 
|---|
| 67 |                      (termlist poly-termlist)
 | 
|---|
| 68 |                      (order poly-term-order))
 | 
|---|
| 69 |         self
 | 
|---|
| 70 |       (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A" 
 | 
|---|
| 71 |               dimension termlist order))))
 | 
|---|
| 72 | 
 | 
|---|
| 73 | (defgeneric change-term-order (self other)
 | 
|---|
| 74 |   (:documentation "Change term order of SELF to the term order of OTHER.")
 | 
|---|
| 75 |   (:method ((self poly) (other poly))
 | 
|---|
| 76 |     (unless (eq (poly-term-order self) (poly-term-order other))
 | 
|---|
| 77 |       (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
 | 
|---|
| 78 |             (poly-term-order self) (poly-term-order other)))
 | 
|---|
| 79 |     self))
 | 
|---|
| 80 | 
 | 
|---|
| 81 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
 | 
|---|
| 82 |   "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
 | 
|---|
| 83 | It can be used to enter simple polynomials by hand, e.g the polynomial
 | 
|---|
| 84 | in two variables, X and Y, given in standard notation as:
 | 
|---|
| 85 | 
 | 
|---|
| 86 |       3*X^2*Y^3+2*Y+7
 | 
|---|
| 87 | 
 | 
|---|
| 88 | can be entered as
 | 
|---|
| 89 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
 | 
|---|
| 90 | 
 | 
|---|
| 91 | NOTE: The primary use is for low-level debugging of the package."
 | 
|---|
| 92 |   (dolist (x alist poly)
 | 
|---|
| 93 |     (poly-insert-term poly (make-instance 'monom :exponents (car x)) (cdr x))))
 | 
|---|
| 94 | 
 | 
|---|
| 95 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
 | 
|---|
| 96 |   "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
 | 
|---|
| 97 |   (reinitialize-instance new
 | 
|---|
| 98 |                          :dimension (monom-dimension old)
 | 
|---|
| 99 |                          :termlist (list (cons monom 1))))
 | 
|---|
| 100 |   
 | 
|---|
| 101 | (defmethod r-equalp ((self poly) (other poly))
 | 
|---|
| 102 |   "POLY instances are R-EQUALP if they have the same
 | 
|---|
| 103 | order and if all terms are R-EQUALP."
 | 
|---|
| 104 |   (and (every #'r-equalp (poly-termlist self) (poly-termlist other))
 | 
|---|
| 105 |        (eq (poly-term-order self) (poly-term-order other))))
 | 
|---|
| 106 | 
 | 
|---|
| 107 | (defgeneric poly-insert-term (self monom coeff)
 | 
|---|
| 108 |   (:method ((self poly) (monom monom) coeff)
 | 
|---|
| 109 |     (cond ((null (poly-dimension self))
 | 
|---|
| 110 |            (setf (poly-dimension self) (monom-dimension monom)))
 | 
|---|
| 111 |           (t (assert (= (poly-dimension self) (monom-dimension monom)))))
 | 
|---|
| 112 |     (push (cons monom coeff) (poly-termlist self))
 | 
|---|
| 113 |     self))
 | 
|---|
| 114 | 
 | 
|---|
| 115 | (defgeneric poly-append-term (self monom coeff)
 | 
|---|
| 116 |   (:method ((self poly) (monom monom) coeff)
 | 
|---|
| 117 |     (cond ((null (poly-dimension self))
 | 
|---|
| 118 |            (setf (poly-dimension self) (monom-dimension monom)))
 | 
|---|
| 119 |           (t (assert (= (poly-dimension self) (monom-dimension monom)))))
 | 
|---|
| 120 |     (setf (cdr (last (poly-termlist self))) (list (cons monom coeff)))
 | 
|---|
| 121 |     self))
 | 
|---|
| 122 | 
 | 
|---|
| 123 | ;; Leading term
 | 
|---|
| 124 | (defgeneric leading-term (object)
 | 
|---|
| 125 |   (:method ((self poly)) 
 | 
|---|
| 126 |     (car (poly-termlist self)))
 | 
|---|
| 127 |   (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
 | 
|---|
| 128 | 
 | 
|---|
| 129 | ;; Second term
 | 
|---|
| 130 | (defgeneric second-leading-term (object) 
 | 
|---|
| 131 |   (:method ((self poly))
 | 
|---|
| 132 |     (cadar (poly-termlist self)))
 | 
|---|
| 133 |   (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
 | 
|---|
| 134 | 
 | 
|---|
| 135 | ;; Leading coefficient
 | 
|---|
| 136 | (defgeneric leading-coefficient (object)
 | 
|---|
| 137 |   (:method ((self poly))
 | 
|---|
| 138 |     (scalar-coeff (leading-term self)))
 | 
|---|
| 139 |   (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
 | 
|---|
| 140 | 
 | 
|---|
| 141 | ;; Second coefficient
 | 
|---|
| 142 | (defgeneric second-leading-coefficient (object)
 | 
|---|
| 143 |   (:method ((self poly)) 
 | 
|---|
| 144 |     (scalar-coeff (second-leading-term self)))
 | 
|---|
| 145 |   (:documentation "The second leading coefficient of a polynomial. It
 | 
|---|
| 146 |   signals error for a polynomial with at most one term."))
 | 
|---|
| 147 | 
 | 
|---|
| 148 | ;; Testing for a zero polynomial
 | 
|---|
| 149 | (defmethod r-zerop ((self poly))
 | 
|---|
| 150 |   (null (poly-termlist self)))
 | 
|---|
| 151 | 
 | 
|---|
| 152 | ;; The number of terms
 | 
|---|
| 153 | (defmethod r-length ((self poly))
 | 
|---|
| 154 |   (length (poly-termlist self)))
 | 
|---|
| 155 | 
 | 
|---|
| 156 | (defmethod multiply-by ((self poly) (other monom))
 | 
|---|
| 157 |   (mapc #'(lambda (term) (multiply-by term other))
 | 
|---|
| 158 |         (poly-termlist self))
 | 
|---|
| 159 |   self)
 | 
|---|
| 160 | 
 | 
|---|
| 161 | (defmethod multiply-by ((self poly) other)
 | 
|---|
| 162 |   (mapc #'(lambda (term) (multiply-by term other))
 | 
|---|
| 163 |         (poly-termlist self))
 | 
|---|
| 164 |   self)
 | 
|---|
| 165 | 
 | 
|---|
| 166 | 
 | 
|---|
| 167 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
 | 
|---|
| 168 |   "Return an expression which will efficiently adds/subtracts two
 | 
|---|
| 169 | polynomials, P and Q.  The addition/subtraction of coefficients is
 | 
|---|
| 170 | performed by calling ADD/SUBTRACT-METHOD-NAME.  If UMINUS-METHOD-NAME
 | 
|---|
| 171 | is supplied, it is used to negate the coefficients of Q which do not
 | 
|---|
| 172 | have a corresponding coefficient in P. The code implements an
 | 
|---|
| 173 | efficient algorithm to add two polynomials represented as sorted lists
 | 
|---|
| 174 | of terms. The code destroys both arguments, reusing the terms to build
 | 
|---|
| 175 | the result."
 | 
|---|
| 176 |   `(macrolet ((lc (x) `(scalar-coeff (car ,x))))
 | 
|---|
| 177 |      (do ((p ,p)
 | 
|---|
| 178 |           (q ,q)
 | 
|---|
| 179 |           r)
 | 
|---|
| 180 |          ((or (endp p) (endp q))
 | 
|---|
| 181 |           ;; NOTE: R contains the result in reverse order. Can it
 | 
|---|
| 182 |           ;; be more efficient to produce the terms in correct order?
 | 
|---|
| 183 |           (unless (endp q) 
 | 
|---|
| 184 |             ;; Upon subtraction, we must change the sign of
 | 
|---|
| 185 |             ;; all coefficients in q
 | 
|---|
| 186 |             ,@(when uminus-fn
 | 
|---|
| 187 |                     `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
 | 
|---|
| 188 |             (setf r (nreconc r q)))
 | 
|---|
| 189 |           r)
 | 
|---|
| 190 |        (multiple-value-bind 
 | 
|---|
| 191 |              (greater-p equal-p)
 | 
|---|
| 192 |            (funcall ,order-fn (car p) (car q))
 | 
|---|
| 193 |          (cond
 | 
|---|
| 194 |            (greater-p
 | 
|---|
| 195 |             (rotatef (cdr p) r p)
 | 
|---|
| 196 |             )
 | 
|---|
| 197 |            (equal-p
 | 
|---|
| 198 |             (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
 | 
|---|
| 199 |               (cond 
 | 
|---|
| 200 |                 ((r-zerop s)
 | 
|---|
| 201 |                  (setf p (cdr p))
 | 
|---|
| 202 |                  )
 | 
|---|
| 203 |                 (t 
 | 
|---|
| 204 |                  (setf (lc p) s)
 | 
|---|
| 205 |                  (rotatef (cdr p) r p))))
 | 
|---|
| 206 |             (setf q (cdr q))
 | 
|---|
| 207 |             )
 | 
|---|
| 208 |            (t 
 | 
|---|
| 209 |             ;;Negate the term of Q if UMINUS provided, signallig
 | 
|---|
| 210 |             ;;that we are doing subtraction
 | 
|---|
| 211 |             ,(when uminus-fn
 | 
|---|
| 212 |                    `(setf (lc q) (funcall ,uminus-fn (lc q))))
 | 
|---|
| 213 |             (rotatef (cdr q) r q)))))))
 | 
|---|
| 214 | 
 | 
|---|
| 215 | 
 | 
|---|
| 216 | (defmacro def-add/subtract-method (add/subtract-method-name
 | 
|---|
| 217 |                                    uminus-method-name
 | 
|---|
| 218 |                                    &optional
 | 
|---|
| 219 |                                      (doc-string nil doc-string-supplied-p))
 | 
|---|
| 220 |   "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
 | 
|---|
| 221 |   `(defmethod ,add/subtract-method-name ((self poly) (other poly))
 | 
|---|
| 222 |      ,@(when doc-string-supplied-p `(,doc-string))
 | 
|---|
| 223 |      ;; Ensure orders are compatible
 | 
|---|
| 224 |      (change-term-order other self)
 | 
|---|
| 225 |      (setf (poly-termlist self) (fast-add/subtract 
 | 
|---|
| 226 |                                  (poly-termlist self) (poly-termlist other)
 | 
|---|
| 227 |                                  (poly-term-order self)
 | 
|---|
| 228 |                                  #',add/subtract-method-name
 | 
|---|
| 229 |                                  ,(when uminus-method-name `(function ,uminus-method-name))))
 | 
|---|
| 230 |      self))
 | 
|---|
| 231 | 
 | 
|---|
| 232 | (eval-when (:compile-toplevel :load-toplevel :execute)
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   (def-add/subtract-method add-to nil
 | 
|---|
| 235 |     "Adds to polynomial SELF another polynomial OTHER.
 | 
|---|
| 236 | This operation destructively modifies both polynomials.
 | 
|---|
| 237 | The result is stored in SELF. This implementation does
 | 
|---|
| 238 | no consing, entirely reusing the sells of SELF and OTHER.")
 | 
|---|
| 239 | 
 | 
|---|
| 240 |   (def-add/subtract-method subtract-from unary-minus
 | 
|---|
| 241 |     "Subtracts from polynomial SELF another polynomial OTHER.
 | 
|---|
| 242 | This operation destructively modifies both polynomials.
 | 
|---|
| 243 | The result is stored in SELF. This implementation does
 | 
|---|
| 244 | no consing, entirely reusing the sells of SELF and OTHER.")
 | 
|---|
| 245 |   )
 | 
|---|
| 246 | 
 | 
|---|
| 247 | (defmethod unary-minus ((self poly))
 | 
|---|
| 248 |   "Destructively modifies the coefficients of the polynomial SELF,
 | 
|---|
| 249 | by changing their sign."
 | 
|---|
| 250 |   (mapc #'unary-minus (poly-termlist self))
 | 
|---|
| 251 |   self)
 | 
|---|
| 252 | 
 | 
|---|
| 253 | (defun add-termlists (p q order-fn)
 | 
|---|
| 254 |   "Destructively adds two termlists P and Q ordered according to ORDER-FN."
 | 
|---|
| 255 |   (fast-add/subtract p q order-fn #'add-to nil))
 | 
|---|
| 256 | 
 | 
|---|
| 257 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist 
 | 
|---|
| 258 |                                                     &optional (reverse-arg-order-P nil))
 | 
|---|
| 259 |   "Multiplies term TERM by a list of term, TERMLIST.
 | 
|---|
| 260 | Takes into accound divisors of zero in the ring, by
 | 
|---|
| 261 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
 | 
|---|
| 262 | is T, change the order of arguments; this may be important
 | 
|---|
| 263 | if we extend the package to non-commutative rings."
 | 
|---|
| 264 |   `(mapcan #'(lambda (other-term) 
 | 
|---|
| 265 |                (let ((prod (r*
 | 
|---|
| 266 |                             ,@(cond 
 | 
|---|
| 267 |                                (reverse-arg-order-p
 | 
|---|
| 268 |                                 `(other-term ,term))
 | 
|---|
| 269 |                                (t 
 | 
|---|
| 270 |                                 `(,term other-term))))))
 | 
|---|
| 271 |                  (cond 
 | 
|---|
| 272 |                    ((r-zerop prod) nil)
 | 
|---|
| 273 |                    (t (list prod)))))
 | 
|---|
| 274 |            ,termlist))
 | 
|---|
| 275 | 
 | 
|---|
| 276 | (defun multiply-termlists (p q order-fn)
 | 
|---|
| 277 |   "A version of polynomial multiplication, operating
 | 
|---|
| 278 | directly on termlists."
 | 
|---|
| 279 |   (cond 
 | 
|---|
| 280 |     ((or (endp p) (endp q))         
 | 
|---|
| 281 |      ;;p or q is 0 (represented by NIL)
 | 
|---|
| 282 |      nil)       
 | 
|---|
| 283 |     ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
 | 
|---|
| 284 |     ((endp (cdr p))
 | 
|---|
| 285 |      (multiply-term-by-termlist-dropping-zeros (car p) q))
 | 
|---|
| 286 |     ((endp (cdr q))
 | 
|---|
| 287 |      (multiply-term-by-termlist-dropping-zeros (car q) p t))
 | 
|---|
| 288 |     (t
 | 
|---|
| 289 |      (cons (r* (car p) (car q))
 | 
|---|
| 290 |            (add-termlists 
 | 
|---|
| 291 |             (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
 | 
|---|
| 292 |             (multiply-termlists (cdr p) q order-fn)
 | 
|---|
| 293 |             order-fn)))))
 | 
|---|
| 294 | 
 | 
|---|
| 295 | (defmethod multiply-by ((self poly) (other poly))
 | 
|---|
| 296 |   (change-term-order other self)
 | 
|---|
| 297 |   (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
 | 
|---|
| 298 |                                                  (poly-termlist other)
 | 
|---|
| 299 |                                                  (poly-term-order self)))
 | 
|---|
| 300 |   self)
 | 
|---|
| 301 | 
 | 
|---|
| 302 | (defmethod r+ ((poly1 poly) poly2)
 | 
|---|
| 303 |   "Non-destructively add POLY1 by POLY2."
 | 
|---|
| 304 |   (add-to (copy-instance POLY1) (change-class (copy-instance POLY2) 'poly)))
 | 
|---|
| 305 | 
 | 
|---|
| 306 | (defmethod r- ((minuend poly) &rest subtrahends)
 | 
|---|
| 307 |   "Non-destructively subtract MINUEND and SUBTRAHENDS."
 | 
|---|
| 308 |   (subtract-from (copy-instance minuend) 
 | 
|---|
| 309 |                  (change-class (reduce #'r+ subtrahends) 'poly)))
 | 
|---|
| 310 | 
 | 
|---|
| 311 | (defmethod r+ ((poly1 monom) poly2)
 | 
|---|
| 312 |   "Non-destructively add POLY1 by POLY2."
 | 
|---|
| 313 |   (add-to (change-class (copy-instance poly1) 'poly)
 | 
|---|
| 314 |           (change-class (copy-instance poly2) 'poly)))
 | 
|---|
| 315 | 
 | 
|---|
| 316 | (defmethod r- ((minuend monom) &rest subtrahends)
 | 
|---|
| 317 |   "Non-destructively subtract MINUEND and SUBTRAHENDS."
 | 
|---|
| 318 |   (subtract-from (change-class (copy-instance minuend) 'poly)
 | 
|---|
| 319 |                  (change-class (reduce #'r+ subtrahends) 'poly)))
 | 
|---|
| 320 | 
 | 
|---|
| 321 | (defmethod r* ((poly1 poly) (poly2 poly))
 | 
|---|
| 322 |   "Non-destructively multiply POLY1 by POLY2."
 | 
|---|
| 323 |   (multiply-by (copy-instance poly1) (copy-instance poly2)))
 | 
|---|
| 324 | 
 | 
|---|
| 325 | (defmethod left-tensor-product-by ((self poly) (other monom))
 | 
|---|
| 326 |   (setf (poly-termlist self) 
 | 
|---|
| 327 |         (mapcan #'(lambda (term) 
 | 
|---|
| 328 |                     (let ((prod (left-tensor-product-by term other)))
 | 
|---|
| 329 |                       (cond 
 | 
|---|
| 330 |                         ((r-zerop prod) nil)
 | 
|---|
| 331 |                         (t (list prod)))))
 | 
|---|
| 332 |                 (poly-termlist self)))
 | 
|---|
| 333 |   (incf (poly-dimension self) (monom-dimension other))
 | 
|---|
| 334 |   self)
 | 
|---|
| 335 | 
 | 
|---|
| 336 | (defmethod right-tensor-product-by ((self poly) (other monom))
 | 
|---|
| 337 |   (setf (poly-termlist self) 
 | 
|---|
| 338 |         (mapcan #'(lambda (term) 
 | 
|---|
| 339 |                     (let ((prod (right-tensor-product-by term other)))
 | 
|---|
| 340 |                       (cond 
 | 
|---|
| 341 |                         ((r-zerop prod) nil)
 | 
|---|
| 342 |                         (t (list prod)))))
 | 
|---|
| 343 |                 (poly-termlist self)))
 | 
|---|
| 344 |   (incf (poly-dimension self) (monom-dimension other))
 | 
|---|
| 345 |   self)
 | 
|---|
| 346 | 
 | 
|---|
| 347 | 
 | 
|---|
| 348 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
 | 
|---|
| 349 |   "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
 | 
|---|
| 350 | is a list of polynomials. Destructively modifies PLIST elements."
 | 
|---|
| 351 |   (mapc #'(lambda (poly)
 | 
|---|
| 352 |             (left-tensor-product-by 
 | 
|---|
| 353 |              poly 
 | 
|---|
| 354 |              (prog1 
 | 
|---|
| 355 |                  (make-monom-variable k i) 
 | 
|---|
| 356 |                (incf i))))
 | 
|---|
| 357 |         plist))
 | 
|---|
| 358 | 
 | 
|---|
| 359 | (defun standard-extension-1 (plist 
 | 
|---|
| 360 |                              &aux 
 | 
|---|
| 361 |                                (plist (standard-extension plist))
 | 
|---|
| 362 |                                (nvars (poly-dimension (car plist))))
 | 
|---|
| 363 |   "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
 | 
|---|
| 364 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
 | 
|---|
| 365 | polynomial.  Subsequently, P1, P2, ..., PK are destructively modified
 | 
|---|
| 366 | tantamount to replacing PI with UI*PI-1. It assumes that all
 | 
|---|
| 367 | polynomials have the same dimension, and only the first polynomial
 | 
|---|
| 368 | is examined to determine this dimension."
 | 
|---|
| 369 |   ;; Implementation note: we use STANDARD-EXTENSION and then subtract
 | 
|---|
| 370 |   ;; 1 from each polynomial; since UI*PI has no constant term,
 | 
|---|
| 371 |   ;; we just need to append the constant term at the end
 | 
|---|
| 372 |   ;; of each termlist.
 | 
|---|
| 373 |   (flet ((subtract-1 (p) 
 | 
|---|
| 374 |            (poly-append-term p (make-instance 'monom :dimension nvars) -1)))
 | 
|---|
| 375 |     (setf plist (mapc #'subtract-1 plist)))
 | 
|---|
| 376 |   plist)
 | 
|---|
| 377 | 
 | 
|---|
| 378 | 
 | 
|---|
| 379 | (defun standard-sum (plist
 | 
|---|
| 380 |                      &aux 
 | 
|---|
| 381 |                        (plist (standard-extension plist))
 | 
|---|
| 382 |                        (nvars (poly-dimension (car plist))))
 | 
|---|
| 383 |   "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
 | 
|---|
| 384 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
 | 
|---|
| 385 | polynomial.  Subsequently, P1, P2, ..., PK are destructively modified
 | 
|---|
| 386 | tantamount to replacing PI with UI*PI, and the resulting polynomials
 | 
|---|
| 387 | are added. Finally, 1 is subtracted.  It should be noted that the term
 | 
|---|
| 388 | order is not modified, which is equivalent to using a lexicographic
 | 
|---|
| 389 | order on the first K variables."
 | 
|---|
| 390 |   (flet ((subtract-1 (p) 
 | 
|---|
| 391 |            (poly-append-term p (make-instance 'monom :dimension nvars) -1)))
 | 
|---|
| 392 |     (subtract-1 
 | 
|---|
| 393 |      (make-instance
 | 
|---|
| 394 |       'poly
 | 
|---|
| 395 |       :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
 | 
|---|
| 396 | 
 | 
|---|
| 397 | #|
 | 
|---|
| 398 | 
 | 
|---|
| 399 | (defun saturation-extension-1 (ring f p) 
 | 
|---|
| 400 |   "Calculate [F, U*P-1]. It destructively modifies F."
 | 
|---|
| 401 |   (declare (type ring ring))
 | 
|---|
| 402 |   (polysaturation-extension ring f (list p)))
 | 
|---|
| 403 | 
 | 
|---|
| 404 | 
 | 
|---|
| 405 | 
 | 
|---|
| 406 | 
 | 
|---|
| 407 | (defun spoly (ring-and-order f g
 | 
|---|
| 408 |               &aux
 | 
|---|
| 409 |                 (ring (ro-ring ring-and-order)))
 | 
|---|
| 410 |   "It yields the S-polynomial of polynomials F and G."
 | 
|---|
| 411 |   (declare (type ring-and-order ring-and-order) (type poly f g))
 | 
|---|
| 412 |   (let* ((lcm (monom-lcm (poly-lm f) (poly-lm g)))
 | 
|---|
| 413 |          (mf (monom-div lcm (poly-lm f)))
 | 
|---|
| 414 |          (mg (monom-div lcm (poly-lm g))))
 | 
|---|
| 415 |     (declare (type monom mf mg))
 | 
|---|
| 416 |     (multiple-value-bind (c cf cg)
 | 
|---|
| 417 |         (funcall (ring-ezgcd ring) (poly-lc f) (poly-lc g))
 | 
|---|
| 418 |       (declare (ignore c))
 | 
|---|
| 419 |       (poly-sub 
 | 
|---|
| 420 |        ring-and-order
 | 
|---|
| 421 |        (scalar-times-poly ring cg (monom-times-poly mf f))
 | 
|---|
| 422 |        (scalar-times-poly ring cf (monom-times-poly mg g))))))
 | 
|---|
| 423 | 
 | 
|---|
| 424 | 
 | 
|---|
| 425 | (defun poly-primitive-part (ring p)
 | 
|---|
| 426 |   "Divide polynomial P with integer coefficients by gcd of its
 | 
|---|
| 427 | coefficients and return the result."
 | 
|---|
| 428 |   (declare (type ring ring) (type poly p))
 | 
|---|
| 429 |   (if (poly-zerop p)
 | 
|---|
| 430 |       (values p 1)
 | 
|---|
| 431 |       (let ((c (poly-content ring p)))
 | 
|---|
| 432 |         (values (make-poly-from-termlist 
 | 
|---|
| 433 |                  (mapcar
 | 
|---|
| 434 |                   #'(lambda (x)
 | 
|---|
| 435 |                       (make-term :monom (term-monom x)
 | 
|---|
| 436 |                                  :coeff (funcall (ring-div ring) (term-coeff x) c)))
 | 
|---|
| 437 |                   (poly-termlist p))
 | 
|---|
| 438 |                  (poly-sugar p))
 | 
|---|
| 439 |                 c))))
 | 
|---|
| 440 | 
 | 
|---|
| 441 | (defun poly-content (ring p)
 | 
|---|
| 442 |   "Greatest common divisor of the coefficients of the polynomial P. Use the RING structure
 | 
|---|
| 443 | to compute the greatest common divisor."
 | 
|---|
| 444 |   (declare (type ring ring) (type poly p))
 | 
|---|
| 445 |   (reduce (ring-gcd ring) (mapcar #'term-coeff (rest (poly-termlist p))) :initial-value (poly-lc p)))
 | 
|---|
| 446 | 
 | 
|---|
| 447 | |#
 | 
|---|