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

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