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