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

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

* empty log message *

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