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

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

* empty log message *

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