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

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

* empty log message *

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