[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"
|
---|
[459] | 32 | ))
|
---|
[148] | 33 |
|
---|
[460] | 34 | (in-package :division)
|
---|
| 35 |
|
---|
[469] | 36 | (defvar $poly_top_reduction_only nil
|
---|
| 37 | "If not FALSE, use top reduction only whenever possible.
|
---|
| 38 | Top reduction means that division algorithm stops after the first reduction.")
|
---|
| 39 |
|
---|
[59] | 40 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 41 | ;;
|
---|
| 42 | ;; An implementation of the division algorithm
|
---|
| 43 | ;;
|
---|
| 44 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 45 |
|
---|
[1176] | 46 | (defun grobner-op (ring-and-order c1 c2 m f g
|
---|
| 47 | &aux
|
---|
| 48 | (ring (ro-ring ring-and-order)))
|
---|
[59] | 49 | "Returns C2*F-C1*M*G, where F and G are polynomials M is a monomial.
|
---|
| 50 | Assume that the leading terms will cancel."
|
---|
[1178] | 51 | (declare (type ring-and-order ring-and-order))
|
---|
[59] | 52 | #+grobner-check(funcall (ring-zerop ring)
|
---|
| 53 | (funcall (ring-sub ring)
|
---|
| 54 | (funcall (ring-mul ring) c2 (poly-lc f))
|
---|
| 55 | (funcall (ring-mul ring) c1 (poly-lc g))))
|
---|
| 56 | #+grobner-check(monom-equal-p (poly-lm f) (monom-mul m (poly-lm 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.
|
---|
[1176] | 62 | (poly-sub ring-and-order
|
---|
[1263] | 63 | (scalar-times-poly-1 ring c2 f)
|
---|
| 64 | (scalar-times-poly-1 ring c1 (monom-times-poly m g))))
|
---|
[59] | 65 |
|
---|
[1242] | 66 | (defun check-loop-invariant (ring-and-order c f a fl r p
|
---|
[1237] | 67 | &aux
|
---|
| 68 | (ring (ro-ring ring-and-order))
|
---|
[1264] | 69 | (p-zero (make-poly-zero))
|
---|
| 70 | (a (mapcar #'poly-reverse a))
|
---|
| 71 | (r (poly-reverse r)))
|
---|
[1238] | 72 | "Check loop invariant of division algorithms, when we divide a
|
---|
| 73 | polynomial F by the list of polynomials FL. The invariant is the
|
---|
[1242] | 74 | identity C*F=SUM AI*FI+R+P, where F0 is the initial value of F, A is
|
---|
[1238] | 75 | the list of partial quotients, R is the intermediate value of the
|
---|
[1242] | 76 | remainder, and P is the intermediate value which eventually becomes
|
---|
[1269] | 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"
|
---|
[1276] | 80 | (format t "----------------------------------------------------------------~%")
|
---|
[1279] | 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)
|
---|
[1242] | 83 | (flet ((p-add (x y) (poly-add ring-and-order x y))
|
---|
| 84 | (p-sub (x y) (poly-sub ring-and-order x y))
|
---|
| 85 | (p-mul (x y) (poly-mul ring-and-order x y)))
|
---|
[1257] | 86 | (let* ((prod (inner-product a fl p-add p-mul p-zero))
|
---|
| 87 | (succeeded-p
|
---|
| 88 | (poly-zerop
|
---|
| 89 | (p-sub
|
---|
| 90 | (scalar-times-poly ring c f)
|
---|
| 91 | (reduce #'p-add (list prod r p))))))
|
---|
[1270] | 92 | (unless succeeded-p
|
---|
[1280] | 93 | (error "#### Polynomial division Loop invariant failed ####:~%C=~A~%F=~A~%A=~A~%FL=~A~%R=~A~%P=~A~%"
|
---|
[1271] | 94 | c f a fl r p))
|
---|
[1257] | 95 | succeeded-p)))
|
---|
[1237] | 96 |
|
---|
| 97 |
|
---|
[1179] | 98 | (defun poly-pseudo-divide (ring-and-order f fl
|
---|
| 99 | &aux
|
---|
| 100 | (ring (ro-ring ring-and-order)))
|
---|
[59] | 101 | "Pseudo-divide a polynomial F by the list of polynomials FL. Return
|
---|
| 102 | multiple values. The first value is a list of quotients A. The second
|
---|
| 103 | value is the remainder R. The third argument is a scalar coefficient
|
---|
| 104 | C, such that C*F can be divided by FL within the ring of coefficients,
|
---|
| 105 | which is not necessarily a field. Finally, the fourth value is an
|
---|
| 106 | integer count of the number of reductions performed. The resulting
|
---|
[1220] | 107 | objects satisfy the equation: C*F= sum A[i]*FL[i] + R. The sugar of
|
---|
[1221] | 108 | the quotients is initialized to default."
|
---|
[59] | 109 | (declare (type poly f) (list fl))
|
---|
[1241] | 110 | ;; Loop invariant: c*f=sum ai*fi+r+p, where p must eventually become 0
|
---|
[59] | 111 | (do ((r (make-poly-zero))
|
---|
| 112 | (c (funcall (ring-unit ring)))
|
---|
| 113 | (a (make-list (length fl) :initial-element (make-poly-zero)))
|
---|
| 114 | (division-count 0)
|
---|
| 115 | (p f))
|
---|
| 116 | ((poly-zerop p)
|
---|
[1278] | 117 | #+grobner-check(check-loop-invariant ring-and-order c f a fl r p)
|
---|
[59] | 118 | (debug-cgb "~&~3T~d reduction~:p" division-count)
|
---|
| 119 | (when (poly-zerop r) (debug-cgb " ---> 0"))
|
---|
[1211] | 120 | ;; We obtained the terms in reverse order, so must fix that
|
---|
[1210] | 121 | (setf a (mapcar #'poly-nreverse a)
|
---|
| 122 | r (poly-nreverse r))
|
---|
[1219] | 123 | ;; Initialize the sugar of the quotients
|
---|
| 124 | (mapc #'poly-reset-sugar a)
|
---|
[1210] | 125 | (values a r c division-count))
|
---|
[59] | 126 | (declare (fixnum division-count))
|
---|
[1252] | 127 | ;; Check the loop invariant here
|
---|
[1277] | 128 | #+grobner-check(check-loop-invariant ring-and-order c f a fl r p)
|
---|
[1207] | 129 | (do ((fl fl (rest fl)) ;scan list of divisors
|
---|
[59] | 130 | (b a (rest b)))
|
---|
| 131 | ((cond
|
---|
[1207] | 132 | ((endp fl) ;no division occurred
|
---|
| 133 | (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
|
---|
| 134 | (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
|
---|
| 135 | (pop (poly-termlist p)) ;remove lt(p) from p
|
---|
| 136 | t)
|
---|
| 137 | ((monom-divides-p (poly-lm (car fl)) (poly-lm p)) ;division occurred
|
---|
| 138 | (incf division-count)
|
---|
| 139 | (multiple-value-bind (gcd c1 c2)
|
---|
| 140 | (funcall (ring-ezgcd ring) (poly-lc (car fl)) (poly-lc p))
|
---|
| 141 | (declare (ignore gcd))
|
---|
| 142 | (let ((m (monom-div (poly-lm p) (poly-lm (car fl)))))
|
---|
| 143 | ;; Multiply the equation c*f=sum ai*fi+r+p by c1.
|
---|
| 144 | (mapl #'(lambda (x)
|
---|
| 145 | (setf (car x) (scalar-times-poly ring c1 (car x))))
|
---|
| 146 | a)
|
---|
| 147 | (setf r (scalar-times-poly ring c1 r)
|
---|
| 148 | c (funcall (ring-mul ring) c c1)
|
---|
| 149 | p (grobner-op ring-and-order c2 c1 m p (car fl)))
|
---|
| 150 | (push (make-term m c2) (poly-termlist (car b))))
|
---|
[1248] | 151 | t))))
|
---|
| 152 | )))
|
---|
[59] | 153 |
|
---|
[1284] | 154 | (defun poly-exact-divide (ring-and-order f g)
|
---|
[59] | 155 | "Divide a polynomial F by another polynomial G. Assume that exact division
|
---|
| 156 | with no remainder is possible. Returns the quotient."
|
---|
[1284] | 157 | (declare (type poly f g) (type ring-and-order ring-and-order))
|
---|
[59] | 158 | (multiple-value-bind (quot rem coeff division-count)
|
---|
[1284] | 159 | (poly-pseudo-divide ring-and-order f (list g))
|
---|
[59] | 160 | (declare (ignore division-count coeff)
|
---|
| 161 | (list quot)
|
---|
| 162 | (type poly rem)
|
---|
| 163 | (type fixnum division-count))
|
---|
| 164 | (unless (poly-zerop rem) (error "Exact division failed."))
|
---|
| 165 | (car quot)))
|
---|
| 166 |
|
---|
| 167 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 168 | ;;
|
---|
| 169 | ;; An implementation of the normal form
|
---|
| 170 | ;;
|
---|
| 171 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 172 |
|
---|
[1180] | 173 | (defun normal-form-step (ring-and-order fl p r c division-count
|
---|
| 174 | &aux
|
---|
| 175 | (ring (ro-ring ring-and-order))
|
---|
| 176 | (g (find (poly-lm p) fl
|
---|
| 177 | :test #'monom-divisible-by-p
|
---|
| 178 | :key #'poly-lm)))
|
---|
[59] | 179 | (cond
|
---|
| 180 | (g ;division possible
|
---|
| 181 | (incf division-count)
|
---|
| 182 | (multiple-value-bind (gcd cg cp)
|
---|
| 183 | (funcall (ring-ezgcd ring) (poly-lc g) (poly-lc p))
|
---|
| 184 | (declare (ignore gcd))
|
---|
| 185 | (let ((m (monom-div (poly-lm p) (poly-lm g))))
|
---|
| 186 | ;; Multiply the equation c*f=sum ai*fi+r+p by cg.
|
---|
| 187 | (setf r (scalar-times-poly ring cg r)
|
---|
| 188 | c (funcall (ring-mul ring) c cg)
|
---|
| 189 | ;; p := cg*p-cp*m*g
|
---|
[1181] | 190 | p (grobner-op ring-and-order cp cg m p g))))
|
---|
[59] | 191 | (debug-cgb "/"))
|
---|
| 192 | (t ;no division possible
|
---|
| 193 | (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
|
---|
| 194 | (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
|
---|
| 195 | (pop (poly-termlist p)) ;remove lt(p) from p
|
---|
| 196 | (debug-cgb "+")))
|
---|
| 197 | (values p r c division-count))
|
---|
| 198 |
|
---|
| 199 | ;; Merge it sometime with poly-pseudo-divide
|
---|
[1182] | 200 | (defun normal-form (ring-and-order f fl
|
---|
| 201 | &optional
|
---|
| 202 | (top-reduction-only $poly_top_reduction_only)
|
---|
| 203 | (ring (ro-ring ring-and-order)))
|
---|
[59] | 204 | #+grobner-check(when (null fl) (warn "normal-form: empty divisor list."))
|
---|
| 205 | (do ((r (make-poly-zero))
|
---|
| 206 | (c (funcall (ring-unit ring)))
|
---|
[1254] | 207 | (division-count 0))
|
---|
[59] | 208 | ((or (poly-zerop f)
|
---|
| 209 | ;;(endp fl)
|
---|
| 210 | (and top-reduction-only (not (poly-zerop r))))
|
---|
| 211 | (progn
|
---|
[1239] | 212 | (debug-cgb "~&~3T~D reduction~:P" division-count)
|
---|
[59] | 213 | (when (poly-zerop r)
|
---|
| 214 | (debug-cgb " ---> 0")))
|
---|
| 215 | (setf (poly-termlist f) (nreconc (poly-termlist r) (poly-termlist f)))
|
---|
| 216 | (values f c division-count))
|
---|
| 217 | (declare (fixnum division-count)
|
---|
| 218 | (type poly r))
|
---|
| 219 | (multiple-value-setq (f r c division-count)
|
---|
[1182] | 220 | (normal-form-step ring-and-order fl f r c division-count))))
|
---|
[59] | 221 |
|
---|
[1187] | 222 | (defun buchberger-criterion (ring-and-order g)
|
---|
[59] | 223 | "Returns T if G is a Grobner basis, by using the Buchberger
|
---|
| 224 | criterion: for every two polynomials h1 and h2 in G the S-polynomial
|
---|
| 225 | S(h1,h2) reduces to 0 modulo G."
|
---|
[1222] | 226 | (every #'poly-zerop
|
---|
| 227 | (makelist (normal-form ring-and-order (spoly ring-and-order (elt g i) (elt g j)) g nil)
|
---|
| 228 | (i 0 (- (length g) 2))
|
---|
| 229 | (j (1+ i) (1- (length g))))))
|
---|
[59] | 230 |
|
---|
[64] | 231 |
|
---|
| 232 | (defun poly-normalize (ring p &aux (c (poly-lc p)))
|
---|
| 233 | "Divide a polynomial by its leading coefficient. It assumes
|
---|
| 234 | that the division is possible, which may not always be the
|
---|
| 235 | case in rings which are not fields. The exact division operator
|
---|
[1197] | 236 | is assumed to be provided by the RING structure."
|
---|
[64] | 237 | (mapc #'(lambda (term)
|
---|
| 238 | (setf (term-coeff term) (funcall (ring-div ring) (term-coeff term) c)))
|
---|
| 239 | (poly-termlist p))
|
---|
| 240 | p)
|
---|
| 241 |
|
---|
| 242 | (defun poly-normalize-list (ring plist)
|
---|
| 243 | "Divide every polynomial in a list PLIST by its leading coefficient. "
|
---|
| 244 | (mapcar #'(lambda (x) (poly-normalize ring x)) plist))
|
---|