| 1 | ;;; -*- Mode: Lisp -*-
|
|---|
| 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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 23 | ;;
|
|---|
| 24 | ;; Load this file into Maxima to bootstrap the Grobner package.
|
|---|
| 25 | ;; NOTE: This file does use symbols defined by Maxima, so it
|
|---|
| 26 | ;; will not work when loaded in Common Lisp.
|
|---|
| 27 | ;;
|
|---|
| 28 | ;; DETAILS: This file implements an interface between the Grobner
|
|---|
| 29 | ;; basis package NGROBNER, which is a pure Common Lisp package, and
|
|---|
| 30 | ;; Maxima. NGROBNER for efficiency uses its own representation of
|
|---|
| 31 | ;; polynomials. Thus, it is necessary to convert Maxima representation
|
|---|
| 32 | ;; to the internal representation and back. The facilities to do so
|
|---|
| 33 | ;; are implemented in this file.
|
|---|
| 34 | ;;
|
|---|
| 35 | ;; Also, since the NGROBNER package consists of many Lisp files, it is
|
|---|
| 36 | ;; necessary to load the files. It is possible and preferrable to use
|
|---|
| 37 | ;; ASDF for this purpose. The default is ASDF. It is also possible to
|
|---|
| 38 | ;; simply used LOAD and COMPILE-FILE to accomplish this task.
|
|---|
| 39 | ;;
|
|---|
| 40 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 41 |
|
|---|
| 42 | (in-package :maxima)
|
|---|
| 43 |
|
|---|
| 44 | (macsyma-module cgb-maxima)
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | (eval-when
|
|---|
| 48 | #+gcl (load eval)
|
|---|
| 49 | #-gcl (:load-toplevel :execute)
|
|---|
| 50 | (format t "~&Loading maxima-grobner ~a ~a~%"
|
|---|
| 51 | "$Revision: 2.0 $" "$Date: 2015/06/02 0:34:17 $"))
|
|---|
| 52 |
|
|---|
| 53 | ;;FUNCTS is loaded because it contains the definition of LCM
|
|---|
| 54 | ($load "functs")
|
|---|
| 55 | #+sbcl(progn (require 'asdf) (load "ngrobner.asd")(asdf:load-system :ngrobner))
|
|---|
| 56 |
|
|---|
| 57 | (use-package :ngrobner)
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 61 | ;;
|
|---|
| 62 | ;; Maxima expression ring
|
|---|
| 63 | ;;
|
|---|
| 64 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 65 | ;;
|
|---|
| 66 | ;; This is how we perform operations on coefficients
|
|---|
| 67 | ;; using Maxima functions.
|
|---|
| 68 | ;;
|
|---|
| 69 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 70 |
|
|---|
| 71 | (defparameter +maxima-ring+
|
|---|
| 72 | (make-ring
|
|---|
| 73 | ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
|
|---|
| 74 | :parse #'(lambda (expr)
|
|---|
| 75 | (when modulus (setf expr ($rat expr)))
|
|---|
| 76 | expr)
|
|---|
| 77 | :unit #'(lambda () (if modulus ($rat 1) 1))
|
|---|
| 78 | :zerop #'(lambda (expr)
|
|---|
| 79 | ;;When is exactly a maxima expression equal to 0?
|
|---|
| 80 | (cond ((numberp expr)
|
|---|
| 81 | (= expr 0))
|
|---|
| 82 | ((atom expr) nil)
|
|---|
| 83 | (t
|
|---|
| 84 | (case (caar expr)
|
|---|
| 85 | (mrat (eql ($ratdisrep expr) 0))
|
|---|
| 86 | (otherwise (eql ($totaldisrep expr) 0))))))
|
|---|
| 87 | :add #'(lambda (x y) (m+ x y))
|
|---|
| 88 | :sub #'(lambda (x y) (m- x y))
|
|---|
| 89 | :uminus #'(lambda (x) (m- x))
|
|---|
| 90 | :mul #'(lambda (x y) (m* x y))
|
|---|
| 91 | ;;(defun coeff-div (x y) (cadr ($divide x y)))
|
|---|
| 92 | :div #'(lambda (x y) (m// x y))
|
|---|
| 93 | :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
|
|---|
| 94 | :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
|
|---|
| 95 | ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
|
|---|
| 96 | :gcd #'(lambda (x y) ($gcd x y))))
|
|---|
| 97 |
|
|---|
| 98 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 99 | ;;
|
|---|
| 100 | ;; Maxima expression parsing
|
|---|
| 101 | ;;
|
|---|
| 102 | ;;
|
|---|
| 103 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 104 | ;;
|
|---|
| 105 | ;; Functions and macros dealing with internal representation
|
|---|
| 106 | ;; structure.
|
|---|
| 107 | ;;
|
|---|
| 108 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 109 |
|
|---|
| 110 | (defun equal-test-p (expr1 expr2)
|
|---|
| 111 | (alike1 expr1 expr2))
|
|---|
| 112 |
|
|---|
| 113 | (defun coerce-maxima-list (expr)
|
|---|
| 114 | "Convert a Maxima list to Lisp list."
|
|---|
| 115 | (cond
|
|---|
| 116 | ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
|
|---|
| 117 | (t expr)))
|
|---|
| 118 |
|
|---|
| 119 | (defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
|
|---|
| 120 |
|
|---|
| 121 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 122 | ;;
|
|---|
| 123 | ;; Order utilities
|
|---|
| 124 | ;;
|
|---|
| 125 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 126 |
|
|---|
| 127 | (defun find-ring-by-name (ring)
|
|---|
| 128 | "This function returns the ring structure bases on input symbol."
|
|---|
| 129 | (cond
|
|---|
| 130 | ((null ring) nil)
|
|---|
| 131 | ((symbolp ring)
|
|---|
| 132 | (print "Its a symbol!!!~A%" ring)
|
|---|
| 133 | (case ring
|
|---|
| 134 | ((maxima-ring :maxima-ring #:maxima-ring $expression_ring #:expression_ring)
|
|---|
| 135 | +maxima-ring+)
|
|---|
| 136 | ((ring-of-integers :ring-of-integers #:ring-of-integers $ring_of_integers) +ring-of-integers+)
|
|---|
| 137 | (otherwise
|
|---|
| 138 | (mtell "~%Warning: Ring ~M not found. Using default.~%" ring))))
|
|---|
| 139 | (t
|
|---|
| 140 | (mtell "~%Ring specification ~M is not recognized. Using default.~%" ring)
|
|---|
| 141 | nil)))
|
|---|
| 142 |
|
|---|
| 143 | (defun find-order-by-name (order)
|
|---|
| 144 | "This function returns the order function bases on its name."
|
|---|
| 145 | (cond
|
|---|
| 146 | ((null order) nil)
|
|---|
| 147 | ((symbolp order)
|
|---|
| 148 | (case order
|
|---|
| 149 | ((lex :lex $lex #:lex)
|
|---|
| 150 | #'lex>)
|
|---|
| 151 | ((grlex :grlex $grlex #:grlex)
|
|---|
| 152 | #'grlex>)
|
|---|
| 153 | ((grevlex :grevlex $grevlex #:grevlex)
|
|---|
| 154 | #'grevlex>)
|
|---|
| 155 | ((invlex :invlex $invlex #:invlex)
|
|---|
| 156 | #'invlex>)
|
|---|
| 157 | (otherwise
|
|---|
| 158 | (mtell "~%Warning: Order ~M not found. Using default.~%" order))))
|
|---|
| 159 | (t
|
|---|
| 160 | (mtell "~%Order specification ~M is not recognized. Using default.~%" order)
|
|---|
| 161 | nil)))
|
|---|
| 162 |
|
|---|
| 163 | (defun maxima->poly (expr vars
|
|---|
| 164 | &optional
|
|---|
| 165 | (ring (find-ring-by-name $poly_coefficient_ring))
|
|---|
| 166 | (order (find-order-by-name $poly_monomial_order))
|
|---|
| 167 | (primary-elimination-order (find-order-by-name $poly_primary_elimination_order))
|
|---|
| 168 | (secondary-elimination-order (find-order-by-name $poly_secondary_elimination_order))
|
|---|
| 169 | &aux
|
|---|
| 170 | (vars (coerce-maxima-list vars))
|
|---|
| 171 | (ring-and-order (make-ring-and-order
|
|---|
| 172 | :ring ring
|
|---|
| 173 | :order order
|
|---|
| 174 | :primary-elimination-order primary-elimination-order
|
|---|
| 175 | :secondary-elimination-order secondary-elimination-order))
|
|---|
| 176 | (ring (ro-ring ring-and-order)))
|
|---|
| 177 | "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
|
|---|
| 178 | (labels ((parse (arg) (maxima->poly arg vars ring order primary-elimination-order secondary-elimination-order))
|
|---|
| 179 | (parse-list (args) (mapcar #'parse args)))
|
|---|
| 180 | (cond
|
|---|
| 181 | ((eql expr 0) (make-poly-zero))
|
|---|
| 182 | ((member expr vars :test #'equal-test-p)
|
|---|
| 183 | (let ((pos (position expr vars :test #'equal-test-p)))
|
|---|
| 184 | (make-poly-variable (ro-ring ring-and-order) (length vars) pos)))
|
|---|
| 185 | ((free-of-vars expr vars)
|
|---|
| 186 | ;;This means that variable-free CRE and Poisson forms will be converted
|
|---|
| 187 | ;;to coefficients intact
|
|---|
| 188 | (coerce-coeff (ro-ring ring-and-order) expr vars))
|
|---|
| 189 | (t
|
|---|
| 190 | (case (caar expr)
|
|---|
| 191 | (mplus (reduce #'(lambda (x y) (poly-add ring-and-order x y)) (parse-list (cdr expr))))
|
|---|
| 192 | (mminus (poly-uminus (ro-ring ring-and-order) (parse (cadr expr))))
|
|---|
| 193 | (mtimes
|
|---|
| 194 | (if (endp (cddr expr)) ;unary
|
|---|
| 195 | (parse (cdr expr))
|
|---|
| 196 | (reduce #'(lambda (p q) (poly-mul ring-and-order p q)) (parse-list (cdr expr)))))
|
|---|
| 197 | (mexpt
|
|---|
| 198 | (cond
|
|---|
| 199 | ((member (cadr expr) vars :test #'equal-test-p)
|
|---|
| 200 | ;;Special handling of (expt var pow)
|
|---|
| 201 | (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
|
|---|
| 202 | (make-poly-variable (ro-ring ring-and-order) (length vars) pos (caddr expr))))
|
|---|
| 203 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
|---|
| 204 | ;; Negative power means division in coefficient ring
|
|---|
| 205 | ;; Non-integer power means non-polynomial coefficient
|
|---|
| 206 | (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
|
|---|
| 207 | expr)
|
|---|
| 208 | (coerce-coeff (ro-ring ring-and-order) expr vars))
|
|---|
| 209 | (t (poly-expt (ro-ring ring-and-order) (parse (cadr expr)) (caddr expr)))))
|
|---|
| 210 | (mrat (parse ($ratdisrep expr)))
|
|---|
| 211 | (mpois (parse ($outofpois expr)))
|
|---|
| 212 | (otherwise
|
|---|
| 213 | (coerce-coeff (ro-ring ring-and-order) expr vars)))))))
|
|---|
| 214 |
|
|---|
| 215 | #|
|
|---|
| 216 |
|
|---|
| 217 | (defun parse-poly-list (expr vars)
|
|---|
| 218 | "Parse a Maxima representation of a list of polynomials."
|
|---|
| 219 | (case (caar expr)
|
|---|
| 220 | (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
|
|---|
| 221 | (t (merror "Expression ~M is not a list of polynomials in variables ~M."
|
|---|
| 222 | expr vars))))
|
|---|
| 223 |
|
|---|
| 224 | (defun parse-poly-list-list (poly-list-list vars)
|
|---|
| 225 | "Parse a Maxima representation of a list of lists of polynomials."
|
|---|
| 226 | (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 230 | ;;
|
|---|
| 231 | ;; Conversion from internal form to Maxima general form
|
|---|
| 232 | ;;
|
|---|
| 233 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 234 |
|
|---|
| 235 | (defun maxima-head ()
|
|---|
| 236 | (if $poly_return_term_list
|
|---|
| 237 | '(mlist)
|
|---|
| 238 | '(mplus)))
|
|---|
| 239 |
|
|---|
| 240 | (defun coerce-to-maxima (poly-type object vars)
|
|---|
| 241 | (case poly-type
|
|---|
| 242 | (:polynomial
|
|---|
| 243 | `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
|
|---|
| 244 | (:poly-list
|
|---|
| 245 | `((mlist) ,@(mapcar #'(lambda (p) (funcall *ratdisrep-fun* (coerce-to-maxima :polynomial p vars))) object)))
|
|---|
| 246 | (:term
|
|---|
| 247 | `((mtimes) ,(funcall *ratdisrep-fun* (term-coeff object))
|
|---|
| 248 | ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
|
|---|
| 249 | vars (coerce (term-monom object) 'list))))
|
|---|
| 250 | ;; Assumes that Lisp and Maxima logicals coincide
|
|---|
| 251 | (:logical object)
|
|---|
| 252 | (otherwise
|
|---|
| 253 | object)))
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 257 | ;;
|
|---|
| 258 | ;; Unary and binary operation definition facility
|
|---|
| 259 | ;;
|
|---|
| 260 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 261 |
|
|---|
| 262 | (defmacro define-unop (maxima-name fun-name
|
|---|
| 263 | &optional (documentation nil documentation-supplied-p))
|
|---|
| 264 | "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
|
|---|
| 265 | `(defun ,maxima-name (p vars
|
|---|
| 266 | &aux
|
|---|
| 267 | (vars (coerce-maxima-list vars))
|
|---|
| 268 | (p (parse-poly p vars)))
|
|---|
| 269 | ,@(when documentation-supplied-p (list documentation))
|
|---|
| 270 | (coerce-to-maxima :polynomial (,fun-name +maxima-ring+ p) vars)))
|
|---|
| 271 |
|
|---|
| 272 | (defmacro define-binop (maxima-name fun-name
|
|---|
| 273 | &optional (documentation nil documentation-supplied-p))
|
|---|
| 274 | "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
|
|---|
| 275 | `(defmfun ,maxima-name (p q vars
|
|---|
| 276 | &aux
|
|---|
| 277 | (vars (coerce-maxima-list vars))
|
|---|
| 278 | (p (parse-poly p vars))
|
|---|
| 279 | (q (parse-poly q vars)))
|
|---|
| 280 | ,@(when documentation-supplied-p (list documentation))
|
|---|
| 281 | (coerce-to-maxima :polynomial (,fun-name +maxima-ring+ p q) vars)))
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 285 | ;;
|
|---|
| 286 | ;; Facilities for evaluating Grobner package expressions
|
|---|
| 287 | ;; within a prepared environment
|
|---|
| 288 | ;;
|
|---|
| 289 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 290 |
|
|---|
| 291 | (defmacro with-monomial-order ((order) &body body)
|
|---|
| 292 | "Evaluate BODY with monomial order set to ORDER."
|
|---|
| 293 | `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
|
|---|
| 294 | . ,body))
|
|---|
| 295 |
|
|---|
| 296 | (defmacro with-coefficient-ring ((ring) &body body)
|
|---|
| 297 | "Evaluate BODY with coefficient ring set to RING."
|
|---|
| 298 | `(let ((+maxima-ring+ (or (find-ring ,ring) +maxima-ring+)))
|
|---|
| 299 | . ,body))
|
|---|
| 300 |
|
|---|
| 301 | (defmacro with-ring-and-order ((ring order) &body body)
|
|---|
| 302 | "Evaluate BODY with monomial order set to ORDER and coefficient ring set to RING."
|
|---|
| 303 | `(let ((*monomial-order* (or (find-order ,order) *monomial-order*))
|
|---|
| 304 | (+maxima-ring+ (or (find-ring ,ring) +maxima-ring+)))
|
|---|
| 305 | . ,body))
|
|---|
| 306 |
|
|---|
| 307 | (defmacro with-elimination-orders ((primary secondary elimination-order)
|
|---|
| 308 | &body body)
|
|---|
| 309 | "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
|
|---|
| 310 | `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
|
|---|
| 311 | (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
|
|---|
| 312 | (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
|
|---|
| 313 | . ,body))
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 317 | ;;
|
|---|
| 318 | ;; Maxima-level interface functions
|
|---|
| 319 | ;;
|
|---|
| 320 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 321 |
|
|---|
| 322 | ;; Auxillary function for removing zero polynomial
|
|---|
| 323 | (defun remzero (plist) (remove #'poly-zerop plist))
|
|---|
| 324 |
|
|---|
| 325 | ;;Simple operators
|
|---|
| 326 |
|
|---|
| 327 | (define-binop $poly_add poly-add
|
|---|
| 328 | "Adds two polynomials P and Q")
|
|---|
| 329 |
|
|---|
| 330 | (define-binop $poly_subtract poly-sub
|
|---|
| 331 | "Subtracts a polynomial Q from P.")
|
|---|
| 332 |
|
|---|
| 333 | (define-binop $poly_multiply poly-mul
|
|---|
| 334 | "Returns the product of polynomials P and Q.")
|
|---|
| 335 |
|
|---|
| 336 | (define-binop $poly_s_polynomial spoly
|
|---|
| 337 | "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
|
|---|
| 338 |
|
|---|
| 339 | (define-unop $poly_primitive_part poly-primitive-part
|
|---|
| 340 | "Returns the polynomial P divided by GCD of its coefficients.")
|
|---|
| 341 |
|
|---|
| 342 | (define-unop $poly_normalize poly-normalize
|
|---|
| 343 | "Returns the polynomial P divided by the leading coefficient.")
|
|---|
| 344 |
|
|---|
| 345 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 346 | ;;
|
|---|
| 347 | ;; Macro facility for writing Maxima-level wrappers for
|
|---|
| 348 | ;; functions operating on internal representation
|
|---|
| 349 | ;;
|
|---|
| 350 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|---|
| 351 |
|
|---|
| 352 | (defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
|
|---|
| 353 | &key (polynomials nil)
|
|---|
| 354 | (poly-lists nil)
|
|---|
| 355 | (poly-list-lists nil)
|
|---|
| 356 | (value-type nil))
|
|---|
| 357 | &body body
|
|---|
| 358 | &aux (vars (gensym))
|
|---|
| 359 | (new-vars (gensym)))
|
|---|
| 360 | `(let ((,vars (coerce-maxima-list ,maxima-vars))
|
|---|
| 361 | ,@(when new-vars-supplied-p
|
|---|
| 362 | (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
|
|---|
| 363 | (coerce-to-maxima
|
|---|
| 364 | ,value-type
|
|---|
| 365 | (with-coefficient-ring ($poly_coefficient_ring)
|
|---|
| 366 | (with-monomial-order ($poly_monomial_order)
|
|---|
| 367 | (with-elimination-orders ($poly_primary_elimination_order
|
|---|
| 368 | $poly_secondary_elimination_order
|
|---|
| 369 | $poly_elimination_order)
|
|---|
| 370 | (let ,(let ((args nil))
|
|---|
| 371 | (dolist (p polynomials args)
|
|---|
| 372 | (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
|
|---|
| 373 | (dolist (p poly-lists args)
|
|---|
| 374 | (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
|
|---|
| 375 | (dolist (p poly-list-lists args)
|
|---|
| 376 | (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
|
|---|
| 377 | . ,body))))
|
|---|
| 378 | ,(if new-vars-supplied-p
|
|---|
| 379 | `(append ,vars ,new-vars)
|
|---|
| 380 | vars))))
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 | ;;Functions
|
|---|
| 384 |
|
|---|
| 385 | (defmfun $poly_expand (p vars)
|
|---|
| 386 | "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
|
|---|
| 387 | If the representation is not compatible with a polynomial in variables VARS,
|
|---|
| 388 | the result is an error."
|
|---|
| 389 | (with-parsed-polynomials ((vars) :polynomials (p)
|
|---|
| 390 | :value-type :polynomial)
|
|---|
| 391 | p))
|
|---|
| 392 |
|
|---|
| 393 | (defmfun $poly_expt (p n vars)
|
|---|
| 394 | (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
|
|---|
| 395 | (poly-expt +maxima-ring+ p n)))
|
|---|
| 396 |
|
|---|
| 397 | (defmfun $poly_content (p vars)
|
|---|
| 398 | (with-parsed-polynomials ((vars) :polynomials (p))
|
|---|
| 399 | (poly-content +maxima-ring+ p)))
|
|---|
| 400 |
|
|---|
| 401 | (defmfun $poly_pseudo_divide (f fl vars
|
|---|
| 402 | &aux (vars (coerce-maxima-list vars))
|
|---|
| 403 | (f (parse-poly f vars))
|
|---|
| 404 | (fl (parse-poly-list fl vars)))
|
|---|
| 405 | (multiple-value-bind (quot rem c division-count)
|
|---|
| 406 | (poly-pseudo-divide +maxima-ring+ f fl)
|
|---|
| 407 | `((mlist)
|
|---|
| 408 | ,(coerce-to-maxima :poly-list quot vars)
|
|---|
| 409 | ,(coerce-to-maxima :polynomial rem vars)
|
|---|
| 410 | ,c
|
|---|
| 411 | ,division-count)))
|
|---|
| 412 |
|
|---|
| 413 | (defmfun $poly_exact_divide (f g vars)
|
|---|
| 414 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
|---|
| 415 | (poly-exact-divide +maxima-ring+ f g)))
|
|---|
| 416 |
|
|---|
| 417 | (defmfun $poly_normal_form (f fl vars)
|
|---|
| 418 | (with-parsed-polynomials ((vars) :polynomials (f)
|
|---|
| 419 | :poly-lists (fl)
|
|---|
| 420 | :value-type :polynomial)
|
|---|
| 421 | (normal-form +maxima-ring+ f (remzero fl) nil)))
|
|---|
| 422 |
|
|---|
| 423 | (defmfun $poly_buchberger_criterion (g vars)
|
|---|
| 424 | (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
|
|---|
| 425 | (buchberger-criterion +maxima-ring+ g)))
|
|---|
| 426 |
|
|---|
| 427 | (defmfun $poly_buchberger (fl vars)
|
|---|
| 428 | (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
|
|---|
| 429 | (buchberger +maxima-ring+ (remzero fl) 0 nil)))
|
|---|
| 430 |
|
|---|
| 431 | (defmfun $poly_reduction (plist vars)
|
|---|
| 432 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 433 | :value-type :poly-list)
|
|---|
| 434 | (reduction +maxima-ring+ plist)))
|
|---|
| 435 |
|
|---|
| 436 | (defmfun $poly_minimization (plist vars)
|
|---|
| 437 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 438 | :value-type :poly-list)
|
|---|
| 439 | (minimization plist)))
|
|---|
| 440 |
|
|---|
| 441 | (defmfun $poly_normalize_list (plist vars)
|
|---|
| 442 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
|---|
| 443 | :value-type :poly-list)
|
|---|
| 444 | (poly-normalize-list +maxima-ring+ plist)))
|
|---|
| 445 |
|
|---|
| 446 | (defmfun $poly_grobner (f vars)
|
|---|
| 447 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 448 | :value-type :poly-list)
|
|---|
| 449 | (grobner +maxima-ring+ (remzero f))))
|
|---|
| 450 |
|
|---|
| 451 | (defmfun $poly_reduced_grobner (f vars)
|
|---|
| 452 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 453 | :value-type :poly-list)
|
|---|
| 454 | (reduced-grobner +maxima-ring+ (remzero f))))
|
|---|
| 455 |
|
|---|
| 456 | (defmfun $poly_depends_p (p var mvars
|
|---|
| 457 | &aux (vars (coerce-maxima-list mvars))
|
|---|
| 458 | (pos (position var vars)))
|
|---|
| 459 | (if (null pos)
|
|---|
| 460 | (merror "~%Variable ~M not in the list of variables ~M." var mvars)
|
|---|
| 461 | (poly-depends-p (parse-poly p vars) pos)))
|
|---|
| 462 |
|
|---|
| 463 | (defmfun $poly_elimination_ideal (flist k vars)
|
|---|
| 464 | (with-parsed-polynomials ((vars) :poly-lists (flist)
|
|---|
| 465 | :value-type :poly-list)
|
|---|
| 466 | (elimination-ideal +maxima-ring+ flist k nil 0)))
|
|---|
| 467 |
|
|---|
| 468 | (defmfun $poly_colon_ideal (f g vars)
|
|---|
| 469 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
|---|
| 470 | (colon-ideal +maxima-ring+ f g nil)))
|
|---|
| 471 |
|
|---|
| 472 | (defmfun $poly_ideal_intersection (f g vars)
|
|---|
| 473 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
|---|
| 474 | (ideal-intersection +maxima-ring+ f g nil)))
|
|---|
| 475 |
|
|---|
| 476 | (defmfun $poly_lcm (f g vars)
|
|---|
| 477 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
|---|
| 478 | (poly-lcm +maxima-ring+ f g)))
|
|---|
| 479 |
|
|---|
| 480 | (defmfun $poly_gcd (f g vars)
|
|---|
| 481 | ($first ($divide (m* f g) ($poly_lcm f g vars))))
|
|---|
| 482 |
|
|---|
| 483 | (defmfun $poly_grobner_equal (g1 g2 vars)
|
|---|
| 484 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
|---|
| 485 | (grobner-equal +maxima-ring+ g1 g2)))
|
|---|
| 486 |
|
|---|
| 487 | (defmfun $poly_grobner_subsetp (g1 g2 vars)
|
|---|
| 488 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
|---|
| 489 | (grobner-subsetp +maxima-ring+ g1 g2)))
|
|---|
| 490 |
|
|---|
| 491 | (defmfun $poly_grobner_member (p g vars)
|
|---|
| 492 | (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
|
|---|
| 493 | (grobner-member +maxima-ring+ p g)))
|
|---|
| 494 |
|
|---|
| 495 | (defmfun $poly_ideal_saturation1 (f p vars)
|
|---|
| 496 | (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
|
|---|
| 497 | :value-type :poly-list)
|
|---|
| 498 | (ideal-saturation-1 +maxima-ring+ f p 0)))
|
|---|
| 499 |
|
|---|
| 500 | (defmfun $poly_saturation_extension (f plist vars new-vars)
|
|---|
| 501 | (with-parsed-polynomials ((vars new-vars)
|
|---|
| 502 | :poly-lists (f plist)
|
|---|
| 503 | :value-type :poly-list)
|
|---|
| 504 | (saturation-extension +maxima-ring+ f plist)))
|
|---|
| 505 |
|
|---|
| 506 | (defmfun $poly_polysaturation_extension (f plist vars new-vars)
|
|---|
| 507 | (with-parsed-polynomials ((vars new-vars)
|
|---|
| 508 | :poly-lists (f plist)
|
|---|
| 509 | :value-type :poly-list)
|
|---|
| 510 | (polysaturation-extension +maxima-ring+ f plist)))
|
|---|
| 511 |
|
|---|
| 512 | (defmfun $poly_ideal_polysaturation1 (f plist vars)
|
|---|
| 513 | (with-parsed-polynomials ((vars) :poly-lists (f plist)
|
|---|
| 514 | :value-type :poly-list)
|
|---|
| 515 | (ideal-polysaturation-1 +maxima-ring+ f plist 0 nil)))
|
|---|
| 516 |
|
|---|
| 517 | (defmfun $poly_ideal_saturation (f g vars)
|
|---|
| 518 | (with-parsed-polynomials ((vars) :poly-lists (f g)
|
|---|
| 519 | :value-type :poly-list)
|
|---|
| 520 | (ideal-saturation +maxima-ring+ f g 0 nil)))
|
|---|
| 521 |
|
|---|
| 522 | (defmfun $poly_ideal_polysaturation (f ideal-list vars)
|
|---|
| 523 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
|---|
| 524 | :poly-list-lists (ideal-list)
|
|---|
| 525 | :value-type :poly-list)
|
|---|
| 526 | (ideal-polysaturation +maxima-ring+ f ideal-list 0 nil)))
|
|---|
| 527 |
|
|---|
| 528 | (defmfun $poly_lt (f vars)
|
|---|
| 529 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
|---|
| 530 | (make-poly-from-termlist (list (poly-lt f)))))
|
|---|
| 531 |
|
|---|
| 532 | (defmfun $poly_lm (f vars)
|
|---|
| 533 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
|---|
| 534 | (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit +maxima-ring+)))))))
|
|---|
| 535 |
|
|---|
| 536 | |#
|
|---|