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

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

* empty log message *

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