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/division.lisp@ 4485

Last change on this file since 4485 was 4485, checked in by Marek Rychlik, 8 years ago

Summary: Replaced grobner-op function with a macro, as grobner-op is called a lot

File size: 9.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 "DIVISION"
23 (:use :cl :copy :utils :monom :polynomial :grobner-debug :symbolic-polynomial)
24 (:export "$POLY_TOP_REDUCTION_ONLY"
25 "POLY-PSEUDO-DIVIDE"
26 "POLY-EXACT-DIVIDE"
27 "NORMAL-FORM-STEP"
28 "NORMAL-FORM"
29 "POLY-NORMALIZE"
30 "POLY-NORMALIZE-LIST"
31 "BUCHBERGER-CRITERION"
32 "GROBNER-TEST"
33 )
34 (:documentation
35 "An implementation of the division algorithm in the polynomial ring."))
36
37(in-package :division)
38
39(defvar $poly_top_reduction_only nil
40 "If not FALSE, use top reduction only whenever possible.
41Top reduction means that division algorithm stops after the first reduction.")
42
43
44(defmacro grobner-op (c1 c2 m f g)
45 "Returns C2*F-C1*M*G, where F and G are polynomials M is a monomial."
46 `(subtract (multiply ,f ,c2) (multiply ,g ,m ,c1)))
47
48(defun check-loop-invariant (c f a fl r p &aux (p-zero (make-zero-for f)))
49 "Check loop invariant of division algorithms, when we divide a
50polynomial F by the list of polynomials FL. The invariant is the
51identity C*F=SUM AI*FI+R+P, where F0 is the initial value of F, A is
52the list of partial quotients, R is the intermediate value of the
53remainder, and P is the intermediate value which eventually becomes
540."
55 #|
56 (format t "~&----------------------------------------------------------------~%")
57 (format t "#### Loop invariant check ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
58 c f a fl r p)
59 |#
60 (let* ((prod (inner-product a fl add multiply p-zero))
61 (succeeded-p (universal-zerop (subtract (multiply f c) (add prod (make-instance 'poly :termlist (reverse r)) p)))))
62 (unless succeeded-p
63 (error "#### Polynomial division Loop invariant failed ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
64 c f a fl r p))
65 succeeded-p))
66
67
68(defun poly-pseudo-divide (f fl)
69 "Pseudo-divide a polynomial F by the list of polynomials FL. Return
70multiple values. The first value is a list of quotients A. The second
71value is the remainder R. The third argument is a scalar coefficient
72C, such that C*F can be divided by FL within the ring of coefficients,
73which is not necessarily a field. Finally, the fourth value is an
74integer count of the number of reductions performed. The resulting
75objects satisfy the equation: C*F= sum A[i]*FL[i] + R. The sugar of
76the quotients is initialized to default."
77 (declare (type poly f) (list fl))
78 ;; Loop invariant: c*f=sum ai*fi+r+p, where p must eventually become 0
79 (do ((r nil)
80 (c (make-unit-for (leading-coefficient f)))
81 (a (make-list (length fl) :initial-element (make-zero-for f)))
82 (division-count 0)
83 (p f))
84 ((universal-zerop p)
85 #+grobner-check(check-loop-invariant c f a fl r p)
86 (debug-cgb "~&~3T~d reduction~:p" division-count)
87 (when (null r) (debug-cgb " ---> 0"))
88 (values a (make-instance 'poly :termlist (nreverse r)) c division-count))
89 (declare (fixnum division-count))
90 ;; Check the loop invariant here
91 #+grobner-check(check-loop-invariant c f a fl r p)
92 (do ((fl fl (rest fl)) ;scan list of divisors
93 (b a (rest b)))
94 ((cond
95 ((endp fl) ;no division occurred
96 (push (poly-remove-term p) r) ;move lt(p) to remainder
97 t)
98 ((divides-p (leading-monomial (car fl)) (leading-monomial p)) ;division occurred
99 (incf division-count)
100 (multiple-value-bind (gcd c1 c2)
101 (universal-ezgcd (leading-coefficient (car fl)) (leading-coefficient p))
102 (declare (ignore gcd))
103 (let ((m (divide (leading-monomial p) (leading-monomial (car fl)))))
104 ;; Multiply the equation c*f=sum ai*fi+r+p by c1.
105 (mapl #'(lambda (x)
106 (setf (car x) (multiply-by (car x) c1)))
107 a)
108 (setf r (mapc #'multiply-by r c1)
109 c (multiply-by c c1)
110 p (grobner-op c2 c1 m p (car fl)))
111 (setf (car b) (add (car b)
112 (change-class m 'term :coeff c2))))
113 t))))
114 )))
115
116(defun poly-exact-divide (f g)
117 "Divide a polynomial F by another polynomial G. Assume that exact division
118with no remainder is possible. Returns the quotient."
119 (declare (type poly f g))
120 (multiple-value-bind (quot rem coeff division-count)
121 (poly-pseudo-divide f (list g))
122 (declare (ignore division-count coeff)
123 (list quot)
124 (type poly rem)
125 (type fixnum division-count))
126 (unless (universal-zerop rem) (error "Exact division failed."))
127 (car quot)))
128
129;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130;;
131;; An implementation of the normal form
132;;
133;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134
135(defun normal-form-step (fl p r c division-count
136 &aux
137 (g (find (leading-monomial p) fl
138 :test #'divisible-by-p
139 :key #'leading-monomial)))
140 ;; NOTE: Currently R is a list of terms of the remainder
141 (cond
142 (g ;division possible
143 (incf division-count)
144 (multiple-value-bind (gcd cg cp)
145 (universal-ezgcd (leading-coefficient g) (leading-coefficient p))
146 (declare (ignore gcd))
147 (let ((m (divide (leading-monomial p) (leading-monomial g))))
148 ;; Multiply the equation c*f=sum ai*fi+r+p by cg.
149 (setf r (mapc #'(lambda (trm) (multiply-by trm cg)) r)
150 c (multiply-by c cg)
151 ;; p := cg*p-cp*m*g
152 p (grobner-op cp cg m p g))))
153 (debug-cgb "/"))
154 (t ;no division possible
155 (setf r (push (poly-remove-term p) r)) ;move lt(p) to remainder
156 (debug-cgb "+")))
157 (values p r c division-count))
158
159;;
160;; Merge NORMAL-FORM someday with POLY-PSEUDO-DIVIDE.
161;;
162;; TODO: It is hard to test normal form as there is no loop invariant,
163;; like for POLY-PSEUDO-DIVIDE. Is there a testing strategy? One
164;; method would be to test NORMAL-FORM using POLY-PSEUDO-DIVIDE.
165;;
166(defun normal-form (f fl &optional (top-reduction-only $poly_top_reduction_only))
167 #+grobner-check(when (null fl) (warn "normal-form: empty divisor list."))
168 (when (universal-zerop f)
169 #+grobner-check(when (null fl) (warn "normal-form: Dividend is zero."))
170 ;; NOTE: When the polynomial F is zero, we cannot constuct the
171 ;; unit in the coefficient field.
172 (return-from normal-form (values f nil 0)))
173 (do ((r nil)
174 (c (make-unit-for (leading-coefficient f)))
175 (division-count 0))
176 ((or (universal-zerop f)
177 ;;(endp fl)
178 (and top-reduction-only (not (null r))))
179 (progn
180 (debug-cgb "~&~3T~D reduction~:P" division-count)
181 (when (null r)
182 (debug-cgb " ---> 0")))
183 (setf (poly-termlist f) (nreconc r (poly-termlist f)))
184 (values f c division-count))
185 (declare (fixnum division-count))
186 (multiple-value-setq (f r c division-count)
187 (normal-form-step fl f r c division-count))))
188
189(defun buchberger-criterion (g)
190 "Returns T if G is a Grobner basis, by using the Buchberger
191criterion: for every two polynomials h1 and h2 in G the S-polynomial
192S(h1,h2) reduces to 0 modulo G."
193 (every #'universal-zerop
194 (makelist (normal-form (s-polynomial (elt g i) (elt g j)) g nil)
195 (i 0 (- (length g) 2))
196 (j (1+ i) (1- (length g))))))
197
198
199(defun poly-normalize (p &aux (c (leading-coefficient p)))
200 "Divide a polynomial by its leading coefficient. It assumes
201that the division is possible, which may not always be the
202case in rings which are not fields. The exact division operator
203is assumed to be provided by the RING structure."
204 (mapc #'(lambda (term)
205 (setf (term-coeff term) (divide (term-coeff term) c)))
206 (poly-termlist p))
207 p)
208
209(defun poly-normalize-list (plist)
210 "Divide every polynomial in a list PLIST by its leading coefficient. "
211 (mapcar #'(lambda (x) (poly-normalize x)) plist))
212
213(defun grobner-test (g f)
214 "Test whether G is a Grobner basis and F is contained in G. Return T
215upon success and NIL otherwise. The function GROBNER-TEST is provided
216primarily for debugging purposes. To enable verification of grobner
217bases with BUCHBERGER-CRITERION, do
218(pushnew :grobner-check *features*) and compile/load this file. With
219this feature, the calculations will slow down CONSIDERABLY."
220 (debug-cgb "~&GROBNER CHECK: ")
221 (let (($poly_grobner_debug nil)
222 (stat1 (buchberger-criterion g))
223 (stat2
224 (every #'universal-zerop
225 (makelist (normal-form (copy-instance (elt f i)) (mapcar #'copy-instance g) nil)
226 (i 0 (1- (length f)))))))
227 (unless stat1
228 (error "~&Buchberger criterion failed, not a grobner basis: ~A" g))
229 (unless stat2
230 (error "~&Original polynomials not in ideal spanned by Grobner basis: ~A" f)))
231 (debug-cgb "~&GROBNER CHECK END")
232 t)
Note: See TracBrowser for help on using the repository browser.