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

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

* empty log message *

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