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