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 | (defpackage "BUCHBERGER"
|
---|
23 | (:use :cl :polynomial :order :grobner-debug :division :criterion)
|
---|
24 | (:export "BUCHBERGER" "PARALLEL-BUCHBERGER"))
|
---|
25 |
|
---|
26 | (in-package :buchberger)
|
---|
27 |
|
---|
28 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
29 | ;;
|
---|
30 | ;; Buchberger Algorithm Implementation
|
---|
31 | ;;
|
---|
32 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
33 |
|
---|
34 | (defun buchberger (ring f start &optional (top-reduction-only $poly_top_reduction_only))
|
---|
35 | "An implementation of the Buchberger algorithm. Return Grobner basis
|
---|
36 | of the ideal generated by the polynomial list F. Polynomials 0 to
|
---|
37 | START-1 are assumed to be a Grobner basis already, so that certain
|
---|
38 | critical pairs will not be examined. If TOP-REDUCTION-ONLY set, top
|
---|
39 | reduction will be preformed. This function assumes that all polynomials
|
---|
40 | in F are non-zero."
|
---|
41 | (declare (type fixnum start))
|
---|
42 | (when (endp f) (return-from buchberger f)) ;cut startup costs
|
---|
43 | (debug-cgb "~&GROBNER BASIS - BUCHBERGER ALGORITHM")
|
---|
44 | (when (plusp start) (debug-cgb "~&INCREMENTAL:~d done" start))
|
---|
45 | #+grobner-check (when (plusp start)
|
---|
46 | (grobner-test ring (subseq f 0 start) (subseq f 0 start)))
|
---|
47 | ;;Initialize critical pairs
|
---|
48 | (let ((b (pair-queue-initialize (make-pair-queue)
|
---|
49 | f start))
|
---|
50 | (b-done (make-hash-table :test #'equal)))
|
---|
51 | (declare (type priority-queue b) (type hash-table b-done))
|
---|
52 | (dotimes (i (1- start))
|
---|
53 | (do ((j (1+ i) (1+ j))) ((>= j start))
|
---|
54 | (setf (gethash (list (elt f i) (elt f j)) b-done) t)))
|
---|
55 | (do ()
|
---|
56 | ((pair-queue-empty-p b)
|
---|
57 | #+grobner-check(grobner-test ring f f)
|
---|
58 | (debug-cgb "~&GROBNER END")
|
---|
59 | f)
|
---|
60 | (let ((pair (pair-queue-remove b)))
|
---|
61 | (declare (type pair pair))
|
---|
62 | (cond
|
---|
63 | ((criterion-1 pair) nil)
|
---|
64 | ((criterion-2 pair b-done f) nil)
|
---|
65 | (t
|
---|
66 | (let ((sp (normal-form ring (spoly ring (pair-first pair)
|
---|
67 | (pair-second pair))
|
---|
68 | f top-reduction-only)))
|
---|
69 | (declare (type poly sp))
|
---|
70 | (cond
|
---|
71 | ((poly-zerop sp)
|
---|
72 | nil)
|
---|
73 | (t
|
---|
74 | (setf sp (poly-primitive-part ring sp)
|
---|
75 | f (nconc f (list sp)))
|
---|
76 | ;; Add new critical pairs
|
---|
77 | (dolist (h f)
|
---|
78 | (pair-queue-insert b (make-pair h sp)))
|
---|
79 | (debug-cgb "~&Sugar: ~d Polynomials: ~d; Pairs left: ~d; Pairs done: ~d;"
|
---|
80 | (pair-sugar pair) (length f) (pair-queue-size b)
|
---|
81 | (hash-table-count b-done)))))))
|
---|
82 | (setf (gethash (list (pair-first pair) (pair-second pair)) b-done)
|
---|
83 | t)))))
|
---|
84 |
|
---|
85 | (defun parallel-buchberger (ring f start &optional (top-reduction-only $poly_top_reduction_only))
|
---|
86 | "An implementation of the Buchberger algorithm. Return Grobner basis
|
---|
87 | of the ideal generated by the polynomial list F. Polynomials 0 to
|
---|
88 | START-1 are assumed to be a Grobner basis already, so that certain
|
---|
89 | critical pairs will not be examined. If TOP-REDUCTION-ONLY set, top
|
---|
90 | reduction will be preformed."
|
---|
91 | (declare (ignore top-reduction-only)
|
---|
92 | (type fixnum start))
|
---|
93 | (when (endp f) (return-from parallel-buchberger f)) ;cut startup costs
|
---|
94 | (debug-cgb "~&GROBNER BASIS - PARALLEL-BUCHBERGER ALGORITHM")
|
---|
95 | (when (plusp start) (debug-cgb "~&INCREMENTAL:~d done" start))
|
---|
96 | #+grobner-check (when (plusp start)
|
---|
97 | (grobner-test ring (subseq f 0 start) (subseq f 0 start)))
|
---|
98 | ;;Initialize critical pairs
|
---|
99 | (let ((b (pair-queue-initialize (make-pair-queue) f start))
|
---|
100 | (b-done (make-hash-table :test #'equal)))
|
---|
101 | (declare (type priority-queue b)
|
---|
102 | (type hash-table b-done))
|
---|
103 | (dotimes (i (1- start))
|
---|
104 | (do ((j (1+ i) (1+ j))) ((>= j start))
|
---|
105 | (declare (type fixnum j))
|
---|
106 | (setf (gethash (list (elt f i) (elt f j)) b-done) t)))
|
---|
107 | (do ()
|
---|
108 | ((pair-queue-empty-p b)
|
---|
109 | #+grobner-check(grobner-test ring f f)
|
---|
110 | (debug-cgb "~&GROBNER END")
|
---|
111 | f)
|
---|
112 | (let ((pair (pair-queue-remove b)))
|
---|
113 | (when (null (pair-division-data pair))
|
---|
114 | (setf (pair-division-data pair) (list (spoly ring
|
---|
115 | (pair-first pair)
|
---|
116 | (pair-second pair))
|
---|
117 | (make-poly-zero)
|
---|
118 | (funcall (ring-unit ring))
|
---|
119 | 0)))
|
---|
120 | (cond
|
---|
121 | ((criterion-1 pair) nil)
|
---|
122 | ((criterion-2 pair b-done f) nil)
|
---|
123 | (t
|
---|
124 | (let* ((dd (pair-division-data pair))
|
---|
125 | (p (first dd))
|
---|
126 | (sp (second dd))
|
---|
127 | (c (third dd))
|
---|
128 | (division-count (fourth dd)))
|
---|
129 | (cond
|
---|
130 | ((poly-zerop p) ;normal form completed
|
---|
131 | (debug-cgb "~&~3T~d reduction~:p" division-count)
|
---|
132 | (cond
|
---|
133 | ((poly-zerop sp)
|
---|
134 | (debug-cgb " ---> 0")
|
---|
135 | nil)
|
---|
136 | (t
|
---|
137 | (setf sp (poly-nreverse sp)
|
---|
138 | sp (poly-primitive-part ring sp)
|
---|
139 | f (nconc f (list sp)))
|
---|
140 | ;; Add new critical pairs
|
---|
141 | (dolist (h f)
|
---|
142 | (pair-queue-insert b (make-pair h sp)))
|
---|
143 | (debug-cgb "~&Sugar: ~d Polynomials: ~d; Pairs left: ~d; Pairs done: ~d;"
|
---|
144 | (pair-sugar pair) (length f) (pair-queue-size b)
|
---|
145 | (hash-table-count b-done))))
|
---|
146 | (setf (gethash (list (pair-first pair) (pair-second pair))
|
---|
147 | b-done) t))
|
---|
148 | (t ;normal form not complete
|
---|
149 | (do ()
|
---|
150 | ((cond
|
---|
151 | ((> (poly-sugar sp) (pair-sugar pair))
|
---|
152 | (debug-cgb "(~a)?" (poly-sugar sp))
|
---|
153 | t)
|
---|
154 | ((poly-zerop p)
|
---|
155 | (debug-cgb ".")
|
---|
156 | t)
|
---|
157 | (t nil))
|
---|
158 | (setf (first dd) p
|
---|
159 | (second dd) sp
|
---|
160 | (third dd) c
|
---|
161 | (fourth dd) division-count
|
---|
162 | (pair-sugar pair) (poly-sugar sp))
|
---|
163 | (pair-queue-insert b pair))
|
---|
164 | (multiple-value-setq (p sp c division-count)
|
---|
165 | (normal-form-step ring f p sp c division-count))))))))))))
|
---|