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