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