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

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