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

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

* empty log message *

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