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

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

* empty log message *

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