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@ 461

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

* empty log message *

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