| [3400] | 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 | ;;----------------------------------------------------------------
|
|---|
| [1201] | 10 | ;;; -*- Mode: Lisp -*-
|
|---|
| [77] | 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 |
|
|---|
| [431] | 31 | (defpackage "POLYNOMIAL"
|
|---|
| [3643] | 32 | (:use :cl :utils :monom :copy)
|
|---|
| [2596] | 33 | (:export "POLY"
|
|---|
| [3270] | 34 | "POLY-DIMENSION"
|
|---|
| [2596] | 35 | "POLY-TERMLIST"
|
|---|
| [3016] | 36 | "POLY-TERM-ORDER"
|
|---|
| [3509] | 37 | "POLY-INSERT-TERM"
|
|---|
| [3642] | 38 | "LEADING-TERM"
|
|---|
| [3657] | 39 | "LEADING-MONOMIAL"
|
|---|
| [3642] | 40 | "LEADING-COEFFICIENT"
|
|---|
| [3657] | 41 | "SECOND-LEADING-TERM"
|
|---|
| 42 | "SECOND-LEADING-MONOMIAL"
|
|---|
| 43 | "SECOND-LEADING-COEFFICIENT"
|
|---|
| [3642] | 44 | "ADD-TO"
|
|---|
| [3646] | 45 | "ADD"
|
|---|
| [3642] | 46 | "SUBTRACT-FROM"
|
|---|
| [3646] | 47 | "SUBTRACT"
|
|---|
| [3071] | 48 | "CHANGE-TERM-ORDER"
|
|---|
| [3099] | 49 | "STANDARD-EXTENSION"
|
|---|
| [3101] | 50 | "STANDARD-EXTENSION-1"
|
|---|
| [3109] | 51 | "STANDARD-SUM"
|
|---|
| [3094] | 52 | "SATURATION-EXTENSION"
|
|---|
| [3655] | 53 | "ALIST->POLY"
|
|---|
| 54 | "UNIVERSAL-EZGCD"
|
|---|
| [3678] | 55 | "S-POLYNOMIAL"
|
|---|
| 56 | "POLY-CONTENT")
|
|---|
| [3489] | 57 | (:documentation "Implements polynomials. A polynomial is essentially
|
|---|
| 58 | a mapping of monomials of the same degree to coefficients. The
|
|---|
| 59 | momomials are ordered according to a monomial order."))
|
|---|
| [143] | 60 |
|
|---|
| [431] | 61 | (in-package :polynomial)
|
|---|
| 62 |
|
|---|
| [1927] | 63 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
|
|---|
| [52] | 64 |
|
|---|
| [2442] | 65 | (defclass poly ()
|
|---|
| [3253] | 66 | ((dimension :initform nil
|
|---|
| [3250] | 67 | :initarg :dimension
|
|---|
| 68 | :accessor poly-dimension
|
|---|
| [3242] | 69 | :documentation "Shared dimension of all terms, the number of variables")
|
|---|
| [3250] | 70 | (termlist :initform nil :initarg :termlist :accessor poly-termlist
|
|---|
| [3619] | 71 | :documentation "List of terms.")
|
|---|
| [3250] | 72 | (order :initform #'lex> :initarg :order :accessor poly-term-order
|
|---|
| [2697] | 73 | :documentation "Monomial/term order."))
|
|---|
| [3262] | 74 | (:default-initargs :dimension nil :termlist nil :order #'lex>)
|
|---|
| [2695] | 75 | (:documentation "A polynomial with a list of terms TERMLIST, ordered
|
|---|
| [2696] | 76 | according to term order ORDER, which defaults to LEX>."))
|
|---|
| [2442] | 77 |
|
|---|
| [2471] | 78 | (defmethod print-object ((self poly) stream)
|
|---|
| [3241] | 79 | (print-unreadable-object (self stream :type t :identity t)
|
|---|
| [3243] | 80 | (with-accessors ((dimension poly-dimension)
|
|---|
| 81 | (termlist poly-termlist)
|
|---|
| 82 | (order poly-term-order))
|
|---|
| [3237] | 83 | self
|
|---|
| [3244] | 84 | (format stream "DIMENSION=~A TERMLIST=~A ORDER=~A"
|
|---|
| 85 | dimension termlist order))))
|
|---|
| [2469] | 86 |
|
|---|
| [3015] | 87 | (defgeneric change-term-order (self other)
|
|---|
| [3012] | 88 | (:documentation "Change term order of SELF to the term order of OTHER.")
|
|---|
| [3010] | 89 | (:method ((self poly) (other poly))
|
|---|
| 90 | (unless (eq (poly-term-order self) (poly-term-order other))
|
|---|
| [3620] | 91 | (setf (poly-termlist self) (sort (poly-termlist self) (poly-term-order other))
|
|---|
| [3010] | 92 | (poly-term-order self) (poly-term-order other)))
|
|---|
| [3012] | 93 | self))
|
|---|
| [3010] | 94 |
|
|---|
| [3621] | 95 | (defgeneric poly-insert-term (self term)
|
|---|
| [3622] | 96 | (:documentation "Insert a term TERM into SELF before all other
|
|---|
| [3621] | 97 | terms. Order is not enforced.")
|
|---|
| 98 | (:method ((self poly) (term term))
|
|---|
| [3510] | 99 | (cond ((null (poly-dimension self))
|
|---|
| [3621] | 100 | (setf (poly-dimension self) (monom-dimension term)))
|
|---|
| 101 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
|---|
| 102 | (push term (poly-termlist self))
|
|---|
| [3510] | 103 | self))
|
|---|
| 104 |
|
|---|
| [3622] | 105 | (defgeneric poly-append-term (self term)
|
|---|
| 106 | (:documentation "Append a term TERM to SELF after all other terms. Order is not enforced.")
|
|---|
| 107 | (:method ((self poly) (term term))
|
|---|
| [3510] | 108 | (cond ((null (poly-dimension self))
|
|---|
| [3622] | 109 | (setf (poly-dimension self) (monom-dimension term)))
|
|---|
| 110 | (t (assert (= (poly-dimension self) (monom-dimension term)))))
|
|---|
| 111 | (setf (cdr (last (poly-termlist self))) (list term))
|
|---|
| [3510] | 112 | self))
|
|---|
| 113 |
|
|---|
| [3095] | 114 | (defun alist->poly (alist &aux (poly (make-instance 'poly)))
|
|---|
| [3126] | 115 | "It reads polynomial from an alist formatted as ( ... (exponents . coeff) ...).
|
|---|
| 116 | It can be used to enter simple polynomials by hand, e.g the polynomial
|
|---|
| 117 | in two variables, X and Y, given in standard notation as:
|
|---|
| 118 |
|
|---|
| 119 | 3*X^2*Y^3+2*Y+7
|
|---|
| 120 |
|
|---|
| 121 | can be entered as
|
|---|
| 122 | (ALIST->POLY '(((2 3) . 3) ((0 1) . 2) ((0 0) . 7))).
|
|---|
| 123 |
|
|---|
| 124 | NOTE: The primary use is for low-level debugging of the package."
|
|---|
| [3099] | 125 | (dolist (x alist poly)
|
|---|
| [3623] | 126 | (poly-insert-term poly (make-instance 'term :exponents (car x) :coeff (cdr x)))))
|
|---|
| [3092] | 127 |
|
|---|
| [3401] | 128 | (defmethod update-instance-for-different-class :after ((old monom) (new poly) &key)
|
|---|
| 129 | "Converts OLD of class MONOM to a NEW of class POLY, by making it into a 1-element TERMLIST."
|
|---|
| 130 | (reinitialize-instance new
|
|---|
| 131 | :dimension (monom-dimension old)
|
|---|
| [3511] | 132 | :termlist (list (cons old 1))))
|
|---|
| [3403] | 133 |
|
|---|
| [3624] | 134 | (defmethod universal-equalp ((self poly) (other poly))
|
|---|
| 135 | "Implements equality of polynomials."
|
|---|
| 136 | (and (eql (poly-dimension self) (poly-dimension other))
|
|---|
| 137 | (every #'universal-equalp (poly-termlist self) (poly-termlist other))
|
|---|
| 138 | (eq (poly-term-order self) (poly-term-order other))))
|
|---|
| [2650] | 139 |
|
|---|
| [3624] | 140 | (defgeneric leading-term (object)
|
|---|
| [2442] | 141 | (:method ((self poly))
|
|---|
| [2525] | 142 | (car (poly-termlist self)))
|
|---|
| 143 | (:documentation "The leading term of a polynomial, or NIL for zero polynomial."))
|
|---|
| [52] | 144 |
|
|---|
| [3625] | 145 | (defgeneric second-leading-term (object)
|
|---|
| [2442] | 146 | (:method ((self poly))
|
|---|
| [2525] | 147 | (cadar (poly-termlist self)))
|
|---|
| 148 | (:documentation "The second leading term of a polynomial, or NIL for a polynomial with at most one term."))
|
|---|
| [52] | 149 |
|
|---|
| [3656] | 150 | (defgeneric leading-monomial (object)
|
|---|
| 151 | (:method ((self poly))
|
|---|
| 152 | (change-class (copy-instance (leading-term self)) 'monom))
|
|---|
| 153 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
|---|
| 154 |
|
|---|
| 155 | (defgeneric second-leading-monomial (object)
|
|---|
| 156 | (:method ((self poly))
|
|---|
| 157 | (change-class (copy-instance (second-leading-term self)) 'monom))
|
|---|
| 158 | (:documentation "The leading monomial of a polynomial, or NIL for zero polynomial."))
|
|---|
| 159 |
|
|---|
| [3625] | 160 | (defgeneric leading-coefficient (object)
|
|---|
| [2442] | 161 | (:method ((self poly))
|
|---|
| [3642] | 162 | (term-coeff (leading-term self)))
|
|---|
| [2545] | 163 | (:documentation "The leading coefficient of a polynomial. It signals error for a zero polynomial."))
|
|---|
| [52] | 164 |
|
|---|
| [2442] | 165 | (defgeneric second-leading-coefficient (object)
|
|---|
| 166 | (:method ((self poly))
|
|---|
| [3645] | 167 | (term-coeff (second-leading-term self)))
|
|---|
| [2906] | 168 | (:documentation "The second leading coefficient of a polynomial. It
|
|---|
| 169 | signals error for a polynomial with at most one term."))
|
|---|
| [52] | 170 |
|
|---|
| [3629] | 171 | (defmethod universal-zerop ((self poly))
|
|---|
| 172 | "Return T iff SELF is a zero polynomial."
|
|---|
| [3639] | 173 | (null (poly-termlist self)))
|
|---|
| [52] | 174 |
|
|---|
| [3518] | 175 | (defgeneric poly-length (self)
|
|---|
| [3630] | 176 | (:documentation "Return the number of terms.")
|
|---|
| [3518] | 177 | (:method ((self poly))
|
|---|
| 178 | (length (poly-termlist self))))
|
|---|
| [52] | 179 |
|
|---|
| [3663] | 180 | (defmethod multiply-by ((self poly) (other monom))
|
|---|
| [3630] | 181 | "Multiply a polynomial SELF by OTHER."
|
|---|
| 182 | (mapc #'(lambda (term) (multiply-by term other))
|
|---|
| 183 | (poly-termlist self))
|
|---|
| 184 | self)
|
|---|
| [2469] | 185 |
|
|---|
| [3672] | 186 | (defmethod multiply-by ((self poly) (other term))
|
|---|
| 187 | "Multiply a polynomial SELF by OTHER."
|
|---|
| 188 | (mapc #'(lambda (term) (multiply-by term other))
|
|---|
| 189 | (poly-termlist self))
|
|---|
| 190 | self)
|
|---|
| 191 |
|
|---|
| [2761] | 192 | (defmacro fast-add/subtract (p q order-fn add/subtract-fn uminus-fn)
|
|---|
| [2755] | 193 | "Return an expression which will efficiently adds/subtracts two
|
|---|
| 194 | polynomials, P and Q. The addition/subtraction of coefficients is
|
|---|
| 195 | performed by calling ADD/SUBTRACT-METHOD-NAME. If UMINUS-METHOD-NAME
|
|---|
| 196 | is supplied, it is used to negate the coefficients of Q which do not
|
|---|
| [2756] | 197 | have a corresponding coefficient in P. The code implements an
|
|---|
| 198 | efficient algorithm to add two polynomials represented as sorted lists
|
|---|
| 199 | of terms. The code destroys both arguments, reusing the terms to build
|
|---|
| 200 | the result."
|
|---|
| [3631] | 201 | `(macrolet ((lc (x) `(term-coeff (car ,x))))
|
|---|
| [2742] | 202 | (do ((p ,p)
|
|---|
| 203 | (q ,q)
|
|---|
| 204 | r)
|
|---|
| 205 | ((or (endp p) (endp q))
|
|---|
| 206 | ;; NOTE: R contains the result in reverse order. Can it
|
|---|
| 207 | ;; be more efficient to produce the terms in correct order?
|
|---|
| [2774] | 208 | (unless (endp q)
|
|---|
| [2776] | 209 | ;; Upon subtraction, we must change the sign of
|
|---|
| 210 | ;; all coefficients in q
|
|---|
| [2774] | 211 | ,@(when uminus-fn
|
|---|
| [2775] | 212 | `((mapc #'(lambda (x) (setf x (funcall ,uminus-fn x))) q)))
|
|---|
| [2774] | 213 | (setf r (nreconc r q)))
|
|---|
| [2742] | 214 | r)
|
|---|
| 215 | (multiple-value-bind
|
|---|
| 216 | (greater-p equal-p)
|
|---|
| [3632] | 217 | (funcall ,order-fn (car p) (car q))
|
|---|
| [2742] | 218 | (cond
|
|---|
| 219 | (greater-p
|
|---|
| 220 | (rotatef (cdr p) r p)
|
|---|
| 221 | )
|
|---|
| 222 | (equal-p
|
|---|
| [2766] | 223 | (let ((s (funcall ,add/subtract-fn (lc p) (lc q))))
|
|---|
| [2742] | 224 | (cond
|
|---|
| [3640] | 225 | ((universal-zerop s)
|
|---|
| [2742] | 226 | (setf p (cdr p))
|
|---|
| 227 | )
|
|---|
| 228 | (t
|
|---|
| 229 | (setf (lc p) s)
|
|---|
| 230 | (rotatef (cdr p) r p))))
|
|---|
| 231 | (setf q (cdr q))
|
|---|
| 232 | )
|
|---|
| 233 | (t
|
|---|
| [2743] | 234 | ;;Negate the term of Q if UMINUS provided, signallig
|
|---|
| 235 | ;;that we are doing subtraction
|
|---|
| [2908] | 236 | ,(when uminus-fn
|
|---|
| 237 | `(setf (lc q) (funcall ,uminus-fn (lc q))))
|
|---|
| [2743] | 238 | (rotatef (cdr q) r q)))))))
|
|---|
| [2585] | 239 |
|
|---|
| [2655] | 240 |
|
|---|
| [3647] | 241 | (defgeneric add-to (self other)
|
|---|
| 242 | (:documentation "Add OTHER to SELF.")
|
|---|
| 243 | (:method ((self number) (other number))
|
|---|
| 244 | (+ self other)))
|
|---|
| 245 |
|
|---|
| 246 | (defgeneric subtract-from (self other)
|
|---|
| [3648] | 247 | (:documentation "Subtract OTHER from SELF.")
|
|---|
| 248 | (:method ((self number) (other number))
|
|---|
| 249 | (- self other)))
|
|---|
| [3647] | 250 |
|
|---|
| [2763] | 251 | (defmacro def-add/subtract-method (add/subtract-method-name
|
|---|
| [2752] | 252 | uminus-method-name
|
|---|
| 253 | &optional
|
|---|
| [2913] | 254 | (doc-string nil doc-string-supplied-p))
|
|---|
| [3647] | 255 | "This macro avoids code duplication for two similar operations: ADD-TO and SUBTRACT-FROM."
|
|---|
| [2749] | 256 | `(defmethod ,add/subtract-method-name ((self poly) (other poly))
|
|---|
| [2615] | 257 | ,@(when doc-string-supplied-p `(,doc-string))
|
|---|
| [2769] | 258 | ;; Ensure orders are compatible
|
|---|
| [3015] | 259 | (change-term-order other self)
|
|---|
| [2772] | 260 | (setf (poly-termlist self) (fast-add/subtract
|
|---|
| 261 | (poly-termlist self) (poly-termlist other)
|
|---|
| 262 | (poly-term-order self)
|
|---|
| 263 | #',add/subtract-method-name
|
|---|
| 264 | ,(when uminus-method-name `(function ,uminus-method-name))))
|
|---|
| [2609] | 265 | self))
|
|---|
| [2487] | 266 |
|
|---|
| [2916] | 267 | (eval-when (:compile-toplevel :load-toplevel :execute)
|
|---|
| [2777] | 268 |
|
|---|
| [3639] | 269 | (def-add/subtract-method add-to nil
|
|---|
| [2777] | 270 | "Adds to polynomial SELF another polynomial OTHER.
|
|---|
| [2610] | 271 | This operation destructively modifies both polynomials.
|
|---|
| 272 | The result is stored in SELF. This implementation does
|
|---|
| [2752] | 273 | no consing, entirely reusing the sells of SELF and OTHER.")
|
|---|
| [2609] | 274 |
|
|---|
| [3639] | 275 | (def-add/subtract-method subtract-from unary-minus
|
|---|
| [2753] | 276 | "Subtracts from polynomial SELF another polynomial OTHER.
|
|---|
| [2610] | 277 | This operation destructively modifies both polynomials.
|
|---|
| 278 | The result is stored in SELF. This implementation does
|
|---|
| [2752] | 279 | no consing, entirely reusing the sells of SELF and OTHER.")
|
|---|
| [2916] | 280 | )
|
|---|
| [2777] | 281 |
|
|---|
| [2691] | 282 | (defmethod unary-minus ((self poly))
|
|---|
| [2694] | 283 | "Destructively modifies the coefficients of the polynomial SELF,
|
|---|
| 284 | by changing their sign."
|
|---|
| [2692] | 285 | (mapc #'unary-minus (poly-termlist self))
|
|---|
| [2683] | 286 | self)
|
|---|
| [52] | 287 |
|
|---|
| [2795] | 288 | (defun add-termlists (p q order-fn)
|
|---|
| [2794] | 289 | "Destructively adds two termlists P and Q ordered according to ORDER-FN."
|
|---|
| [3639] | 290 | (fast-add/subtract p q order-fn #'add-to nil))
|
|---|
| [2794] | 291 |
|
|---|
| [2800] | 292 | (defmacro multiply-term-by-termlist-dropping-zeros (term termlist
|
|---|
| [2927] | 293 | &optional (reverse-arg-order-P nil))
|
|---|
| [2799] | 294 | "Multiplies term TERM by a list of term, TERMLIST.
|
|---|
| [2792] | 295 | Takes into accound divisors of zero in the ring, by
|
|---|
| [2927] | 296 | deleting zero terms. Optionally, if REVERSE-ARG-ORDER-P
|
|---|
| [2928] | 297 | is T, change the order of arguments; this may be important
|
|---|
| [2927] | 298 | if we extend the package to non-commutative rings."
|
|---|
| [2800] | 299 | `(mapcan #'(lambda (other-term)
|
|---|
| [3633] | 300 | (let ((prod (multiply
|
|---|
| [2923] | 301 | ,@(cond
|
|---|
| [2930] | 302 | (reverse-arg-order-p
|
|---|
| [2925] | 303 | `(other-term ,term))
|
|---|
| 304 | (t
|
|---|
| 305 | `(,term other-term))))))
|
|---|
| [2800] | 306 | (cond
|
|---|
| [3633] | 307 | ((universal-zerop prod) nil)
|
|---|
| [2800] | 308 | (t (list prod)))))
|
|---|
| 309 | ,termlist))
|
|---|
| [2790] | 310 |
|
|---|
| [2796] | 311 | (defun multiply-termlists (p q order-fn)
|
|---|
| [3127] | 312 | "A version of polynomial multiplication, operating
|
|---|
| 313 | directly on termlists."
|
|---|
| [2787] | 314 | (cond
|
|---|
| [2917] | 315 | ((or (endp p) (endp q))
|
|---|
| 316 | ;;p or q is 0 (represented by NIL)
|
|---|
| 317 | nil)
|
|---|
| [2789] | 318 | ;; If p= p0+p1 and q=q0+q1 then p*q=p0*q0+p0*q1+p1*q
|
|---|
| [2787] | 319 | ((endp (cdr p))
|
|---|
| [2918] | 320 | (multiply-term-by-termlist-dropping-zeros (car p) q))
|
|---|
| 321 | ((endp (cdr q))
|
|---|
| [2919] | 322 | (multiply-term-by-termlist-dropping-zeros (car q) p t))
|
|---|
| 323 | (t
|
|---|
| [3633] | 324 | (cons (multiply (car p) (car q))
|
|---|
| [2949] | 325 | (add-termlists
|
|---|
| 326 | (multiply-term-by-termlist-dropping-zeros (car p) (cdr q))
|
|---|
| 327 | (multiply-termlists (cdr p) q order-fn)
|
|---|
| 328 | order-fn)))))
|
|---|
| [2793] | 329 |
|
|---|
| [2803] | 330 | (defmethod multiply-by ((self poly) (other poly))
|
|---|
| [3014] | 331 | (change-term-order other self)
|
|---|
| [2803] | 332 | (setf (poly-termlist self) (multiply-termlists (poly-termlist self)
|
|---|
| 333 | (poly-termlist other)
|
|---|
| 334 | (poly-term-order self)))
|
|---|
| 335 | self)
|
|---|
| 336 |
|
|---|
| [3634] | 337 | (defun add (object1 object2)
|
|---|
| [3374] | 338 | "Non-destructively add POLY1 by POLY2."
|
|---|
| [3634] | 339 | (add-to (copy-instance object1) (copy-instance object2)))
|
|---|
| [3374] | 340 |
|
|---|
| [3634] | 341 | (defun subtract (minuend &rest subtrahends)
|
|---|
| [3427] | 342 | "Non-destructively subtract MINUEND and SUBTRAHENDS."
|
|---|
| [3634] | 343 | (subtract-from (copy-instance minuend) (reduce #'add subtrahends)))
|
|---|
| [3374] | 344 |
|
|---|
| [3062] | 345 | (defmethod left-tensor-product-by ((self poly) (other monom))
|
|---|
| 346 | (setf (poly-termlist self)
|
|---|
| 347 | (mapcan #'(lambda (term)
|
|---|
| 348 | (let ((prod (left-tensor-product-by term other)))
|
|---|
| 349 | (cond
|
|---|
| [3640] | 350 | ((universal-zerop prod) nil)
|
|---|
| [3062] | 351 | (t (list prod)))))
|
|---|
| 352 | (poly-termlist self)))
|
|---|
| [3249] | 353 | (incf (poly-dimension self) (monom-dimension other))
|
|---|
| [3062] | 354 | self)
|
|---|
| [3044] | 355 |
|
|---|
| [3062] | 356 | (defmethod right-tensor-product-by ((self poly) (other monom))
|
|---|
| 357 | (setf (poly-termlist self)
|
|---|
| 358 | (mapcan #'(lambda (term)
|
|---|
| 359 | (let ((prod (right-tensor-product-by term other)))
|
|---|
| 360 | (cond
|
|---|
| [3640] | 361 | ((universal-zerop prod) nil)
|
|---|
| [3062] | 362 | (t (list prod)))))
|
|---|
| 363 | (poly-termlist self)))
|
|---|
| [3249] | 364 | (incf (poly-dimension self) (monom-dimension other))
|
|---|
| [3062] | 365 | self)
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| [3084] | 368 | (defun standard-extension (plist &aux (k (length plist)) (i 0))
|
|---|
| [2716] | 369 | "Calculate [U1*P1,U2*P2,...,UK*PK], where PLIST=[P1,P2,...,PK]
|
|---|
| [3060] | 370 | is a list of polynomials. Destructively modifies PLIST elements."
|
|---|
| [3061] | 371 | (mapc #'(lambda (poly)
|
|---|
| [3085] | 372 | (left-tensor-product-by
|
|---|
| 373 | poly
|
|---|
| 374 | (prog1
|
|---|
| 375 | (make-monom-variable k i)
|
|---|
| 376 | (incf i))))
|
|---|
| [3061] | 377 | plist))
|
|---|
| [52] | 378 |
|
|---|
| [3087] | 379 | (defun standard-extension-1 (plist
|
|---|
| 380 | &aux
|
|---|
| [3096] | 381 | (plist (standard-extension plist))
|
|---|
| [3087] | 382 | (nvars (poly-dimension (car plist))))
|
|---|
| [3081] | 383 | "Calculate [U1*P1-1,U2*P2-1,...,UK*PK-1], where PLIST=[P1,P2,...,PK].
|
|---|
| [3087] | 384 | Firstly, new K variables U1, U2, ..., UK, are inserted into each
|
|---|
| 385 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
|---|
| [3105] | 386 | tantamount to replacing PI with UI*PI-1. It assumes that all
|
|---|
| [3106] | 387 | polynomials have the same dimension, and only the first polynomial
|
|---|
| 388 | is examined to determine this dimension."
|
|---|
| [3089] | 389 | ;; Implementation note: we use STANDARD-EXTENSION and then subtract
|
|---|
| 390 | ;; 1 from each polynomial; since UI*PI has no constant term,
|
|---|
| 391 | ;; we just need to append the constant term at the end
|
|---|
| 392 | ;; of each termlist.
|
|---|
| [3064] | 393 | (flet ((subtract-1 (p)
|
|---|
| [3641] | 394 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
|---|
| [3083] | 395 | (setf plist (mapc #'subtract-1 plist)))
|
|---|
| [3077] | 396 | plist)
|
|---|
| [52] | 397 |
|
|---|
| 398 |
|
|---|
| [3107] | 399 | (defun standard-sum (plist
|
|---|
| 400 | &aux
|
|---|
| 401 | (plist (standard-extension plist))
|
|---|
| 402 | (nvars (poly-dimension (car plist))))
|
|---|
| [3087] | 403 | "Calculate the polynomial U1*P1+U2*P2+...+UK*PK-1, where PLIST=[P1,P2,...,PK].
|
|---|
| 404 | Firstly, new K variables, U1, U2, ..., UK, are inserted into each
|
|---|
| 405 | polynomial. Subsequently, P1, P2, ..., PK are destructively modified
|
|---|
| 406 | tantamount to replacing PI with UI*PI, and the resulting polynomials
|
|---|
| [3117] | 407 | are added. Finally, 1 is subtracted. It should be noted that the term
|
|---|
| 408 | order is not modified, which is equivalent to using a lexicographic
|
|---|
| 409 | order on the first K variables."
|
|---|
| [3107] | 410 | (flet ((subtract-1 (p)
|
|---|
| [3641] | 411 | (poly-append-term p (make-instance 'term :dimension nvars :coeff -1))))
|
|---|
| [3108] | 412 | (subtract-1
|
|---|
| 413 | (make-instance
|
|---|
| 414 | 'poly
|
|---|
| [3115] | 415 | :termlist (apply #'nconc (mapcar #'poly-termlist plist))))))
|
|---|
| [52] | 416 |
|
|---|
| [3653] | 417 | (defgeneric universal-ezgcd (x y)
|
|---|
| 418 | (:documentation "Solves the diophantine system: X=C*X1, Y=C*X2,
|
|---|
| 419 | C=GCD(X,Y). It returns C, X1 and Y1. The result may be obtained by
|
|---|
| 420 | the Euclidean algorithm.")
|
|---|
| 421 | (:method ((x integer) (y integer)
|
|---|
| 422 | &aux (c (gcd x y)))
|
|---|
| 423 | (values c (/ x c) (/ y c)))
|
|---|
| 424 | )
|
|---|
| 425 |
|
|---|
| [3655] | 426 | (defgeneric s-polynomial (object1 object2)
|
|---|
| [3651] | 427 | (:documentation "Yields the S-polynomial of OBJECT1 and OBJECT2.")
|
|---|
| 428 | (:method ((f poly) (g poly))
|
|---|
| 429 | (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
|
|---|
| 430 | (mf (divide lcm (leading-monomial f)))
|
|---|
| 431 | (mg (divide lcm (leading-monomial g))))
|
|---|
| 432 | (multiple-value-bind (c cf cg)
|
|---|
| [3652] | 433 | (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
|
|---|
| [3651] | 434 | (declare (ignore c))
|
|---|
| 435 | (subtract
|
|---|
| [3673] | 436 | (multiply f (change-class mf 'term :coeff cg))
|
|---|
| 437 | (multiply g (change-class mg 'term :coeff cf)))))))
|
|---|
| [3651] | 438 |
|
|---|
| [3676] | 439 | (defgeneric poly-content (object)
|
|---|
| 440 | (:documentation "Greatest common divisor of the coefficients of the polynomial object OBJECT.")
|
|---|
| [3677] | 441 | (:method ((self poly))
|
|---|
| 442 | (reduce #'universal-gcd
|
|---|
| [3679] | 443 | (mapcar #'term-coeff (rest (poly-termlist self)))
|
|---|
| 444 | :initial-value (leading-coefficient self))))
|
|---|
| [3676] | 445 |
|
|---|
| [3682] | 446 | (defgeneric poly-primitive-part (self)
|
|---|
| 447 | (:documentation "Divide polynomial SELF by gcd of its
|
|---|
| 448 | coefficients. Return SELF.")
|
|---|
| 449 | (:method ((self poly))
|
|---|
| 450 | (if (universal-zerop object)
|
|---|
| 451 | (values object 1)
|
|---|
| 452 | (let ((c (poly-content object)))
|
|---|
| 453 | (mapc #'(lambda (x)
|
|---|
| 454 | (setf (term-coeff x) (divide (term-coeff x) c)))
|
|---|
| 455 | (poly-termlist p))))
|
|---|
| 456 | self))
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| [3122] | 459 | #|
|
|---|
| 460 |
|
|---|
| [1477] | 461 | (defun saturation-extension-1 (ring f p)
|
|---|
| [1497] | 462 | "Calculate [F, U*P-1]. It destructively modifies F."
|
|---|
| [1908] | 463 | (declare (type ring ring))
|
|---|
| [1477] | 464 | (polysaturation-extension ring f (list p)))
|
|---|
| [53] | 465 |
|
|---|
| [55] | 466 |
|
|---|
| [1066] | 467 |
|
|---|
| [2456] | 468 | |#
|
|---|