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

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

* empty log message *

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