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

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

* empty log message *

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