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