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

Last change on this file since 1403 was 1403, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 11.0 KB
RevLine 
[1199]1;;; -*- Mode: Lisp -*-
[148]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
[459]22(defpackage "DIVISION"
[1177]23 (:use :cl :utils :ring :monomial :polynomial :grobner-debug :term :ring-and-order)
[470]24 (:export "$POLY_TOP_REDUCTION_ONLY"
25 "POLY-PSEUDO-DIVIDE"
[459]26 "POLY-EXACT-DIVIDE"
[491]27 "NORMAL-FORM-STEP"
[459]28 "NORMAL-FORM"
29 "POLY-NORMALIZE"
[472]30 "POLY-NORMALIZE-LIST"
[473]31 "BUCHBERGER-CRITERION"
[1299]32 "GROBNER-TEST"
[459]33 ))
[148]34
[460]35(in-package :division)
36
[469]37(defvar $poly_top_reduction_only nil
38 "If not FALSE, use top reduction only whenever possible.
39Top reduction means that division algorithm stops after the first reduction.")
40
[59]41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42;;
43;; An implementation of the division algorithm
44;;
45;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
[1176]47(defun grobner-op (ring-and-order c1 c2 m f g
48 &aux
49 (ring (ro-ring ring-and-order)))
[59]50 "Returns C2*F-C1*M*G, where F and G are polynomials M is a monomial.
51Assume that the leading terms will cancel."
[1178]52 (declare (type ring-and-order ring-and-order))
[59]53 #+grobner-check(funcall (ring-zerop ring)
54 (funcall (ring-sub ring)
55 (funcall (ring-mul ring) c2 (poly-lc f))
56 (funcall (ring-mul ring) c1 (poly-lc g))))
57 #+grobner-check(monom-equal-p (poly-lm f) (monom-mul m (poly-lm g)))
[1205]58 ;; Note that below we can drop the leading terms of f ang g for the
[1206]59 ;; purpose of polynomial arithmetic.
60 ;;
[1212]61 ;; TODO: Make sure that the sugar calculation is correct if leading
62 ;; terms are dropped.
[1176]63 (poly-sub ring-and-order
[1263]64 (scalar-times-poly-1 ring c2 f)
65 (scalar-times-poly-1 ring c1 (monom-times-poly m g))))
[59]66
[1242]67(defun check-loop-invariant (ring-and-order c f a fl r p
[1237]68 &aux
69 (ring (ro-ring ring-and-order))
[1264]70 (p-zero (make-poly-zero))
71 (a (mapcar #'poly-reverse a))
72 (r (poly-reverse r)))
[1238]73 "Check loop invariant of division algorithms, when we divide a
74polynomial F by the list of polynomials FL. The invariant is the
[1242]75identity C*F=SUM AI*FI+R+P, where F0 is the initial value of F, A is
[1238]76the list of partial quotients, R is the intermediate value of the
[1242]77remainder, and P is the intermediate value which eventually becomes
[1269]780. A thing to remember is that the terms of polynomials in A and
79the polynomial R have their terms in reversed order. Hence, before
80the arithmetic is performed, we need to fix the order of terms"
[1276]81 (format t "----------------------------------------------------------------~%")
[1279]82 (format t "#### Loop invariant check ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
[1275]83 c f a fl r p)
[1242]84 (flet ((p-add (x y) (poly-add ring-and-order x y))
85 (p-sub (x y) (poly-sub ring-and-order x y))
86 (p-mul (x y) (poly-mul ring-and-order x y)))
[1257]87 (let* ((prod (inner-product a fl p-add p-mul p-zero))
88 (succeeded-p
89 (poly-zerop
90 (p-sub
91 (scalar-times-poly ring c f)
92 (reduce #'p-add (list prod r p))))))
[1270]93 (unless succeeded-p
[1280]94 (error "#### Polynomial division Loop invariant failed ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
[1271]95 c f a fl r p))
[1257]96 succeeded-p)))
[1237]97
98
[1179]99(defun poly-pseudo-divide (ring-and-order f fl
100 &aux
101 (ring (ro-ring ring-and-order)))
[59]102 "Pseudo-divide a polynomial F by the list of polynomials FL. Return
103multiple values. The first value is a list of quotients A. The second
104value is the remainder R. The third argument is a scalar coefficient
105C, such that C*F can be divided by FL within the ring of coefficients,
106which is not necessarily a field. Finally, the fourth value is an
107integer count of the number of reductions performed. The resulting
[1220]108objects satisfy the equation: C*F= sum A[i]*FL[i] + R. The sugar of
[1221]109the quotients is initialized to default."
[59]110 (declare (type poly f) (list fl))
[1241]111 ;; Loop invariant: c*f=sum ai*fi+r+p, where p must eventually become 0
[59]112 (do ((r (make-poly-zero))
113 (c (funcall (ring-unit ring)))
114 (a (make-list (length fl) :initial-element (make-poly-zero)))
115 (division-count 0)
116 (p f))
117 ((poly-zerop p)
[1278]118 #+grobner-check(check-loop-invariant ring-and-order c f a fl r p)
[59]119 (debug-cgb "~&~3T~d reduction~:p" division-count)
120 (when (poly-zerop r) (debug-cgb " ---> 0"))
[1211]121 ;; We obtained the terms in reverse order, so must fix that
[1210]122 (setf a (mapcar #'poly-nreverse a)
123 r (poly-nreverse r))
[1219]124 ;; Initialize the sugar of the quotients
125 (mapc #'poly-reset-sugar a)
[1210]126 (values a r c division-count))
[59]127 (declare (fixnum division-count))
[1252]128 ;; Check the loop invariant here
[1277]129 #+grobner-check(check-loop-invariant ring-and-order c f a fl r p)
[1207]130 (do ((fl fl (rest fl)) ;scan list of divisors
[59]131 (b a (rest b)))
132 ((cond
[1207]133 ((endp fl) ;no division occurred
134 (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
135 (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
136 (pop (poly-termlist p)) ;remove lt(p) from p
137 t)
138 ((monom-divides-p (poly-lm (car fl)) (poly-lm p)) ;division occurred
139 (incf division-count)
140 (multiple-value-bind (gcd c1 c2)
141 (funcall (ring-ezgcd ring) (poly-lc (car fl)) (poly-lc p))
142 (declare (ignore gcd))
143 (let ((m (monom-div (poly-lm p) (poly-lm (car fl)))))
144 ;; Multiply the equation c*f=sum ai*fi+r+p by c1.
145 (mapl #'(lambda (x)
146 (setf (car x) (scalar-times-poly ring c1 (car x))))
147 a)
148 (setf r (scalar-times-poly ring c1 r)
149 c (funcall (ring-mul ring) c c1)
150 p (grobner-op ring-and-order c2 c1 m p (car fl)))
151 (push (make-term m c2) (poly-termlist (car b))))
[1248]152 t))))
153 )))
[59]154
[1284]155(defun poly-exact-divide (ring-and-order f g)
[59]156 "Divide a polynomial F by another polynomial G. Assume that exact division
157with no remainder is possible. Returns the quotient."
[1284]158 (declare (type poly f g) (type ring-and-order ring-and-order))
[59]159 (multiple-value-bind (quot rem coeff division-count)
[1284]160 (poly-pseudo-divide ring-and-order f (list g))
[59]161 (declare (ignore division-count coeff)
162 (list quot)
163 (type poly rem)
164 (type fixnum division-count))
165 (unless (poly-zerop rem) (error "Exact division failed."))
166 (car quot)))
167
168;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169;;
170;; An implementation of the normal form
171;;
172;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
173
[1180]174(defun normal-form-step (ring-and-order fl p r c division-count
175 &aux
176 (ring (ro-ring ring-and-order))
177 (g (find (poly-lm p) fl
178 :test #'monom-divisible-by-p
179 :key #'poly-lm)))
[59]180 (cond
181 (g ;division possible
182 (incf division-count)
183 (multiple-value-bind (gcd cg cp)
184 (funcall (ring-ezgcd ring) (poly-lc g) (poly-lc p))
185 (declare (ignore gcd))
186 (let ((m (monom-div (poly-lm p) (poly-lm g))))
187 ;; Multiply the equation c*f=sum ai*fi+r+p by cg.
188 (setf r (scalar-times-poly ring cg r)
189 c (funcall (ring-mul ring) c cg)
190 ;; p := cg*p-cp*m*g
[1181]191 p (grobner-op ring-and-order cp cg m p g))))
[59]192 (debug-cgb "/"))
193 (t ;no division possible
194 (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
195 (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
196 (pop (poly-termlist p)) ;remove lt(p) from p
197 (debug-cgb "+")))
198 (values p r c division-count))
199
200;; Merge it sometime with poly-pseudo-divide
[1182]201(defun normal-form (ring-and-order f fl
202 &optional
203 (top-reduction-only $poly_top_reduction_only)
204 (ring (ro-ring ring-and-order)))
[59]205 #+grobner-check(when (null fl) (warn "normal-form: empty divisor list."))
206 (do ((r (make-poly-zero))
207 (c (funcall (ring-unit ring)))
[1254]208 (division-count 0))
[59]209 ((or (poly-zerop f)
210 ;;(endp fl)
211 (and top-reduction-only (not (poly-zerop r))))
212 (progn
[1239]213 (debug-cgb "~&~3T~D reduction~:P" division-count)
[59]214 (when (poly-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)
[1182]221 (normal-form-step ring-and-order fl f r c division-count))))
[59]222
[1187]223(defun buchberger-criterion (ring-and-order g)
[59]224 "Returns T if G is a Grobner basis, by using the Buchberger
225criterion: for every two polynomials h1 and h2 in G the S-polynomial
226S(h1,h2) reduces to 0 modulo G."
[1222]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))))))
[59]231
[64]232
233(defun poly-normalize (ring p &aux (c (poly-lc p)))
234 "Divide a polynomial by its leading coefficient. It assumes
235that the division is possible, which may not always be the
236case in rings which are not fields. The exact division operator
[1197]237is assumed to be provided by the RING structure."
[64]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))
[1297]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
[1298]256(defun grobner-test (ring-and-order g f)
[1297]257 "Test whether G is a Grobner basis and F is contained in G. Return T
258upon success and NIL otherwise."
259 (debug-cgb "~&GROBNER CHECK: ")
260 (let (($poly_grobner_debug nil)
[1298]261 (stat1 (buchberger-criterion ring-and-order g))
[1297]262 (stat2
263 (every #'poly-zerop
[1298]264 (makelist (normal-form ring-and-order (copy-tree (elt f i)) g nil)
[1297]265 (i 0 (1- (length f)))))))
266 (unless stat1 (error "~&Buchberger criterion failed."))
267 (unless stat2
268 (error "~&Original polys not in ideal spanned by Grobner.")))
269 (debug-cgb "~&GROBNER CHECK END")
270 t)
Note: See TracBrowser for help on using the repository browser.