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

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

* empty log message *

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