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

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

* empty log message *

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