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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
23 | ;;
|
---|
24 | ;; Load this file into Maxima to bootstrap the Grobner package
|
---|
25 | ;;
|
---|
26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
27 |
|
---|
28 | (in-package :maxima)
|
---|
29 |
|
---|
30 | (macsyma-module cgb-maxima)
|
---|
31 |
|
---|
32 | (eval-when
|
---|
33 | #+gcl (load eval)
|
---|
34 | #-gcl (:load-toplevel :execute)
|
---|
35 | (format t "~&Loading maxima-grobner ~a ~a~%"
|
---|
36 | "$Revision: 2.0 $" "$Date: 2015/06/02 0:34:17 $"))
|
---|
37 |
|
---|
38 | ;;FUNCTS is loaded because it contains the definition of LCM
|
---|
39 | ($load "functs")
|
---|
40 |
|
---|
41 | ($load "ngrobner-package")
|
---|
42 | ($load "utils")
|
---|
43 | ($load "ngrobner")
|
---|
44 | ($load "monomial")
|
---|
45 | ($load "order")
|
---|
46 | ($load "order-mk")
|
---|
47 | ($load "term")
|
---|
48 | ($load "termlist")
|
---|
49 | ($load "polynomial")
|
---|
50 | ($load "priority-queue")
|
---|
51 | ($load "pair-queue")
|
---|
52 | ($load "division")
|
---|
53 | ($load "criterion")
|
---|
54 | ($load "buchberger")
|
---|
55 | ($load "gebauer-moeller")
|
---|
56 | ($load "gb-postprocessing")
|
---|
57 | ($load "ideal")
|
---|
58 |
|
---|
59 | (dolist (s '(buchberger buchberger-criterion coerce-coeff
|
---|
60 | coerce-to-maxima colon-ideal elimination-ideal grobner
|
---|
61 | grobner-equal grobner-member grobner-subsetp
|
---|
62 | ideal-intersection ideal-polysaturation
|
---|
63 | ideal-polysaturation-1 ideal-saturation
|
---|
64 | ideal-saturation-1 make-poly-from-termlist make-poly-zero
|
---|
65 | make-term make-variable minimization normal-form poly-add
|
---|
66 | poly-content poly-depends-p poly-exact-divide poly-expt
|
---|
67 | poly-lcm poly-lm poly-lt poly-mul poly-normalize
|
---|
68 | poly-normalize-list poly-primitive-part
|
---|
69 | poly-pseudo-divide poly-sub poly-uminus poly-zerop
|
---|
70 | polysaturation-extension reduced-grobner reduction
|
---|
71 | ring-unit saturation-extension spoly
|
---|
72 | with-parsed-polynomial make-ring))
|
---|
73 | (mtell "Importing ~M~%" s)
|
---|
74 | (import s :ngrobner))
|
---|
75 |
|
---|
76 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
77 | ;;
|
---|
78 | ;; Maxima expression ring
|
---|
79 | ;;
|
---|
80 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
81 |
|
---|
82 | (defparameter *expression-ring*
|
---|
83 | (ngrobner:make-ring
|
---|
84 | ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
|
---|
85 | :parse #'(lambda (expr)
|
---|
86 | (when modulus (setf expr ($rat expr)))
|
---|
87 | expr)
|
---|
88 | :unit #'(lambda () (if modulus ($rat 1) 1))
|
---|
89 | :zerop #'(lambda (expr)
|
---|
90 | ;;When is exactly a maxima expression equal to 0?
|
---|
91 | (cond ((numberp expr)
|
---|
92 | (= expr 0))
|
---|
93 | ((atom expr) nil)
|
---|
94 | (t
|
---|
95 | (case (caar expr)
|
---|
96 | (mrat (eql ($ratdisrep expr) 0))
|
---|
97 | (otherwise (eql ($totaldisrep expr) 0))))))
|
---|
98 | :add #'(lambda (x y) (m+ x y))
|
---|
99 | :sub #'(lambda (x y) (m- x y))
|
---|
100 | :uminus #'(lambda (x) (m- x))
|
---|
101 | :mul #'(lambda (x y) (m* x y))
|
---|
102 | ;;(defun coeff-div (x y) (cadr ($divide x y)))
|
---|
103 | :div #'(lambda (x y) (m// x y))
|
---|
104 | :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
|
---|
105 | :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
|
---|
106 | ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
|
---|
107 | :gcd #'(lambda (x y) ($gcd x y))))
|
---|
108 |
|
---|
109 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
110 | ;;
|
---|
111 | ;; Maxima expression parsing
|
---|
112 | ;;
|
---|
113 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
114 |
|
---|
115 | (defun equal-test-p (expr1 expr2)
|
---|
116 | (alike1 expr1 expr2))
|
---|
117 |
|
---|
118 | (defun coerce-maxima-list (expr)
|
---|
119 | "convert a maxima list to lisp list."
|
---|
120 | (cond
|
---|
121 | ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
|
---|
122 | (t expr)))
|
---|
123 |
|
---|
124 | (defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
|
---|
125 |
|
---|
126 | (defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
|
---|
127 | "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
|
---|
128 | (labels ((parse (arg) (parse-poly arg vars))
|
---|
129 | (parse-list (args) (mapcar #'parse args)))
|
---|
130 | (cond
|
---|
131 | ((eql expr 0) (make-poly-zero))
|
---|
132 | ((member expr vars :test #'equal-test-p)
|
---|
133 | (let ((pos (position expr vars :test #'equal-test-p)))
|
---|
134 | (make-variable *maxima-ring* (length vars) pos)))
|
---|
135 | ((free-of-vars expr vars)
|
---|
136 | ;;This means that variable-free CRE and Poisson forms will be converted
|
---|
137 | ;;to coefficients intact
|
---|
138 | (coerce-coeff *maxima-ring* expr vars))
|
---|
139 | (t
|
---|
140 | (case (caar expr)
|
---|
141 | (mplus (reduce #'(lambda (x y) (poly-add *maxima-ring* x y)) (parse-list (cdr expr))))
|
---|
142 | (mminus (poly-uminus *maxima-ring* (parse (cadr expr))))
|
---|
143 | (mtimes
|
---|
144 | (if (endp (cddr expr)) ;unary
|
---|
145 | (parse (cdr expr))
|
---|
146 | (reduce #'(lambda (p q) (poly-mul *maxima-ring* p q)) (parse-list (cdr expr)))))
|
---|
147 | (mexpt
|
---|
148 | (cond
|
---|
149 | ((member (cadr expr) vars :test #'equal-test-p)
|
---|
150 | ;;Special handling of (expt var pow)
|
---|
151 | (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
|
---|
152 | (make-variable *maxima-ring* (length vars) pos (caddr expr))))
|
---|
153 | ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
|
---|
154 | ;; Negative power means division in coefficient ring
|
---|
155 | ;; Non-integer power means non-polynomial coefficient
|
---|
156 | (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
|
---|
157 | expr)
|
---|
158 | (coerce-coeff *maxima-ring* expr vars))
|
---|
159 | (t (poly-expt *maxima-ring* (parse (cadr expr)) (caddr expr)))))
|
---|
160 | (mrat (parse ($ratdisrep expr)))
|
---|
161 | (mpois (parse ($outofpois expr)))
|
---|
162 | (otherwise
|
---|
163 | (coerce-coeff *maxima-ring* expr vars)))))))
|
---|
164 |
|
---|
165 | (defun parse-poly-list (expr vars)
|
---|
166 | (case (caar expr)
|
---|
167 | (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
|
---|
168 | (t (merror "Expression ~M is not a list of polynomials in variables ~M."
|
---|
169 | expr vars))))
|
---|
170 | (defun parse-poly-list-list (poly-list-list vars)
|
---|
171 | (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
|
---|
172 |
|
---|
173 |
|
---|
174 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
175 | ;;
|
---|
176 | ;; Unary and binary operation definition facility
|
---|
177 | ;;
|
---|
178 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
179 |
|
---|
180 | (defmacro define-unop (maxima-name fun-name
|
---|
181 | &optional (documentation nil documentation-supplied-p))
|
---|
182 | "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
|
---|
183 | `(defun ,maxima-name (p vars
|
---|
184 | &aux
|
---|
185 | (vars (coerce-maxima-list vars))
|
---|
186 | (p (parse-poly p vars)))
|
---|
187 | ,@(when documentation-supplied-p (list documentation))
|
---|
188 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
|
---|
189 |
|
---|
190 | (defmacro define-binop (maxima-name fun-name
|
---|
191 | &optional (documentation nil documentation-supplied-p))
|
---|
192 | "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
|
---|
193 | `(defmfun ,maxima-name (p q vars
|
---|
194 | &aux
|
---|
195 | (vars (coerce-maxima-list vars))
|
---|
196 | (p (parse-poly p vars))
|
---|
197 | (q (parse-poly q vars)))
|
---|
198 | ,@(when documentation-supplied-p (list documentation))
|
---|
199 | (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
|
---|
200 |
|
---|
201 |
|
---|
202 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
203 | ;;
|
---|
204 | ;; Maxima-level interface functions
|
---|
205 | ;;
|
---|
206 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
207 |
|
---|
208 | ;; Auxillary function for removing zero polynomial
|
---|
209 | (defun remzero (plist) (remove #'poly-zerop plist))
|
---|
210 |
|
---|
211 | ;;Simple operators
|
---|
212 |
|
---|
213 | (define-binop $poly_add poly-add
|
---|
214 | "Adds two polynomials P and Q")
|
---|
215 |
|
---|
216 | (define-binop $poly_subtract poly-sub
|
---|
217 | "Subtracts a polynomial Q from P.")
|
---|
218 |
|
---|
219 | (define-binop $poly_multiply poly-mul
|
---|
220 | "Returns the product of polynomials P and Q.")
|
---|
221 |
|
---|
222 | (define-binop $poly_s_polynomial spoly
|
---|
223 | "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
|
---|
224 |
|
---|
225 | (define-unop $poly_primitive_part poly-primitive-part
|
---|
226 | "Returns the polynomial P divided by GCD of its coefficients.")
|
---|
227 |
|
---|
228 | (define-unop $poly_normalize poly-normalize
|
---|
229 | "Returns the polynomial P divided by the leading coefficient.")
|
---|
230 |
|
---|
231 | ;;Functions
|
---|
232 |
|
---|
233 | (defmfun $poly_expand (p vars)
|
---|
234 | "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
|
---|
235 | If the representation is not compatible with a polynomial in variables VARS,
|
---|
236 | the result is an error."
|
---|
237 | (with-parsed-polynomials ((vars) :polynomials (p)
|
---|
238 | :value-type :polynomial)
|
---|
239 | p))
|
---|
240 |
|
---|
241 | (defmfun $poly_expt (p n vars)
|
---|
242 | (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
|
---|
243 | (poly-expt *maxima-ring* p n)))
|
---|
244 |
|
---|
245 | (defmfun $poly_content (p vars)
|
---|
246 | (with-parsed-polynomials ((vars) :polynomials (p))
|
---|
247 | (poly-content *maxima-ring* p)))
|
---|
248 |
|
---|
249 | (defmfun $poly_pseudo_divide (f fl vars
|
---|
250 | &aux (vars (coerce-maxima-list vars))
|
---|
251 | (f (parse-poly f vars))
|
---|
252 | (fl (parse-poly-list fl vars)))
|
---|
253 | (multiple-value-bind (quot rem c division-count)
|
---|
254 | (poly-pseudo-divide *maxima-ring* f fl)
|
---|
255 | `((mlist)
|
---|
256 | ,(coerce-to-maxima :poly-list quot vars)
|
---|
257 | ,(coerce-to-maxima :polynomial rem vars)
|
---|
258 | ,c
|
---|
259 | ,division-count)))
|
---|
260 |
|
---|
261 | (defmfun $poly_exact_divide (f g vars)
|
---|
262 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
263 | (poly-exact-divide *maxima-ring* f g)))
|
---|
264 |
|
---|
265 | (defmfun $poly_normal_form (f fl vars)
|
---|
266 | (with-parsed-polynomials ((vars) :polynomials (f)
|
---|
267 | :poly-lists (fl)
|
---|
268 | :value-type :polynomial)
|
---|
269 | (normal-form *maxima-ring* f (remzero fl) nil)))
|
---|
270 |
|
---|
271 | (defmfun $poly_buchberger_criterion (g vars)
|
---|
272 | (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
|
---|
273 | (buchberger-criterion *maxima-ring* g)))
|
---|
274 |
|
---|
275 | (defmfun $poly_buchberger (fl vars)
|
---|
276 | (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
|
---|
277 | (buchberger *maxima-ring* (remzero fl) 0 nil)))
|
---|
278 |
|
---|
279 | (defmfun $poly_reduction (plist vars)
|
---|
280 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
281 | :value-type :poly-list)
|
---|
282 | (reduction *maxima-ring* plist)))
|
---|
283 |
|
---|
284 | (defmfun $poly_minimization (plist vars)
|
---|
285 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
286 | :value-type :poly-list)
|
---|
287 | (minimization plist)))
|
---|
288 |
|
---|
289 | (defmfun $poly_normalize_list (plist vars)
|
---|
290 | (with-parsed-polynomials ((vars) :poly-lists (plist)
|
---|
291 | :value-type :poly-list)
|
---|
292 | (poly-normalize-list *maxima-ring* plist)))
|
---|
293 |
|
---|
294 | (defmfun $poly_grobner (f vars)
|
---|
295 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
296 | :value-type :poly-list)
|
---|
297 | (grobner *maxima-ring* (remzero f))))
|
---|
298 |
|
---|
299 | (defmfun $poly_reduced_grobner (f vars)
|
---|
300 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
301 | :value-type :poly-list)
|
---|
302 | (reduced-grobner *maxima-ring* (remzero f))))
|
---|
303 |
|
---|
304 | (defmfun $poly_depends_p (p var mvars
|
---|
305 | &aux (vars (coerce-maxima-list mvars))
|
---|
306 | (pos (position var vars)))
|
---|
307 | (if (null pos)
|
---|
308 | (merror "~%Variable ~M not in the list of variables ~M." var mvars)
|
---|
309 | (poly-depends-p (parse-poly p vars) pos)))
|
---|
310 |
|
---|
311 | (defmfun $poly_elimination_ideal (flist k vars)
|
---|
312 | (with-parsed-polynomials ((vars) :poly-lists (flist)
|
---|
313 | :value-type :poly-list)
|
---|
314 | (elimination-ideal *maxima-ring* flist k nil 0)))
|
---|
315 |
|
---|
316 | (defmfun $poly_colon_ideal (f g vars)
|
---|
317 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
318 | (colon-ideal *maxima-ring* f g nil)))
|
---|
319 |
|
---|
320 | (defmfun $poly_ideal_intersection (f g vars)
|
---|
321 | (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
|
---|
322 | (ideal-intersection *maxima-ring* f g nil)))
|
---|
323 |
|
---|
324 | (defmfun $poly_lcm (f g vars)
|
---|
325 | (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
|
---|
326 | (poly-lcm *maxima-ring* f g)))
|
---|
327 |
|
---|
328 | (defmfun $poly_gcd (f g vars)
|
---|
329 | ($first ($divide (m* f g) ($poly_lcm f g vars))))
|
---|
330 |
|
---|
331 | (defmfun $poly_grobner_equal (g1 g2 vars)
|
---|
332 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
333 | (grobner-equal *maxima-ring* g1 g2)))
|
---|
334 |
|
---|
335 | (defmfun $poly_grobner_subsetp (g1 g2 vars)
|
---|
336 | (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
|
---|
337 | (grobner-subsetp *maxima-ring* g1 g2)))
|
---|
338 |
|
---|
339 | (defmfun $poly_grobner_member (p g vars)
|
---|
340 | (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
|
---|
341 | (grobner-member *maxima-ring* p g)))
|
---|
342 |
|
---|
343 | (defmfun $poly_ideal_saturation1 (f p vars)
|
---|
344 | (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
|
---|
345 | :value-type :poly-list)
|
---|
346 | (ideal-saturation-1 *maxima-ring* f p 0)))
|
---|
347 |
|
---|
348 | (defmfun $poly_saturation_extension (f plist vars new-vars)
|
---|
349 | (with-parsed-polynomials ((vars new-vars)
|
---|
350 | :poly-lists (f plist)
|
---|
351 | :value-type :poly-list)
|
---|
352 | (saturation-extension *maxima-ring* f plist)))
|
---|
353 |
|
---|
354 | (defmfun $poly_polysaturation_extension (f plist vars new-vars)
|
---|
355 | (with-parsed-polynomials ((vars new-vars)
|
---|
356 | :poly-lists (f plist)
|
---|
357 | :value-type :poly-list)
|
---|
358 | (polysaturation-extension *maxima-ring* f plist)))
|
---|
359 |
|
---|
360 | (defmfun $poly_ideal_polysaturation1 (f plist vars)
|
---|
361 | (with-parsed-polynomials ((vars) :poly-lists (f plist)
|
---|
362 | :value-type :poly-list)
|
---|
363 | (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
|
---|
364 |
|
---|
365 | (defmfun $poly_ideal_saturation (f g vars)
|
---|
366 | (with-parsed-polynomials ((vars) :poly-lists (f g)
|
---|
367 | :value-type :poly-list)
|
---|
368 | (ideal-saturation *maxima-ring* f g 0 nil)))
|
---|
369 |
|
---|
370 | (defmfun $poly_ideal_polysaturation (f ideal-list vars)
|
---|
371 | (with-parsed-polynomials ((vars) :poly-lists (f)
|
---|
372 | :poly-list-lists (ideal-list)
|
---|
373 | :value-type :poly-list)
|
---|
374 | (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
|
---|
375 |
|
---|
376 | (defmfun $poly_lt (f vars)
|
---|
377 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
378 | (make-poly-from-termlist (list (poly-lt f)))))
|
---|
379 |
|
---|
380 | (defmfun $poly_lm (f vars)
|
---|
381 | (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
|
---|
382 | (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
|
---|
383 |
|
---|