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

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

* empty log message *

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