| 1 | ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
|
|---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 3 | ;;;
|
|---|
| 4 | ;;; Copyright (C) 1999, 2002, 2009 Marek Rychlik <rychlik@u.arizona.edu>
|
|---|
| 5 | ;;;
|
|---|
| 6 | ;;; This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | ;;; it under the terms of the GNU General Public License as published by
|
|---|
| 8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
|---|
| 9 | ;;; (at your option) any later version.
|
|---|
| 10 | ;;;
|
|---|
| 11 | ;;; This program is distributed in the hope that it will be useful,
|
|---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | ;;; GNU General Public License for more details.
|
|---|
| 15 | ;;;
|
|---|
| 16 | ;;; You should have received a copy of the GNU General Public License
|
|---|
| 17 | ;;; along with this program; if not, write to the Free Software
|
|---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 19 | ;;;
|
|---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 21 |
|
|---|
| 22 | (in-package :maxima)
|
|---|
| 23 |
|
|---|
| 24 | (macsyma-module cgb-maxima)
|
|---|
| 25 |
|
|---|
| 26 | (eval-when
|
|---|
| 27 | #+gcl (load eval)
|
|---|
| 28 | #-gcl (:load-toplevel :execute)
|
|---|
| 29 | (format t "~&Loading maxima-grobner ~a ~a~%"
|
|---|
| 30 | "$Revision: 2.0 $" "$Date: 2015/06/02 0:34:17 $"))
|
|---|
| 31 |
|
|---|
| 32 | ;;FUNCTS is loaded because it contains the definition of LCM
|
|---|
| 33 | ($load "functs")
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | |
|---|
| 39 |
|
|---|
| 40 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 41 | ;;
|
|---|
| 42 | ;; Global switches
|
|---|
| 43 | ;; (Can be used in Maxima just fine)
|
|---|
| 44 | ;;
|
|---|
| 45 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 46 |
|
|---|
| 47 | (defmvar $poly_monomial_order '$lex
|
|---|
| 48 | "This switch controls which monomial order is used in polynomial
|
|---|
| 49 | and Grobner basis calculations. If not set, LEX will be used")
|
|---|
| 50 |
|
|---|
| 51 | (defmvar $poly_coefficient_ring '$expression_ring
|
|---|
| 52 | "This switch indicates the coefficient ring of the polynomials
|
|---|
| 53 | that will be used in grobner calculations. If not set, Maxima's
|
|---|
| 54 | general expression ring will be used. This variable may be set
|
|---|
| 55 | to RING_OF_INTEGERS if desired.")
|
|---|
| 56 |
|
|---|
| 57 | (defmvar $poly_primary_elimination_order nil
|
|---|
| 58 | "Name of the default order for eliminated variables in elimination-based functions.
|
|---|
| 59 | If not set, LEX will be used.")
|
|---|
| 60 |
|
|---|
| 61 | (defmvar $poly_secondary_elimination_order nil
|
|---|
| 62 | "Name of the default order for kept variables in elimination-based functions.
|
|---|
| 63 | If not set, LEX will be used.")
|
|---|
| 64 |
|
|---|
| 65 | (defmvar $poly_elimination_order nil
|
|---|
| 66 | "Name of the default elimination order used in elimination calculations.
|
|---|
| 67 | If set, it overrides the settings in variables POLY_PRIMARY_ELIMINATION_ORDER
|
|---|
| 68 | and SECONDARY_ELIMINATION_ORDER. The user must ensure that this is a true
|
|---|
| 69 | elimination order valid for the number of eliminated variables.")
|
|---|
| 70 |
|
|---|
| 71 | (defmvar $poly_return_term_list nil
|
|---|
| 72 | "If set to T, all functions in this package will return each polynomial as a
|
|---|
| 73 | list of terms in the current monomial order rather than a Maxima general expression.")
|
|---|
| 74 |
|
|---|
| 75 | (defmvar $poly_grobner_debug nil
|
|---|
| 76 | "If set to TRUE, produce debugging and tracing output.")
|
|---|
| 77 |
|
|---|
| 78 | (defmvar $poly_grobner_algorithm '$buchberger
|
|---|
| 79 | "The name of the algorithm used to find grobner bases.")
|
|---|
| 80 |
|
|---|
| 81 | (defmvar $poly_top_reduction_only nil
|
|---|
| 82 | "If not FALSE, use top reduction only whenever possible.
|
|---|
| 83 | Top reduction means that division algorithm stops after the first reduction.")
|
|---|
| 84 |
|
|---|
| 85 | |
|---|
| 86 |
|
|---|
| 87 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 88 | ;;
|
|---|
| 89 | ;; Coefficient ring operations
|
|---|
| 90 | ;;
|
|---|
| 91 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 92 | ;;
|
|---|
| 93 | ;; These are ALL operations that are performed on the coefficients by
|
|---|
| 94 | ;; the package, and thus the coefficient ring can be changed by merely
|
|---|
| 95 | ;; redefining these operations.
|
|---|
| 96 | ;;
|
|---|
| 97 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 98 |
|
|---|
| 99 | (defstruct (ring)
|
|---|
| 100 | (parse #'identity :type function)
|
|---|
| 101 | (unit #'identity :type function)
|
|---|
| 102 | (zerop #'identity :type function)
|
|---|
| 103 | (add #'identity :type function)
|
|---|
| 104 | (sub #'identity :type function)
|
|---|
| 105 | (uminus #'identity :type function)
|
|---|
| 106 | (mul #'identity :type function)
|
|---|
| 107 | (div #'identity :type function)
|
|---|
| 108 | (lcm #'identity :type function)
|
|---|
| 109 | (ezgcd #'identity :type function)
|
|---|
| 110 | (gcd #'identity :type function))
|
|---|
| 111 |
|
|---|
| 112 | (defparameter *ring-of-integers*
|
|---|
| 113 | (make-ring
|
|---|
| 114 | :parse #'identity
|
|---|
| 115 | :unit #'(lambda () 1)
|
|---|
| 116 | :zerop #'zerop
|
|---|
| 117 | :add #'+
|
|---|
| 118 | :sub #'-
|
|---|
| 119 | :uminus #'-
|
|---|
| 120 | :mul #'*
|
|---|
| 121 | :div #'/
|
|---|
| 122 | :lcm #'lcm
|
|---|
| 123 | :ezgcd #'(lambda (x y &aux (c (gcd x y))) (values c (/ x c) (/ y c)))
|
|---|
| 124 | :gcd #'gcd)
|
|---|
| 125 | "The ring of integers.")
|
|---|
| 126 |
|
|---|
| 127 | |
|---|
| 128 |
|
|---|
| 129 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 130 | ;;
|
|---|
| 131 | ;; This is how we perform operations on coefficients
|
|---|
| 132 | ;; using Maxima functions.
|
|---|
| 133 | ;;
|
|---|
| 134 | ;; Functions and macros dealing with internal representation structure
|
|---|
| 135 | ;;
|
|---|
| 136 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 | |
|---|
| 140 |
|
|---|
| 141 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 142 | ;;
|
|---|
| 143 | ;; Low-level polynomial arithmetic done on
|
|---|
| 144 | ;; lists of terms
|
|---|
| 145 | ;;
|
|---|
| 146 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 147 |
|
|---|
| 148 | (defmacro termlist-lt (p) `(car ,p))
|
|---|
| 149 | (defun termlist-lm (p) (term-monom (termlist-lt p)))
|
|---|
| 150 | (defun termlist-lc (p) (term-coeff (termlist-lt p)))
|
|---|
| 151 |
|
|---|
| 152 | (define-modify-macro scalar-mul (c) coeff-mul)
|
|---|
| 153 |
|
|---|
| 154 | (defun scalar-times-termlist (ring c p)
|
|---|
| 155 | "Multiply scalar C by a polynomial P. This function works
|
|---|
| 156 | even if there are divisors of 0."
|
|---|
| 157 | (mapcan
|
|---|
| 158 | #'(lambda (term)
|
|---|
| 159 | (let ((c1 (funcall (ring-mul ring) c (term-coeff term))))
|
|---|
| 160 | (unless (funcall (ring-zerop ring) c1)
|
|---|
| 161 | (list (make-term (term-monom term) c1)))))
|
|---|
| 162 | p))
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | (defun term-mul (ring term1 term2)
|
|---|
| 166 | "Returns (LIST TERM) wheter TERM is the product of the terms TERM1 TERM2,
|
|---|
| 167 | or NIL when the product is 0. This definition takes care of divisors of 0
|
|---|
| 168 | in the coefficient ring."
|
|---|
| 169 | (let ((c (funcall (ring-mul ring) (term-coeff term1) (term-coeff term2))))
|
|---|
| 170 | (unless (funcall (ring-zerop ring) c)
|
|---|
| 171 | (list (make-term (monom-mul (term-monom term1) (term-monom term2)) c)))))
|
|---|
| 172 |
|
|---|
| 173 | (defun term-times-termlist (ring term f)
|
|---|
| 174 | (declare (type ring ring))
|
|---|
| 175 | (mapcan #'(lambda (term-f) (term-mul ring term term-f)) f))
|
|---|
| 176 |
|
|---|
| 177 | (defun termlist-times-term (ring f term)
|
|---|
| 178 | (mapcan #'(lambda (term-f) (term-mul ring term-f term)) f))
|
|---|
| 179 |
|
|---|
| 180 | (defun monom-times-term (m term)
|
|---|
| 181 | (make-term (monom-mul m (term-monom term)) (term-coeff term)))
|
|---|
| 182 |
|
|---|
| 183 | (defun monom-times-termlist (m f)
|
|---|
| 184 | (cond
|
|---|
| 185 | ((null f) nil)
|
|---|
| 186 | (t
|
|---|
| 187 | (mapcar #'(lambda (x) (monom-times-term m x)) f))))
|
|---|
| 188 |
|
|---|
| 189 | (defun termlist-uminus (ring f)
|
|---|
| 190 | (mapcar #'(lambda (x)
|
|---|
| 191 | (make-term (term-monom x) (funcall (ring-uminus ring) (term-coeff x))))
|
|---|
| 192 | f))
|
|---|
| 193 |
|
|---|
| 194 | (defun termlist-add (ring p q)
|
|---|
| 195 | (declare (type list p q))
|
|---|
| 196 | (do (r)
|
|---|
| 197 | ((cond
|
|---|
| 198 | ((endp p)
|
|---|
| 199 | (setf r (revappend r q)) t)
|
|---|
| 200 | ((endp q)
|
|---|
| 201 | (setf r (revappend r p)) t)
|
|---|
| 202 | (t
|
|---|
| 203 | (multiple-value-bind
|
|---|
| 204 | (lm-greater lm-equal)
|
|---|
| 205 | (monomial-order (termlist-lm p) (termlist-lm q))
|
|---|
| 206 | (cond
|
|---|
| 207 | (lm-equal
|
|---|
| 208 | (let ((s (funcall (ring-add ring) (termlist-lc p) (termlist-lc q))))
|
|---|
| 209 | (unless (funcall (ring-zerop ring) s) ;check for cancellation
|
|---|
| 210 | (setf r (cons (make-term (termlist-lm p) s) r)))
|
|---|
| 211 | (setf p (cdr p) q (cdr q))))
|
|---|
| 212 | (lm-greater
|
|---|
| 213 | (setf r (cons (car p) r)
|
|---|
| 214 | p (cdr p)))
|
|---|
| 215 | (t (setf r (cons (car q) r)
|
|---|
| 216 | q (cdr q)))))
|
|---|
| 217 | nil))
|
|---|
| 218 | r)))
|
|---|
| 219 |
|
|---|
| 220 | (defun termlist-sub (ring p q)
|
|---|
| 221 | (declare (type list p q))
|
|---|
| 222 | (do (r)
|
|---|
| 223 | ((cond
|
|---|
| 224 | ((endp p)
|
|---|
| 225 | (setf r (revappend r (termlist-uminus ring q)))
|
|---|
| 226 | t)
|
|---|
| 227 | ((endp q)
|
|---|
| 228 | (setf r (revappend r p))
|
|---|
| 229 | t)
|
|---|
| 230 | (t
|
|---|
| 231 | (multiple-value-bind
|
|---|
| 232 | (mgreater mequal)
|
|---|
| 233 | (monomial-order (termlist-lm p) (termlist-lm q))
|
|---|
| 234 | (cond
|
|---|
| 235 | (mequal
|
|---|
| 236 | (let ((s (funcall (ring-sub ring) (termlist-lc p) (termlist-lc q))))
|
|---|
| 237 | (unless (funcall (ring-zerop ring) s) ;check for cancellation
|
|---|
| 238 | (setf r (cons (make-term (termlist-lm p) s) r)))
|
|---|
| 239 | (setf p (cdr p) q (cdr q))))
|
|---|
| 240 | (mgreater
|
|---|
| 241 | (setf r (cons (car p) r)
|
|---|
| 242 | p (cdr p)))
|
|---|
| 243 | (t (setf r (cons (make-term (termlist-lm q) (funcall (ring-uminus ring) (termlist-lc q))) r)
|
|---|
| 244 | q (cdr q)))))
|
|---|
| 245 | nil))
|
|---|
| 246 | r)))
|
|---|
| 247 |
|
|---|
| 248 | ;; Multiplication of polynomials
|
|---|
| 249 | ;; Non-destructive version
|
|---|
| 250 | (defun termlist-mul (ring p q)
|
|---|
| 251 | (cond ((or (endp p) (endp q)) nil) ;p or q is 0 (represented by NIL)
|
|---|
| 252 | ;; If p=p0+p1 and q=q0+q1 then pq=p0q0+p0q1+p1q
|
|---|
| 253 | ((endp (cdr p))
|
|---|
| 254 | (term-times-termlist ring (car p) q))
|
|---|
| 255 | ((endp (cdr q))
|
|---|
| 256 | (termlist-times-term ring p (car q)))
|
|---|
| 257 | (t
|
|---|
| 258 | (let ((head (term-mul ring (termlist-lt p) (termlist-lt q)))
|
|---|
| 259 | (tail (termlist-add ring (term-times-termlist ring (car p) (cdr q))
|
|---|
| 260 | (termlist-mul ring (cdr p) q))))
|
|---|
| 261 | (cond ((null head) tail)
|
|---|
| 262 | ((null tail) head)
|
|---|
| 263 | (t (nconc head tail)))))))
|
|---|
| 264 |
|
|---|
| 265 | (defun termlist-unit (ring dimension)
|
|---|
| 266 | (declare (fixnum dimension))
|
|---|
| 267 | (list (make-term (make-monom dimension :initial-element 0)
|
|---|
| 268 | (funcall (ring-unit ring)))))
|
|---|
| 269 |
|
|---|
| 270 | (defun termlist-expt (ring poly n &aux (dim (monom-dimension (termlist-lm poly))))
|
|---|
| 271 | (declare (type fixnum n dim))
|
|---|
| 272 | (cond
|
|---|
| 273 | ((minusp n) (error "termlist-expt: Negative exponent."))
|
|---|
| 274 | ((endp poly) (if (zerop n) (termlist-unit ring dim) nil))
|
|---|
| 275 | (t
|
|---|
| 276 | (do ((k 1 (ash k 1))
|
|---|
| 277 | (q poly (termlist-mul ring q q)) ;keep squaring
|
|---|
| 278 | (p (termlist-unit ring dim) (if (not (zerop (logand k n))) (termlist-mul ring p q) p)))
|
|---|
| 279 | ((> k n) p)
|
|---|
| 280 | (declare (fixnum k))))))
|
|---|
| 281 |
|
|---|
| 282 | |
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 288 | ;;
|
|---|
| 289 | ;; Debugging/tracing
|
|---|
| 290 | ;;
|
|---|
| 291 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 292 | (defmacro debug-cgb (&rest args)
|
|---|
| 293 | `(when $poly_grobner_debug (format *terminal-io* ,@args)))
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 | |
|---|
| 298 |
|
|---|
| 299 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 300 | ;;
|
|---|
| 301 | ;; These are provided mostly for debugging purposes To enable
|
|---|
| 302 | ;; verification of grobner bases with BUCHBERGER-CRITERION, do
|
|---|
| 303 | ;; (pushnew :grobner-check *features*) and compile/load this file.
|
|---|
| 304 | ;; With this feature, the calculations will slow down CONSIDERABLY.
|
|---|
| 305 | ;;
|
|---|
| 306 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 307 |
|
|---|
| 308 | (defun grobner-test (ring g f)
|
|---|
| 309 | "Test whether G is a Grobner basis and F is contained in G. Return T
|
|---|
| 310 | upon success and NIL otherwise."
|
|---|
| 311 | (debug-cgb "~&GROBNER CHECK: ")
|
|---|
| 312 | (let (($poly_grobner_debug nil)
|
|---|
| 313 | (stat1 (buchberger-criterion ring g))
|
|---|
| 314 | (stat2
|
|---|
| 315 | (every #'poly-zerop
|
|---|
| 316 | (makelist (normal-form ring (copy-tree (elt f i)) g nil)
|
|---|
| 317 | (i 0 (1- (length f)))))))
|
|---|
| 318 | (unless stat1 (error "~&Buchberger criterion failed."))
|
|---|
| 319 | (unless stat2
|
|---|
| 320 | (error "~&Original polys not in ideal spanned by Grobner.")))
|
|---|
| 321 | (debug-cgb "~&GROBNER CHECK END")
|
|---|
| 322 | t)
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 326 | ;;
|
|---|
| 327 | ;; Selection of algorithm and pair heuristic
|
|---|
| 328 | ;;
|
|---|
| 329 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 330 |
|
|---|
| 331 | (defun find-grobner-function (algorithm)
|
|---|
| 332 | "Return a function which calculates Grobner basis, based on its
|
|---|
| 333 | names. Names currently used are either Lisp symbols, Maxima symbols or
|
|---|
| 334 | keywords."
|
|---|
| 335 | (ecase algorithm
|
|---|
| 336 | ((buchberger :buchberger $buchberger) #'buchberger)
|
|---|
| 337 | ((parallel-buchberger :parallel-buchberger $parallel_buchberger) #'parallel-buchberger)
|
|---|
| 338 | ((gebauer-moeller :gebauer_moeller $gebauer_moeller) #'gebauer-moeller)))
|
|---|
| 339 |
|
|---|
| 340 | (defun grobner (ring f &optional (start 0) (top-reduction-only nil))
|
|---|
| 341 | ;;(setf F (sort F #'< :key #'sugar))
|
|---|
| 342 | (funcall
|
|---|
| 343 | (find-grobner-function $poly_grobner_algorithm)
|
|---|
| 344 | ring f start top-reduction-only))
|
|---|
| 345 |
|
|---|
| 346 | (defun reduced-grobner (ring f &optional (start 0) (top-reduction-only $poly_top_reduction_only))
|
|---|
| 347 | (reduction ring (grobner ring f start top-reduction-only)))
|
|---|
| 348 |
|
|---|
| 349 | (defun set-pair-heuristic (method)
|
|---|
| 350 | "Sets up variables *PAIR-KEY-FUNCTION* and *PAIR-ORDER* used
|
|---|
| 351 | to determine the priority of critical pairs in the priority queue."
|
|---|
| 352 | (ecase method
|
|---|
| 353 | ((sugar :sugar $sugar)
|
|---|
| 354 | (setf *pair-key-function* #'sugar-pair-key
|
|---|
| 355 | *pair-order* #'sugar-order))
|
|---|
| 356 | ; ((minimal-mock-spoly :minimal-mock-spoly $minimal_mock_spoly)
|
|---|
| 357 | ; (setf *pair-key-function* #'mock-spoly
|
|---|
| 358 | ; *pair-order* #'mock-spoly-order))
|
|---|
| 359 | ((minimal-lcm :minimal-lcm $minimal_lcm)
|
|---|
| 360 | (setf *pair-key-function* #'(lambda (p q)
|
|---|
| 361 | (monom-lcm (poly-lm p) (poly-lm q)))
|
|---|
| 362 | *pair-order* #'reverse-monomial-order))
|
|---|
| 363 | ((minimal-total-degree :minimal-total-degree $minimal_total_degree)
|
|---|
| 364 | (setf *pair-key-function* #'(lambda (p q)
|
|---|
| 365 | (monom-total-degree
|
|---|
| 366 | (monom-lcm (poly-lm p) (poly-lm q))))
|
|---|
| 367 | *pair-order* #'<))
|
|---|
| 368 | ((minimal-length :minimal-length $minimal_length)
|
|---|
| 369 | (setf *pair-key-function* #'(lambda (p q)
|
|---|
| 370 | (+ (poly-length p) (poly-length q)))
|
|---|
| 371 | *pair-order* #'<))))
|
|---|
| 372 |
|
|---|
| 373 | |
|---|
| 374 |
|
|---|
| 375 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 376 | ;;
|
|---|
| 377 | ;; Operations in ideal theory
|
|---|
| 378 | ;;
|
|---|
| 379 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 380 |
|
|---|
| 381 | ;; Does the term depend on variable K?
|
|---|
| 382 | (defun term-depends-p (term k)
|
|---|
| 383 | "Return T if the term TERM depends on variable number K."
|
|---|
| 384 | (monom-depends-p (term-monom term) k))
|
|---|
| 385 |
|
|---|
| 386 | ;; Does the polynomial P depend on variable K?
|
|---|
| 387 | (defun poly-depends-p (p k)
|
|---|
| 388 | "Return T if the term polynomial P depends on variable number K."
|
|---|
| 389 | (some #'(lambda (term) (term-depends-p term k)) (poly-termlist p)))
|
|---|
| 390 |
|
|---|
| 391 | (defun ring-intersection (plist k)
|
|---|
| 392 | "This function assumes that polynomial list PLIST is a Grobner basis
|
|---|
| 393 | and it calculates the intersection with the ring R[x[k+1],...,x[n]], i.e.
|
|---|
| 394 | it discards polynomials which depend on variables x[0], x[1], ..., x[k]."
|
|---|
| 395 | (dotimes (i k plist)
|
|---|
| 396 | (setf plist
|
|---|
| 397 | (remove-if #'(lambda (p)
|
|---|
| 398 | (poly-depends-p p i))
|
|---|
| 399 | plist))))
|
|---|
| 400 |
|
|---|
| 401 | (defun elimination-ideal (ring flist k
|
|---|
| 402 | &optional (top-reduction-only $poly_top_reduction_only) (start 0)
|
|---|
| 403 | &aux (*monomial-order*
|
|---|
| 404 | (or *elimination-order*
|
|---|
| 405 | (elimination-order k))))
|
|---|
| 406 | (ring-intersection (reduced-grobner ring flist start top-reduction-only) k))
|
|---|
| 407 |
|
|---|
| 408 | (defun colon-ideal (ring f g &optional (top-reduction-only $poly_top_reduction_only))
|
|---|
| 409 | "Returns the reduced Grobner basis of the colon ideal Id(F):Id(G),
|
|---|
| 410 | where F and G are two lists of polynomials. The colon ideal I:J is
|
|---|
| 411 | defined as the set of polynomials H such that for all polynomials W in
|
|---|
| 412 | J the polynomial W*H belongs to I."
|
|---|
| 413 | (cond
|
|---|
| 414 | ((endp g)
|
|---|
| 415 | ;;Id(G) consists of 0 only so W*0=0 belongs to Id(F)
|
|---|
| 416 | (if (every #'poly-zerop f)
|
|---|
| 417 | (error "First ideal must be non-zero.")
|
|---|
| 418 | (list (make-poly
|
|---|
| 419 | (list (make-term
|
|---|
| 420 | (make-monom (monom-dimension (poly-lm (find-if-not #'poly-zerop f)))
|
|---|
| 421 | :initial-element 0)
|
|---|
| 422 | (funcall (ring-unit ring))))))))
|
|---|
| 423 | ((endp (cdr g))
|
|---|
| 424 | (colon-ideal-1 ring f (car g) top-reduction-only))
|
|---|
| 425 | (t
|
|---|
| 426 | (ideal-intersection ring
|
|---|
| 427 | (colon-ideal-1 ring f (car g) top-reduction-only)
|
|---|
| 428 | (colon-ideal ring f (rest g) top-reduction-only)
|
|---|
| 429 | top-reduction-only))))
|
|---|
| 430 |
|
|---|
| 431 | (defun colon-ideal-1 (ring f g &optional (top-reduction-only $poly_top_reduction_only))
|
|---|
| 432 | "Returns the reduced Grobner basis of the colon ideal Id(F):Id({G}), where
|
|---|
| 433 | F is a list of polynomials and G is a polynomial."
|
|---|
| 434 | (mapcar #'(lambda (x) (poly-exact-divide ring x g)) (ideal-intersection ring f (list g) top-reduction-only)))
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 | (defun ideal-intersection (ring f g &optional (top-reduction-only $poly_top_reduction_only)
|
|---|
| 438 | &aux (*monomial-order* (or *elimination-order*
|
|---|
| 439 | #'elimination-order-1)))
|
|---|
| 440 | (mapcar #'poly-contract
|
|---|
| 441 | (ring-intersection
|
|---|
| 442 | (reduced-grobner
|
|---|
| 443 | ring
|
|---|
| 444 | (append (mapcar #'(lambda (p) (poly-extend p (make-monom 1 :initial-element 1))) f)
|
|---|
| 445 | (mapcar #'(lambda (p)
|
|---|
| 446 | (poly-append (poly-extend (poly-uminus ring p)
|
|---|
| 447 | (make-monom 1 :initial-element 1))
|
|---|
| 448 | (poly-extend p)))
|
|---|
| 449 | g))
|
|---|
| 450 | 0
|
|---|
| 451 | top-reduction-only)
|
|---|
| 452 | 1)))
|
|---|
| 453 |
|
|---|
| 454 | (defun poly-lcm (ring f g)
|
|---|
| 455 | "Return LCM (least common multiple) of two polynomials F and G.
|
|---|
| 456 | The polynomials must be ordered according to monomial order PRED
|
|---|
| 457 | and their coefficients must be compatible with the RING structure
|
|---|
| 458 | defined in the COEFFICIENT-RING package."
|
|---|
| 459 | (cond
|
|---|
| 460 | ((poly-zerop f) f)
|
|---|
| 461 | ((poly-zerop g) g)
|
|---|
| 462 | ((and (endp (cdr (poly-termlist f))) (endp (cdr (poly-termlist g))))
|
|---|
| 463 | (let ((m (monom-lcm (poly-lm f) (poly-lm g))))
|
|---|
| 464 | (make-poly-from-termlist (list (make-term m (funcall (ring-lcm ring) (poly-lc f) (poly-lc g)))))))
|
|---|
| 465 | (t
|
|---|
| 466 | (multiple-value-bind (f f-cont)
|
|---|
| 467 | (poly-primitive-part ring f)
|
|---|
| 468 | (multiple-value-bind (g g-cont)
|
|---|
| 469 | (poly-primitive-part ring g)
|
|---|
| 470 | (scalar-times-poly
|
|---|
| 471 | ring
|
|---|
| 472 | (funcall (ring-lcm ring) f-cont g-cont)
|
|---|
| 473 | (poly-primitive-part ring (car (ideal-intersection ring (list f) (list g) nil)))))))))
|
|---|
| 474 |
|
|---|
| 475 | ;; Do two Grobner bases yield the same ideal?
|
|---|
| 476 | (defun grobner-equal (ring g1 g2)
|
|---|
| 477 | "Returns T if two lists of polynomials G1 and G2, assumed to be Grobner bases,
|
|---|
| 478 | generate the same ideal, and NIL otherwise."
|
|---|
| 479 | (and (grobner-subsetp ring g1 g2) (grobner-subsetp ring g2 g1)))
|
|---|
| 480 |
|
|---|
| 481 | (defun grobner-subsetp (ring g1 g2)
|
|---|
| 482 | "Returns T if a list of polynomials G1 generates
|
|---|
| 483 | an ideal contained in the ideal generated by a polynomial list G2,
|
|---|
| 484 | both G1 and G2 assumed to be Grobner bases. Returns NIL otherwise."
|
|---|
| 485 | (every #'(lambda (p) (grobner-member ring p g2)) g1))
|
|---|
| 486 |
|
|---|
| 487 | (defun grobner-member (ring p g)
|
|---|
| 488 | "Returns T if a polynomial P belongs to the ideal generated by the
|
|---|
| 489 | polynomial list G, which is assumed to be a Grobner basis. Returns NIL otherwise."
|
|---|
| 490 | (poly-zerop (normal-form ring p g nil)))
|
|---|
| 491 |
|
|---|
| 492 | ;; Calculate F : p^inf
|
|---|
| 493 | (defun ideal-saturation-1 (ring f p start &optional (top-reduction-only $poly_top_reduction_only)
|
|---|
| 494 | &aux (*monomial-order* (or *elimination-order*
|
|---|
| 495 | #'elimination-order-1)))
|
|---|
| 496 | "Returns the reduced Grobner basis of the saturation of the ideal
|
|---|
| 497 | generated by a polynomial list F in the ideal generated by a single
|
|---|
| 498 | polynomial P. The saturation ideal is defined as the set of
|
|---|
| 499 | polynomials H such for some natural number n (* (EXPT P N) H) is in the ideal
|
|---|
| 500 | F. Geometrically, over an algebraically closed field, this is the set
|
|---|
| 501 | of polynomials in the ideal generated by F which do not identically
|
|---|
| 502 | vanish on the variety of P."
|
|---|
| 503 | (mapcar
|
|---|
| 504 | #'poly-contract
|
|---|
| 505 | (ring-intersection
|
|---|
| 506 | (reduced-grobner
|
|---|
| 507 | ring
|
|---|
| 508 | (saturation-extension-1 ring f p)
|
|---|
| 509 | start top-reduction-only)
|
|---|
| 510 | 1)))
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 | ;; Calculate F : p1^inf : p2^inf : ... : ps^inf
|
|---|
| 515 | (defun ideal-polysaturation-1 (ring f plist start &optional (top-reduction-only $poly_top_reduction_only))
|
|---|
| 516 | "Returns the reduced Grobner basis of the ideal obtained by a
|
|---|
| 517 | sequence of successive saturations in the polynomials
|
|---|
| 518 | of the polynomial list PLIST of the ideal generated by the
|
|---|
| 519 | polynomial list F."
|
|---|
| 520 | (cond
|
|---|
| 521 | ((endp plist) (reduced-grobner ring f start top-reduction-only))
|
|---|
| 522 | (t (let ((g (ideal-saturation-1 ring f (car plist) start top-reduction-only)))
|
|---|
| 523 | (ideal-polysaturation-1 ring g (rest plist) (length g) top-reduction-only)))))
|
|---|
| 524 |
|
|---|
| 525 | (defun ideal-saturation (ring f g start &optional (top-reduction-only $poly_top_reduction_only)
|
|---|
| 526 | &aux
|
|---|
| 527 | (k (length g))
|
|---|
| 528 | (*monomial-order* (or *elimination-order*
|
|---|
| 529 | (elimination-order k))))
|
|---|
| 530 | "Returns the reduced Grobner basis of the saturation of the ideal
|
|---|
| 531 | generated by a polynomial list F in the ideal generated a polynomial
|
|---|
| 532 | list G. The saturation ideal is defined as the set of polynomials H
|
|---|
| 533 | such for some natural number n and some P in the ideal generated by G
|
|---|
| 534 | the polynomial P**N * H is in the ideal spanned by F. Geometrically,
|
|---|
| 535 | over an algebraically closed field, this is the set of polynomials in
|
|---|
| 536 | the ideal generated by F which do not identically vanish on the
|
|---|
| 537 | variety of G."
|
|---|
| 538 | (mapcar
|
|---|
| 539 | #'(lambda (q) (poly-contract q k))
|
|---|
| 540 | (ring-intersection
|
|---|
| 541 | (reduced-grobner ring
|
|---|
| 542 | (polysaturation-extension ring f g)
|
|---|
| 543 | start
|
|---|
| 544 | top-reduction-only)
|
|---|
| 545 | k)))
|
|---|
| 546 |
|
|---|
| 547 | (defun ideal-polysaturation (ring f ideal-list start &optional (top-reduction-only $poly_top_reduction_only))
|
|---|
| 548 | "Returns the reduced Grobner basis of the ideal obtained by a
|
|---|
| 549 | successive applications of IDEAL-SATURATION to F and lists of
|
|---|
| 550 | polynomials in the list IDEAL-LIST."
|
|---|
| 551 | (cond
|
|---|
| 552 | ((endp ideal-list) f)
|
|---|
| 553 | (t (let ((h (ideal-saturation ring f (car ideal-list) start top-reduction-only)))
|
|---|
| 554 | (ideal-polysaturation ring h (rest ideal-list) (length h) top-reduction-only)))))
|
|---|
| 555 |
|
|---|
| 556 | |
|---|
| 557 |
|
|---|
| 558 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 559 | ;;
|
|---|
| 560 | ;; Set up the coefficients to be polynomials
|
|---|
| 561 | ;;
|
|---|
| 562 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 563 |
|
|---|
| 564 | ;; (defun poly-ring (ring vars)
|
|---|
| 565 | ;; (make-ring
|
|---|
| 566 | ;; :parse #'(lambda (expr) (poly-eval ring expr vars))
|
|---|
| 567 | ;; :unit #'(lambda () (poly-unit ring (length vars)))
|
|---|
| 568 | ;; :zerop #'poly-zerop
|
|---|
| 569 | ;; :add #'(lambda (x y) (poly-add ring x y))
|
|---|
| 570 | ;; :sub #'(lambda (x y) (poly-sub ring x y))
|
|---|
| 571 | ;; :uminus #'(lambda (x) (poly-uminus ring x))
|
|---|
| 572 | ;; :mul #'(lambda (x y) (poly-mul ring x y))
|
|---|
| 573 | ;; :div #'(lambda (x y) (poly-exact-divide ring x y))
|
|---|
| 574 | ;; :lcm #'(lambda (x y) (poly-lcm ring x y))
|
|---|
| 575 | ;; :ezgcd #'(lambda (x y &aux (gcd (poly-gcd ring x y)))
|
|---|
| 576 | ;; (values gcd
|
|---|
| 577 | ;; (poly-exact-divide ring x gcd)
|
|---|
| 578 | ;; (poly-exact-divide ring y gcd)))
|
|---|
| 579 | ;; :gcd #'(lambda (x y) (poly-gcd x y))))
|
|---|
| 580 |
|
|---|
| 581 | |
|---|
| 582 |
|
|---|
| 583 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 584 | ;;
|
|---|
| 585 | ;; Conversion from internal to infix form
|
|---|
| 586 | ;;
|
|---|
| 587 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 588 |
|
|---|
| 589 | (defun coerce-to-infix (poly-type object vars)
|
|---|
| 590 | (case poly-type
|
|---|
| 591 | (:termlist
|
|---|
| 592 | `(+ ,@(mapcar #'(lambda (term) (coerce-to-infix :term term vars)) object)))
|
|---|
| 593 | (:polynomial
|
|---|
| 594 | (coerce-to-infix :termlist (poly-termlist object) vars))
|
|---|
| 595 | (:poly-list
|
|---|
| 596 | `([ ,@(mapcar #'(lambda (p) (coerce-to-infix :polynomial p vars)) object)))
|
|---|
| 597 | (:term
|
|---|
| 598 | `(* ,(term-coeff object)
|
|---|
| 599 | ,@(mapcar #'(lambda (var power) `(expt ,var ,power))
|
|---|
| 600 | vars (monom-exponents (term-monom object)))))
|
|---|
| 601 | (otherwise
|
|---|
| 602 | object)))
|
|---|
| 603 |
|
|---|
| 604 | |
|---|
| 605 |
|
|---|
| 606 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 607 | ;;
|
|---|
| 608 | ;; Maxima expression ring
|
|---|
| 609 | ;;
|
|---|
| 610 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 611 |
|
|---|
| 612 | (defparameter *expression-ring*
|
|---|
| 613 | (make-ring
|
|---|
| 614 | ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
|
|---|
| 615 | :parse #'(lambda (expr)
|
|---|
| 616 | (when modulus (setf expr ($rat expr)))
|
|---|
| 617 | expr)
|
|---|
| 618 | :unit #'(lambda () (if modulus ($rat 1) 1))
|
|---|
| 619 | :zerop #'(lambda (expr)
|
|---|
| 620 | ;;When is exactly a maxima expression equal to 0?
|
|---|
| 621 | (cond ((numberp expr)
|
|---|
| 622 | (= expr 0))
|
|---|
| 623 | ((atom expr) nil)
|
|---|
| 624 | (t
|
|---|
| 625 | (case (caar expr)
|
|---|
| 626 | (mrat (eql ($ratdisrep expr) 0))
|
|---|
| 627 | (otherwise (eql ($totaldisrep expr) 0))))))
|
|---|
| 628 | :add #'(lambda (x y) (m+ x y))
|
|---|
| 629 | :sub #'(lambda (x y) (m- x y))
|
|---|
| 630 | :uminus #'(lambda (x) (m- x))
|
|---|
| 631 | :mul #'(lambda (x y) (m* x y))
|
|---|
| 632 | ;;(defun coeff-div (x y) (cadr ($divide x y)))
|
|---|
| 633 | :div #'(lambda (x y) (m// x y))
|
|---|
| 634 | :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
|
|---|
| 635 | :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
|
|---|
| 636 | ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
|
|---|
| 637 | :gcd #'(lambda (x y) ($gcd x y))))
|
|---|
| 638 |
|
|---|
| 639 | (defvar *maxima-ring* *expression-ring*
|
|---|
| 640 | "The ring of coefficients, over which all polynomials
|
|---|
| 641 | are assumed to be defined.")
|
|---|
| 642 |
|
|---|
| 643 | |
|---|
| 644 |
|
|---|
| 645 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 646 | ;;
|
|---|
| 647 | ;; Maxima expression parsing
|
|---|
| 648 | ;;
|
|---|
| 649 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 650 |
|
|---|
| 651 | (defun equal-test-p (expr1 expr2)
|
|---|
| 652 | (alike1 expr1 expr2))
|
|---|
| 653 |
|
|---|
| 654 | (defun coerce-maxima-list (expr)
|
|---|
| 655 | "convert a maxima list to lisp list."
|
|---|
| 656 | (cond
|
|---|
| 657 | ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
|
|---|
| 658 | (t expr)))
|
|---|
| 659 |
|
|---|
| 660 | (defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
|
|---|
| 661 |
|
|---|
| 662 | (defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
|
|---|
| 663 | "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
|
|---|
| 664 | (labels ((parse (arg) (parse-poly arg vars))
|
|---|
| 665 | (parse-list (args) (mapcar #'parse args)))
|
|---|
| 666 | (cond
|
|---|
| 667 | ((eql expr 0) (make-poly-zero))
|
|---|
| 668 | ((member expr vars :test #'equal-test-p)
|
|---|
| 669 | (let ((pos (position expr vars :test #'equal-test-p)))
|
|---|
| 670 | (make-variable *maxima-ring* (length vars) pos)))
|
|---|
| 671 | ((free-of-vars expr vars)
|
|---|
| 672 | ;;This means that variable-free CRE and Poisson forms will be converted
|
|---|
| 673 | ;;to coefficients intact
|
|---|
| 674 | (coerce-coeff *maxima-ring* expr vars))
|
|---|
| 675 | (t
|
|---|
| 676 | (case (caar expr)
|
|---|
| 677 | (mplus (reduce #'(lambda (x y) (poly-add *maxima-ring* x y)) (parse-list (cdr expr))))
|
|---|
| 678 | (mminus (poly-uminus *maxima-ring* (parse (cadr expr))))
|
|---|
| 679 | (mtimes
|
|---|
| 680 | (if (endp (cddr expr)) ;unary
|
|---|
| 681 | (parse (cdr expr))
|
|---|
| 682 | (reduce #'(lambda (p q) (poly-mul *maxima-ring* p q)) (parse-list (cdr expr)))))
|
|---|
| 683 | (mexpt
|
|---|
| 684 | (cond
|
|---|
| 685 | ((member (cadr expr) vars :test #'equal-test-p)
|
|---|
| 686 | ;;Special handling of (expt var pow)
|
|---|
| 687 | (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
|
|---|
| 688 | (make-variable *maxima-ring* (length vars) pos (caddr expr))))
|
|---|
| 689 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
|---|
| 690 | ;; Negative power means division in coefficient ring
|
|---|
| 691 | ;; Non-integer power means non-polynomial coefficient
|
|---|
| 692 | (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
|
|---|
| 693 | expr)
|
|---|
| 694 | (coerce-coeff *maxima-ring* expr vars))
|
|---|
| 695 | (t (poly-expt *maxima-ring* (parse (cadr expr)) (caddr expr)))))
|
|---|
| 696 | (mrat (parse ($ratdisrep expr)))
|
|---|
| 697 | (mpois (parse ($outofpois expr)))
|
|---|
| 698 | (otherwise
|
|---|
| 699 | (coerce-coeff *maxima-ring* expr vars)))))))
|
|---|
| 700 |
|
|---|
| 701 | (defun parse-poly-list (expr vars)
|
|---|
| 702 | (case (caar expr)
|
|---|
| 703 | (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
|
|---|
| 704 | (t (merror "Expression ~M is not a list of polynomials in variables ~M."
|
|---|
| 705 | expr vars))))
|
|---|
| 706 | (defun parse-poly-list-list (poly-list-list vars)
|
|---|
| 707 | (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
|
|---|
| 708 |
|
|---|
| 709 | |
|---|
| 710 |
|
|---|
| 711 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 712 | ;;
|
|---|
| 713 | ;; Order utilities
|
|---|
| 714 | ;;
|
|---|
| 715 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 716 | (defun find-order (order)
|
|---|
| 717 | "This function returns the order function bases on its name."
|
|---|
| 718 | (cond
|
|---|
| 719 | ((null order) nil)
|
|---|
| 720 | ((symbolp order)
|
|---|
| 721 | (case order
|
|---|
| 722 | ((lex :lex $lex) #'lex>)
|
|---|
| 723 | ((grlex :grlex $grlex) #'grlex>)
|
|---|
| 724 | ((grevlex :grevlex $grevlex) #'grevlex>)
|
|---|
| 725 | ((invlex :invlex $invlex) #'invlex>)
|
|---|
| 726 | ((elimination-order-1 :elimination-order-1 elimination_order_1) #'elimination-order-1)
|
|---|
| 727 | (otherwise
|
|---|
| 728 | (mtell "~%Warning: Order ~M not found. Using default.~%" order))))
|
|---|
| 729 | (t
|
|---|
| 730 | (mtell "~%Order specification ~M is not recognized. Using default.~%" order)
|
|---|
| 731 | nil)))
|
|---|
| 732 |
|
|---|
| 733 | (defun find-ring (ring)
|
|---|
| 734 | "This function returns the ring structure bases on input symbol."
|
|---|
| 735 | (cond
|
|---|
| 736 | ((null ring) nil)
|
|---|
| 737 | ((symbolp ring)
|
|---|
| 738 | (case ring
|
|---|
| 739 | ((expression-ring :expression-ring $expression_ring) *expression-ring*)
|
|---|
| 740 | ((ring-of-integers :ring-of-integers $ring_of_integers) *ring-of-integers*)
|
|---|
| 741 | (otherwise
|
|---|
| 742 | (mtell "~%Warning: Ring ~M not found. Using default.~%" ring))))
|
|---|
| 743 | (t
|
|---|
| 744 | (mtell "~%Ring specification ~M is not recognized. Using default.~%" ring)
|
|---|
| 745 | nil)))
|
|---|
| 746 |
|
|---|
| 747 | (defmacro with-monomial-order ((order) &body body)
|
|---|
| 748 | "Evaluate BODY with monomial order set to ORDER."
|
|---|
| 749 | `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
|
|---|
| 750 | . ,body))
|
|---|
| 751 |
|
|---|
| 752 | (defmacro with-coefficient-ring ((ring) &body body)
|
|---|
| 753 | "Evaluate BODY with coefficient ring set to RING."
|
|---|
| 754 | `(let ((*maxima-ring* (or (find-ring ,ring) *maxima-ring*)))
|
|---|
| 755 | . ,body))
|
|---|
| 756 |
|
|---|
| 757 | (defmacro with-elimination-orders ((primary secondary elimination-order)
|
|---|
| 758 | &body body)
|
|---|
| 759 | "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
|
|---|
| 760 | `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
|
|---|
| 761 | (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
|
|---|
| 762 | (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
|
|---|
| 763 | . ,body))
|
|---|
| 764 |
|
|---|
| 765 | |
|---|
| 766 |
|
|---|
| 767 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 768 | ;;
|
|---|
| 769 | ;; Conversion from internal form to Maxima general form
|
|---|
| 770 | ;;
|
|---|
| 771 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 772 |
|
|---|
| 773 | (defun maxima-head ()
|
|---|
| 774 | (if $poly_return_term_list
|
|---|
| 775 | '(mlist)
|
|---|
| 776 | '(mplus)))
|
|---|
| 777 |
|
|---|
| 778 | (defun coerce-to-maxima (poly-type object vars)
|
|---|
| 779 | (case poly-type
|
|---|
| 780 | (:polynomial
|
|---|
| 781 | `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
|
|---|
| 782 | (:poly-list
|
|---|
| 783 | `((mlist) ,@(mapcar #'(lambda (p) ($ratdisrep (coerce-to-maxima :polynomial p vars))) object)))
|
|---|
| 784 | (:term
|
|---|
| 785 | `((mtimes) ,($ratdisrep (term-coeff object))
|
|---|
| 786 | ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
|
|---|
| 787 | vars (monom-exponents (term-monom object)))))
|
|---|
| 788 | ;; Assumes that Lisp and Maxima logicals coincide
|
|---|
| 789 | (:logical object)
|
|---|
| 790 | (otherwise
|
|---|
| 791 | object)))
|
|---|
| 792 |
|
|---|
| 793 | |
|---|
| 794 |
|
|---|
| 795 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 796 | ;;
|
|---|
| 797 | ;; Macro facility for writing Maxima-level wrappers for
|
|---|
| 798 | ;; functions operating on internal representation
|
|---|
| 799 | ;;
|
|---|
| 800 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 801 |
|
|---|
| 802 | (defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
|
|---|
| 803 | &key (polynomials nil)
|
|---|
| 804 | (poly-lists nil)
|
|---|
| 805 | (poly-list-lists nil)
|
|---|
| 806 | (value-type nil))
|
|---|
| 807 | &body body
|
|---|
| 808 | &aux (vars (gensym))
|
|---|
| 809 | (new-vars (gensym)))
|
|---|
| 810 | `(let ((,vars (coerce-maxima-list ,maxima-vars))
|
|---|
| 811 | ,@(when new-vars-supplied-p
|
|---|
| 812 | (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
|
|---|
| 813 | (coerce-to-maxima
|
|---|
| 814 | ,value-type
|
|---|
| 815 | (with-coefficient-ring ($poly_coefficient_ring)
|
|---|
| 816 | (with-monomial-order ($poly_monomial_order)
|
|---|
| 817 | (with-elimination-orders ($poly_primary_elimination_order
|
|---|
| 818 | $poly_secondary_elimination_order
|
|---|
| 819 | $poly_elimination_order)
|
|---|
| 820 | (let ,(let ((args nil))
|
|---|
| 821 | (dolist (p polynomials args)
|
|---|
| 822 | (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
|
|---|
| 823 | (dolist (p poly-lists args)
|
|---|
| 824 | (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
|
|---|
| 825 | (dolist (p poly-list-lists args)
|
|---|
| 826 | (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
|
|---|
| 827 | . ,body))))
|
|---|
| 828 | ,(if new-vars-supplied-p
|
|---|
| 829 | `(append ,vars ,new-vars)
|
|---|
| 830 | vars))))
|
|---|
| 831 |
|
|---|
| 832 | (defmacro define-unop (maxima-name fun-name
|
|---|
| 833 | &optional (documentation nil documentation-supplied-p))
|
|---|
| 834 | "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
|
|---|
| 835 | `(defun ,maxima-name (p vars
|
|---|
| 836 | &aux
|
|---|
| 837 | (vars (coerce-maxima-list vars))
|
|---|
| 838 | (p (parse-poly p vars)))
|
|---|
| 839 | ,@(when documentation-supplied-p (list documentation))
|
|---|
| 840 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
|
|---|
| 841 |
|
|---|
| 842 | (defmacro define-binop (maxima-name fun-name
|
|---|
| 843 | &optional (documentation nil documentation-supplied-p))
|
|---|
| 844 | "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
|
|---|
| 845 | `(defmfun ,maxima-name (p q vars
|
|---|
| 846 | &aux
|
|---|
| 847 | (vars (coerce-maxima-list vars))
|
|---|
| 848 | (p (parse-poly p vars))
|
|---|
| 849 | (q (parse-poly q vars)))
|
|---|
| 850 | ,@(when documentation-supplied-p (list documentation))
|
|---|
| 851 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
|
|---|
| 852 |
|
|---|
| 853 | |
|---|
| 854 |
|
|---|
| 855 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 856 | ;;
|
|---|
| 857 | ;; Maxima-level interface functions
|
|---|
| 858 | ;;
|
|---|
| 859 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 860 |
|
|---|
| 861 | ;; Auxillary function for removing zero polynomial
|
|---|
| 862 | (defun remzero (plist) (remove #'poly-zerop plist))
|
|---|
| 863 |
|
|---|
| 864 | ;;Simple operators
|
|---|
| 865 |
|
|---|
| 866 | (define-binop $poly_add poly-add
|
|---|
| 867 | "Adds two polynomials P and Q")
|
|---|
| 868 |
|
|---|
| 869 | (define-binop $poly_subtract poly-sub
|
|---|
| 870 | "Subtracts a polynomial Q from P.")
|
|---|
| 871 |
|
|---|
| 872 | (define-binop $poly_multiply poly-mul
|
|---|
| 873 | "Returns the product of polynomials P and Q.")
|
|---|
| 874 |
|
|---|
| 875 | (define-binop $poly_s_polynomial spoly
|
|---|
| 876 | "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
|
|---|
| 877 |
|
|---|
| 878 | (define-unop $poly_primitive_part poly-primitive-part
|
|---|
| 879 | "Returns the polynomial P divided by GCD of its coefficients.")
|
|---|
| 880 |
|
|---|
| 881 | (define-unop $poly_normalize poly-normalize
|
|---|
| 882 | "Returns the polynomial P divided by the leading coefficient.")
|
|---|
| 883 |
|
|---|
| 884 | ;;Functions
|
|---|
| 885 |
|
|---|
| 886 | (defmfun $poly_expand (p vars)
|
|---|
| 887 | "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
|
|---|
| 888 | If the representation is not compatible with a polynomial in variables VARS,
|
|---|
| 889 | the result is an error."
|
|---|
| 890 | (with-parsed-polynomials ((vars) :polynomials (p)
|
|---|
| 891 | :value-type :polynomial)
|
|---|
| 892 | p))
|
|---|
| 893 |
|
|---|
| 894 | (defmfun $poly_expt (p n vars)
|
|---|
| 895 | (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
|
|---|
| 896 | (poly-expt *maxima-ring* p n)))
|
|---|
| 897 |
|
|---|
| 898 | (defmfun $poly_content (p vars)
|
|---|
| 899 | (with-parsed-polynomials ((vars) :polynomials (p))
|
|---|
| 900 | (poly-content *maxima-ring* p)))
|
|---|
| 901 |
|
|---|
| 902 | (defmfun $poly_pseudo_divide (f fl vars
|
|---|
| 903 | &aux (vars (coerce-maxima-list vars))
|
|---|
| 904 | (f (parse-poly f vars))
|
|---|
| 905 | (fl (parse-poly-list fl vars)))
|
|---|
| 906 | (multiple-value-bind (quot rem c division-count)
|
|---|
| 907 | (poly-pseudo-divide *maxima-ring* f fl)
|
|---|
| 908 | `((mlist)
|
|---|
| 909 | ,(coerce-to-maxima :poly-list quot vars)
|
|---|
| 910 | ,(coerce-to-maxima :polynomial rem vars)
|
|---|
| 911 | ,c
|
|---|
| 912 | ,division-count)))
|
|---|
| 913 |
|
|---|
| 914 | (defmfun $poly_exact_divide (f g vars)
|
|---|
| 915 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
|---|
| 916 | (poly-exact-divide *maxima-ring* f g)))
|
|---|
| 917 |
|
|---|
| 918 | (defmfun $poly_normal_form (f fl vars)
|
|---|
| 919 | (with-parsed-polynomials ((vars) :polynomials (f)
|
|---|
| 920 | :poly-lists (fl)
|
|---|
| 921 | :value-type :polynomial)
|
|---|
| 922 | (normal-form *maxima-ring* f (remzero fl) nil)))
|
|---|
| 923 |
|
|---|
| 924 | (defmfun $poly_buchberger_criterion (g vars)
|
|---|
| 925 | (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
|
|---|
| 926 | (buchberger-criterion *maxima-ring* g)))
|
|---|
| 927 |
|
|---|
| 928 | (defmfun $poly_buchberger (fl vars)
|
|---|
| 929 | (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
|
|---|
| 930 | (buchberger *maxima-ring* (remzero fl) 0 nil)))
|
|---|
| 931 |
|
|---|
| 932 | (defmfun $poly_reduction (plist vars)
|
|---|
| 933 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 934 | :value-type :poly-list)
|
|---|
| 935 | (reduction *maxima-ring* plist)))
|
|---|
| 936 |
|
|---|
| 937 | (defmfun $poly_minimization (plist vars)
|
|---|
| 938 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 939 | :value-type :poly-list)
|
|---|
| 940 | (minimization plist)))
|
|---|
| 941 |
|
|---|
| 942 | (defmfun $poly_normalize_list (plist vars)
|
|---|
| 943 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 944 | :value-type :poly-list)
|
|---|
| 945 | (poly-normalize-list *maxima-ring* plist)))
|
|---|
| 946 |
|
|---|
| 947 | (defmfun $poly_grobner (f vars)
|
|---|
| 948 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 949 | :value-type :poly-list)
|
|---|
| 950 | (grobner *maxima-ring* (remzero f))))
|
|---|
| 951 |
|
|---|
| 952 | (defmfun $poly_reduced_grobner (f vars)
|
|---|
| 953 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 954 | :value-type :poly-list)
|
|---|
| 955 | (reduced-grobner *maxima-ring* (remzero f))))
|
|---|
| 956 |
|
|---|
| 957 | (defmfun $poly_depends_p (p var mvars
|
|---|
| 958 | &aux (vars (coerce-maxima-list mvars))
|
|---|
| 959 | (pos (position var vars)))
|
|---|
| 960 | (if (null pos)
|
|---|
| 961 | (merror "~%Variable ~M not in the list of variables ~M." var mvars)
|
|---|
| 962 | (poly-depends-p (parse-poly p vars) pos)))
|
|---|
| 963 |
|
|---|
| 964 | (defmfun $poly_elimination_ideal (flist k vars)
|
|---|
| 965 | (with-parsed-polynomials ((vars) :poly-lists (flist)
|
|---|
| 966 | :value-type :poly-list)
|
|---|
| 967 | (elimination-ideal *maxima-ring* flist k nil 0)))
|
|---|
| 968 |
|
|---|
| 969 | (defmfun $poly_colon_ideal (f g vars)
|
|---|
| 970 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
|---|
| 971 | (colon-ideal *maxima-ring* f g nil)))
|
|---|
| 972 |
|
|---|
| 973 | (defmfun $poly_ideal_intersection (f g vars)
|
|---|
| 974 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
|---|
| 975 | (ideal-intersection *maxima-ring* f g nil)))
|
|---|
| 976 |
|
|---|
| 977 | (defmfun $poly_lcm (f g vars)
|
|---|
| 978 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
|---|
| 979 | (poly-lcm *maxima-ring* f g)))
|
|---|
| 980 |
|
|---|
| 981 | (defmfun $poly_gcd (f g vars)
|
|---|
| 982 | ($first ($divide (m* f g) ($poly_lcm f g vars))))
|
|---|
| 983 |
|
|---|
| 984 | (defmfun $poly_grobner_equal (g1 g2 vars)
|
|---|
| 985 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
|---|
| 986 | (grobner-equal *maxima-ring* g1 g2)))
|
|---|
| 987 |
|
|---|
| 988 | (defmfun $poly_grobner_subsetp (g1 g2 vars)
|
|---|
| 989 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
|---|
| 990 | (grobner-subsetp *maxima-ring* g1 g2)))
|
|---|
| 991 |
|
|---|
| 992 | (defmfun $poly_grobner_member (p g vars)
|
|---|
| 993 | (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
|
|---|
| 994 | (grobner-member *maxima-ring* p g)))
|
|---|
| 995 |
|
|---|
| 996 | (defmfun $poly_ideal_saturation1 (f p vars)
|
|---|
| 997 | (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
|
|---|
| 998 | :value-type :poly-list)
|
|---|
| 999 | (ideal-saturation-1 *maxima-ring* f p 0)))
|
|---|
| 1000 |
|
|---|
| 1001 | (defmfun $poly_saturation_extension (f plist vars new-vars)
|
|---|
| 1002 | (with-parsed-polynomials ((vars new-vars)
|
|---|
| 1003 | :poly-lists (f plist)
|
|---|
| 1004 | :value-type :poly-list)
|
|---|
| 1005 | (saturation-extension *maxima-ring* f plist)))
|
|---|
| 1006 |
|
|---|
| 1007 | (defmfun $poly_polysaturation_extension (f plist vars new-vars)
|
|---|
| 1008 | (with-parsed-polynomials ((vars new-vars)
|
|---|
| 1009 | :poly-lists (f plist)
|
|---|
| 1010 | :value-type :poly-list)
|
|---|
| 1011 | (polysaturation-extension *maxima-ring* f plist)))
|
|---|
| 1012 |
|
|---|
| 1013 | (defmfun $poly_ideal_polysaturation1 (f plist vars)
|
|---|
| 1014 | (with-parsed-polynomials ((vars) :poly-lists (f plist)
|
|---|
| 1015 | :value-type :poly-list)
|
|---|
| 1016 | (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
|
|---|
| 1017 |
|
|---|
| 1018 | (defmfun $poly_ideal_saturation (f g vars)
|
|---|
| 1019 | (with-parsed-polynomials ((vars) :poly-lists (f g)
|
|---|
| 1020 | :value-type :poly-list)
|
|---|
| 1021 | (ideal-saturation *maxima-ring* f g 0 nil)))
|
|---|
| 1022 |
|
|---|
| 1023 | (defmfun $poly_ideal_polysaturation (f ideal-list vars)
|
|---|
| 1024 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 1025 | :poly-list-lists (ideal-list)
|
|---|
| 1026 | :value-type :poly-list)
|
|---|
| 1027 | (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
|
|---|
| 1028 |
|
|---|
| 1029 | (defmfun $poly_lt (f vars)
|
|---|
| 1030 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
|---|
| 1031 | (make-poly-from-termlist (list (poly-lt f)))))
|
|---|
| 1032 |
|
|---|
| 1033 | (defmfun $poly_lm (f vars)
|
|---|
| 1034 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
|---|
| 1035 | (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
|
|---|
| 1036 |
|
|---|