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

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

* empty log message *

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