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

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

* empty log message *

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