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

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