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

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

* empty log message *

File size: 12.8 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
42;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43;;
44;; Maxima expression ring
45;;
46;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47
48(defparameter *expression-ring*
49 (make-ring
50 ;;(defun coeff-zerop (expr) (meval1 `(($is) (($equal) ,expr 0))))
51 :parse #'(lambda (expr)
52 (when modulus (setf expr ($rat expr)))
53 expr)
54 :unit #'(lambda () (if modulus ($rat 1) 1))
55 :zerop #'(lambda (expr)
56 ;;When is exactly a maxima expression equal to 0?
57 (cond ((numberp expr)
58 (= expr 0))
59 ((atom expr) nil)
60 (t
61 (case (caar expr)
62 (mrat (eql ($ratdisrep expr) 0))
63 (otherwise (eql ($totaldisrep expr) 0))))))
64 :add #'(lambda (x y) (m+ x y))
65 :sub #'(lambda (x y) (m- x y))
66 :uminus #'(lambda (x) (m- x))
67 :mul #'(lambda (x y) (m* x y))
68 ;;(defun coeff-div (x y) (cadr ($divide x y)))
69 :div #'(lambda (x y) (m// x y))
70 :lcm #'(lambda (x y) (meval1 `((|$LCM|) ,x ,y)))
71 :ezgcd #'(lambda (x y) (apply #'values (cdr ($ezgcd ($totaldisrep x) ($totaldisrep y)))))
72 ;; :gcd #'(lambda (x y) (second ($ezgcd x y)))))
73 :gcd #'(lambda (x y) ($gcd x y))))
74
75;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76;;
77;; Maxima expression parsing
78;;
79;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80
81(defun equal-test-p (expr1 expr2)
82 (alike1 expr1 expr2))
83
84(defun coerce-maxima-list (expr)
85 "convert a maxima list to lisp list."
86 (cond
87 ((and (consp (car expr)) (eql (caar expr) 'mlist)) (cdr expr))
88 (t expr)))
89
90(defun free-of-vars (expr vars) (apply #'$freeof `(,@vars ,expr)))
91
92(defun parse-poly (expr vars &aux (vars (coerce-maxima-list vars)))
93 "Convert a maxima polynomial expression EXPR in variables VARS to internal form."
94 (labels ((parse (arg) (parse-poly arg vars))
95 (parse-list (args) (mapcar #'parse args)))
96 (cond
97 ((eql expr 0) (make-poly-zero))
98 ((member expr vars :test #'equal-test-p)
99 (let ((pos (position expr vars :test #'equal-test-p)))
100 (make-variable *maxima-ring* (length vars) pos)))
101 ((free-of-vars expr vars)
102 ;;This means that variable-free CRE and Poisson forms will be converted
103 ;;to coefficients intact
104 (coerce-coeff *maxima-ring* expr vars))
105 (t
106 (case (caar expr)
107 (mplus (reduce #'(lambda (x y) (poly-add *maxima-ring* x y)) (parse-list (cdr expr))))
108 (mminus (poly-uminus *maxima-ring* (parse (cadr expr))))
109 (mtimes
110 (if (endp (cddr expr)) ;unary
111 (parse (cdr expr))
112 (reduce #'(lambda (p q) (poly-mul *maxima-ring* p q)) (parse-list (cdr expr)))))
113 (mexpt
114 (cond
115 ((member (cadr expr) vars :test #'equal-test-p)
116 ;;Special handling of (expt var pow)
117 (let ((pos (position (cadr expr) vars :test #'equal-test-p)))
118 (make-variable *maxima-ring* (length vars) pos (caddr expr))))
119 ((not (and (integerp (caddr expr)) (plusp (caddr expr))))
120 ;; Negative power means division in coefficient ring
121 ;; Non-integer power means non-polynomial coefficient
122 (mtell "~%Warning: Expression ~%~M~%contains power which is not a positive integer. Parsing as coefficient.~%"
123 expr)
124 (coerce-coeff *maxima-ring* expr vars))
125 (t (poly-expt *maxima-ring* (parse (cadr expr)) (caddr expr)))))
126 (mrat (parse ($ratdisrep expr)))
127 (mpois (parse ($outofpois expr)))
128 (otherwise
129 (coerce-coeff *maxima-ring* expr vars)))))))
130
131(defun parse-poly-list (expr vars)
132 (case (caar expr)
133 (mlist (mapcar #'(lambda (p) (parse-poly p vars)) (cdr expr)))
134 (t (merror "Expression ~M is not a list of polynomials in variables ~M."
135 expr vars))))
136(defun parse-poly-list-list (poly-list-list vars)
137 (mapcar #'(lambda (g) (parse-poly-list g vars)) (coerce-maxima-list poly-list-list)))
138
139
140;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141;;
142;; Unary and binary operation definition facility
143;;
144;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
145
146(defmacro define-unop (maxima-name fun-name
147 &optional (documentation nil documentation-supplied-p))
148 "Define a MAXIMA-level unary operator MAXIMA-NAME corresponding to unary function FUN-NAME."
149 `(defun ,maxima-name (p vars
150 &aux
151 (vars (coerce-maxima-list vars))
152 (p (parse-poly p vars)))
153 ,@(when documentation-supplied-p (list documentation))
154 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p) vars)))
155
156(defmacro define-binop (maxima-name fun-name
157 &optional (documentation nil documentation-supplied-p))
158 "Define a MAXIMA-level binary operator MAXIMA-NAME corresponding to binary function FUN-NAME."
159 `(defmfun ,maxima-name (p q vars
160 &aux
161 (vars (coerce-maxima-list vars))
162 (p (parse-poly p vars))
163 (q (parse-poly q vars)))
164 ,@(when documentation-supplied-p (list documentation))
165 (coerce-to-maxima :polynomial (,fun-name *maxima-ring* p q) vars)))
166
167
168;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169;;
170;; Maxima-level interface functions
171;;
172;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173
174;; Auxillary function for removing zero polynomial
175(defun remzero (plist) (remove #'poly-zerop plist))
176
177;;Simple operators
178
179(define-binop $poly_add poly-add
180 "Adds two polynomials P and Q")
181
182(define-binop $poly_subtract poly-sub
183 "Subtracts a polynomial Q from P.")
184
185(define-binop $poly_multiply poly-mul
186 "Returns the product of polynomials P and Q.")
187
188(define-binop $poly_s_polynomial spoly
189 "Returns the syzygy polynomial (S-polynomial) of two polynomials P and Q.")
190
191(define-unop $poly_primitive_part poly-primitive-part
192 "Returns the polynomial P divided by GCD of its coefficients.")
193
194(define-unop $poly_normalize poly-normalize
195 "Returns the polynomial P divided by the leading coefficient.")
196
197;;Functions
198
199(defmfun $poly_expand (p vars)
200 "This function is equivalent to EXPAND(P) if P parses correctly to a polynomial.
201If the representation is not compatible with a polynomial in variables VARS,
202the result is an error."
203 (with-parsed-polynomials ((vars) :polynomials (p)
204 :value-type :polynomial)
205 p))
206
207(defmfun $poly_expt (p n vars)
208 (with-parsed-polynomials ((vars) :polynomials (p) :value-type :polynomial)
209 (poly-expt *maxima-ring* p n)))
210
211(defmfun $poly_content (p vars)
212 (with-parsed-polynomials ((vars) :polynomials (p))
213 (poly-content *maxima-ring* p)))
214
215(defmfun $poly_pseudo_divide (f fl vars
216 &aux (vars (coerce-maxima-list vars))
217 (f (parse-poly f vars))
218 (fl (parse-poly-list fl vars)))
219 (multiple-value-bind (quot rem c division-count)
220 (poly-pseudo-divide *maxima-ring* f fl)
221 `((mlist)
222 ,(coerce-to-maxima :poly-list quot vars)
223 ,(coerce-to-maxima :polynomial rem vars)
224 ,c
225 ,division-count)))
226
227(defmfun $poly_exact_divide (f g vars)
228 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
229 (poly-exact-divide *maxima-ring* f g)))
230
231(defmfun $poly_normal_form (f fl vars)
232 (with-parsed-polynomials ((vars) :polynomials (f)
233 :poly-lists (fl)
234 :value-type :polynomial)
235 (normal-form *maxima-ring* f (remzero fl) nil)))
236
237(defmfun $poly_buchberger_criterion (g vars)
238 (with-parsed-polynomials ((vars) :poly-lists (g) :value-type :logical)
239 (buchberger-criterion *maxima-ring* g)))
240
241(defmfun $poly_buchberger (fl vars)
242 (with-parsed-polynomials ((vars) :poly-lists (fl) :value-type :poly-list)
243 (buchberger *maxima-ring* (remzero fl) 0 nil)))
244
245(defmfun $poly_reduction (plist vars)
246 (with-parsed-polynomials ((vars) :poly-lists (plist)
247 :value-type :poly-list)
248 (reduction *maxima-ring* plist)))
249
250(defmfun $poly_minimization (plist vars)
251 (with-parsed-polynomials ((vars) :poly-lists (plist)
252 :value-type :poly-list)
253 (minimization plist)))
254
255(defmfun $poly_normalize_list (plist vars)
256 (with-parsed-polynomials ((vars) :poly-lists (plist)
257 :value-type :poly-list)
258 (poly-normalize-list *maxima-ring* plist)))
259
260(defmfun $poly_grobner (f vars)
261 (with-parsed-polynomials ((vars) :poly-lists (f)
262 :value-type :poly-list)
263 (grobner *maxima-ring* (remzero f))))
264
265(defmfun $poly_reduced_grobner (f vars)
266 (with-parsed-polynomials ((vars) :poly-lists (f)
267 :value-type :poly-list)
268 (reduced-grobner *maxima-ring* (remzero f))))
269
270(defmfun $poly_depends_p (p var mvars
271 &aux (vars (coerce-maxima-list mvars))
272 (pos (position var vars)))
273 (if (null pos)
274 (merror "~%Variable ~M not in the list of variables ~M." var mvars)
275 (poly-depends-p (parse-poly p vars) pos)))
276
277(defmfun $poly_elimination_ideal (flist k vars)
278 (with-parsed-polynomials ((vars) :poly-lists (flist)
279 :value-type :poly-list)
280 (elimination-ideal *maxima-ring* flist k nil 0)))
281
282(defmfun $poly_colon_ideal (f g vars)
283 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
284 (colon-ideal *maxima-ring* f g nil)))
285
286(defmfun $poly_ideal_intersection (f g vars)
287 (with-parsed-polynomials ((vars) :poly-lists (f g) :value-type :poly-list)
288 (ideal-intersection *maxima-ring* f g nil)))
289
290(defmfun $poly_lcm (f g vars)
291 (with-parsed-polynomials ((vars) :polynomials (f g) :value-type :polynomial)
292 (poly-lcm *maxima-ring* f g)))
293
294(defmfun $poly_gcd (f g vars)
295 ($first ($divide (m* f g) ($poly_lcm f g vars))))
296
297(defmfun $poly_grobner_equal (g1 g2 vars)
298 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
299 (grobner-equal *maxima-ring* g1 g2)))
300
301(defmfun $poly_grobner_subsetp (g1 g2 vars)
302 (with-parsed-polynomials ((vars) :poly-lists (g1 g2))
303 (grobner-subsetp *maxima-ring* g1 g2)))
304
305(defmfun $poly_grobner_member (p g vars)
306 (with-parsed-polynomials ((vars) :polynomials (p) :poly-lists (g))
307 (grobner-member *maxima-ring* p g)))
308
309(defmfun $poly_ideal_saturation1 (f p vars)
310 (with-parsed-polynomials ((vars) :poly-lists (f) :polynomials (p)
311 :value-type :poly-list)
312 (ideal-saturation-1 *maxima-ring* f p 0)))
313
314(defmfun $poly_saturation_extension (f plist vars new-vars)
315 (with-parsed-polynomials ((vars new-vars)
316 :poly-lists (f plist)
317 :value-type :poly-list)
318 (saturation-extension *maxima-ring* f plist)))
319
320(defmfun $poly_polysaturation_extension (f plist vars new-vars)
321 (with-parsed-polynomials ((vars new-vars)
322 :poly-lists (f plist)
323 :value-type :poly-list)
324 (polysaturation-extension *maxima-ring* f plist)))
325
326(defmfun $poly_ideal_polysaturation1 (f plist vars)
327 (with-parsed-polynomials ((vars) :poly-lists (f plist)
328 :value-type :poly-list)
329 (ideal-polysaturation-1 *maxima-ring* f plist 0 nil)))
330
331(defmfun $poly_ideal_saturation (f g vars)
332 (with-parsed-polynomials ((vars) :poly-lists (f g)
333 :value-type :poly-list)
334 (ideal-saturation *maxima-ring* f g 0 nil)))
335
336(defmfun $poly_ideal_polysaturation (f ideal-list vars)
337 (with-parsed-polynomials ((vars) :poly-lists (f)
338 :poly-list-lists (ideal-list)
339 :value-type :poly-list)
340 (ideal-polysaturation *maxima-ring* f ideal-list 0 nil)))
341
342(defmfun $poly_lt (f vars)
343 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
344 (make-poly-from-termlist (list (poly-lt f)))))
345
346(defmfun $poly_lm (f vars)
347 (with-parsed-polynomials ((vars) :polynomials (f) :value-type :polynomial)
348 (make-poly-from-termlist (list (make-term (poly-lm f) (funcall (ring-unit *maxima-ring*)))))))
349
Note: See TracBrowser for help on using the repository browser.