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

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

* empty log message *

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