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

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

* empty log message *

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