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

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