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