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

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

* empty log message *

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