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

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

* empty log message *

File size: 15.7 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(use-package :ngrobner)
60
61;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62;;
63;; Maxima expression ring
64;;
65;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66
67(defparameter *maxima-ring*
68 (make-ring
69 ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
70 :parse #'(lambda (expr)
71 (when modulus (setf expr ($rat expr)))
72 expr)
73 :unit #'(lambda () (if modulus ($rat 1) 1))
74 :zerop #'(lambda (expr)
75 ;;When is exactly a maxima expression equal to 0?
76 (cond ((numberp expr)
77 (= expr 0))
78 ((atom expr) nil)
79 (t
80 (case (caar expr)
81 (mrat (eql ($ratdisrep expr) 0))
82 (otherwise (eql ($totaldisrep expr) 0))))))
83 :add #'(lambda (x y) (m+ x y))
84 :sub #'(lambda (x y) (m- x y))
85 :uminus #'(lambda (x) (m- x))
86 :mul #'(lambda (x y) (m* x y))
87 ;;(defun coeff-div (x y) (cadr ($divide x y)))
88 :div #'(lambda (x y) (m// x y))
89 :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
90 :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
91 ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
92 :gcd #'(lambda (x y) ($gcd x y))))
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;; Unary and binary operation definition facility
162;;
163;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164
165(defmacro define-unop (maxima-name fun-name
166 &optional (documentation nil documentation-supplied-p))
167 "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
168 `(defun ,maxima-name (p vars
169 &aux
170 (vars (coerce-maxima-list vars))
171 (p (parse-poly p vars)))
172 ,@(when documentation-supplied-p (list documentation))
173 (coerce-to-maxima :polynomial (,fun-name *expression-ring* p) vars)))
174
175(defmacro define-binop (maxima-name fun-name
176 &optional (documentation nil documentation-supplied-p))
177 "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
178 `(defmfun ,maxima-name (p q vars
179 &aux
180 (vars (coerce-maxima-list vars))
181 (p (parse-poly p vars))
182 (q (parse-poly q vars)))
183 ,@(when documentation-supplied-p (list documentation))
184 (coerce-to-maxima :polynomial (,fun-name *expression-ring* p q) vars)))
185
186
187;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188;;
189;; Facilities for evaluating Grobner package expressions
190;; within a prepared environment
191;;
192;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193
194(defmacro with-monomial-order ((order) &body body)
195 "Evaluate BODY with monomial order set to ORDER."
196 `(let ((*monomial-order* (or (find-order ,order) *monomial-order*)))
197 . ,body))
198
199(defmacro with-coefficient-ring ((ring) &body body)
200 "Evaluate BODY with coefficient ring set to RING."
201 `(let ((*expression-ring* (or (find-ring ,ring) *expression-ring*)))
202 . ,body))
203
204(defmacro with-elimination-orders ((primary secondary elimination-order)
205 &body body)
206 "Evaluate BODY with primary and secondary elimination orders set to PRIMARY and SECONDARY."
207 `(let ((*primary-elimination-order* (or (find-order ,primary) *primary-elimination-order*))
208 (*secondary-elimination-order* (or (find-order ,secondary) *secondary-elimination-order*))
209 (*elimination-order* (or (find-order ,elimination-order) *elimination-order*)))
210 . ,body))
211
212
213;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
214;;
215;; Maxima-level interface functions
216;;
217;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
218
219;; Auxillary function for removing zero polynomial
220(defun remzero (plist) (remove #'poly-zerop plist))
221
222;;Simple operators
223
224(define-binop $poly_add poly-add
225 "Adds two polynomials P and Q")
226
227(define-binop $poly_subtract poly-sub
228 "Subtracts a polynomial Q from P.")
229
230(define-binop $poly_multiply poly-mul
231 "Returns the product of polynomials P and Q.")
232
233(define-binop $poly_s_polynomial spoly
234 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
235
236(define-unop $poly_primitive_part poly-primitive-part
237 "Returns the polynomial P divided by GCD of its coefficients.")
238
239(define-unop $poly_normalize poly-normalize
240 "Returns the polynomial P divided by the leading coefficient.")
241
242;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243;;
244;; Macro facility for writing Maxima-level wrappers for
245;; functions operating on internal representation
246;;
247;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
248
249(defmacro with-parsed-polynomials (((maxima-vars &optional (maxima-new-vars nil new-vars-supplied-p))
250 &key (polynomials nil)
251 (poly-lists nil)
252 (poly-list-lists nil)
253 (value-type nil))
254 &body body
255 &aux (vars (gensym))
256 (new-vars (gensym)))
257 `(let ((,vars (coerce-maxima-list ,maxima-vars))
258 ,@(when new-vars-supplied-p
259 (list `(,new-vars (coerce-maxima-list ,maxima-new-vars)))))
260 (coerce-to-maxima
261 ,value-type
262 (with-coefficient-ring ($poly_coefficient_ring)
263 (with-monomial-order ($poly_monomial_order)
264 (with-elimination-orders ($poly_primary_elimination_order
265 $poly_secondary_elimination_order
266 $poly_elimination_order)
267 (let ,(let ((args nil))
268 (dolist (p polynomials args)
269 (setf args (cons `(,p (parse-poly ,p ,vars)) args)))
270 (dolist (p poly-lists args)
271 (setf args (cons `(,p (parse-poly-list ,p ,vars)) args)))
272 (dolist (p poly-list-lists args)
273 (setf args (cons `(,p (parse-poly-list-list ,p ,vars)) args))))
274 . ,body))))
275 ,(if new-vars-supplied-p
276 `(append ,vars ,new-vars)
277 vars))))
278
279
280;;Functions
281
282(defmfun $poly_expand (p vars)
283 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
284If the representation is not compatible with a polynomial in variables VARS,
285the result is an error."
286 (with-parsed-polynomials ((vars) :polynomials (p)
287 :value-type :polynomial)
288 p))
289
290(defmfun $poly_expt (p n vars)
291 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
292 (poly-expt *expression-ring* p n)))
293
294(defmfun $poly_content (p vars)
295 (with-parsed-polynomials ((vars) :polynomials (p))
296 (poly-content *expression-ring* p)))
297
298(defmfun $poly_pseudo_divide (f fl vars
299 &aux (vars (coerce-maxima-list vars))
300 (f (parse-poly f vars))
301 (fl (parse-poly-list fl vars)))
302 (multiple-value-bind (quot rem c division-count)
303 (poly-pseudo-divide *expression-ring* f fl)
304 `((mlist)
305 ,(coerce-to-maxima :poly-list quot vars)
306 ,(coerce-to-maxima :polynomial rem vars)
307 ,c
308 ,division-count)))
309
310(defmfun $poly_exact_divide (f g vars)
311 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
312 (poly-exact-divide *expression-ring* f g)))
313
314(defmfun $poly_normal_form (f fl vars)
315 (with-parsed-polynomials ((vars) :polynomials (f)
316 :poly-lists (fl)
317 :value-type :polynomial)
318 (normal-form *expression-ring* f (remzero fl) nil)))
319
320(defmfun $poly_buchberger_criterion (g vars)
321 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
322 (buchberger-criterion *expression-ring* g)))
323
324(defmfun $poly_buchberger (fl vars)
325 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
326 (buchberger *expression-ring* (remzero fl) 0 nil)))
327
328(defmfun $poly_reduction (plist vars)
329 (with-parsed-polynomials ((vars) :poly-lists (plist)
330 :value-type :poly-list)
331 (reduction *expression-ring* plist)))
332
333(defmfun $poly_minimization (plist vars)
334 (with-parsed-polynomials ((vars) :poly-lists (plist)
335 :value-type :poly-list)
336 (minimization plist)))
337
338(defmfun $poly_normalize_list (plist vars)
339 (with-parsed-polynomials ((vars) :poly-lists (plist)
340 :value-type :poly-list)
341 (poly-normalize-list *expression-ring* plist)))
342
343(defmfun $poly_grobner (f vars)
344 (with-parsed-polynomials ((vars) :poly-lists (f)
345 :value-type :poly-list)
346 (grobner *expression-ring* (remzero f))))
347
348(defmfun $poly_reduced_grobner (f vars)
349 (with-parsed-polynomials ((vars) :poly-lists (f)
350 :value-type :poly-list)
351 (reduced-grobner *expression-ring* (remzero f))))
352
353(defmfun $poly_depends_p (p var mvars
354 &aux (vars (coerce-maxima-list mvars))
355 (pos (position var vars)))
356 (if (null pos)
357 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
358 (poly-depends-p (parse-poly p vars) pos)))
359
360(defmfun $poly_elimination_ideal (flist k vars)
361 (with-parsed-polynomials ((vars) :poly-lists (flist)
362 :value-type :poly-list)
363 (elimination-ideal *expression-ring* flist k nil 0)))
364
365(defmfun $poly_colon_ideal (f g vars)
366 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
367 (colon-ideal *expression-ring* f g nil)))
368
369(defmfun $poly_ideal_intersection (f g vars)
370 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
371 (ideal-intersection *expression-ring* f g nil)))
372
373(defmfun $poly_lcm (f g vars)
374 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
375 (poly-lcm *expression-ring* f g)))
376
377(defmfun $poly_gcd (f g vars)
378 ($first ($divide (m* f g) ($poly_lcm f g vars))))
379
380(defmfun $poly_grobner_equal (g1 g2 vars)
381 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
382 (grobner-equal *expression-ring* g1 g2)))
383
384(defmfun $poly_grobner_subsetp (g1 g2 vars)
385 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
386 (grobner-subsetp *expression-ring* g1 g2)))
387
388(defmfun $poly_grobner_member (p g vars)
389 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
390 (grobner-member *expression-ring* p g)))
391
392(defmfun $poly_ideal_saturation1 (f p vars)
393 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
394 :value-type :poly-list)
395 (ideal-saturation-1 *expression-ring* f p 0)))
396
397(defmfun $poly_saturation_extension (f plist vars new-vars)
398 (with-parsed-polynomials ((vars new-vars)
399 :poly-lists (f plist)
400 :value-type :poly-list)
401 (saturation-extension *expression-ring* f plist)))
402
403(defmfun $poly_polysaturation_extension (f plist vars new-vars)
404 (with-parsed-polynomials ((vars new-vars)
405 :poly-lists (f plist)
406 :value-type :poly-list)
407 (polysaturation-extension *expression-ring* f plist)))
408
409(defmfun $poly_ideal_polysaturation1 (f plist vars)
410 (with-parsed-polynomials ((vars) :poly-lists (f plist)
411 :value-type :poly-list)
412 (ideal-polysaturation-1 *expression-ring* f plist 0 nil)))
413
414(defmfun $poly_ideal_saturation (f g vars)
415 (with-parsed-polynomials ((vars) :poly-lists (f g)
416 :value-type :poly-list)
417 (ideal-saturation *expression-ring* f g 0 nil)))
418
419(defmfun $poly_ideal_polysaturation (f ideal-list vars)
420 (with-parsed-polynomials ((vars) :poly-lists (f)
421 :poly-list-lists (ideal-list)
422 :value-type :poly-list)
423 (ideal-polysaturation *expression-ring* f ideal-list 0 nil)))
424
425(defmfun $poly_lt (f vars)
426 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
427 (make-poly-from-termlist (list (poly-lt f)))))
428
429(defmfun $poly_lm (f vars)
430 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
431 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *expression-ring*)))))))
432
Note: See TracBrowser for help on using the repository browser.