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

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

* empty log message *

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