close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/division.lisp@ 470

Last change on this file since 470 was 470, checked in by Marek Rychlik, 9 years ago

* empty log message *

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