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

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

* empty log message *

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