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/buchberger.lisp@ 1309

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

* empty log message *

File size: 6.5 KB
Line 
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
44of the ideal generated by the polynomial list F. Polynomials 0 to
45START-1 are assumed to be a Grobner basis already, so that certain
46critical pairs will not be examined. If TOP-REDUCTION-ONLY set, top
47reduction will be preformed. This function assumes that all polynomials
48in 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 start
96 &optional
97 (top-reduction-only $poly_top_reduction_only)
98 &aux
99 (ring (ro-ring ring-and-order)))
100 "An implementation of the Buchberger algorithm. Return Grobner basis
101of the ideal generated by the polynomial list F. Polynomials 0 to
102START-1 are assumed to be a Grobner basis already, so that certain
103critical pairs will not be examined. If TOP-REDUCTION-ONLY set, top
104reduction will be preformed."
105 (declare (ignore top-reduction-only)
106 (type fixnum start))
107 (when (endp f) (return-from parallel-buchberger f)) ;cut startup costs
108 (debug-cgb "~&GROBNER BASIS - PARALLEL-BUCHBERGER ALGORITHM")
109 (when (plusp start) (debug-cgb "~&INCREMENTAL:~d done" start))
110 #+grobner-check (when (plusp start)
111 (grobner-test ring (subseq f 0 start) (subseq f 0 start)))
112 ;;Initialize critical pairs
113 (let ((b (pair-queue-initialize (make-pair-queue) f start))
114 (b-done (make-hash-table :test #'equal)))
115 (declare (type priority-queue b)
116 (type hash-table b-done))
117 (dotimes (i (1- start))
118 (do ((j (1+ i) (1+ j))) ((>= j start))
119 (declare (type fixnum j))
120 (setf (gethash (list (elt f i) (elt f j)) b-done) t)))
121 (do ()
122 ((pair-queue-empty-p b)
123 #+grobner-check(grobner-test ring f f)
124 (debug-cgb "~&GROBNER END")
125 f)
126 (let ((pair (pair-queue-remove b)))
127 (when (null (pair-division-data pair))
128 (setf (pair-division-data pair) (list (spoly ring-and-order
129 (pair-first pair)
130 (pair-second pair))
131 (make-poly-zero)
132 (funcall (ring-unit ring))
133 0)))
134 (cond
135 ((criterion-1 pair) nil)
136 ((criterion-2 pair b-done f) nil)
137 (t
138 (let* ((dd (pair-division-data pair))
139 (p (first dd))
140 (sp (second dd))
141 (c (third dd))
142 (division-count (fourth dd)))
143 (cond
144 ((poly-zerop p) ;normal form completed
145 (debug-cgb "~&~3T~d reduction~:p" division-count)
146 (cond
147 ((poly-zerop sp)
148 (debug-cgb " ---> 0")
149 nil)
150 (t
151 (setf sp (poly-nreverse sp)
152 sp (poly-primitive-part ring sp)
153 f (nconc f (list sp)))
154 ;; Add new critical pairs
155 (dolist (h f)
156 (pair-queue-insert b (make-pair h sp)))
157 (debug-cgb "~&Sugar: ~d Polynomials: ~d; Pairs left: ~d; Pairs done: ~d;"
158 (pair-sugar pair) (length f) (pair-queue-size b)
159 (hash-table-count b-done))))
160 (setf (gethash (list (pair-first pair) (pair-second pair))
161 b-done) t))
162 (t ;normal form not complete
163 (do ()
164 ((cond
165 ((> (poly-sugar sp) (pair-sugar pair))
166 (debug-cgb "(~a)?" (poly-sugar sp))
167 t)
168 ((poly-zerop p)
169 (debug-cgb ".")
170 t)
171 (t nil))
172 (setf (first dd) p
173 (second dd) sp
174 (third dd) c
175 (fourth dd) division-count
176 (pair-sugar pair) (poly-sugar sp))
177 (pair-queue-insert b pair))
178 (multiple-value-setq (p sp c division-count)
179 (normal-form-step ring-and-order f p sp c division-count))))))))))))
Note: See TracBrowser for help on using the repository browser.