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