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

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

* empty log message *

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