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

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