33#include "DGtal/base/Common.h"
34#include "DGtal/arithmetic/IntegerComputer.h"
44template <
typename Integer>
47 unsigned int nbok = 0;
52 trace.
info() <<
"GCD(" << a <<
"," << b <<
")"
53 <<
" = " << g << std::endl;
56 nbok += ic.
isZero( ra ) ? 1 : 0;
58 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
59 << a <<
" % " << g <<
" == 0" << std::endl;
60 nbok += ic.
isZero( rb ) ? 1 : 0;
62 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
63 << b <<
" % " << g <<
" == 0" << std::endl;
66 nbok += g ==
Integer( 1 ) ? 1 : 0;
68 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
69 <<
"GCD(" << a <<
"," << b <<
") == 1" << std::endl;
74 nbok += g == c ? 1 : 0;
76 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
77 <<
"GCD(" << a <<
"," << b <<
") == " << c << std::endl;
81template <
typename Integer>
84 unsigned int nbok = 0;
89 trace.
info() <<
"a / b = " << a <<
" / " << b << std::endl;
90 std::vector<Integer> quotients;
92 nbok += g == g2 ? 1 : 0;
94 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
95 << g <<
" == " << g2 << std::endl;
97 for (
typename std::vector<Integer>::const_iterator it = quotients.begin(),
98 it_end = quotients.end(); it != it_end; ++it )
103 double q = floor( da / db );
104 nbok +=
Integer( (
int) q ) == quotients[ 0 ] ? 1 : 0;
106 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
107 << q <<
" == " << quotients[ 0 ] << std::endl;
109 Point2I p = ic.
convergent( quotients, quotients.size() );
110 nbok += p[ 0 ] == ( a / g ) ? 1 : 0;
112 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
113 <<
"convergent p[ 0 ] " << p[ 0 ] <<
" == a / g " << std::endl;
114 nbok += p[ 1 ] == ( b / g ) ? 1 : 0;
116 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
117 <<
"convergent p[ 1 ] " << p[ 1 ] <<
" == b / g " << std::endl;
121template <
typename Integer>
124 unsigned int nbok = 0;
130 if ( ic.
isZero( b ) ) ++b;
131 trace.
info() <<
"- a / b = " << a <<
" / " << b << std::endl;
136 nbok += fl == fl2 ? 1 : 0;
138 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
139 <<
"fl == fl2 " << fl2 << std::endl;
140 nbok += ce == ce2 ? 1 : 0;
142 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
143 <<
"ce == ce2 " << ce2 << std::endl;
145 nbok += ( ( m == 0 ) && ( fl == ce ) ) || ( fl + 1 == ce ) ? 1 : 0;
147 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
148 <<
"( ( m == 0 ) && ( fl == ce ) ) || ( fl+1 == ce )"
153template <
typename Integer>
157 unsigned int nbok = 0;
162 trace.
info() <<
"a / b = " << a <<
" / " << b
163 <<
" gcd=" << g << std::endl;
165 trace.
info() <<
"Bezout = " << v[ 0 ] <<
"," << v[ 1 ] << std::endl;
166 Integer rem = a * v[ 0 ] + b * v[ 1 ];
167 nbok += rem == g ? 1 : 0;
169 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
170 <<
"a * v[ 0 ] + b * v[ 1 ] == g "
171 <<
"(" << rem <<
" == " << g <<
")" << std::endl;
175template <
typename Integer>
179 unsigned int nbok = 0;
183 p = Point2I( rand(), rand() );
184 N = Point2I( rand() , rand() );
186 Point2I u( rand() / 100, rand() / 100);
187 trace.
info() <<
"p = " << p << std::endl;
188 trace.
info() <<
"u = " << u << std::endl;
189 trace.
info() <<
"N = " << N << std::endl;
190 trace.
info() <<
"c = " << c << std::endl;
193 trace.
info() <<
"fl = " << fl <<
", ce = " << ce << std::endl;
194 Point2I p1 = p + (u * fl);
195 Point2I p2 = p + (u * ce);
200 <<
" <= c2 = " << c2 << std::endl;
201 nbok += ( ( c1 == c2 ) && ( c == c1 ) )
202 || ( ( c1 <= c ) && ( c < c2 ) ) ? 1 : 0;
204 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
205 <<
"( ( c1 == c2 ) && ( c == c1 ) ) || ( ( c1 <= c ) && ( c < c2 ) )" << std::endl;
209template <
typename Integer>
214 unsigned int nbok = 0;
217 Point2I A( rand(), rand() );
218 Vector2I u( rand() / 100, rand() / 100 );
220 Vector2I N( rand(), rand() );
221 Vector2I N2( rand(), rand() );
224 trace.
info() <<
"A = " << A << std::endl;
225 trace.
info() <<
"u = " << u << std::endl;
226 trace.
info() <<
"N = " << N << std::endl;
227 trace.
info() <<
"c = " << c << std::endl;
228 trace.
info() <<
"N2 = " << N2 << std::endl;
229 trace.
info() <<
"c2 = " << c2 << std::endl;
231 A, u, N, c, N2, c2,
true );
232 trace.
info() <<
"-> v = " << v << std::endl;
234 nbok += ( ic.
abs( a0 ) == 1 ) ? 1 : 0;
236 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
237 <<
" v^u = " << a0 <<
" == +/- 1 " << std::endl;
247 nbok += a2 <= c2 ? 1 : 0;
249 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
250 <<
"(A+v).N2 = " << a2
251 <<
" <= c = " << c2 << std::endl;
252 nbok += a3 > c2 ? 1 : 0;
254 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") "
255 <<
"(A+v+u).N2 = " << a2
256 <<
" > c2 = " << c2 << std::endl;
266 unsigned int nbtests = 50;
267 unsigned int nbok = 0;
272 for (
unsigned int i = 0; i < nbtests; ++i )
274 nbok += testGCD<Integer>( ic ) ? 1 : 0;
277 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") gcd tests." << std::endl;
281 for (
unsigned int i = 0; i < nbtests; ++i )
283 nbok += testCFrac<Integer>( ic ) ? 1 : 0;
286 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") cfrac tests." << std::endl;
290 for (
unsigned int i = 0; i < nbtests; ++i )
292 nbok += testCeilFloorDiv<Integer>( ic ) ? 1 : 0;
295 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") ceil floor division." << std::endl;
298 trace.
beginBlock (
"Testing block: multiple Bezout / extended euclid: ax+by= gcd(a,b)." );
299 for (
unsigned int i = 0; i < nbtests; ++i )
301 nbok += testExtendedEuclid<Integer>( ic ) ? 1 : 0;
304 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") Bezout / extended euclid." << std::endl;
307 trace.
beginBlock (
"Testing block: multiple coefficient intersection." );
308 for (
unsigned int i = 0; i < nbtests; ++i )
313 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") coefficient intersection." << std::endl;
317 for (
unsigned int i = 0; i < nbtests; ++i )
322 trace.
info() <<
"(" << nbok <<
"/" << nb <<
") valid bezout." << std::endl;
335 trace.
emphase() << ( res ?
"Passed." :
"Error." ) << endl;
Aim: This class gathers several types and methods to make computation with integers.
Integer getCFrac(std::vector< Integer > "ients, IntegerParamType a, IntegerParamType b) const
void getGcd(Integer &g, IntegerParamType a, IntegerParamType b) const
Integer crossProduct(const Vector2I &u, const Vector2I &v) const
static bool isZero(IntegerParamType a)
static Integer abs(IntegerParamType a)
Integer ceilDiv(IntegerParamType na, IntegerParamType nb) const
void reduce(Vector2I &p) const
SpaceND< 2, Integer >::Vector Vector2I
Integer dotProduct(const Vector2I &u, const Vector2I &v) const
SpaceND< 2, Integer >::Point Point2I
Point2I convergent(const std::vector< Integer > "ients, unsigned int k) const
Vector2I extendedEuclid(IntegerParamType a, IntegerParamType b, IntegerParamType c) const
void getCoefficientIntersection(Integer &fl, Integer &ce, const Vector2I &p, const Vector2I &u, const Vector2I &N, IntegerParamType c) const
void getValidBezout(Vector2I &v, const Point2I &A, const Vector2I &u, const Vector2I &N, IntegerParamType c, const Vector2I &N2, IntegerParamType c2, bool compute_v=true) const
void getFloorCeilDiv(Integer &fl, Integer &ce, IntegerParamType na, IntegerParamType nb) const
Integer gcd(IntegerParamType a, IntegerParamType b) const
Integer floorDiv(IntegerParamType na, IntegerParamType nb) const
void beginBlock(const std::string &keyword="")
Point::Coordinate Integer
DGtal is the top-level namespace which contains all DGtal functions and types.
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Aim: The traits class for all models of Cinteger.
bool testGCD(const IntegerComputer< Integer > &ic)
bool testCeilFloorDiv(const IntegerComputer< Integer > &ic)
bool testValidBezout(const IntegerComputer< Integer > &ic)
bool testCoefficientIntersection(const IntegerComputer< Integer > &ic)
bool testCFrac(const IntegerComputer< Integer > &ic)
bool testIntegerComputer()
bool testExtendedEuclid(const IntegerComputer< Integer > &ic)