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

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

* empty log message *

File size: 17.9 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
47(eval-when
48 #+gcl (load eval)
49 #-gcl (:load-toplevel :execute)
50 (format t "~&Loading maxima-grobner ~a ~a~%"
51 "$Revision: 2.0 $" "$Date: 2015/06/02 0:34:17 $"))
52
53;;FUNCTS is loaded because it contains the definition of LCM
54($load "functs")
55#+sbcl(progn (require 'asdf) (load "ngrobner.asd")(asdf:load-system :ngrobner))
56
57(use-package :ngrobner)
58
59
60;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61;;
62;; Maxima expression ring
63;;
64;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65;;
66;; This is how we perform operations on coefficients
67;; using Maxima functions.
68;;
69;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70
71(defparameter *maxima-ring*
72 (make-ring
73 ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
74 :parse #'(lambda (expr)
75 (when modulus (setf expr ($rat expr)))
76 expr)
77 :unit #'(lambda () (if modulus ($rat 1) 1))
78 :zerop #'(lambda (expr)
79 ;;When is exactly a maxima expression equal to 0?
80 (cond ((numberp expr)
81 (= expr 0))
82 ((atom expr) nil)
83 (t
84 (case (caar expr)
85 (mrat (eql ($ratdisrep expr) 0))
86 (otherwise (eql ($totaldisrep expr) 0))))))
87 :add #'(lambda (x y) (m+ x y))
88 :sub #'(lambda (x y) (m- x y))
89 :uminus #'(lambda (x) (m- x))
90 :mul #'(lambda (x y) (m* x y))
91 ;;(defun coeff-div (x y) (cadr ($divide x y)))
92 :div #'(lambda (x y) (m// x y))
93 :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
94 :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
95 ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
96 :gcd #'(lambda (x y) ($gcd x y))))
97
98;; Rebind some global variables for Maxima environment
99(setf *expression-ring* *maxima-ring* ; Coefficient arithmetic done by Maxima
100 *ratdisrep-fun* '$ratdisrep ; Coefficients are converted to general form
101 )
102
103;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104;;
105;; Maxima expression parsing
106;;
107;;
108;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109;;
110;; Functions and macros dealing with internal representation
111;; structure.
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 *expression-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 *expression-ring* expr vars))
139 (t
140 (case (caar expr)
141 (mplus (reduce #'(lambda (x y) (poly-add *expression-ring* x y)) (parse-list (cdr expr))))
142 (mminus (poly-uminus *expression-ring* (parse (cadr expr))))
143 (mtimes
144 (if (endp (cddr expr)) ;unary
145 (parse (cdr expr))
146 (reduce #'(lambda (p q) (poly-mul *expression-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 *expression-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 *expression-ring* expr vars))
159 (t (poly-expt *expression-ring* (parse (cadr expr)) (caddr expr)))))
160 (mrat (parse ($ratdisrep expr)))
161 (mpois (parse ($outofpois expr)))
162 (otherwise
163 (coerce-coeff *expression-ring* expr vars)))))))
164
165(defun parse-poly-list (expr vars)
166 "Parse a Maxima representation of a list of polynomials."
167 (case (caar expr)
168 (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
169 (t (merror "Expression ~M is not a list of polynomials in variables ~M."
170 expr vars))))
171
172(defun parse-poly-list-list (poly-list-list vars)
173 "Parse a Maxima representation of a list of lists of polynomials."
174 (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
175
176
177;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178;;
179;; Conversion from internal form to Maxima general form
180;;
181;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
182
183(defun maxima-head ()
184 (if $poly_return_term_list
185 '(mlist)
186 '(mplus)))
187
188(defun coerce-to-maxima (poly-type object vars)
189 (case poly-type
190 (:polynomial
191 `(,(maxima-head) ,@(mapcar #'(lambda (term) (coerce-to-maxima :term term vars)) (poly-termlist object))))
192 (:poly-list
193 `((mlist) ,@(mapcar #'(lambda (p) (funcall *ratdisrep-fun* (coerce-to-maxima :polynomial p vars))) object)))
194 (:term
195 `((mtimes) ,(funcall *ratdisrep-fun* (term-coeff object))
196 ,@(mapcar #'(lambda (var power) `((mexpt) ,var ,power))
197 vars (coerce (term-monom object) 'list))))
198 ;; Assumes that Lisp and Maxima logicals coincide
199 (:logical object)
200 (otherwise
201 object)))
202
203
204;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205;;
206;; Unary and binary operation definition facility
207;;
208;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209
210(defmacro define-unop (maxima-name fun-name
211 &optional (documentation nil documentation-supplied-p))
212 "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
213 `(defun ,maxima-name (p vars
214 &aux
215 (vars (coerce-maxima-list vars))
216 (p (parse-poly p vars)))
217 ,@(when documentation-supplied-p (list documentation))
218 (coerce-to-maxima :polynomial (,fun-name *expression-ring* p) vars)))
219
220(defmacro define-binop (maxima-name fun-name
221 &optional (documentation nil documentation-supplied-p))
222 "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
223 `(defmfun ,maxima-name (p q vars
224 &aux
225 (vars (coerce-maxima-list vars))
226 (p (parse-poly p vars))
227 (q (parse-poly q vars)))
228 ,@(when documentation-supplied-p (list documentation))
229 (coerce-to-maxima :polynomial (,fun-name *expression-ring* p q) vars)))
230
231
232;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233;;
234;; Facilities for evaluating Grobner package expressions
235;; within a prepared environment
236;;
237;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238
239(defmacro with-monomial-order ((order) &body body)
240 "Evaluate BODY with monomial order set to ORDER."
241 `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
242 . ,body))
243
244(defmacro with-coefficient-ring ((ring) &body body)
245 "Evaluate BODY with coefficient ring set to RING."
246 `(let ((*expression-ring* (or (find-ring ,ring) *expression-ring*)))
247 . ,body))
248
249(defmacro with-ring-and-order ((ring order) &body body)
250 "Evaluate BODY with monomial order set to ORDER and coefficient ring set to RING."
251 `(let ((*monomial-order* (or (find-order ,order) *monomial-order*))
252 (*expression-ring* (or (find-ring ,ring) *expression-ring*)))
253 . ,body))
254
255(defmacro with-elimination-orders ((primary secondary elimination-order)
256 &body body)
257 "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
258 `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
259 (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
260 (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
261 . ,body))
262
263
264;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265;;
266;; Maxima-level interface functions
267;;
268;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269
270;; Auxillary function for removing zero polynomial
271(defun remzero (plist) (remove #'poly-zerop plist))
272
273;;Simple operators
274
275(define-binop $poly_add poly-add
276 "Adds two polynomials P and Q")
277
278(define-binop $poly_subtract poly-sub
279 "Subtracts a polynomial Q from P.")
280
281(define-binop $poly_multiply poly-mul
282 "Returns the product of polynomials P and Q.")
283
284(define-binop $poly_s_polynomial spoly
285 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
286
287(define-unop $poly_primitive_part poly-primitive-part
288 "Returns the polynomial P divided by GCD of its coefficients.")
289
290(define-unop $poly_normalize poly-normalize
291 "Returns the polynomial P divided by the leading coefficient.")
292
293;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
294;;
295;; Macro facility for writing Maxima-level wrappers for
296;; functions operating on internal representation
297;;
298;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
299
300(defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
301 &key (polynomials nil)
302 (poly-lists nil)
303 (poly-list-lists nil)
304 (value-type nil))
305 &body body
306 &aux (vars (gensym))
307 (new-vars (gensym)))
308 `(let ((,vars (coerce-maxima-list ,maxima-vars))
309 ,@(when new-vars-supplied-p
310 (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
311 (coerce-to-maxima
312 ,value-type
313 (with-coefficient-ring ($poly_coefficient_ring)
314 (with-monomial-order ($poly_monomial_order)
315 (with-elimination-orders ($poly_primary_elimination_order
316 $poly_secondary_elimination_order
317 $poly_elimination_order)
318 (let ,(let ((args nil))
319 (dolist (p polynomials args)
320 (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
321 (dolist (p poly-lists args)
322 (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
323 (dolist (p poly-list-lists args)
324 (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
325 . ,body))))
326 ,(if new-vars-supplied-p
327 `(append ,vars ,new-vars)
328 vars))))
329
330
331;;Functions
332
333(defmfun $poly_expand (p vars)
334 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
335If the representation is not compatible with a polynomial in variables VARS,
336the result is an error."
337 (with-parsed-polynomials ((vars) :polynomials (p)
338 :value-type :polynomial)
339 p))
340
341(defmfun $poly_expt (p n vars)
342 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
343 (poly-expt *expression-ring* p n)))
344
345(defmfun $poly_content (p vars)
346 (with-parsed-polynomials ((vars) :polynomials (p))
347 (poly-content *expression-ring* p)))
348
349(defmfun $poly_pseudo_divide (f fl vars
350 &aux (vars (coerce-maxima-list vars))
351 (f (parse-poly f vars))
352 (fl (parse-poly-list fl vars)))
353 (multiple-value-bind (quot rem c division-count)
354 (poly-pseudo-divide *expression-ring* f fl)
355 `((mlist)
356 ,(coerce-to-maxima :poly-list quot vars)
357 ,(coerce-to-maxima :polynomial rem vars)
358 ,c
359 ,division-count)))
360
361(defmfun $poly_exact_divide (f g vars)
362 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
363 (poly-exact-divide *expression-ring* f g)))
364
365(defmfun $poly_normal_form (f fl vars)
366 (with-parsed-polynomials ((vars) :polynomials (f)
367 :poly-lists (fl)
368 :value-type :polynomial)
369 (normal-form *expression-ring* f (remzero fl) nil)))
370
371(defmfun $poly_buchberger_criterion (g vars)
372 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
373 (buchberger-criterion *expression-ring* g)))
374
375(defmfun $poly_buchberger (fl vars)
376 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
377 (buchberger *expression-ring* (remzero fl) 0 nil)))
378
379(defmfun $poly_reduction (plist vars)
380 (with-parsed-polynomials ((vars) :poly-lists (plist)
381 :value-type :poly-list)
382 (reduction *expression-ring* plist)))
383
384(defmfun $poly_minimization (plist vars)
385 (with-parsed-polynomials ((vars) :poly-lists (plist)
386 :value-type :poly-list)
387 (minimization plist)))
388
389(defmfun $poly_normalize_list (plist vars)
390 (with-parsed-polynomials ((vars) :poly-lists (plist)
391 :value-type :poly-list)
392 (poly-normalize-list *expression-ring* plist)))
393
394(defmfun $poly_grobner (f vars)
395 (with-parsed-polynomials ((vars) :poly-lists (f)
396 :value-type :poly-list)
397 (grobner *expression-ring* (remzero f))))
398
399(defmfun $poly_reduced_grobner (f vars)
400 (with-parsed-polynomials ((vars) :poly-lists (f)
401 :value-type :poly-list)
402 (reduced-grobner *expression-ring* (remzero f))))
403
404(defmfun $poly_depends_p (p var mvars
405 &aux (vars (coerce-maxima-list mvars))
406 (pos (position var vars)))
407 (if (null pos)
408 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
409 (poly-depends-p (parse-poly p vars) pos)))
410
411(defmfun $poly_elimination_ideal (flist k vars)
412 (with-parsed-polynomials ((vars) :poly-lists (flist)
413 :value-type :poly-list)
414 (elimination-ideal *expression-ring* flist k nil 0)))
415
416(defmfun $poly_colon_ideal (f g vars)
417 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
418 (colon-ideal *expression-ring* f g nil)))
419
420(defmfun $poly_ideal_intersection (f g vars)
421 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
422 (ideal-intersection *expression-ring* f g nil)))
423
424(defmfun $poly_lcm (f g vars)
425 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
426 (poly-lcm *expression-ring* f g)))
427
428(defmfun $poly_gcd (f g vars)
429 ($first ($divide (m* f g) ($poly_lcm f g vars))))
430
431(defmfun $poly_grobner_equal (g1 g2 vars)
432 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
433 (grobner-equal *expression-ring* g1 g2)))
434
435(defmfun $poly_grobner_subsetp (g1 g2 vars)
436 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
437 (grobner-subsetp *expression-ring* g1 g2)))
438
439(defmfun $poly_grobner_member (p g vars)
440 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
441 (grobner-member *expression-ring* p g)))
442
443(defmfun $poly_ideal_saturation1 (f p vars)
444 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
445 :value-type :poly-list)
446 (ideal-saturation-1 *expression-ring* f p 0)))
447
448(defmfun $poly_saturation_extension (f plist vars new-vars)
449 (with-parsed-polynomials ((vars new-vars)
450 :poly-lists (f plist)
451 :value-type :poly-list)
452 (saturation-extension *expression-ring* f plist)))
453
454(defmfun $poly_polysaturation_extension (f plist vars new-vars)
455 (with-parsed-polynomials ((vars new-vars)
456 :poly-lists (f plist)
457 :value-type :poly-list)
458 (polysaturation-extension *expression-ring* f plist)))
459
460(defmfun $poly_ideal_polysaturation1 (f plist vars)
461 (with-parsed-polynomials ((vars) :poly-lists (f plist)
462 :value-type :poly-list)
463 (ideal-polysaturation-1 *expression-ring* f plist 0 nil)))
464
465(defmfun $poly_ideal_saturation (f g vars)
466 (with-parsed-polynomials ((vars) :poly-lists (f g)
467 :value-type :poly-list)
468 (ideal-saturation *expression-ring* f g 0 nil)))
469
470(defmfun $poly_ideal_polysaturation (f ideal-list vars)
471 (with-parsed-polynomials ((vars) :poly-lists (f)
472 :poly-list-lists (ideal-list)
473 :value-type :poly-list)
474 (ideal-polysaturation *expression-ring* f ideal-list 0 nil)))
475
476(defmfun $poly_lt (f vars)
477 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
478 (make-poly-from-termlist (list (poly-lt f)))))
479
480(defmfun $poly_lm (f vars)
481 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
482 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *expression-ring*)))))))
483
Note: See TracBrowser for help on using the repository browser.