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

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

* empty log message *

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