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

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