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

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

* empty log message *

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