[148] | 1 | ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*-
|
---|
| 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"
|
---|
[471] | 23 | (:use :cl :utils :ring :monomial :polynomial :grobner-debug :term)
|
---|
[470] | 24 | (:export "$POLY_TOP_REDUCTION_ONLY"
|
---|
| 25 | "POLY-PSEUDO-DIVIDE"
|
---|
[459] | 26 | "POLY-EXACT-DIVIDE"
|
---|
| 27 | "NORMAL-FORM"
|
---|
| 28 | "POLY-NORMALIZE"
|
---|
[472] | 29 | "POLY-NORMALIZE-LIST"
|
---|
[473] | 30 | "BUCHBERGER-CRITERION"
|
---|
[459] | 31 | ))
|
---|
[148] | 32 |
|
---|
[460] | 33 | (in-package :division)
|
---|
| 34 |
|
---|
[469] | 35 | (defvar $poly_top_reduction_only nil
|
---|
| 36 | "If not FALSE, use top reduction only whenever possible.
|
---|
| 37 | Top reduction means that division algorithm stops after the first reduction.")
|
---|
| 38 |
|
---|
[59] | 39 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 40 | ;;
|
---|
| 41 | ;; An implementation of the division algorithm
|
---|
| 42 | ;;
|
---|
| 43 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 44 |
|
---|
| 45 | (defun grobner-op (ring c1 c2 m f g)
|
---|
| 46 | "Returns C2*F-C1*M*G, where F and G are polynomials M is a monomial.
|
---|
| 47 | Assume that the leading terms will cancel."
|
---|
| 48 | #+grobner-check(funcall (ring-zerop ring)
|
---|
| 49 | (funcall (ring-sub ring)
|
---|
| 50 | (funcall (ring-mul ring) c2 (poly-lc f))
|
---|
| 51 | (funcall (ring-mul ring) c1 (poly-lc g))))
|
---|
| 52 | #+grobner-check(monom-equal-p (poly-lm f) (monom-mul m (poly-lm g)))
|
---|
| 53 | ;; Note that we can drop the leading terms of f ang g
|
---|
| 54 | (poly-sub ring
|
---|
| 55 | (scalar-times-poly-1 ring c2 f)
|
---|
| 56 | (scalar-times-poly-1 ring c1 (monom-times-poly m g))))
|
---|
| 57 |
|
---|
| 58 | (defun poly-pseudo-divide (ring f fl)
|
---|
| 59 | "Pseudo-divide a polynomial F by the list of polynomials FL. Return
|
---|
| 60 | multiple values. The first value is a list of quotients A. The second
|
---|
| 61 | value is the remainder R. The third argument is a scalar coefficient
|
---|
| 62 | C, such that C*F can be divided by FL within the ring of coefficients,
|
---|
| 63 | which is not necessarily a field. Finally, the fourth value is an
|
---|
| 64 | integer count of the number of reductions performed. The resulting
|
---|
| 65 | objects satisfy the equation: C*F= sum A[i]*FL[i] + R."
|
---|
| 66 | (declare (type poly f) (list fl))
|
---|
| 67 | (do ((r (make-poly-zero))
|
---|
| 68 | (c (funcall (ring-unit ring)))
|
---|
| 69 | (a (make-list (length fl) :initial-element (make-poly-zero)))
|
---|
| 70 | (division-count 0)
|
---|
| 71 | (p f))
|
---|
| 72 | ((poly-zerop p)
|
---|
| 73 | (debug-cgb "~&~3T~d reduction~:p" division-count)
|
---|
| 74 | (when (poly-zerop r) (debug-cgb " ---> 0"))
|
---|
| 75 | (values (mapcar #'poly-nreverse a) (poly-nreverse r) c division-count))
|
---|
| 76 | (declare (fixnum division-count))
|
---|
| 77 | (do ((fl fl (rest fl)) ;scan list of divisors
|
---|
| 78 | (b a (rest b)))
|
---|
| 79 | ((cond
|
---|
| 80 | ((endp fl) ;no division occurred
|
---|
| 81 | (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
|
---|
| 82 | (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
|
---|
| 83 | (pop (poly-termlist p)) ;remove lt(p) from p
|
---|
| 84 | t)
|
---|
| 85 | ((monom-divides-p (poly-lm (car fl)) (poly-lm p)) ;division occurred
|
---|
| 86 | (incf division-count)
|
---|
| 87 | (multiple-value-bind (gcd c1 c2)
|
---|
| 88 | (funcall (ring-ezgcd ring) (poly-lc (car fl)) (poly-lc p))
|
---|
| 89 | (declare (ignore gcd))
|
---|
| 90 | (let ((m (monom-div (poly-lm p) (poly-lm (car fl)))))
|
---|
| 91 | ;; Multiply the equation c*f=sum ai*fi+r+p by c1.
|
---|
| 92 | (mapl #'(lambda (x)
|
---|
| 93 | (setf (car x) (scalar-times-poly ring c1 (car x))))
|
---|
| 94 | a)
|
---|
| 95 | (setf r (scalar-times-poly ring c1 r)
|
---|
| 96 | c (funcall (ring-mul ring) c c1)
|
---|
| 97 | p (grobner-op ring c2 c1 m p (car fl)))
|
---|
| 98 | (push (make-term m c2) (poly-termlist (car b))))
|
---|
| 99 | t)))))))
|
---|
| 100 |
|
---|
| 101 | (defun poly-exact-divide (ring f g)
|
---|
| 102 | "Divide a polynomial F by another polynomial G. Assume that exact division
|
---|
| 103 | with no remainder is possible. Returns the quotient."
|
---|
| 104 | (declare (type poly f g))
|
---|
| 105 | (multiple-value-bind (quot rem coeff division-count)
|
---|
| 106 | (poly-pseudo-divide ring f (list g))
|
---|
| 107 | (declare (ignore division-count coeff)
|
---|
| 108 | (list quot)
|
---|
| 109 | (type poly rem)
|
---|
| 110 | (type fixnum division-count))
|
---|
| 111 | (unless (poly-zerop rem) (error "Exact division failed."))
|
---|
| 112 | (car quot)))
|
---|
| 113 |
|
---|
| 114 | |
---|
| 115 |
|
---|
| 116 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 117 | ;;
|
---|
| 118 | ;; An implementation of the normal form
|
---|
| 119 | ;;
|
---|
| 120 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 121 |
|
---|
| 122 | (defun normal-form-step (ring fl p r c division-count
|
---|
| 123 | &aux (g (find (poly-lm p) fl
|
---|
| 124 | :test #'monom-divisible-by-p
|
---|
| 125 | :key #'poly-lm)))
|
---|
| 126 | (cond
|
---|
| 127 | (g ;division possible
|
---|
| 128 | (incf division-count)
|
---|
| 129 | (multiple-value-bind (gcd cg cp)
|
---|
| 130 | (funcall (ring-ezgcd ring) (poly-lc g) (poly-lc p))
|
---|
| 131 | (declare (ignore gcd))
|
---|
| 132 | (let ((m (monom-div (poly-lm p) (poly-lm g))))
|
---|
| 133 | ;; Multiply the equation c*f=sum ai*fi+r+p by cg.
|
---|
| 134 | (setf r (scalar-times-poly ring cg r)
|
---|
| 135 | c (funcall (ring-mul ring) c cg)
|
---|
| 136 | ;; p := cg*p-cp*m*g
|
---|
| 137 | p (grobner-op ring cp cg m p g))))
|
---|
| 138 | (debug-cgb "/"))
|
---|
| 139 | (t ;no division possible
|
---|
| 140 | (push (poly-lt p) (poly-termlist r)) ;move lt(p) to remainder
|
---|
| 141 | (setf (poly-sugar r) (max (poly-sugar r) (term-sugar (poly-lt p))))
|
---|
| 142 | (pop (poly-termlist p)) ;remove lt(p) from p
|
---|
| 143 | (debug-cgb "+")))
|
---|
| 144 | (values p r c division-count))
|
---|
| 145 |
|
---|
| 146 | ;; Merge it sometime with poly-pseudo-divide
|
---|
| 147 | (defun normal-form (ring f fl &optional (top-reduction-only $poly_top_reduction_only))
|
---|
| 148 | ;; Loop invariant: c*f0=sum ai*fi+r+f, where f0 is the initial value of f
|
---|
| 149 | #+grobner-check(when (null fl) (warn "normal-form: empty divisor list."))
|
---|
| 150 | (do ((r (make-poly-zero))
|
---|
| 151 | (c (funcall (ring-unit ring)))
|
---|
| 152 | (division-count 0))
|
---|
| 153 | ((or (poly-zerop f)
|
---|
| 154 | ;;(endp fl)
|
---|
| 155 | (and top-reduction-only (not (poly-zerop r))))
|
---|
| 156 | (progn
|
---|
| 157 | (debug-cgb "~&~3T~d reduction~:p" division-count)
|
---|
| 158 | (when (poly-zerop r)
|
---|
| 159 | (debug-cgb " ---> 0")))
|
---|
| 160 | (setf (poly-termlist f) (nreconc (poly-termlist r) (poly-termlist f)))
|
---|
| 161 | (values f c division-count))
|
---|
| 162 | (declare (fixnum division-count)
|
---|
| 163 | (type poly r))
|
---|
| 164 | (multiple-value-setq (f r c division-count)
|
---|
| 165 | (normal-form-step ring fl f r c division-count))))
|
---|
| 166 |
|
---|
| 167 | (defun buchberger-criterion (ring g)
|
---|
| 168 | "Returns T if G is a Grobner basis, by using the Buchberger
|
---|
| 169 | criterion: for every two polynomials h1 and h2 in G the S-polynomial
|
---|
| 170 | S(h1,h2) reduces to 0 modulo G."
|
---|
| 171 | (every
|
---|
| 172 | #'poly-zerop
|
---|
| 173 | (makelist (normal-form ring (spoly ring (elt g i) (elt g j)) g nil)
|
---|
| 174 | (i 0 (- (length g) 2))
|
---|
| 175 | (j (1+ i) (1- (length g))))))
|
---|
[64] | 176 |
|
---|
| 177 |
|
---|
| 178 | (defun poly-normalize (ring p &aux (c (poly-lc p)))
|
---|
| 179 | "Divide a polynomial by its leading coefficient. It assumes
|
---|
| 180 | that the division is possible, which may not always be the
|
---|
| 181 | case in rings which are not fields. The exact division operator
|
---|
| 182 | is assumed to be provided by the RING structure of the
|
---|
| 183 | COEFFICIENT-RING package."
|
---|
| 184 | (mapc #'(lambda (term)
|
---|
| 185 | (setf (term-coeff term) (funcall (ring-div ring) (term-coeff term) c)))
|
---|
| 186 | (poly-termlist p))
|
---|
| 187 | p)
|
---|
| 188 |
|
---|
| 189 | (defun poly-normalize-list (ring plist)
|
---|
| 190 | "Divide every polynomial in a list PLIST by its leading coefficient. "
|
---|
| 191 | (mapcar #'(lambda (x) (poly-normalize ring x)) plist))
|
---|