34 #include "DGtal/arithmetic/IntegerComputer.h"
47 template <
typename TInteger,
typename TQuotient,
typename TMap>
52 : p( p1 ), q( q1 ), u( u1 ), k( k1 ),
60 template <
typename TInteger,
typename TQuotient,
typename TMap>
66 typedef typename MapQuotientToNode::iterator Iterator;
72 Iterator itkey = myChildren.find( v );
73 if ( itkey != myChildren.end() )
83 myChildren[ v ] = newNode;
96 new Node( p * _v - ( _v - 1 ) * ( p - _pp ) / (_u - 1),
97 q * _v - ( _v - 1 ) * ( q - _qq ) / (_u - 1),
101 myChildren[ v ] = newNode;
106 template <
typename TInteger,
typename TQuotient,
typename TMap>
115 template <
typename TInteger,
typename TQuotient,
typename TMap>
121 ASSERT( origin() != 0 );
122 Node* prevNode = origin();
124 prevNode = prevNode->
origin();
129 template <
typename TInteger,
typename TQuotient,
typename TMap>
135 ASSERT( origin() != 0 );
136 Node* prevNode = origin();
145 template <
typename TInteger,
typename TQuotient,
typename TMap>
155 bool sup1 = aP >= aQ;
156 if ( ! sup1 ) std::swap( aP, aQ );
167 node = node->
child( v + 1 );
174 myNode = node->
child( v );
179 template <
typename TInteger,
typename TQuotient,
typename TMap>
183 : myNode( sb_node ), mySup1( sup1 )
187 template <
typename TInteger,
typename TQuotient,
typename TMap>
191 : myNode( other.myNode ), mySup1( other.mySup1 )
195 template <
typename TInteger,
typename TQuotient,
typename TMap>
201 if (
this != &other )
209 template <
typename TInteger,
typename TQuotient,
typename TMap>
218 template <
typename TInteger,
typename TQuotient,
typename TMap>
224 return myNode ? ( mySup1 ? myNode->p : myNode->q ) : 0;
227 template <
typename TInteger,
typename TQuotient,
typename TMap>
233 return myNode ? ( mySup1 ? myNode->q : myNode->p ) : 0;
236 template <
typename TInteger,
typename TQuotient,
typename TMap>
242 ASSERT( myNode != 0 );
248 template <
typename TInteger,
typename TQuotient,
typename TMap>
254 ASSERT( myNode != 0 );
267 template <
typename TInteger,
typename TQuotient,
typename TMap>
273 return ( this->p() == p1 ) && ( this->q() == q1 );
276 template <
typename TInteger,
typename TQuotient,
typename TMap>
282 Integer d = p() * q1 - q() * p1;
283 return d < NumberTraits<Integer>::ZERO;
286 template <
typename TInteger,
typename TQuotient,
typename TMap>
292 Integer d = p() * q1 - q() * p1;
296 template <
typename TInteger,
typename TQuotient,
typename TMap>
302 if ( mySup1 == other.
mySup1 )
303 return ( myNode == other.
myNode );
305 return ( ( myNode->p == other.
myNode->
q )
306 && ( myNode->q == other.
myNode->
p ) );
309 template <
typename TInteger,
typename TQuotient,
typename TMap>
318 template <
typename TInteger,
typename TQuotient,
typename TMap>
324 return this->lessThan( other.
p(), other.
q() );
327 template <
typename TInteger,
typename TQuotient,
typename TMap>
333 return this->moreThan( other.
p(), other.
q() );
338 template <
typename TInteger,
typename TQuotient,
typename TMap>
344 ASSERT( ! this->null() );
345 typedef typename MapQuotientToNode::iterator Iterator;
352 template <
typename TInteger,
typename TQuotient,
typename TMap>
358 ASSERT( ! this->null() );
362 node = ( myNode->isSameDepthLeft() )
364 : myNode->
child( 2 );
368 template <
typename TInteger,
typename TQuotient,
typename TMap>
374 ASSERT( ! this->null() );
378 node = ( ! myNode->isSameDepthLeft() )
380 : myNode->
child( 2 );
384 template <
typename TInteger,
typename TQuotient,
typename TMap>
393 template <
typename TInteger,
typename TQuotient,
typename TMap>
402 template <
typename TInteger,
typename TQuotient,
typename TMap>
408 return Fraction( myNode->origin(), mySup1 );
411 template <
typename TInteger,
typename TQuotient,
typename TMap>
417 return Fraction( myNode->child( v ), mySup1 );
420 template <
typename TInteger,
typename TQuotient,
typename TMap>
426 return Fraction( myNode->ancestor(), mySup1 );
429 template <
typename TInteger,
typename TQuotient,
typename TMap>
438 template <
typename TInteger,
typename TQuotient,
typename TMap>
444 return Fraction( myNode->father(), mySup1 );
447 template <
typename TInteger,
typename TQuotient,
typename TMap>
454 return Fraction( myNode->origin()->child( m ), mySup1 );
459 template <
typename TInteger,
typename TQuotient,
typename TMap>
468 template <
typename TInteger,
typename TQuotient,
typename TMap>
480 template <
typename TInteger,
typename TQuotient,
typename TMap>
486 ASSERT( ( ((
Quotient)-2) <= kp ) && ( kp <= k() ) );
487 return reduced( k() - kp );
490 template <
typename TInteger,
typename TQuotient,
typename TMap>
507 if ( ! mySup1 && ( i == k() ) )
518 template <
typename TInteger,
typename TQuotient,
typename TMap>
522 push_back(
const std::pair<Quotient, Quotient> & quotient )
524 pushBack( quotient );
527 template <
typename TInteger,
typename TQuotient,
typename TMap>
531 pushBack(
const std::pair<Quotient, Quotient> & quotient )
574 .child( quotient.first ) );
580 this->
operator=( child( quotient.first ) );
583 this->
operator=( child( 2 ).child( quotient.first ) );
593 template <
typename TInteger,
typename TQuotient,
typename TMap>
611 template <
typename TInteger,
typename TQuotient,
typename TMap>
634 template <
typename TInteger,
typename TQuotient,
typename TMap>
638 getCFrac( std::vector<Quotient> & quotients )
const
642 if ( null() )
return;
643 quotients.resize( i + 1 );
645 quotients[ i-- ] = f.
u();
649 for ( ; i >= 1; --i )
659 template <
typename TInteger,
typename TQuotient,
typename TMap>
666 this->getCFrac( *seq );
670 template <
typename TInteger,
typename TQuotient,
typename TMap>
680 template <
typename TInteger,
typename TQuotient,
typename TMap>
686 if ( this->null() ) out <<
"[Fraction null]";
689 out <<
"[Fraction f=" << this->p()
691 <<
" u=" << this->u()
692 <<
" k=" << this->k()
694 std::vector<Quotient> quotients;
695 if ( this->k() >= 0 )
697 this->getCFrac( quotients );
698 out <<
" [" << quotients[ 0 ];
699 for (
unsigned int i = 1; i < quotients.size(); ++i )
700 out <<
"," << quotients[ i ];
711 template <
typename TInteger,
typename TQuotient,
typename TMap>
719 template <
typename TInteger,
typename TQuotient,
typename TMap>
737 template <
typename TInteger,
typename TQuotient,
typename TMap>
748 template <
typename TInteger,
typename TQuotient,
typename TMap>
756 template <
typename TInteger,
typename TQuotient,
typename TMap>
764 template <
typename TInteger,
typename TQuotient,
typename TMap>
779 template <
typename TInteger,
typename TQuotient,
typename TMap>
785 if ( f.
null() ) out <<
"[Fraction null]";
788 out <<
"[Fraction f=" << f.
p()
794 std::vector<Quotient> quotients;
798 out <<
" [" << quotients[ 0 ];
799 for (
unsigned int i = 1; i < quotients.size(); ++i )
800 out <<
"," << quotients[ i ];
811 template <
typename TInteger,
typename TQuotient,
typename TMap>
822 template <
typename TInteger,
typename TQuotient,
typename TMap>