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

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

* empty log message *

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