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

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

* empty log message *

File size: 11.0 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(defun grobner-op (c1 c2 m f g)
45 "Returns C2*F-C1*M*G, where F and G are polynomials M is a monomial.
46Assume that the leading terms will cancel."
47 (declare (type monom m)
48 (type poly f g))
49 #+grobner-check(universal-zerop
50 (subtract
51 (multiply c2 (leading-coefficient f))
52 (multiply c1 (leading-coefficient g))))
53 #+grobner-check(universal-equalp (leading-monomial f) (multiply m (leading-monomial g)))
54 ;; Note that below we can drop the leading terms of f ang g for the
55 ;; purpose of polynomial arithmetic.
56 ;;
57 ;; TODO: Make sure that the sugar calculation is correct if leading
58 ;; terms are dropped.
59 (subtract
60 (multiply f c2)
61 (multiply m g c1)))
62
63(defun check-loop-invariant (c f a fl r p
64 &aux
65 (p-zero (make-zero-for f))
66 (a (mapcar #'poly-reverse a))
67 (r (poly-reverse r)))
68 "Check loop invariant of division algorithms, when we divide a
69polynomial F by the list of polynomials FL. The invariant is the
70identity C*F=SUM AI*FI+R+P, where F0 is the initial value of F, A is
71the list of partial quotients, R is the intermediate value of the
72remainder, and P is the intermediate value which eventually becomes
730. A thing to remember is that the terms of polynomials in A and
74the polynomial R have their terms in reversed order. Hence, before
75the arithmetic is performed, we need to fix the order of terms"
76 #|
77 (format t "~&----------------------------------------------------------------~%")
78 (format t "#### Loop invariant check ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
79 c f a fl r p)
80 |#
81 (let* ((prod (inner-product a fl add multiply p-zero))
82 (succeeded-p (universal-zerop (subtract (multiply f c) (add prod r p)))))
83 (unless succeeded-p
84 (error "#### Polynomial division Loop invariant failed ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
85 c f a fl r p))
86 succeeded-p))
87
88
89(defun poly-pseudo-divide (f fl)
90 "Pseudo-divide a polynomial F by the list of polynomials FL. Return
91multiple values. The first value is a list of quotients A. The second
92value is the remainder R. The third argument is a scalar coefficient
93C, such that C*F can be divided by FL within the ring of coefficients,
94which is not necessarily a field. Finally, the fourth value is an
95integer count of the number of reductions performed. The resulting
96objects satisfy the equation: C*F= sum A[i]*FL[i] + R. The sugar of
97the quotients is initialized to default."
98 (declare (type poly f) (list fl))
99 ;; Loop invariant: c*f=sum ai*fi+r+p, where p must eventually become 0
100 (do ((r (make-zero-for f))
101 (c 1)
102 (a (make-list (length fl) :initial-element (make-zero-for f)))
103 (division-count 0)
104 (p f))
105 ((universal-zerop p)
106 #+grobner-check(check-loop-invariant c f a fl r p)
107 (debug-cgb "~&~3T~d reduction~:p" division-count)
108 (when (universal-zerop r) (debug-cgb " ---> 0"))
109 ;; We obtained the terms in reverse order, so must fix that
110 (setf a (mapcar #'poly-reverse a)
111 r (poly-reverse r))
112 ;; Initialize the sugar of the quotients
113 ;; (mapc #'poly-reset-sugar a) ;; TODO: Sugar is currently unimplemented
114 (values a r c division-count))
115 (declare (fixnum division-count))
116 ;; Check the loop invariant here
117 #+grobner-check(check-loop-invariant c f a fl r p)
118 (do ((fl fl (rest fl)) ;scan list of divisors
119 (b a (rest b)))
120 ((cond
121 ((endp fl) ;no division occurred
122 ;; TODO: 3 lines below implement sugar strategy (unimplemented in this version)
123 ;;(push (leading-term p) (poly-termlist r)) ;move lt(p) to remainder
124 ;;(setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
125 ;;(pop (poly-termlist p)) ;remove lt(p) from p
126 t)
127 ((divides-p (leading-monomial (car fl)) (leading-monomial p)) ;division occurred
128 (incf division-count)
129 (multiple-value-bind (gcd c1 c2)
130 (universal-ezgcd (leading-coefficient (car fl)) (leading-coefficient p))
131 (declare (ignore gcd))
132 (let ((m (divide (leading-monomial p) (leading-monomial (car fl)))))
133 ;; Multiply the equation c*f=sum ai*fi+r+p by c1.
134 (mapl #'(lambda (x)
135 (setf (car x) (multiply (car x) c1)))
136 a)
137 (setf r (multiply r c1)
138 c (multiply c c1)
139 p (grobner-op c2 c1 m p (car fl)))
140 (push (change-class m 'term :coeff c2)
141 (poly-termlist (car b))))
142 t))))
143 )))
144
145(defun poly-exact-divide (f g)
146 "Divide a polynomial F by another polynomial G. Assume that exact division
147with no remainder is possible. Returns the quotient."
148 (declare (type poly f g))
149 (multiple-value-bind (quot rem coeff division-count)
150 (poly-pseudo-divide f (list g))
151 (declare (ignore division-count coeff)
152 (list quot)
153 (type poly rem)
154 (type fixnum division-count))
155 (unless (universal-zerop rem) (error "Exact division failed."))
156 (car quot)))
157
158;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159;;
160;; An implementation of the normal form
161;;
162;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163
164(defun normal-form-step (fl p r c division-count
165 &aux
166 (g (find (leading-monomial p) fl
167 :test #'divisible-by-p
168 :key #'leading-monomial)))
169 (cond
170 (g ;division possible
171 (incf division-count)
172 (multiple-value-bind (gcd cg cp)
173 (universal-ezgcd (leading-coefficient g) (leading-coefficient p))
174 (declare (ignore gcd))
175 (let ((m (divide (leading-monomial p) (leading-monomial g))))
176 ;; Multiply the equation c*f=sum ai*fi+r+p by cg.
177 (setf r (multiply r cg)
178 c (multiply c cg)
179 ;; p := cg*p-cp*m*g
180 p (grobner-op cp cg m p g))))
181 (debug-cgb "/"))
182 (t ;no division possible
183 (push (leading-term p) (poly-termlist r)) ;move lt(p) to remainder
184 ;;(setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
185 (pop (poly-termlist p)) ;remove lt(p) from p
186 (debug-cgb "+")))
187 (values p r c division-count))
188
189;;
190;; Merge NORMAL-FORM someday with POLY-PSEUDO-DIVIDE.
191;;
192;; TODO: It is hard to test normal form as there is no loop invariant,
193;; like for POLY-PSEUDO-DIVIDE. Is there a testing strategy? One
194;; method would be to test NORMAL-FORM using POLY-PSEUDO-DIVIDE.
195;;
196(defun normal-form (f fl
197 &optional
198 (top-reduction-only $poly_top_reduction_only))
199 #+grobner-check(when (null fl) (warn "normal-form: empty divisor list."))
200 (do ((r (make-zero-for f))
201 (c 1)
202 (division-count 0))
203 ((or (universal-zerop f)
204 ;;(endp fl)
205 (and top-reduction-only (not (universal-zerop r))))
206 (progn
207 (debug-cgb "~&~3T~D reduction~:P" division-count)
208 (when (universal-zerop r)
209 (debug-cgb " ---> 0")))
210 (setf (poly-termlist f) (nreconc (poly-termlist r) (poly-termlist f)))
211 (values f c division-count))
212 (declare (fixnum division-count)
213 (type poly r))
214 (multiple-value-setq (f r c division-count)
215 (normal-form-step fl f r c division-count))))
216
217(defun spoly (f g)
218 "It yields the S-polynomial of polynomials F and G."
219 (declare (type poly f g))
220 (let* ((lcm (universal-lcm (leading-monomial f) (leading-monomial g)))
221 (mf (divide lcm (leading-monomial f)))
222 (mg (divide lcm (leading-monomial g))))
223 (declare (type monom mf mg))
224 (multiple-value-bind (c cf cg)
225 (universal-ezgcd (leading-coefficient f) (leading-coefficient g))
226 (declare (ignore c))
227 (subtract
228 (multiply (multiply mf f) cg)
229 (multiply (multiply mg g) cf)))))
230
231(defun buchberger-criterion (g)
232 "Returns T if G is a Grobner basis, by using the Buchberger
233criterion: for every two polynomials h1 and h2 in G the S-polynomial
234S(h1,h2) reduces to 0 modulo G."
235 (every #'universal-zerop
236 (makelist (normal-form (spoly (elt g i) (elt g j)) g nil)
237 (i 0 (- (length g) 2))
238 (j (1+ i) (1- (length g))))))
239
240
241(defun poly-normalize (p &aux (c (leading-coefficient p)))
242 "Divide a polynomial by its leading coefficient. It assumes
243that the division is possible, which may not always be the
244case in rings which are not fields. The exact division operator
245is assumed to be provided by the RING structure."
246 (mapc #'(lambda (term)
247 (setf (term-coeff term) (divide (term-coeff term) c)))
248 (poly-termlist p))
249 p)
250
251(defun poly-normalize-list (plist)
252 "Divide every polynomial in a list PLIST by its leading coefficient. "
253 (mapcar #'(lambda (x) (poly-normalize x)) plist))
254
255;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256;;
257;; The function GROBNER-TEST is provided primarily for debugging purposes. To
258;; enable verification of grobner bases with BUCHBERGER-CRITERION, do
259;; (pushnew :grobner-check *features*) and compile/load this file.
260;; With this feature, the calculations will slow down CONSIDERABLY.
261;;
262;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263
264(defun grobner-test (g f)
265 "Test whether G is a Grobner basis and F is contained in G. Return T
266upon success and NIL otherwise."
267 (debug-cgb "~&GROBNER CHECK: ")
268 (let (($poly_grobner_debug nil)
269 (stat1 (buchberger-criterion g))
270 (stat2
271 (every #'universal-zerop
272 (makelist (normal-form (copy-instance (elt f i)) g nil)
273 (i 0 (1- (length f)))))))
274 (unless stat1 (error "~&Buchberger criterion failed, not a grobner basis: ~A" g))
275 (unless stat2
276 (error "~&Original polynomials not in ideal spanned by Grobner basis: ~A" f)))
277 (debug-cgb "~&GROBNER CHECK END")
278 t)
Note: See TracBrowser for help on using the repository browser.