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

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

* empty log message *

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