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

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

* empty log message *

File size: 18.0 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;; Debugging/tracing
39;;
40;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41(defmacro debug-cgb (&rest args)
42 `(when $poly_grobner_debug (format *terminal-io* ,@args)))
43
44;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45;;
46;; Selection of algorithm and pair heuristic
47;;
48;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49
50(defun find-grobner-function (algorithm)
51 "Return a function which calculates Grobner basis, based on its
52names. Names currently used are either Lisp symbols, Maxima symbols or
53keywords."
54 (ecase algorithm
55 ((buchberger :buchberger $buchberger) #'buchberger)
56 ((parallel-buchberger :parallel-buchberger $parallel_buchberger) #'parallel-buchberger)
57 ((gebauer-moeller :gebauer_moeller $gebauer_moeller) #'gebauer-moeller)))
58
59(defun grobner (ring f &optional (start 0) (top-reduction-only nil))
60 ;;(setf F (sort F #'< :key #'sugar))
61 (funcall
62 (find-grobner-function $poly_grobner_algorithm)
63 ring f start top-reduction-only))
64
65(defun reduced-grobner (ring f &optional (start 0) (top-reduction-only $poly_top_reduction_only))
66 (reduction ring (grobner ring f start top-reduction-only)))
67
68(defun set-pair-heuristic (method)
69 "Sets up variables *PAIR-KEY-FUNCTION* and *PAIR-ORDER* used
70to determine the priority of critical pairs in the priority queue."
71 (ecase method
72 ((sugar :sugar $sugar)
73 (setf *pair-key-function* #'sugar-pair-key
74 *pair-order* #'sugar-order))
75; ((minimal-mock-spoly :minimal-mock-spoly $minimal_mock_spoly)
76; (setf *pair-key-function* #'mock-spoly
77; *pair-order* #'mock-spoly-order))
78 ((minimal-lcm :minimal-lcm $minimal_lcm)
79 (setf *pair-key-function* #'(lambda (p q)
80 (monom-lcm (poly-lm p) (poly-lm q)))
81 *pair-order* #'reverse-monomial-order))
82 ((minimal-total-degree :minimal-total-degree $minimal_total_degree)
83 (setf *pair-key-function* #'(lambda (p q)
84 (monom-total-degree
85 (monom-lcm (poly-lm p) (poly-lm q))))
86 *pair-order* #'<))
87 ((minimal-length :minimal-length $minimal_length)
88 (setf *pair-key-function* #'(lambda (p q)
89 (+ (poly-length p) (poly-length q)))
90 *pair-order* #'<))))
91
92
93
94
95;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96;;
97;; Set up the coefficients to be polynomials
98;;
99;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
100
101;; (defun poly-ring (ring vars)
102;; (make-ring
103;; :parse #'(lambda (expr) (poly-eval ring expr vars))
104;; :unit #'(lambda () (poly-unit ring (length vars)))
105;; :zerop #'poly-zerop
106;; :add #'(lambda (x y) (poly-add ring x y))
107;; :sub #'(lambda (x y) (poly-sub ring x y))
108;; :uminus #'(lambda (x) (poly-uminus ring x))
109;; :mul #'(lambda (x y) (poly-mul ring x y))
110;; :div #'(lambda (x y) (poly-exact-divide ring x y))
111;; :lcm #'(lambda (x y) (poly-lcm ring x y))
112;; :ezgcd #'(lambda (x y &aux (gcd (poly-gcd ring x y)))
113;; (values gcd
114;; (poly-exact-divide ring x gcd)
115;; (poly-exact-divide ring y gcd)))
116;; :gcd #'(lambda (x y) (poly-gcd x y))))
117
118
119
120;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
121;;
122;; Conversion from internal to infix form
123;;
124;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125
126(defun coerce-to-infix (poly-type object vars)
127 (case poly-type
128 (:termlist
129 `(+ ,@(mapcar #'(lambda (term) (coerce-to-infix :term term vars)) object)))
130 (:polynomial
131 (coerce-to-infix :termlist (poly-termlist object) vars))
132 (:poly-list
133 `([ ,@(mapcar #'(lambda (p) (coerce-to-infix :polynomial p vars)) object)))
134 (:term
135 `(* ,(term-coeff object)
136 ,@(mapcar #'(lambda (var power) `(expt ,var ,power))
137 vars (monom-exponents (term-monom object)))))
138 (otherwise
139 object)))
140
141
142
143;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144;;
145;; Maxima expression ring
146;;
147;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
148
149(defparameter *expression-ring*
150 (make-ring
151 ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
152 :parse #'(lambda (expr)
153 (when modulus (setf expr ($rat expr)))
154 expr)
155 :unit #'(lambda () (if modulus ($rat 1) 1))
156 :zerop #'(lambda (expr)
157 ;;When is exactly a maxima expression equal to 0?
158 (cond ((numberp expr)
159 (= expr 0))
160 ((atom expr) nil)
161 (t
162 (case (caar expr)
163 (mrat (eql ($ratdisrep expr) 0))
164 (otherwise (eql ($totaldisrep expr) 0))))))
165 :add #'(lambda (x y) (m+ x y))
166 :sub #'(lambda (x y) (m- x y))
167 :uminus #'(lambda (x) (m- x))
168 :mul #'(lambda (x y) (m* x y))
169 ;;(defun coeff-div (x y) (cadr ($divide x y)))
170 :div #'(lambda (x y) (m// x y))
171 :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
172 :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
173 ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
174 :gcd #'(lambda (x y) ($gcd x y))))
175
176(defvar *maxima-ring* *expression-ring*
177 "The ring of coefficients, over which all polynomials
178are assumed to be defined.")
179
180
181;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182;;
183;; Order utilities
184;;
185;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186(defun find-order (order)
187 "This function returns the order function bases on its name."
188 (cond
189 ((null order) nil)
190 ((symbolp order)
191 (case order
192 ((lex :lex $lex) #'lex>)
193 ((grlex :grlex $grlex) #'grlex>)
194 ((grevlex :grevlex $grevlex) #'grevlex>)
195 ((invlex :invlex $invlex) #'invlex>)
196 ((elimination-order-1 :elimination-order-1 elimination_order_1) #'elimination-order-1)
197 (otherwise
198 (mtell "~%Warning: Order ~M not found. Using default.~%" order))))
199 (t
200 (mtell "~%Order specification ~M is not recognized. Using default.~%" order)
201 nil)))
202
203(defun find-ring (ring)
204 "This function returns the ring structure bases on input symbol."
205 (cond
206 ((null ring) nil)
207 ((symbolp ring)
208 (case ring
209 ((expression-ring :expression-ring $expression_ring) *expression-ring*)
210 ((ring-of-integers :ring-of-integers $ring_of_integers) *ring-of-integers*)
211 (otherwise
212 (mtell "~%Warning: Ring ~M not found. Using default.~%" ring))))
213 (t
214 (mtell "~%Ring specification ~M is not recognized. Using default.~%" ring)
215 nil)))
216
217(defmacro with-monomial-order ((order) &body body)
218 "Evaluate BODY with monomial order set to ORDER."
219 `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
220 . ,body))
221
222(defmacro with-coefficient-ring ((ring) &body body)
223 "Evaluate BODY with coefficient ring set to RING."
224 `(let ((*maxima-ring* (or (find-ring ,ring) *maxima-ring*)))
225 . ,body))
226
227(defmacro with-elimination-orders ((primary secondary elimination-order)
228 &body body)
229 "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
230 `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
231 (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
232 (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
233 . ,body))
234
235
236
237;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238;;
239;; Conversion from internal form to Maxima general form
240;;
241;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242
243(defun maxima-head ()
244 (if $poly_return_term_list
245 '(mlist)
246 '(mplus)))
247
248(defun coerce-to-maxima (poly-type object vars)
249 (case poly-type
250 (:polynomial
251 `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
252 (:poly-list
253 `((mlist) ,@(mapcar #'(lambda (p) ($ratdisrep (coerce-to-maxima :polynomial p vars))) object)))
254 (:term
255 `((mtimes) ,($ratdisrep (term-coeff object))
256 ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
257 vars (monom-exponents (term-monom object)))))
258 ;; Assumes that Lisp and Maxima logicals coincide
259 (:logical object)
260 (otherwise
261 object)))
262
263
264
265;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266;;
267;; Macro facility for writing Maxima-level wrappers for
268;; functions operating on internal representation
269;;
270;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
271
272(defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
273 &key (polynomials nil)
274 (poly-lists nil)
275 (poly-list-lists nil)
276 (value-type nil))
277 &body body
278 &aux (vars (gensym))
279 (new-vars (gensym)))
280 `(let ((,vars (coerce-maxima-list ,maxima-vars))
281 ,@(when new-vars-supplied-p
282 (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
283 (coerce-to-maxima
284 ,value-type
285 (with-coefficient-ring ($poly_coefficient_ring)
286 (with-monomial-order ($poly_monomial_order)
287 (with-elimination-orders ($poly_primary_elimination_order
288 $poly_secondary_elimination_order
289 $poly_elimination_order)
290 (let ,(let ((args nil))
291 (dolist (p polynomials args)
292 (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
293 (dolist (p poly-lists args)
294 (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
295 (dolist (p poly-list-lists args)
296 (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
297 . ,body))))
298 ,(if new-vars-supplied-p
299 `(append ,vars ,new-vars)
300 vars))))
301
302(defmacro define-unop (maxima-name fun-name
303 &optional (documentation nil documentation-supplied-p))
304 "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
305 `(defun ,maxima-name (p vars
306 &aux
307 (vars (coerce-maxima-list vars))
308 (p (parse-poly p vars)))
309 ,@(when documentation-supplied-p (list documentation))
310 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
311
312(defmacro define-binop (maxima-name fun-name
313 &optional (documentation nil documentation-supplied-p))
314 "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
315 `(defmfun ,maxima-name (p q vars
316 &aux
317 (vars (coerce-maxima-list vars))
318 (p (parse-poly p vars))
319 (q (parse-poly q vars)))
320 ,@(when documentation-supplied-p (list documentation))
321 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
322
323
324
325;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
326;;
327;; Maxima-level interface functions
328;;
329;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
330
331;; Auxillary function for removing zero polynomial
332(defun remzero (plist) (remove #'poly-zerop plist))
333
334;;Simple operators
335
336(define-binop $poly_add poly-add
337 "Adds two polynomials P and Q")
338
339(define-binop $poly_subtract poly-sub
340 "Subtracts a polynomial Q from P.")
341
342(define-binop $poly_multiply poly-mul
343 "Returns the product of polynomials P and Q.")
344
345(define-binop $poly_s_polynomial spoly
346 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
347
348(define-unop $poly_primitive_part poly-primitive-part
349 "Returns the polynomial P divided by GCD of its coefficients.")
350
351(define-unop $poly_normalize poly-normalize
352 "Returns the polynomial P divided by the leading coefficient.")
353
354;;Functions
355
356(defmfun $poly_expand (p vars)
357 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
358If the representation is not compatible with a polynomial in variables VARS,
359the result is an error."
360 (with-parsed-polynomials ((vars) :polynomials (p)
361 :value-type :polynomial)
362 p))
363
364(defmfun $poly_expt (p n vars)
365 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
366 (poly-expt *maxima-ring* p n)))
367
368(defmfun $poly_content (p vars)
369 (with-parsed-polynomials ((vars) :polynomials (p))
370 (poly-content *maxima-ring* p)))
371
372(defmfun $poly_pseudo_divide (f fl vars
373 &aux (vars (coerce-maxima-list vars))
374 (f (parse-poly f vars))
375 (fl (parse-poly-list fl vars)))
376 (multiple-value-bind (quot rem c division-count)
377 (poly-pseudo-divide *maxima-ring* f fl)
378 `((mlist)
379 ,(coerce-to-maxima :poly-list quot vars)
380 ,(coerce-to-maxima :polynomial rem vars)
381 ,c
382 ,division-count)))
383
384(defmfun $poly_exact_divide (f g vars)
385 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
386 (poly-exact-divide *maxima-ring* f g)))
387
388(defmfun $poly_normal_form (f fl vars)
389 (with-parsed-polynomials ((vars) :polynomials (f)
390 :poly-lists (fl)
391 :value-type :polynomial)
392 (normal-form *maxima-ring* f (remzero fl) nil)))
393
394(defmfun $poly_buchberger_criterion (g vars)
395 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
396 (buchberger-criterion *maxima-ring* g)))
397
398(defmfun $poly_buchberger (fl vars)
399 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
400 (buchberger *maxima-ring* (remzero fl) 0 nil)))
401
402(defmfun $poly_reduction (plist vars)
403 (with-parsed-polynomials ((vars) :poly-lists (plist)
404 :value-type :poly-list)
405 (reduction *maxima-ring* plist)))
406
407(defmfun $poly_minimization (plist vars)
408 (with-parsed-polynomials ((vars) :poly-lists (plist)
409 :value-type :poly-list)
410 (minimization plist)))
411
412(defmfun $poly_normalize_list (plist vars)
413 (with-parsed-polynomials ((vars) :poly-lists (plist)
414 :value-type :poly-list)
415 (poly-normalize-list *maxima-ring* plist)))
416
417(defmfun $poly_grobner (f vars)
418 (with-parsed-polynomials ((vars) :poly-lists (f)
419 :value-type :poly-list)
420 (grobner *maxima-ring* (remzero f))))
421
422(defmfun $poly_reduced_grobner (f vars)
423 (with-parsed-polynomials ((vars) :poly-lists (f)
424 :value-type :poly-list)
425 (reduced-grobner *maxima-ring* (remzero f))))
426
427(defmfun $poly_depends_p (p var mvars
428 &aux (vars (coerce-maxima-list mvars))
429 (pos (position var vars)))
430 (if (null pos)
431 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
432 (poly-depends-p (parse-poly p vars) pos)))
433
434(defmfun $poly_elimination_ideal (flist k vars)
435 (with-parsed-polynomials ((vars) :poly-lists (flist)
436 :value-type :poly-list)
437 (elimination-ideal *maxima-ring* flist k nil 0)))
438
439(defmfun $poly_colon_ideal (f g vars)
440 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
441 (colon-ideal *maxima-ring* f g nil)))
442
443(defmfun $poly_ideal_intersection (f g vars)
444 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
445 (ideal-intersection *maxima-ring* f g nil)))
446
447(defmfun $poly_lcm (f g vars)
448 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
449 (poly-lcm *maxima-ring* f g)))
450
451(defmfun $poly_gcd (f g vars)
452 ($first ($divide (m* f g) ($poly_lcm f g vars))))
453
454(defmfun $poly_grobner_equal (g1 g2 vars)
455 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
456 (grobner-equal *maxima-ring* g1 g2)))
457
458(defmfun $poly_grobner_subsetp (g1 g2 vars)
459 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
460 (grobner-subsetp *maxima-ring* g1 g2)))
461
462(defmfun $poly_grobner_member (p g vars)
463 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
464 (grobner-member *maxima-ring* p g)))
465
466(defmfun $poly_ideal_saturation1 (f p vars)
467 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
468 :value-type :poly-list)
469 (ideal-saturation-1 *maxima-ring* f p 0)))
470
471(defmfun $poly_saturation_extension (f plist vars new-vars)
472 (with-parsed-polynomials ((vars new-vars)
473 :poly-lists (f plist)
474 :value-type :poly-list)
475 (saturation-extension *maxima-ring* f plist)))
476
477(defmfun $poly_polysaturation_extension (f plist vars new-vars)
478 (with-parsed-polynomials ((vars new-vars)
479 :poly-lists (f plist)
480 :value-type :poly-list)
481 (polysaturation-extension *maxima-ring* f plist)))
482
483(defmfun $poly_ideal_polysaturation1 (f plist vars)
484 (with-parsed-polynomials ((vars) :poly-lists (f plist)
485 :value-type :poly-list)
486 (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
487
488(defmfun $poly_ideal_saturation (f g vars)
489 (with-parsed-polynomials ((vars) :poly-lists (f g)
490 :value-type :poly-list)
491 (ideal-saturation *maxima-ring* f g 0 nil)))
492
493(defmfun $poly_ideal_polysaturation (f ideal-list vars)
494 (with-parsed-polynomials ((vars) :poly-lists (f)
495 :poly-list-lists (ideal-list)
496 :value-type :poly-list)
497 (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
498
499(defmfun $poly_lt (f vars)
500 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
501 (make-poly-from-termlist (list (poly-lt f)))))
502
503(defmfun $poly_lm (f vars)
504 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
505 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
506
Note: See TracBrowser for help on using the repository browser.