[1] | 1 | ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
|
---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 3 | ;;;
|
---|
[72] | 4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
[1] | 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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 23 | ;;
|
---|
| 24 | ;; Global switches
|
---|
| 25 | ;;
|
---|
[94] | 26 | ;; Can be used in Maxima just fine, as they observe the
|
---|
| 27 | ;; Maxima naming convention, i.e. all names begin with a '$'.
|
---|
| 28 | ;;
|
---|
[1] | 29 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 30 |
|
---|
| 31 | (defmvar $poly_monomial_order '$lex
|
---|
| 32 | "This switch controls which monomial order is used in polynomial
|
---|
| 33 | and Grobner basis calculations. If not set, LEX will be used")
|
---|
| 34 |
|
---|
| 35 | (defmvar $poly_coefficient_ring '$expression_ring
|
---|
| 36 | "This switch indicates the coefficient ring of the polynomials
|
---|
| 37 | that will be used in grobner calculations. If not set, Maxima's
|
---|
| 38 | general expression ring will be used. This variable may be set
|
---|
| 39 | to RING_OF_INTEGERS if desired.")
|
---|
| 40 |
|
---|
| 41 | (defmvar $poly_primary_elimination_order nil
|
---|
| 42 | "Name of the default order for eliminated variables in elimination-based functions.
|
---|
| 43 | If not set, LEX will be used.")
|
---|
| 44 |
|
---|
| 45 | (defmvar $poly_secondary_elimination_order nil
|
---|
| 46 | "Name of the default order for kept variables in elimination-based functions.
|
---|
| 47 | If not set, LEX will be used.")
|
---|
| 48 |
|
---|
| 49 | (defmvar $poly_elimination_order nil
|
---|
| 50 | "Name of the default elimination order used in elimination calculations.
|
---|
| 51 | If set, it overrides the settings in variables POLY_PRIMARY_ELIMINATION_ORDER
|
---|
| 52 | and SECONDARY_ELIMINATION_ORDER. The user must ensure that this is a true
|
---|
| 53 | elimination order valid for the number of eliminated variables.")
|
---|
| 54 |
|
---|
| 55 | (defmvar $poly_return_term_list nil
|
---|
| 56 | "If set to T, all functions in this package will return each polynomial as a
|
---|
| 57 | list of terms in the current monomial order rather than a Maxima general expression.")
|
---|
| 58 |
|
---|
| 59 | (defmvar $poly_grobner_debug nil
|
---|
| 60 | "If set to TRUE, produce debugging and tracing output.")
|
---|
| 61 |
|
---|
| 62 | (defmvar $poly_grobner_algorithm '$buchberger
|
---|
| 63 | "The name of the algorithm used to find grobner bases.")
|
---|
| 64 |
|
---|
| 65 | (defmvar $poly_top_reduction_only nil
|
---|
| 66 | "If not FALSE, use top reduction only whenever possible.
|
---|
| 67 | Top reduction means that division algorithm stops after the first reduction.")
|
---|
| 68 |
|
---|
| 69 | |
---|
| 70 |
|
---|
| 71 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 72 | ;;
|
---|
| 73 | ;; Coefficient ring operations
|
---|
| 74 | ;;
|
---|
| 75 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 76 | ;;
|
---|
| 77 | ;; These are ALL operations that are performed on the coefficients by
|
---|
| 78 | ;; the package, and thus the coefficient ring can be changed by merely
|
---|
| 79 | ;; redefining these operations.
|
---|
| 80 | ;;
|
---|
| 81 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 82 |
|
---|
| 83 | (defstruct (ring)
|
---|
| 84 | (parse #'identity :type function)
|
---|
| 85 | (unit #'identity :type function)
|
---|
| 86 | (zerop #'identity :type function)
|
---|
| 87 | (add #'identity :type function)
|
---|
| 88 | (sub #'identity :type function)
|
---|
| 89 | (uminus #'identity :type function)
|
---|
| 90 | (mul #'identity :type function)
|
---|
| 91 | (div #'identity :type function)
|
---|
| 92 | (lcm #'identity :type function)
|
---|
| 93 | (ezgcd #'identity :type function)
|
---|
| 94 | (gcd #'identity :type function))
|
---|
| 95 |
|
---|
| 96 | (defparameter *ring-of-integers*
|
---|
| 97 | (make-ring
|
---|
| 98 | :parse #'identity
|
---|
| 99 | :unit #'(lambda () 1)
|
---|
| 100 | :zerop #'zerop
|
---|
| 101 | :add #'+
|
---|
| 102 | :sub #'-
|
---|
| 103 | :uminus #'-
|
---|
| 104 | :mul #'*
|
---|
| 105 | :div #'/
|
---|
| 106 | :lcm #'lcm
|
---|
| 107 | :ezgcd #'(lambda (x y &aux (c (gcd x y))) (values c (/ x c) (/ y c)))
|
---|
| 108 | :gcd #'gcd)
|
---|
| 109 | "The ring of integers.")
|
---|
| 110 |
|
---|
| 111 | |
---|
| 112 |
|
---|
| 113 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 114 | ;;
|
---|
| 115 | ;; This is how we perform operations on coefficients
|
---|
| 116 | ;; using Maxima functions.
|
---|
| 117 | ;;
|
---|
| 118 | ;; Functions and macros dealing with internal representation structure
|
---|
| 119 | ;;
|
---|
| 120 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 124 | ;;
|
---|
| 125 | ;; Debugging/tracing
|
---|
| 126 | ;;
|
---|
| 127 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 128 | (defmacro debug-cgb (&rest args)
|
---|
| 129 | `(when $poly_grobner_debug (format *terminal-io* ,@args)))
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 134 | ;;
|
---|
| 135 | ;; These are provided mostly for debugging purposes To enable
|
---|
| 136 | ;; verification of grobner bases with BUCHBERGER-CRITERION, do
|
---|
| 137 | ;; (pushnew :grobner-check *features*) and compile/load this file.
|
---|
| 138 | ;; With this feature, the calculations will slow down CONSIDERABLY.
|
---|
| 139 | ;;
|
---|
| 140 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 141 |
|
---|
| 142 | (defun grobner-test (ring g f)
|
---|
| 143 | "Test whether G is a Grobner basis and F is contained in G. Return T
|
---|
| 144 | upon success and NIL otherwise."
|
---|
| 145 | (debug-cgb "~&GROBNER CHECK: ")
|
---|
| 146 | (let (($poly_grobner_debug nil)
|
---|
| 147 | (stat1 (buchberger-criterion ring g))
|
---|
| 148 | (stat2
|
---|
| 149 | (every #'poly-zerop
|
---|
| 150 | (makelist (normal-form ring (copy-tree (elt f i)) g nil)
|
---|
| 151 | (i 0 (1- (length f)))))))
|
---|
| 152 | (unless stat1 (error "~&Buchberger criterion failed."))
|
---|
| 153 | (unless stat2
|
---|
| 154 | (error "~&Original polys not in ideal spanned by Grobner.")))
|
---|
| 155 | (debug-cgb "~&GROBNER CHECK END")
|
---|
| 156 | t)
|
---|
[66] | 157 |
|
---|
| 158 |
|
---|
| 159 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 160 | ;;
|
---|
| 161 | ;; Selection of algorithm and pair heuristic
|
---|
| 162 | ;;
|
---|
[1] | 163 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 164 |
|
---|
| 165 | (defun find-grobner-function (algorithm)
|
---|
| 166 | "Return a function which calculates Grobner basis, based on its
|
---|
| 167 | names. Names currently used are either Lisp symbols, Maxima symbols or
|
---|
| 168 | keywords."
|
---|
| 169 | (ecase algorithm
|
---|
| 170 | ((buchberger :buchberger $buchberger) #'buchberger)
|
---|
| 171 | ((parallel-buchberger :parallel-buchberger $parallel_buchberger) #'parallel-buchberger)
|
---|
| 172 | ((gebauer-moeller :gebauer_moeller $gebauer_moeller) #'gebauer-moeller)))
|
---|
| 173 |
|
---|
| 174 | (defun grobner (ring f &optional (start 0) (top-reduction-only nil))
|
---|
| 175 | ;;(setf F (sort F #'< :key #'sugar))
|
---|
| 176 | (funcall
|
---|
| 177 | (find-grobner-function $poly_grobner_algorithm)
|
---|
| 178 | ring f start top-reduction-only))
|
---|
| 179 |
|
---|
| 180 | (defun reduced-grobner (ring f &optional (start 0) (top-reduction-only $poly_top_reduction_only))
|
---|
| 181 | (reduction ring (grobner ring f start top-reduction-only)))
|
---|
| 182 |
|
---|
| 183 | (defun set-pair-heuristic (method)
|
---|
| 184 | "Sets up variables *PAIR-KEY-FUNCTION* and *PAIR-ORDER* used
|
---|
| 185 | to determine the priority of critical pairs in the priority queue."
|
---|
| 186 | (ecase method
|
---|
| 187 | ((sugar :sugar $sugar)
|
---|
| 188 | (setf *pair-key-function* #'sugar-pair-key
|
---|
| 189 | *pair-order* #'sugar-order))
|
---|
| 190 | ; ((minimal-mock-spoly :minimal-mock-spoly $minimal_mock_spoly)
|
---|
| 191 | ; (setf *pair-key-function* #'mock-spoly
|
---|
| 192 | ; *pair-order* #'mock-spoly-order))
|
---|
| 193 | ((minimal-lcm :minimal-lcm $minimal_lcm)
|
---|
| 194 | (setf *pair-key-function* #'(lambda (p q)
|
---|
| 195 | (monom-lcm (poly-lm p) (poly-lm q)))
|
---|
| 196 | *pair-order* #'reverse-monomial-order))
|
---|
| 197 | ((minimal-total-degree :minimal-total-degree $minimal_total_degree)
|
---|
| 198 | (setf *pair-key-function* #'(lambda (p q)
|
---|
| 199 | (monom-total-degree
|
---|
| 200 | (monom-lcm (poly-lm p) (poly-lm q))))
|
---|
| 201 | *pair-order* #'<))
|
---|
| 202 | ((minimal-length :minimal-length $minimal_length)
|
---|
| 203 | (setf *pair-key-function* #'(lambda (p q)
|
---|
| 204 | (+ (poly-length p) (poly-length q)))
|
---|
| 205 | *pair-order* #'<))))
|
---|
| 206 |
|
---|
| 207 |
|
---|
| 208 | |
---|
| 209 |
|
---|
| 210 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 211 | ;;
|
---|
| 212 | ;; Set up the coefficients to be polynomials
|
---|
| 213 | ;;
|
---|
| 214 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 215 |
|
---|
| 216 | ;; (defun poly-ring (ring vars)
|
---|
| 217 | ;; (make-ring
|
---|
| 218 | ;; :parse #'(lambda (expr) (poly-eval ring expr vars))
|
---|
| 219 | ;; :unit #'(lambda () (poly-unit ring (length vars)))
|
---|
| 220 | ;; :zerop #'poly-zerop
|
---|
| 221 | ;; :add #'(lambda (x y) (poly-add ring x y))
|
---|
| 222 | ;; :sub #'(lambda (x y) (poly-sub ring x y))
|
---|
| 223 | ;; :uminus #'(lambda (x) (poly-uminus ring x))
|
---|
| 224 | ;; :mul #'(lambda (x y) (poly-mul ring x y))
|
---|
| 225 | ;; :div #'(lambda (x y) (poly-exact-divide ring x y))
|
---|
| 226 | ;; :lcm #'(lambda (x y) (poly-lcm ring x y))
|
---|
| 227 | ;; :ezgcd #'(lambda (x y &aux (gcd (poly-gcd ring x y)))
|
---|
| 228 | ;; (values gcd
|
---|
| 229 | ;; (poly-exact-divide ring x gcd)
|
---|
| 230 | ;; (poly-exact-divide ring y gcd)))
|
---|
| 231 | ;; :gcd #'(lambda (x y) (poly-gcd x y))))
|
---|
| 232 |
|
---|
| 233 | |
---|
| 234 |
|
---|
| 235 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 236 | ;;
|
---|
| 237 | ;; Conversion from internal to infix form
|
---|
| 238 | ;;
|
---|
| 239 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 240 |
|
---|
| 241 | (defun coerce-to-infix (poly-type object vars)
|
---|
| 242 | (case poly-type
|
---|
| 243 | (:termlist
|
---|
| 244 | `(+ ,@(mapcar #'(lambda (term) (coerce-to-infix :term term vars)) object)))
|
---|
| 245 | (:polynomial
|
---|
| 246 | (coerce-to-infix :termlist (poly-termlist object) vars))
|
---|
| 247 | (:poly-list
|
---|
| 248 | `([ ,@(mapcar #'(lambda (p) (coerce-to-infix :polynomial p vars)) object)))
|
---|
| 249 | (:term
|
---|
| 250 | `(* ,(term-coeff object)
|
---|
| 251 | ,@(mapcar #'(lambda (var power) `(expt ,var ,power))
|
---|
| 252 | vars (monom-exponents (term-monom object)))))
|
---|
| 253 | (otherwise
|
---|
| 254 | object)))
|
---|
| 255 |
|
---|
| 256 | |
---|
| 257 |
|
---|
| 258 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 259 | ;;
|
---|
| 260 | ;; Maxima expression ring
|
---|
| 261 | ;;
|
---|
| 262 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 263 |
|
---|
| 264 | (defparameter *expression-ring*
|
---|
| 265 | (make-ring
|
---|
| 266 | ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
|
---|
| 267 | :parse #'(lambda (expr)
|
---|
| 268 | (when modulus (setf expr ($rat expr)))
|
---|
| 269 | expr)
|
---|
| 270 | :unit #'(lambda () (if modulus ($rat 1) 1))
|
---|
| 271 | :zerop #'(lambda (expr)
|
---|
| 272 | ;;When is exactly a maxima expression equal to 0?
|
---|
| 273 | (cond ((numberp expr)
|
---|
| 274 | (= expr 0))
|
---|
| 275 | ((atom expr) nil)
|
---|
| 276 | (t
|
---|
| 277 | (case (caar expr)
|
---|
| 278 | (mrat (eql ($ratdisrep expr) 0))
|
---|
| 279 | (otherwise (eql ($totaldisrep expr) 0))))))
|
---|
| 280 | :add #'(lambda (x y) (m+ x y))
|
---|
| 281 | :sub #'(lambda (x y) (m- x y))
|
---|
[12] | 282 | :uminus #'(lambda (x) (m- x))
|
---|
| 283 | :mul #'(lambda (x y) (m* x y))
|
---|
| 284 | ;;(defun coeff-div (x y) (cadr ($divide x y)))
|
---|
[1] | 285 | :div #'(lambda (x y) (m// x y))
|
---|
| 286 | :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
|
---|
| 287 | :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
|
---|
| 288 | ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
|
---|
| 289 | :gcd #'(lambda (x y) ($gcd x y))))
|
---|
| 290 |
|
---|
| 291 | (defvar *maxima-ring* *expression-ring*
|
---|
| 292 | "The ring of coefficients, over which all polynomials
|
---|
| 293 | are assumed to be defined.")
|
---|
| 294 |
|
---|
| 295 |
|
---|
| 296 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 297 | ;;
|
---|
| 298 | ;; Order utilities
|
---|
| 299 | ;;
|
---|
| 300 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 301 | (defun find-order (order)
|
---|
| 302 | "This function returns the order function bases on its name."
|
---|
| 303 | (cond
|
---|
| 304 | ((null order) nil)
|
---|
| 305 | ((symbolp order)
|
---|
| 306 | (case order
|
---|
| 307 | ((lex :lex $lex) #'lex>)
|
---|
| 308 | ((grlex :grlex $grlex) #'grlex>)
|
---|
| 309 | ((grevlex :grevlex $grevlex) #'grevlex>)
|
---|
| 310 | ((invlex :invlex $invlex) #'invlex>)
|
---|
| 311 | ((elimination-order-1 :elimination-order-1 elimination_order_1) #'elimination-order-1)
|
---|
| 312 | (otherwise
|
---|
| 313 | (mtell "~%Warning: Order ~M not found. Using default.~%" order))))
|
---|
| 314 | (t
|
---|
| 315 | (mtell "~%Order specification ~M is not recognized. Using default.~%" order)
|
---|
| 316 | nil)))
|
---|
| 317 |
|
---|
| 318 | (defun find-ring (ring)
|
---|
| 319 | "This function returns the ring structure bases on input symbol."
|
---|
| 320 | (cond
|
---|
| 321 | ((null ring) nil)
|
---|
| 322 | ((symbolp ring)
|
---|
| 323 | (case ring
|
---|
| 324 | ((expression-ring :expression-ring $expression_ring) *expression-ring*)
|
---|
| 325 | ((ring-of-integers :ring-of-integers $ring_of_integers) *ring-of-integers*)
|
---|
| 326 | (otherwise
|
---|
| 327 | (mtell "~%Warning: Ring ~M not found. Using default.~%" ring))))
|
---|
| 328 | (t
|
---|
| 329 | (mtell "~%Ring specification ~M is not recognized. Using default.~%" ring)
|
---|
| 330 | nil)))
|
---|
| 331 |
|
---|
| 332 | (defmacro with-monomial-order ((order) &body body)
|
---|
| 333 | "Evaluate BODY with monomial order set to ORDER."
|
---|
| 334 | `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
|
---|
| 335 | . ,body))
|
---|
| 336 |
|
---|
| 337 | (defmacro with-coefficient-ring ((ring) &body body)
|
---|
| 338 | "Evaluate BODY with coefficient ring set to RING."
|
---|
| 339 | `(let ((*maxima-ring* (or (find-ring ,ring) *maxima-ring*)))
|
---|
| 340 | . ,body))
|
---|
| 341 |
|
---|
| 342 | (defmacro with-elimination-orders ((primary secondary elimination-order)
|
---|
| 343 | &body body)
|
---|
| 344 | "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
|
---|
| 345 | `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
|
---|
| 346 | (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
|
---|
| 347 | (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
|
---|
| 348 | . ,body))
|
---|
| 349 |
|
---|
| 350 | |
---|
| 351 |
|
---|
| 352 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 353 | ;;
|
---|
| 354 | ;; Conversion from internal form to Maxima general form
|
---|
| 355 | ;;
|
---|
| 356 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 357 |
|
---|
[17] | 358 | (defun maxima-head ()
|
---|
| 359 | (if $poly_return_term_list
|
---|
| 360 | '(mlist)
|
---|
| 361 | '(mplus)))
|
---|
[19] | 362 |
|
---|
[17] | 363 | (defun coerce-to-maxima (poly-type object vars)
|
---|
[20] | 364 | (case poly-type
|
---|
[17] | 365 | (:polynomial
|
---|
| 366 | `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
|
---|
[26] | 367 | (:poly-list
|
---|
| 368 | `((mlist) ,@(mapcar #'(lambda (p) ($ratdisrep (coerce-to-maxima :polynomial p vars))) object)))
|
---|
[17] | 369 | (:term
|
---|
| 370 | `((mtimes) ,($ratdisrep (term-coeff object))
|
---|
[1] | 371 | ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
|
---|
| 372 | vars (monom-exponents (term-monom object)))))
|
---|
| 373 | ;; Assumes that Lisp and Maxima logicals coincide
|
---|
| 374 | (:logical object)
|
---|
| 375 | (otherwise
|
---|
| 376 | object)))
|
---|
| 377 |
|
---|
| 378 | |
---|
| 379 |
|
---|
| 380 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 381 | ;;
|
---|
| 382 | ;; Macro facility for writing Maxima-level wrappers for
|
---|
| 383 | ;; functions operating on internal representation
|
---|
| 384 | ;;
|
---|
| 385 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 386 |
|
---|
| 387 | (defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
|
---|
| 388 | &key (polynomials nil)
|
---|
| 389 | (poly-lists nil)
|
---|
| 390 | (poly-list-lists nil)
|
---|
| 391 | (value-type nil))
|
---|
| 392 | &body body
|
---|
| 393 | &aux (vars (gensym))
|
---|
| 394 | (new-vars (gensym)))
|
---|
| 395 | `(let ((,vars (coerce-maxima-list ,maxima-vars))
|
---|
| 396 | ,@(when new-vars-supplied-p
|
---|
| 397 | (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
|
---|
| 398 | (coerce-to-maxima
|
---|
| 399 | ,value-type
|
---|
| 400 | (with-coefficient-ring ($poly_coefficient_ring)
|
---|
| 401 | (with-monomial-order ($poly_monomial_order)
|
---|
| 402 | (with-elimination-orders ($poly_primary_elimination_order
|
---|
| 403 | $poly_secondary_elimination_order
|
---|
| 404 | $poly_elimination_order)
|
---|
| 405 | (let ,(let ((args nil))
|
---|
| 406 | (dolist (p polynomials args)
|
---|
| 407 | (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
|
---|
| 408 | (dolist (p poly-lists args)
|
---|
| 409 | (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
|
---|
| 410 | (dolist (p poly-list-lists args)
|
---|
| 411 | (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
|
---|
| 412 | . ,body))))
|
---|
| 413 | ,(if new-vars-supplied-p
|
---|
| 414 | `(append ,vars ,new-vars)
|
---|
| 415 | vars))))
|
---|
| 416 |
|
---|
| 417 | (defmacro define-unop (maxima-name fun-name
|
---|
[18] | 418 | &optional (documentation nil documentation-supplied-p))
|
---|
[1] | 419 | "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
|
---|
| 420 | `(defun ,maxima-name (p vars
|
---|
| 421 | &aux
|
---|
| 422 | (vars (coerce-maxima-list vars))
|
---|
| 423 | (p (parse-poly p vars)))
|
---|
| 424 | ,@(when documentation-supplied-p (list documentation))
|
---|
| 425 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
|
---|
| 426 |
|
---|
| 427 | (defmacro define-binop (maxima-name fun-name
|
---|
| 428 | &optional (documentation nil documentation-supplied-p))
|
---|
[18] | 429 | "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
|
---|
[1] | 430 | `(defmfun ,maxima-name (p q vars
|
---|
| 431 | &aux
|
---|
| 432 | (vars (coerce-maxima-list vars))
|
---|
| 433 | (p (parse-poly p vars))
|
---|
| 434 | (q (parse-poly q vars)))
|
---|
| 435 | ,@(when documentation-supplied-p (list documentation))
|
---|
| 436 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
|
---|
| 437 |
|
---|
| 438 | |
---|
| 439 |
|
---|
| 440 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 441 | ;;
|
---|
| 442 | ;; Maxima-level interface functions
|
---|
| 443 | ;;
|
---|
| 444 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 445 |
|
---|
| 446 | ;; Auxillary function for removing zero polynomial
|
---|
| 447 | (defun remzero (plist) (remove #'poly-zerop plist))
|
---|
| 448 |
|
---|
| 449 | ;;Simple operators
|
---|
| 450 |
|
---|
| 451 | (define-binop $poly_add poly-add
|
---|
| 452 | "Adds two polynomials P and Q")
|
---|
| 453 |
|
---|
| 454 | (define-binop $poly_subtract poly-sub
|
---|
| 455 | "Subtracts a polynomial Q from P.")
|
---|
| 456 |
|
---|
| 457 | (define-binop $poly_multiply poly-mul
|
---|
| 458 | "Returns the product of polynomials P and Q.")
|
---|
| 459 |
|
---|
| 460 | (define-binop $poly_s_polynomial spoly
|
---|
| 461 | "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
|
---|
| 462 |
|
---|
| 463 | (define-unop $poly_primitive_part poly-primitive-part
|
---|
| 464 | "Returns the polynomial P divided by GCD of its coefficients.")
|
---|
| 465 |
|
---|
| 466 | (define-unop $poly_normalize poly-normalize
|
---|
| 467 | "Returns the polynomial P divided by the leading coefficient.")
|
---|
| 468 |
|
---|
| 469 | ;;Functions
|
---|
| 470 |
|
---|
| 471 | (defmfun $poly_expand (p vars)
|
---|
| 472 | "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
|
---|
| 473 | If the representation is not compatible with a polynomial in variables VARS,
|
---|
| 474 | the result is an error."
|
---|
| 475 | (with-parsed-polynomials ((vars) :polynomials (p)
|
---|
| 476 | :value-type :polynomial)
|
---|
| 477 | p))
|
---|
| 478 |
|
---|
| 479 | (defmfun $poly_expt (p n vars)
|
---|
| 480 | (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
|
---|
| 481 | (poly-expt *maxima-ring* p n)))
|
---|
| 482 |
|
---|
| 483 | (defmfun $poly_content (p vars)
|
---|
| 484 | (with-parsed-polynomials ((vars) :polynomials (p))
|
---|
| 485 | (poly-content *maxima-ring* p)))
|
---|
| 486 |
|
---|
| 487 | (defmfun $poly_pseudo_divide (f fl vars
|
---|
| 488 | &aux (vars (coerce-maxima-list vars))
|
---|
| 489 | (f (parse-poly f vars))
|
---|
| 490 | (fl (parse-poly-list fl vars)))
|
---|
| 491 | (multiple-value-bind (quot rem c division-count)
|
---|
| 492 | (poly-pseudo-divide *maxima-ring* f fl)
|
---|
| 493 | `((mlist)
|
---|
| 494 | ,(coerce-to-maxima :poly-list quot vars)
|
---|
| 495 | ,(coerce-to-maxima :polynomial rem vars)
|
---|
| 496 | ,c
|
---|
| 497 | ,division-count)))
|
---|
| 498 |
|
---|
| 499 | (defmfun $poly_exact_divide (f g vars)
|
---|
| 500 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
| 501 | (poly-exact-divide *maxima-ring* f g)))
|
---|
[29] | 502 |
|
---|
[1] | 503 | (defmfun $poly_normal_form (f fl vars)
|
---|
| 504 | (with-parsed-polynomials ((vars) :polynomials (f)
|
---|
| 505 | :poly-lists (fl)
|
---|
| 506 | :value-type :polynomial)
|
---|
| 507 | (normal-form *maxima-ring* f (remzero fl) nil)))
|
---|
| 508 |
|
---|
| 509 | (defmfun $poly_buchberger_criterion (g vars)
|
---|
| 510 | (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
|
---|
| 511 | (buchberger-criterion *maxima-ring* g)))
|
---|
| 512 |
|
---|
| 513 | (defmfun $poly_buchberger (fl vars)
|
---|
| 514 | (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
|
---|
| 515 | (buchberger *maxima-ring* (remzero fl) 0 nil)))
|
---|
| 516 |
|
---|
| 517 | (defmfun $poly_reduction (plist vars)
|
---|
| 518 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 519 | :value-type :poly-list)
|
---|
| 520 | (reduction *maxima-ring* plist)))
|
---|
| 521 |
|
---|
| 522 | (defmfun $poly_minimization (plist vars)
|
---|
| 523 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 524 | :value-type :poly-list)
|
---|
| 525 | (minimization plist)))
|
---|
| 526 |
|
---|
| 527 | (defmfun $poly_normalize_list (plist vars)
|
---|
| 528 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 529 | :value-type :poly-list)
|
---|
| 530 | (poly-normalize-list *maxima-ring* plist)))
|
---|
| 531 |
|
---|
| 532 | (defmfun $poly_grobner (f vars)
|
---|
| 533 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 534 | :value-type :poly-list)
|
---|
| 535 | (grobner *maxima-ring* (remzero f))))
|
---|
| 536 |
|
---|
| 537 | (defmfun $poly_reduced_grobner (f vars)
|
---|
| 538 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 539 | :value-type :poly-list)
|
---|
| 540 | (reduced-grobner *maxima-ring* (remzero f))))
|
---|
| 541 |
|
---|
| 542 | (defmfun $poly_depends_p (p var mvars
|
---|
| 543 | &aux (vars (coerce-maxima-list mvars))
|
---|
| 544 | (pos (position var vars)))
|
---|
| 545 | (if (null pos)
|
---|
| 546 | (merror "~%Variable ~M not in the list of variables ~M." var mvars)
|
---|
| 547 | (poly-depends-p (parse-poly p vars) pos)))
|
---|
| 548 |
|
---|
| 549 | (defmfun $poly_elimination_ideal (flist k vars)
|
---|
| 550 | (with-parsed-polynomials ((vars) :poly-lists (flist)
|
---|
| 551 | :value-type :poly-list)
|
---|
| 552 | (elimination-ideal *maxima-ring* flist k nil 0)))
|
---|
| 553 |
|
---|
| 554 | (defmfun $poly_colon_ideal (f g vars)
|
---|
| 555 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
| 556 | (colon-ideal *maxima-ring* f g nil)))
|
---|
| 557 |
|
---|
| 558 | (defmfun $poly_ideal_intersection (f g vars)
|
---|
| 559 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
| 560 | (ideal-intersection *maxima-ring* f g nil)))
|
---|
| 561 |
|
---|
| 562 | (defmfun $poly_lcm (f g vars)
|
---|
| 563 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
| 564 | (poly-lcm *maxima-ring* f g)))
|
---|
| 565 |
|
---|
| 566 | (defmfun $poly_gcd (f g vars)
|
---|
| 567 | ($first ($divide (m* f g) ($poly_lcm f g vars))))
|
---|
| 568 |
|
---|
| 569 | (defmfun $poly_grobner_equal (g1 g2 vars)
|
---|
| 570 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
| 571 | (grobner-equal *maxima-ring* g1 g2)))
|
---|
| 572 |
|
---|
| 573 | (defmfun $poly_grobner_subsetp (g1 g2 vars)
|
---|
| 574 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
| 575 | (grobner-subsetp *maxima-ring* g1 g2)))
|
---|
| 576 |
|
---|
| 577 | (defmfun $poly_grobner_member (p g vars)
|
---|
| 578 | (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
|
---|
| 579 | (grobner-member *maxima-ring* p g)))
|
---|
| 580 |
|
---|
| 581 | (defmfun $poly_ideal_saturation1 (f p vars)
|
---|
| 582 | (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
|
---|
| 583 | :value-type :poly-list)
|
---|
| 584 | (ideal-saturation-1 *maxima-ring* f p 0)))
|
---|
| 585 |
|
---|
| 586 | (defmfun $poly_saturation_extension (f plist vars new-vars)
|
---|
| 587 | (with-parsed-polynomials ((vars new-vars)
|
---|
| 588 | :poly-lists (f plist)
|
---|
| 589 | :value-type :poly-list)
|
---|
| 590 | (saturation-extension *maxima-ring* f plist)))
|
---|
| 591 |
|
---|
| 592 | (defmfun $poly_polysaturation_extension (f plist vars new-vars)
|
---|
| 593 | (with-parsed-polynomials ((vars new-vars)
|
---|
| 594 | :poly-lists (f plist)
|
---|
| 595 | :value-type :poly-list)
|
---|
| 596 | (polysaturation-extension *maxima-ring* f plist)))
|
---|
| 597 |
|
---|
| 598 | (defmfun $poly_ideal_polysaturation1 (f plist vars)
|
---|
| 599 | (with-parsed-polynomials ((vars) :poly-lists (f plist)
|
---|
| 600 | :value-type :poly-list)
|
---|
| 601 | (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
|
---|
| 602 |
|
---|
| 603 | (defmfun $poly_ideal_saturation (f g vars)
|
---|
| 604 | (with-parsed-polynomials ((vars) :poly-lists (f g)
|
---|
| 605 | :value-type :poly-list)
|
---|
[26] | 606 | (ideal-saturation *maxima-ring* f g 0 nil)))
|
---|
| 607 |
|
---|
| 608 | (defmfun $poly_ideal_polysaturation (f ideal-list vars)
|
---|
| 609 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 610 | :poly-list-lists (ideal-list)
|
---|
| 611 | :value-type :poly-list)
|
---|
| 612 | (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
|
---|
| 613 |
|
---|
| 614 | (defmfun $poly_lt (f vars)
|
---|
| 615 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
| 616 | (make-poly-from-termlist (list (poly-lt f)))))
|
---|
| 617 |
|
---|
| 618 | (defmfun $poly_lm (f vars)
|
---|
| 619 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
| 620 | (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
|
---|
| 621 |
|
---|