DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
convergents.cpp
Go to the documentation of this file.
1
13
32
33
36
#include <iostream>
37
#include "DGtal/arithmetic/LighterSternBrocot.h"
39
41
42
using namespace
DGtal
;
43
45
46
void
usage
(
int
,
char
** argv )
47
{
48
std::cerr <<
"Usage: "
<< argv[ 0 ] <<
" <p> <q>"
<< std::endl;
49
std::cerr <<
"\t - computes the successive convergent of the fraction p / q."
<< std::endl;
50
}
51
55
int
main
(
int
argc,
char
** argv )
56
{
57
if
( argc < 3 )
58
{
59
usage
( argc, argv );
60
return
1;
61
}
62
64
typedef
LighterSternBrocot<DGtal::int64_t, DGtal::int64_t, StdMapRebinder>
SB;
// the type of the Stern-Brocot tree
65
typedef
SB::Fraction Fraction;
// the type for fractions
66
typedef
Fraction::ConstIterator
ConstIterator
;
// the iterator type for visiting quotients
67
typedef
Fraction::Value
Value
;
// the value of the iterator, a pair (quotient,depth).
69
71
DGtal::int64_t
p = atoll( argv[ 1 ] );
72
DGtal::int64_t
q = atoll( argv[ 2 ] );
73
Fraction f( p, q );
// fraction p/q
75
77
// Visit quotients u_k as pair (u_k,k)
78
std::cout <<
"z = "
;
79
ConstIterator
itbegin = f.begin(), itend = f.end();
80
for
(
ConstIterator
it = itbegin; it != itend; ++it )
81
{
82
Value
u = *it;
83
std::cout << ( ( it == itbegin ) ?
"["
:
","
)
84
<< u.first;
85
}
86
std::cout <<
"]"
<< std::endl;
88
90
Fraction
g
;
// fraction null, 0/0, invalid
91
for
(
ConstIterator
it = itbegin; it != itend; ++it )
92
{
93
Value
u = *it;
94
std::cout <<
"z_"
<< u.second <<
" = "
;
95
g
.push_back( u );
// add (u_i,i) to existing fractions
96
std::cout <<
g
.p() <<
" / "
<<
g
.q() << std::endl;
97
}
99
return
0;
100
}
DGtal::ConstIterator
DGtal::LighterSternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition
LighterSternBrocot.h:109
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::int64_t
std::int64_t int64_t
signed 94-bit integer.
Definition
BasicTypes.h:73
DGtal::Value
g
std::mt19937 g(rd())
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
usage
void usage(int, char **argv)
Definition
testPolynomial.cpp:58
examples
arithmetic
convergents.cpp
Generated on
for DGtal by
1.18.0