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

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

* empty log message *

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