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

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

* empty log message *

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