mirror of https://github.com/CGAL/cgal
new / more robust version .-)
This commit is contained in:
parent
c05f1298fa
commit
7dcb9b1258
|
|
@ -23,46 +23,53 @@
|
||||||
#define CGAL_EXTENDED_EUCLIDEAN_ALGORITHM_H 1
|
#define CGAL_EXTENDED_EUCLIDEAN_ALGORITHM_H 1
|
||||||
|
|
||||||
#include <CGAL/basic.h>
|
#include <CGAL/basic.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
CGAL_BEGIN_NAMESPACE
|
CGAL_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template< class NT >
|
// EEA computing the normalized gcd
|
||||||
NT extended_euclidean_algorithm(const NT& a_, const NT& b_, NT& u, NT& v){
|
// Modern Computer Algebra (Hardcover)
|
||||||
|
// by Joachim von zur Gathen (Author), Jürgen Gerhard (Author)
|
||||||
|
// Publisher: Cambridge University Press; 2 edition (September 1, 2003)
|
||||||
|
// Language: English
|
||||||
|
// ISBN-10: 0521826462
|
||||||
|
// ISBN-13: 978-0521826464
|
||||||
|
// pp.: 55
|
||||||
|
|
||||||
typedef Algebraic_structure_traits<NT> AST;
|
template< class AS >
|
||||||
typename AST::Div_mod div_mod;
|
AS extended_euclidean_algorithm(const AS& f, const AS& g, AS& s_, AS& t_){
|
||||||
typename AST::Unit_part unit_part;
|
typename Algebraic_structure_traits<AS>::Integral_division idiv;
|
||||||
typename AST::Integral_division idiv;
|
typename Algebraic_structure_traits<AS>::Div div;
|
||||||
|
typename Algebraic_structure_traits<AS>::Unit_part unit_part;
|
||||||
|
|
||||||
NT unit_part_a(unit_part(a_));
|
std::vector<AS> p,r,s,t,q;
|
||||||
NT unit_part_b(unit_part(b_));
|
p.push_back(unit_part(f));
|
||||||
|
r.push_back(idiv(f,p[0]));
|
||||||
|
s.push_back(idiv(AS(1),p[0]));
|
||||||
|
t.push_back(AS(0));
|
||||||
|
q.push_back(AS(0));
|
||||||
|
|
||||||
NT a(idiv(a_,unit_part_a));
|
p.push_back(unit_part(g));
|
||||||
NT b(idiv(b_,unit_part_b));
|
r.push_back(idiv(g,p[1]));
|
||||||
|
s.push_back(AS(0));
|
||||||
|
t.push_back(idiv(AS(1),p[1]));
|
||||||
|
|
||||||
NT x(0),y(1),last_x(1),last_y(0);
|
int i = 1;
|
||||||
NT temp, quotient;
|
while(!is_zero(r[i])){
|
||||||
|
q.push_back(div(r[i-1],r[i]));
|
||||||
//TODO: unroll to avoid swapping
|
r.push_back(r[i-1]-q[i]*r[i]);
|
||||||
while (b != 0){
|
p.push_back(unit_part(r[i+1]));
|
||||||
temp = b;
|
r[i+1] = idiv(r[i+1],p[i+1]);
|
||||||
div_mod(a,b,quotient,b);
|
s.push_back(idiv(s[i-1]-q[i]*s[i],p[i+1]));
|
||||||
a = temp;
|
t.push_back(idiv(t[i-1]-q[i]*t[i],p[i+1]));
|
||||||
|
i++;
|
||||||
temp = x;
|
|
||||||
x = last_x-quotient*x;
|
|
||||||
last_x = temp;
|
|
||||||
|
|
||||||
temp = y;
|
|
||||||
y = last_y-quotient*y;
|
|
||||||
last_y = temp;
|
|
||||||
}
|
}
|
||||||
u = last_x * unit_part_a;
|
|
||||||
v = last_y * unit_part_b;
|
|
||||||
|
|
||||||
CGAL_precondition(unit_part(a) == NT(1));
|
s_=s[i-1];
|
||||||
CGAL_precondition(a == a_*u + b_*v);
|
t_=t[i-1];
|
||||||
return a;
|
AS h = r[i-1];
|
||||||
|
CGAL_precondition( h == f*s_ + g*t_);
|
||||||
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
CGAL_END_NAMESPACE
|
CGAL_END_NAMESPACE
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue