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

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