[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 | ;; Maxima expression ring
|
---|
| 39 | ;;
|
---|
| 40 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 41 |
|
---|
| 42 | (defparameter *expression-ring*
|
---|
| 43 | (make-ring
|
---|
| 44 | ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
|
---|
| 45 | :parse #'(lambda (expr)
|
---|
| 46 | (when modulus (setf expr ($rat expr)))
|
---|
| 47 | expr)
|
---|
| 48 | :unit #'(lambda () (if modulus ($rat 1) 1))
|
---|
| 49 | :zerop #'(lambda (expr)
|
---|
| 50 | ;;When is exactly a maxima expression equal to 0?
|
---|
| 51 | (cond ((numberp expr)
|
---|
| 52 | (= expr 0))
|
---|
| 53 | ((atom expr) nil)
|
---|
| 54 | (t
|
---|
| 55 | (case (caar expr)
|
---|
| 56 | (mrat (eql ($ratdisrep expr) 0))
|
---|
| 57 | (otherwise (eql ($totaldisrep expr) 0))))))
|
---|
| 58 | :add #'(lambda (x y) (m+ x y))
|
---|
| 59 | :sub #'(lambda (x y) (m- x y))
|
---|
| 60 | :uminus #'(lambda (x) (m- x))
|
---|
| 61 | :mul #'(lambda (x y) (m* x y))
|
---|
| 62 | ;;(defun coeff-div (x y) (cadr ($divide x y)))
|
---|
| 63 | :div #'(lambda (x y) (m// x y))
|
---|
| 64 | :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
|
---|
| 65 | :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
|
---|
| 66 | ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
|
---|
| 67 | :gcd #'(lambda (x y) ($gcd x y))))
|
---|
| 68 |
|
---|
| 69 | (defvar *maxima-ring* *expression-ring*
|
---|
| 70 | "The ring of coefficients, over which all polynomials
|
---|
| 71 | are assumed to be defined.")
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | |
---|
| 75 |
|
---|
| 76 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 77 | ;;
|
---|
| 78 | ;; Maxima-level interface functions
|
---|
| 79 | ;;
|
---|
| 80 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 81 |
|
---|
| 82 | ;; Auxillary function for removing zero polynomial
|
---|
| 83 | (defun remzero (plist) (remove #'poly-zerop plist))
|
---|
| 84 |
|
---|
| 85 | ;;Simple operators
|
---|
| 86 |
|
---|
| 87 | (define-binop $poly_add poly-add
|
---|
| 88 | "Adds two polynomials P and Q")
|
---|
| 89 |
|
---|
| 90 | (define-binop $poly_subtract poly-sub
|
---|
| 91 | "Subtracts a polynomial Q from P.")
|
---|
| 92 |
|
---|
| 93 | (define-binop $poly_multiply poly-mul
|
---|
| 94 | "Returns the product of polynomials P and Q.")
|
---|
| 95 |
|
---|
| 96 | (define-binop $poly_s_polynomial spoly
|
---|
| 97 | "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
|
---|
| 98 |
|
---|
| 99 | (define-unop $poly_primitive_part poly-primitive-part
|
---|
| 100 | "Returns the polynomial P divided by GCD of its coefficients.")
|
---|
| 101 |
|
---|
| 102 | (define-unop $poly_normalize poly-normalize
|
---|
| 103 | "Returns the polynomial P divided by the leading coefficient.")
|
---|
| 104 |
|
---|
| 105 | ;;Functions
|
---|
| 106 |
|
---|
| 107 | (defmfun $poly_expand (p vars)
|
---|
| 108 | "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
|
---|
| 109 | If the representation is not compatible with a polynomial in variables VARS,
|
---|
| 110 | the result is an error."
|
---|
| 111 | (with-parsed-polynomials ((vars) :polynomials (p)
|
---|
| 112 | :value-type :polynomial)
|
---|
| 113 | p))
|
---|
| 114 |
|
---|
| 115 | (defmfun $poly_expt (p n vars)
|
---|
| 116 | (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
|
---|
| 117 | (poly-expt *maxima-ring* p n)))
|
---|
| 118 |
|
---|
| 119 | (defmfun $poly_content (p vars)
|
---|
| 120 | (with-parsed-polynomials ((vars) :polynomials (p))
|
---|
| 121 | (poly-content *maxima-ring* p)))
|
---|
| 122 |
|
---|
| 123 | (defmfun $poly_pseudo_divide (f fl vars
|
---|
| 124 | &aux (vars (coerce-maxima-list vars))
|
---|
| 125 | (f (parse-poly f vars))
|
---|
| 126 | (fl (parse-poly-list fl vars)))
|
---|
| 127 | (multiple-value-bind (quot rem c division-count)
|
---|
| 128 | (poly-pseudo-divide *maxima-ring* f fl)
|
---|
| 129 | `((mlist)
|
---|
| 130 | ,(coerce-to-maxima :poly-list quot vars)
|
---|
| 131 | ,(coerce-to-maxima :polynomial rem vars)
|
---|
| 132 | ,c
|
---|
| 133 | ,division-count)))
|
---|
| 134 |
|
---|
| 135 | (defmfun $poly_exact_divide (f g vars)
|
---|
| 136 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
| 137 | (poly-exact-divide *maxima-ring* f g)))
|
---|
| 138 |
|
---|
| 139 | (defmfun $poly_normal_form (f fl vars)
|
---|
| 140 | (with-parsed-polynomials ((vars) :polynomials (f)
|
---|
| 141 | :poly-lists (fl)
|
---|
| 142 | :value-type :polynomial)
|
---|
| 143 | (normal-form *maxima-ring* f (remzero fl) nil)))
|
---|
| 144 |
|
---|
| 145 | (defmfun $poly_buchberger_criterion (g vars)
|
---|
| 146 | (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
|
---|
| 147 | (buchberger-criterion *maxima-ring* g)))
|
---|
| 148 |
|
---|
| 149 | (defmfun $poly_buchberger (fl vars)
|
---|
| 150 | (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
|
---|
| 151 | (buchberger *maxima-ring* (remzero fl) 0 nil)))
|
---|
| 152 |
|
---|
| 153 | (defmfun $poly_reduction (plist vars)
|
---|
| 154 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 155 | :value-type :poly-list)
|
---|
| 156 | (reduction *maxima-ring* plist)))
|
---|
| 157 |
|
---|
| 158 | (defmfun $poly_minimization (plist vars)
|
---|
| 159 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 160 | :value-type :poly-list)
|
---|
| 161 | (minimization plist)))
|
---|
| 162 |
|
---|
| 163 | (defmfun $poly_normalize_list (plist vars)
|
---|
| 164 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
| 165 | :value-type :poly-list)
|
---|
| 166 | (poly-normalize-list *maxima-ring* plist)))
|
---|
| 167 |
|
---|
| 168 | (defmfun $poly_grobner (f vars)
|
---|
| 169 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 170 | :value-type :poly-list)
|
---|
| 171 | (grobner *maxima-ring* (remzero f))))
|
---|
| 172 |
|
---|
| 173 | (defmfun $poly_reduced_grobner (f vars)
|
---|
| 174 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 175 | :value-type :poly-list)
|
---|
| 176 | (reduced-grobner *maxima-ring* (remzero f))))
|
---|
| 177 |
|
---|
| 178 | (defmfun $poly_depends_p (p var mvars
|
---|
| 179 | &aux (vars (coerce-maxima-list mvars))
|
---|
| 180 | (pos (position var vars)))
|
---|
| 181 | (if (null pos)
|
---|
| 182 | (merror "~%Variable ~M not in the list of variables ~M." var mvars)
|
---|
| 183 | (poly-depends-p (parse-poly p vars) pos)))
|
---|
| 184 |
|
---|
| 185 | (defmfun $poly_elimination_ideal (flist k vars)
|
---|
| 186 | (with-parsed-polynomials ((vars) :poly-lists (flist)
|
---|
| 187 | :value-type :poly-list)
|
---|
| 188 | (elimination-ideal *maxima-ring* flist k nil 0)))
|
---|
| 189 |
|
---|
| 190 | (defmfun $poly_colon_ideal (f g vars)
|
---|
| 191 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
| 192 | (colon-ideal *maxima-ring* f g nil)))
|
---|
| 193 |
|
---|
| 194 | (defmfun $poly_ideal_intersection (f g vars)
|
---|
| 195 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
| 196 | (ideal-intersection *maxima-ring* f g nil)))
|
---|
| 197 |
|
---|
| 198 | (defmfun $poly_lcm (f g vars)
|
---|
| 199 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
| 200 | (poly-lcm *maxima-ring* f g)))
|
---|
| 201 |
|
---|
| 202 | (defmfun $poly_gcd (f g vars)
|
---|
| 203 | ($first ($divide (m* f g) ($poly_lcm f g vars))))
|
---|
| 204 |
|
---|
| 205 | (defmfun $poly_grobner_equal (g1 g2 vars)
|
---|
| 206 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
| 207 | (grobner-equal *maxima-ring* g1 g2)))
|
---|
| 208 |
|
---|
| 209 | (defmfun $poly_grobner_subsetp (g1 g2 vars)
|
---|
| 210 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
| 211 | (grobner-subsetp *maxima-ring* g1 g2)))
|
---|
| 212 |
|
---|
| 213 | (defmfun $poly_grobner_member (p g vars)
|
---|
| 214 | (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
|
---|
| 215 | (grobner-member *maxima-ring* p g)))
|
---|
| 216 |
|
---|
| 217 | (defmfun $poly_ideal_saturation1 (f p vars)
|
---|
| 218 | (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
|
---|
| 219 | :value-type :poly-list)
|
---|
| 220 | (ideal-saturation-1 *maxima-ring* f p 0)))
|
---|
| 221 |
|
---|
| 222 | (defmfun $poly_saturation_extension (f plist vars new-vars)
|
---|
| 223 | (with-parsed-polynomials ((vars new-vars)
|
---|
| 224 | :poly-lists (f plist)
|
---|
| 225 | :value-type :poly-list)
|
---|
| 226 | (saturation-extension *maxima-ring* f plist)))
|
---|
| 227 |
|
---|
| 228 | (defmfun $poly_polysaturation_extension (f plist vars new-vars)
|
---|
| 229 | (with-parsed-polynomials ((vars new-vars)
|
---|
| 230 | :poly-lists (f plist)
|
---|
| 231 | :value-type :poly-list)
|
---|
| 232 | (polysaturation-extension *maxima-ring* f plist)))
|
---|
| 233 |
|
---|
| 234 | (defmfun $poly_ideal_polysaturation1 (f plist vars)
|
---|
| 235 | (with-parsed-polynomials ((vars) :poly-lists (f plist)
|
---|
| 236 | :value-type :poly-list)
|
---|
| 237 | (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
|
---|
| 238 |
|
---|
| 239 | (defmfun $poly_ideal_saturation (f g vars)
|
---|
| 240 | (with-parsed-polynomials ((vars) :poly-lists (f g)
|
---|
| 241 | :value-type :poly-list)
|
---|
| 242 | (ideal-saturation *maxima-ring* f g 0 nil)))
|
---|
| 243 |
|
---|
| 244 | (defmfun $poly_ideal_polysaturation (f ideal-list vars)
|
---|
| 245 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
| 246 | :poly-list-lists (ideal-list)
|
---|
| 247 | :value-type :poly-list)
|
---|
| 248 | (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
|
---|
| 249 |
|
---|
| 250 | (defmfun $poly_lt (f vars)
|
---|
| 251 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
| 252 | (make-poly-from-termlist (list (poly-lt f)))))
|
---|
| 253 |
|
---|
| 254 | (defmfun $poly_lm (f vars)
|
---|
| 255 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
| 256 | (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
|
---|
| 257 |
|
---|