close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/mx-grobner.lisp@ 104

Last change on this file since 104 was 104, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 12.2 KB
Line 
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
71are assumed to be defined.")
72
73
74
75
76;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77;;
78;; Conversion from internal form to Maxima general form
79;;
80;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
82(defun maxima-head ()
83 (if $poly_return_term_list
84 '(mlist)
85 '(mplus)))
86
87(defun coerce-to-maxima (poly-type object vars)
88 (case poly-type
89 (:polynomial
90 `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
91 (:poly-list
92 `((mlist) ,@(mapcar #'(lambda (p) ($ratdisrep (coerce-to-maxima :polynomial p vars))) object)))
93 (:term
94 `((mtimes) ,($ratdisrep (term-coeff object))
95 ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
96 vars (monom-exponents (term-monom object)))))
97 ;; Assumes that Lisp and Maxima logicals coincide
98 (:logical object)
99 (otherwise
100 object)))
101
102
103
104;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105;;
106;; Macro facility for writing Maxima-level wrappers for
107;; functions operating on internal representation
108;;
109;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110
111(defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
112 &key (polynomials nil)
113 (poly-lists nil)
114 (poly-list-lists nil)
115 (value-type nil))
116 &body body
117 &aux (vars (gensym))
118 (new-vars (gensym)))
119 `(let ((,vars (coerce-maxima-list ,maxima-vars))
120 ,@(when new-vars-supplied-p
121 (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
122 (coerce-to-maxima
123 ,value-type
124 (with-coefficient-ring ($poly_coefficient_ring)
125 (with-monomial-order ($poly_monomial_order)
126 (with-elimination-orders ($poly_primary_elimination_order
127 $poly_secondary_elimination_order
128 $poly_elimination_order)
129 (let ,(let ((args nil))
130 (dolist (p polynomials args)
131 (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
132 (dolist (p poly-lists args)
133 (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
134 (dolist (p poly-list-lists args)
135 (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
136 . ,body))))
137 ,(if new-vars-supplied-p
138 `(append ,vars ,new-vars)
139 vars))))
140
141(defmacro define-unop (maxima-name fun-name
142 &optional (documentation nil documentation-supplied-p))
143 "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
144 `(defun ,maxima-name (p vars
145 &aux
146 (vars (coerce-maxima-list vars))
147 (p (parse-poly p vars)))
148 ,@(when documentation-supplied-p (list documentation))
149 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
150
151(defmacro define-binop (maxima-name fun-name
152 &optional (documentation nil documentation-supplied-p))
153 "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
154 `(defmfun ,maxima-name (p q vars
155 &aux
156 (vars (coerce-maxima-list vars))
157 (p (parse-poly p vars))
158 (q (parse-poly q vars)))
159 ,@(when documentation-supplied-p (list documentation))
160 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
161
162
163
164;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165;;
166;; Maxima-level interface functions
167;;
168;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169
170;; Auxillary function for removing zero polynomial
171(defun remzero (plist) (remove #'poly-zerop plist))
172
173;;Simple operators
174
175(define-binop $poly_add poly-add
176 "Adds two polynomials P and Q")
177
178(define-binop $poly_subtract poly-sub
179 "Subtracts a polynomial Q from P.")
180
181(define-binop $poly_multiply poly-mul
182 "Returns the product of polynomials P and Q.")
183
184(define-binop $poly_s_polynomial spoly
185 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
186
187(define-unop $poly_primitive_part poly-primitive-part
188 "Returns the polynomial P divided by GCD of its coefficients.")
189
190(define-unop $poly_normalize poly-normalize
191 "Returns the polynomial P divided by the leading coefficient.")
192
193;;Functions
194
195(defmfun $poly_expand (p vars)
196 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
197If the representation is not compatible with a polynomial in variables VARS,
198the result is an error."
199 (with-parsed-polynomials ((vars) :polynomials (p)
200 :value-type :polynomial)
201 p))
202
203(defmfun $poly_expt (p n vars)
204 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
205 (poly-expt *maxima-ring* p n)))
206
207(defmfun $poly_content (p vars)
208 (with-parsed-polynomials ((vars) :polynomials (p))
209 (poly-content *maxima-ring* p)))
210
211(defmfun $poly_pseudo_divide (f fl vars
212 &aux (vars (coerce-maxima-list vars))
213 (f (parse-poly f vars))
214 (fl (parse-poly-list fl vars)))
215 (multiple-value-bind (quot rem c division-count)
216 (poly-pseudo-divide *maxima-ring* f fl)
217 `((mlist)
218 ,(coerce-to-maxima :poly-list quot vars)
219 ,(coerce-to-maxima :polynomial rem vars)
220 ,c
221 ,division-count)))
222
223(defmfun $poly_exact_divide (f g vars)
224 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
225 (poly-exact-divide *maxima-ring* f g)))
226
227(defmfun $poly_normal_form (f fl vars)
228 (with-parsed-polynomials ((vars) :polynomials (f)
229 :poly-lists (fl)
230 :value-type :polynomial)
231 (normal-form *maxima-ring* f (remzero fl) nil)))
232
233(defmfun $poly_buchberger_criterion (g vars)
234 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
235 (buchberger-criterion *maxima-ring* g)))
236
237(defmfun $poly_buchberger (fl vars)
238 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
239 (buchberger *maxima-ring* (remzero fl) 0 nil)))
240
241(defmfun $poly_reduction (plist vars)
242 (with-parsed-polynomials ((vars) :poly-lists (plist)
243 :value-type :poly-list)
244 (reduction *maxima-ring* plist)))
245
246(defmfun $poly_minimization (plist vars)
247 (with-parsed-polynomials ((vars) :poly-lists (plist)
248 :value-type :poly-list)
249 (minimization plist)))
250
251(defmfun $poly_normalize_list (plist vars)
252 (with-parsed-polynomials ((vars) :poly-lists (plist)
253 :value-type :poly-list)
254 (poly-normalize-list *maxima-ring* plist)))
255
256(defmfun $poly_grobner (f vars)
257 (with-parsed-polynomials ((vars) :poly-lists (f)
258 :value-type :poly-list)
259 (grobner *maxima-ring* (remzero f))))
260
261(defmfun $poly_reduced_grobner (f vars)
262 (with-parsed-polynomials ((vars) :poly-lists (f)
263 :value-type :poly-list)
264 (reduced-grobner *maxima-ring* (remzero f))))
265
266(defmfun $poly_depends_p (p var mvars
267 &aux (vars (coerce-maxima-list mvars))
268 (pos (position var vars)))
269 (if (null pos)
270 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
271 (poly-depends-p (parse-poly p vars) pos)))
272
273(defmfun $poly_elimination_ideal (flist k vars)
274 (with-parsed-polynomials ((vars) :poly-lists (flist)
275 :value-type :poly-list)
276 (elimination-ideal *maxima-ring* flist k nil 0)))
277
278(defmfun $poly_colon_ideal (f g vars)
279 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
280 (colon-ideal *maxima-ring* f g nil)))
281
282(defmfun $poly_ideal_intersection (f g vars)
283 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
284 (ideal-intersection *maxima-ring* f g nil)))
285
286(defmfun $poly_lcm (f g vars)
287 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
288 (poly-lcm *maxima-ring* f g)))
289
290(defmfun $poly_gcd (f g vars)
291 ($first ($divide (m* f g) ($poly_lcm f g vars))))
292
293(defmfun $poly_grobner_equal (g1 g2 vars)
294 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
295 (grobner-equal *maxima-ring* g1 g2)))
296
297(defmfun $poly_grobner_subsetp (g1 g2 vars)
298 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
299 (grobner-subsetp *maxima-ring* g1 g2)))
300
301(defmfun $poly_grobner_member (p g vars)
302 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
303 (grobner-member *maxima-ring* p g)))
304
305(defmfun $poly_ideal_saturation1 (f p vars)
306 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
307 :value-type :poly-list)
308 (ideal-saturation-1 *maxima-ring* f p 0)))
309
310(defmfun $poly_saturation_extension (f plist vars new-vars)
311 (with-parsed-polynomials ((vars new-vars)
312 :poly-lists (f plist)
313 :value-type :poly-list)
314 (saturation-extension *maxima-ring* f plist)))
315
316(defmfun $poly_polysaturation_extension (f plist vars new-vars)
317 (with-parsed-polynomials ((vars new-vars)
318 :poly-lists (f plist)
319 :value-type :poly-list)
320 (polysaturation-extension *maxima-ring* f plist)))
321
322(defmfun $poly_ideal_polysaturation1 (f plist vars)
323 (with-parsed-polynomials ((vars) :poly-lists (f plist)
324 :value-type :poly-list)
325 (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
326
327(defmfun $poly_ideal_saturation (f g vars)
328 (with-parsed-polynomials ((vars) :poly-lists (f g)
329 :value-type :poly-list)
330 (ideal-saturation *maxima-ring* f g 0 nil)))
331
332(defmfun $poly_ideal_polysaturation (f ideal-list vars)
333 (with-parsed-polynomials ((vars) :poly-lists (f)
334 :poly-list-lists (ideal-list)
335 :value-type :poly-list)
336 (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
337
338(defmfun $poly_lt (f vars)
339 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
340 (make-poly-from-termlist (list (poly-lt f)))))
341
342(defmfun $poly_lm (f vars)
343 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
344 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
345
Note: See TracBrowser for help on using the repository browser.