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