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

Last change on this file since 369 was 369, checked in by Marek Rychlik, 10 years ago

* empty log message *

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