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

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

* empty log message *

File size: 15.0 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;;Functions
258
259(defmfun $poly_expand (p vars)
260 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
261If the representation is not compatible with a polynomial in variables VARS,
262the result is an error."
263 (with-parsed-polynomials ((vars) :polynomials (p)
264 :value-type :polynomial)
265 p))
266
267(defmfun $poly_expt (p n vars)
268 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
269 (poly-expt *maxima-ring* p n)))
270
271(defmfun $poly_content (p vars)
272 (with-parsed-polynomials ((vars) :polynomials (p))
273 (poly-content *maxima-ring* p)))
274
275(defmfun $poly_pseudo_divide (f fl vars
276 &aux (vars (coerce-maxima-list vars))
277 (f (parse-poly f vars))
278 (fl (parse-poly-list fl vars)))
279 (multiple-value-bind (quot rem c division-count)
280 (poly-pseudo-divide *maxima-ring* f fl)
281 `((mlist)
282 ,(coerce-to-maxima :poly-list quot vars)
283 ,(coerce-to-maxima :polynomial rem vars)
284 ,c
285 ,division-count)))
286
287(defmfun $poly_exact_divide (f g vars)
288 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
289 (poly-exact-divide *maxima-ring* f g)))
290
291(defmfun $poly_normal_form (f fl vars)
292 (with-parsed-polynomials ((vars) :polynomials (f)
293 :poly-lists (fl)
294 :value-type :polynomial)
295 (normal-form *maxima-ring* f (remzero fl) nil)))
296
297(defmfun $poly_buchberger_criterion (g vars)
298 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
299 (buchberger-criterion *maxima-ring* g)))
300
301(defmfun $poly_buchberger (fl vars)
302 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
303 (buchberger *maxima-ring* (remzero fl) 0 nil)))
304
305(defmfun $poly_reduction (plist vars)
306 (with-parsed-polynomials ((vars) :poly-lists (plist)
307 :value-type :poly-list)
308 (reduction *maxima-ring* plist)))
309
310(defmfun $poly_minimization (plist vars)
311 (with-parsed-polynomials ((vars) :poly-lists (plist)
312 :value-type :poly-list)
313 (minimization plist)))
314
315(defmfun $poly_normalize_list (plist vars)
316 (with-parsed-polynomials ((vars) :poly-lists (plist)
317 :value-type :poly-list)
318 (poly-normalize-list *maxima-ring* plist)))
319
320(defmfun $poly_grobner (f vars)
321 (with-parsed-polynomials ((vars) :poly-lists (f)
322 :value-type :poly-list)
323 (grobner *maxima-ring* (remzero f))))
324
325(defmfun $poly_reduced_grobner (f vars)
326 (with-parsed-polynomials ((vars) :poly-lists (f)
327 :value-type :poly-list)
328 (reduced-grobner *maxima-ring* (remzero f))))
329
330(defmfun $poly_depends_p (p var mvars
331 &aux (vars (coerce-maxima-list mvars))
332 (pos (position var vars)))
333 (if (null pos)
334 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
335 (poly-depends-p (parse-poly p vars) pos)))
336
337(defmfun $poly_elimination_ideal (flist k vars)
338 (with-parsed-polynomials ((vars) :poly-lists (flist)
339 :value-type :poly-list)
340 (elimination-ideal *maxima-ring* flist k nil 0)))
341
342(defmfun $poly_colon_ideal (f g vars)
343 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
344 (colon-ideal *maxima-ring* f g nil)))
345
346(defmfun $poly_ideal_intersection (f g vars)
347 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
348 (ideal-intersection *maxima-ring* f g nil)))
349
350(defmfun $poly_lcm (f g vars)
351 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
352 (poly-lcm *maxima-ring* f g)))
353
354(defmfun $poly_gcd (f g vars)
355 ($first ($divide (m* f g) ($poly_lcm f g vars))))
356
357(defmfun $poly_grobner_equal (g1 g2 vars)
358 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
359 (grobner-equal *maxima-ring* g1 g2)))
360
361(defmfun $poly_grobner_subsetp (g1 g2 vars)
362 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
363 (grobner-subsetp *maxima-ring* g1 g2)))
364
365(defmfun $poly_grobner_member (p g vars)
366 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
367 (grobner-member *maxima-ring* p g)))
368
369(defmfun $poly_ideal_saturation1 (f p vars)
370 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
371 :value-type :poly-list)
372 (ideal-saturation-1 *maxima-ring* f p 0)))
373
374(defmfun $poly_saturation_extension (f plist vars new-vars)
375 (with-parsed-polynomials ((vars new-vars)
376 :poly-lists (f plist)
377 :value-type :poly-list)
378 (saturation-extension *maxima-ring* f plist)))
379
380(defmfun $poly_polysaturation_extension (f plist vars new-vars)
381 (with-parsed-polynomials ((vars new-vars)
382 :poly-lists (f plist)
383 :value-type :poly-list)
384 (polysaturation-extension *maxima-ring* f plist)))
385
386(defmfun $poly_ideal_polysaturation1 (f plist vars)
387 (with-parsed-polynomials ((vars) :poly-lists (f plist)
388 :value-type :poly-list)
389 (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
390
391(defmfun $poly_ideal_saturation (f g vars)
392 (with-parsed-polynomials ((vars) :poly-lists (f g)
393 :value-type :poly-list)
394 (ideal-saturation *maxima-ring* f g 0 nil)))
395
396(defmfun $poly_ideal_polysaturation (f ideal-list vars)
397 (with-parsed-polynomials ((vars) :poly-lists (f)
398 :poly-list-lists (ideal-list)
399 :value-type :poly-list)
400 (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
401
402(defmfun $poly_lt (f vars)
403 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
404 (make-poly-from-termlist (list (poly-lt f)))))
405
406(defmfun $poly_lm (f vars)
407 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
408 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
409
Note: See TracBrowser for help on using the repository browser.