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

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

* empty log message *

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