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

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

* empty log message *

File size: 17.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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23;;
24;; Load this file into Maxima to bootstrap the Grobner package.
25;; NOTE: This file does use symbols defined by Maxima, so it
26;; will not work when loaded in Common Lisp.
27;;
28;; DETAILS: This file implements an interface between the Grobner
29;; basis package NGROBNER, which is a pure Common Lisp package, and
30;; Maxima. NGROBNER for efficiency uses its own representation of
31;; polynomials. Thus, it is necessary to convert Maxima representation
32;; to the internal representation and back. The facilities to do so
33;; are implemented in this file.
34;;
35;; Also, since the NGROBNER package consists of many Lisp files, it is
36;; necessary to load the files. It is possible and preferrable to use
37;; ASDF for this purpose. The default is ASDF. It is also possible to
38;; simply used LOAD and COMPILE-FILE to accomplish this task.
39;;
40;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
42(in-package :maxima)
43
44(macsyma-module cgb-maxima)
45
46(eval-when
47 #+gcl (load eval)
48 #-gcl (:load-toplevel :execute)
49 (format t "~&Loading maxima-grobner ~a ~a~%"
50 "$Revision: 2.0 $" "$Date: 2015/06/02 0:34:17 $"))
51
52;;FUNCTS is loaded because it contains the definition of LCM
53($load "functs")
54
55#+sbcl(progn (require 'asdf) (load "ngrobner.asd")(asdf:load-system :ngrobner))
56
57(use-package :ngrobner)
58
59;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60;;
61;; Maxima expression ring
62;;
63;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64
65(defparameter *maxima-ring*
66 (make-ring
67 ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
68 :parse #'(lambda (expr)
69 (when modulus (setf expr ($rat expr)))
70 expr)
71 :unit #'(lambda () (if modulus ($rat 1) 1))
72 :zerop #'(lambda (expr)
73 ;;When is exactly a maxima expression equal to 0?
74 (cond ((numberp expr)
75 (= expr 0))
76 ((atom expr) nil)
77 (t
78 (case (caar expr)
79 (mrat (eql ($ratdisrep expr) 0))
80 (otherwise (eql ($totaldisrep expr) 0))))))
81 :add #'(lambda (x y) (m+ x y))
82 :sub #'(lambda (x y) (m- x y))
83 :uminus #'(lambda (x) (m- x))
84 :mul #'(lambda (x y) (m* x y))
85 ;;(defun coeff-div (x y) (cadr ($divide x y)))
86 :div #'(lambda (x y) (m// x y))
87 :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
88 :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
89 ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
90 :gcd #'(lambda (x y) ($gcd x y))))
91
92;; Rebind some global variables for Maxima environment
93(setf *expression-ring* *maxima-ring* ; Coefficient arithmetic done by Maxima
94 *ratdisrep-fun* '$ratdisrep ; Coefficients are converted to general form
95 )
96
97;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
98;;
99;; Maxima expression parsing
100;;
101;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102
103(defun equal-test-p (expr1 expr2)
104 (alike1 expr1 expr2))
105
106(defun coerce-maxima-list (expr)
107 "Convert a Maxima list to Lisp list."
108 (cond
109 ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
110 (t expr)))
111
112(defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
113
114(defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
115 "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
116 (labels ((parse (arg) (parse-poly arg vars))
117 (parse-list (args) (mapcar #'parse args)))
118 (cond
119 ((eql expr 0) (make-poly-zero))
120 ((member expr vars :test #'equal-test-p)
121 (let ((pos (position expr vars :test #'equal-test-p)))
122 (make-variable *expression-ring* (length vars) pos)))
123 ((free-of-vars expr vars)
124 ;;This means that variable-free CRE and Poisson forms will be converted
125 ;;to coefficients intact
126 (coerce-coeff *expression-ring* expr vars))
127 (t
128 (case (caar expr)
129 (mplus (reduce #'(lambda (x y) (poly-add *expression-ring* x y)) (parse-list (cdr expr))))
130 (mminus (poly-uminus *expression-ring* (parse (cadr expr))))
131 (mtimes
132 (if (endp (cddr expr)) ;unary
133 (parse (cdr expr))
134 (reduce #'(lambda (p q) (poly-mul *expression-ring* p q)) (parse-list (cdr expr)))))
135 (mexpt
136 (cond
137 ((member (cadr expr) vars :test #'equal-test-p)
138 ;;Special handling of (expt var pow)
139 (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
140 (make-variable *expression-ring* (length vars) pos (caddr expr))))
141 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
142 ;; Negative power means division in coefficient ring
143 ;; Non-integer power means non-polynomial coefficient
144 (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
145 expr)
146 (coerce-coeff *expression-ring* expr vars))
147 (t (poly-expt *expression-ring* (parse (cadr expr)) (caddr expr)))))
148 (mrat (parse ($ratdisrep expr)))
149 (mpois (parse ($outofpois expr)))
150 (otherwise
151 (coerce-coeff *expression-ring* expr vars)))))))
152
153(defun parse-poly-list (expr vars)
154 (case (caar expr)
155 (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
156 (t (merror "Expression ~M is not a list of polynomials in variables ~M."
157 expr vars))))
158(defun parse-poly-list-list (poly-list-list vars)
159 (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
160
161
162;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163;;
164;; Conversion from internal form to Maxima general form
165;;
166;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167
168(defun maxima-head ()
169 (if $poly_return_term_list
170 '(mlist)
171 '(mplus)))
172
173(defun coerce-to-maxima (poly-type object vars)
174 (case poly-type
175 (:polynomial
176 `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
177 (:poly-list
178 `((mlist) ,@(mapcar #'(lambda (p) (funcall *ratdisrep-fun* (coerce-to-maxima :polynomial p vars))) object)))
179 (:term
180 `((mtimes) ,(funcall *ratdisrep-fun* (term-coeff object))
181 ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
182 vars (monom-exponents (term-monom object)))))
183 ;; Assumes that Lisp and Maxima logicals coincide
184 (:logical object)
185 (otherwise
186 object)))
187
188
189;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190;;
191;; Unary and binary operation definition facility
192;;
193;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 *expression-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 *expression-ring* p q) vars)))
215
216
217;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218;;
219;; Facilities for evaluating Grobner package expressions
220;; within a prepared environment
221;;
222;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223
224(defmacro with-monomial-order ((order) &body body)
225 "Evaluate BODY with monomial order set to ORDER."
226 `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
227 . ,body))
228
229(defmacro with-coefficient-ring ((ring) &body body)
230 "Evaluate BODY with coefficient ring set to RING."
231 `(let ((*expression-ring* (or (find-ring ,ring) *expression-ring*)))
232 . ,body))
233
234(defmacro with-elimination-orders ((primary secondary elimination-order)
235 &body body)
236 "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
237 `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
238 (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
239 (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
240 . ,body))
241
242
243;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
244;;
245;; Maxima-level interface functions
246;;
247;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248
249;; Auxillary function for removing zero polynomial
250(defun remzero (plist) (remove #'poly-zerop plist))
251
252;;Simple operators
253
254(define-binop $poly_add poly-add
255 "Adds two polynomials P and Q")
256
257(define-binop $poly_subtract poly-sub
258 "Subtracts a polynomial Q from P.")
259
260(define-binop $poly_multiply poly-mul
261 "Returns the product of polynomials P and Q.")
262
263(define-binop $poly_s_polynomial spoly
264 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
265
266(define-unop $poly_primitive_part poly-primitive-part
267 "Returns the polynomial P divided by GCD of its coefficients.")
268
269(define-unop $poly_normalize poly-normalize
270 "Returns the polynomial P divided by the leading coefficient.")
271
272;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
273;;
274;; Macro facility for writing Maxima-level wrappers for
275;; functions operating on internal representation
276;;
277;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278
279(defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
280 &key (polynomials nil)
281 (poly-lists nil)
282 (poly-list-lists nil)
283 (value-type nil))
284 &body body
285 &aux (vars (gensym))
286 (new-vars (gensym)))
287 `(let ((,vars (coerce-maxima-list ,maxima-vars))
288 ,@(when new-vars-supplied-p
289 (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
290 (coerce-to-maxima
291 ,value-type
292 (with-coefficient-ring ($poly_coefficient_ring)
293 (with-monomial-order ($poly_monomial_order)
294 (with-elimination-orders ($poly_primary_elimination_order
295 $poly_secondary_elimination_order
296 $poly_elimination_order)
297 (let ,(let ((args nil))
298 (dolist (p polynomials args)
299 (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
300 (dolist (p poly-lists args)
301 (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
302 (dolist (p poly-list-lists args)
303 (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
304 . ,body))))
305 ,(if new-vars-supplied-p
306 `(append ,vars ,new-vars)
307 vars))))
308
309
310;;Functions
311
312(defmfun $poly_expand (p vars)
313 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
314If the representation is not compatible with a polynomial in variables VARS,
315the result is an error."
316 (with-parsed-polynomials ((vars) :polynomials (p)
317 :value-type :polynomial)
318 p))
319
320(defmfun $poly_expt (p n vars)
321 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
322 (poly-expt *expression-ring* p n)))
323
324(defmfun $poly_content (p vars)
325 (with-parsed-polynomials ((vars) :polynomials (p))
326 (poly-content *expression-ring* p)))
327
328(defmfun $poly_pseudo_divide (f fl vars
329 &aux (vars (coerce-maxima-list vars))
330 (f (parse-poly f vars))
331 (fl (parse-poly-list fl vars)))
332 (multiple-value-bind (quot rem c division-count)
333 (poly-pseudo-divide *expression-ring* f fl)
334 `((mlist)
335 ,(coerce-to-maxima :poly-list quot vars)
336 ,(coerce-to-maxima :polynomial rem vars)
337 ,c
338 ,division-count)))
339
340(defmfun $poly_exact_divide (f g vars)
341 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
342 (poly-exact-divide *expression-ring* f g)))
343
344(defmfun $poly_normal_form (f fl vars)
345 (with-parsed-polynomials ((vars) :polynomials (f)
346 :poly-lists (fl)
347 :value-type :polynomial)
348 (normal-form *expression-ring* f (remzero fl) nil)))
349
350(defmfun $poly_buchberger_criterion (g vars)
351 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
352 (buchberger-criterion *expression-ring* g)))
353
354(defmfun $poly_buchberger (fl vars)
355 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
356 (buchberger *expression-ring* (remzero fl) 0 nil)))
357
358(defmfun $poly_reduction (plist vars)
359 (with-parsed-polynomials ((vars) :poly-lists (plist)
360 :value-type :poly-list)
361 (reduction *expression-ring* plist)))
362
363(defmfun $poly_minimization (plist vars)
364 (with-parsed-polynomials ((vars) :poly-lists (plist)
365 :value-type :poly-list)
366 (minimization plist)))
367
368(defmfun $poly_normalize_list (plist vars)
369 (with-parsed-polynomials ((vars) :poly-lists (plist)
370 :value-type :poly-list)
371 (poly-normalize-list *expression-ring* plist)))
372
373(defmfun $poly_grobner (f vars)
374 (with-parsed-polynomials ((vars) :poly-lists (f)
375 :value-type :poly-list)
376 (grobner *expression-ring* (remzero f))))
377
378(defmfun $poly_reduced_grobner (f vars)
379 (with-parsed-polynomials ((vars) :poly-lists (f)
380 :value-type :poly-list)
381 (reduced-grobner *expression-ring* (remzero f))))
382
383(defmfun $poly_depends_p (p var mvars
384 &aux (vars (coerce-maxima-list mvars))
385 (pos (position var vars)))
386 (if (null pos)
387 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
388 (poly-depends-p (parse-poly p vars) pos)))
389
390(defmfun $poly_elimination_ideal (flist k vars)
391 (with-parsed-polynomials ((vars) :poly-lists (flist)
392 :value-type :poly-list)
393 (elimination-ideal *expression-ring* flist k nil 0)))
394
395(defmfun $poly_colon_ideal (f g vars)
396 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
397 (colon-ideal *expression-ring* f g nil)))
398
399(defmfun $poly_ideal_intersection (f g vars)
400 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
401 (ideal-intersection *expression-ring* f g nil)))
402
403(defmfun $poly_lcm (f g vars)
404 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
405 (poly-lcm *expression-ring* f g)))
406
407(defmfun $poly_gcd (f g vars)
408 ($first ($divide (m* f g) ($poly_lcm f g vars))))
409
410(defmfun $poly_grobner_equal (g1 g2 vars)
411 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
412 (grobner-equal *expression-ring* g1 g2)))
413
414(defmfun $poly_grobner_subsetp (g1 g2 vars)
415 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
416 (grobner-subsetp *expression-ring* g1 g2)))
417
418(defmfun $poly_grobner_member (p g vars)
419 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
420 (grobner-member *expression-ring* p g)))
421
422(defmfun $poly_ideal_saturation1 (f p vars)
423 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
424 :value-type :poly-list)
425 (ideal-saturation-1 *expression-ring* f p 0)))
426
427(defmfun $poly_saturation_extension (f plist vars new-vars)
428 (with-parsed-polynomials ((vars new-vars)
429 :poly-lists (f plist)
430 :value-type :poly-list)
431 (saturation-extension *expression-ring* f plist)))
432
433(defmfun $poly_polysaturation_extension (f plist vars new-vars)
434 (with-parsed-polynomials ((vars new-vars)
435 :poly-lists (f plist)
436 :value-type :poly-list)
437 (polysaturation-extension *expression-ring* f plist)))
438
439(defmfun $poly_ideal_polysaturation1 (f plist vars)
440 (with-parsed-polynomials ((vars) :poly-lists (f plist)
441 :value-type :poly-list)
442 (ideal-polysaturation-1 *expression-ring* f plist 0 nil)))
443
444(defmfun $poly_ideal_saturation (f g vars)
445 (with-parsed-polynomials ((vars) :poly-lists (f g)
446 :value-type :poly-list)
447 (ideal-saturation *expression-ring* f g 0 nil)))
448
449(defmfun $poly_ideal_polysaturation (f ideal-list vars)
450 (with-parsed-polynomials ((vars) :poly-lists (f)
451 :poly-list-lists (ideal-list)
452 :value-type :poly-list)
453 (ideal-polysaturation *expression-ring* f ideal-list 0 nil)))
454
455(defmfun $poly_lt (f vars)
456 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
457 (make-poly-from-termlist (list (poly-lt f)))))
458
459(defmfun $poly_lm (f vars)
460 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
461 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *expression-ring*)))))))
462
Note: See TracBrowser for help on using the repository browser.