DGtal  1.5.beta
convergents.cpp File Reference
#include <iostream>
#include "DGtal/arithmetic/LighterSternBrocot.h"
Include dependency graph for convergents.cpp:

Go to the source code of this file.

Functions

void usage (int, char **argv)
 
int main (int argc, char **argv)
 

Detailed Description

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2012/02/06

An example file named convergents.

This file is part of the DGtal library.

Definition in file convergents.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Main.

[convergents-types]

[convergents-types]

[convergents-instantiation]

[convergents-instantiation]

[convergents-cfrac]

[convergents-cfrac]

[convergents-convergents]

[convergents-convergents]

Definition at line 55 of file convergents.cpp.

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 }
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
void usage(int, char **argv)
Definition: convergents.cpp:46
MyDigitalSurface::ConstIterator ConstIterator
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74

References usage().

◆ usage()

void usage ( int  ,
char **  argv 
)

Definition at line 46 of file convergents.cpp.

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 }

Referenced by main().