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