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