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

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

* empty log message *

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