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@ 103

Last change on this file since 103 was 103, checked in by Marek Rychlik, 10 years ago

* empty log message *

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