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