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

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

* empty log message *

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