34 #include "DGtal/arithmetic/IntegerComputer.h"
47 template <
typename TInteger,
typename TQuotient>
51 Node* ascendant_left1,
Node* ascendant_right1,
52 Node* descendant_left1,
Node* descendant_right1,
54 : p( p1 ), q( q1 ), u( u1 ), k( k1 ),
55 ascendantLeft( ascendant_left1 ),
56 ascendantRight( ascendant_right1 ),
57 descendantLeft( descendant_left1 ),
58 descendantRight( descendant_right1 ),
67 template <
typename TInteger,
typename TQuotient>
72 this->
operator=( SternBrocotTree::fraction( aP, aQ, ancestor ) );
75 template <
typename TInteger,
typename TQuotient>
83 template <
typename TInteger,
typename TQuotient>
87 : myNode( other.myNode )
91 template <
typename TInteger,
typename TQuotient>
104 template <
typename TInteger,
typename TQuotient>
113 template <
typename TInteger,
typename TQuotient>
119 return myNode ? myNode->p : 0;
122 template <
typename TInteger,
typename TQuotient>
128 return myNode ? myNode->q : 0;
131 template <
typename TInteger,
typename TQuotient>
137 ASSERT( myNode != 0 );
141 template <
typename TInteger,
typename TQuotient>
147 ASSERT( myNode != 0 );
151 template <
typename TInteger,
typename TQuotient>
157 return ( this->p() == p1 ) && ( this->q() == q1 );
160 template <
typename TInteger,
typename TQuotient>
166 Integer d = p() * q1 - q() * p1;
167 return d < NumberTraits<Integer>::ZERO;
170 template <
typename TInteger,
typename TQuotient>
176 Integer d = p() * q1 - q() * p1;
180 template <
typename TInteger,
typename TQuotient>
186 return myNode == other.
myNode;
189 template <
typename TInteger,
typename TQuotient>
195 return myNode != other.
myNode;
198 template <
typename TInteger,
typename TQuotient>
204 return this->lessThan( other.
p(), other.
q() );
207 template <
typename TInteger,
typename TQuotient>
213 return this->moreThan( other.
p(), other.
q() );
216 template <
typename TInteger,
typename TQuotient>
222 if ( myNode->descendantLeft == 0 )
228 odd() ? k() : k() + 1,
233 Node* invn =
new Node( inv.
p() + invpright->
p,
234 inv.
q() + invpright->
q,
236 inv.
even() ? inv.
k() : inv.
k() + 1,
237 myNode->inverse, invpright,
244 return Fraction( myNode->descendantLeft );
247 template <
typename TInteger,
typename TQuotient>
253 if ( myNode->descendantRight == 0 )
257 ASSERT( myNode->descendantRight != 0 );
259 return Fraction( myNode->descendantRight );
262 template <
typename TInteger,
typename TQuotient>
268 return ( k() & 1 ) == 0;
271 template <
typename TInteger,
typename TQuotient>
277 return ( k() & 1 ) != 0;
280 template <
typename TInteger,
typename TQuotient>
286 return Fraction( odd() ? myNode->ascendantRight : myNode->ascendantLeft );
289 template <
typename TInteger,
typename TQuotient>
304 return odd() ? previousPartial().right() : previousPartial().left();
310 template <
typename TInteger,
typename TQuotient>
316 return Fraction( odd() ? myNode->ascendantLeft : myNode->ascendantRight );
322 template <
typename TInteger,
typename TQuotient>
331 template <
typename TInteger,
typename TQuotient>
337 ASSERT( ( ((
Quotient)-2) <= kp ) && ( kp <= k() ) );
338 return reduced( k() - kp );
341 template <
typename TInteger,
typename TQuotient>
348 Node* n = this->myNode;
360 template <
typename TInteger,
typename TQuotient>
364 push_back(
const std::pair<Quotient, Quotient> & quotient )
366 pushBack( quotient );
369 template <
typename TInteger,
typename TQuotient>
373 pushBack(
const std::pair<Quotient, Quotient> & quotient )
396 for (
Quotient i = 0; i < quotient.first; ++i )
404 for (
Quotient i = 1; i < quotient.first; ++i )
411 for (
Quotient i = 1; i < quotient.first; ++i )
423 template <
typename TInteger,
typename TQuotient>
433 template <
typename TInteger,
typename TQuotient>
456 template <
typename TInteger,
typename TQuotient>
460 getCFrac( std::vector<Quotient> & quotients )
const
464 quotients.resize( (
unsigned int)i + 1 );
465 quotients[ (
unsigned int)i-- ] = this->u();
467 bool bleft = odd() ?
true :
false;
472 quotients[ (
unsigned int)i ] =
479 template <
typename TInteger,
typename TQuotient>
486 this->getCFrac( *seq );
490 template <
typename TInteger,
typename TQuotient>
500 template <
typename TInteger,
typename TQuotient>
506 if ( this->null() ) out <<
"[Fraction null]";
509 out <<
"[Fraction f=" << this->p()
511 <<
" u=" << this->u()
512 <<
" k=" << this->k()
514 std::vector<Quotient> quotients;
515 if ( this->k() >= 0 )
517 this->getCFrac( quotients );
518 out <<
" [" << quotients[ 0 ];
519 for (
unsigned int i = 1; i < quotients.size(); ++i )
520 out <<
"," << quotients[ i ];
531 template <
typename TInteger,
typename TQuotient>
540 template <
typename TInteger,
typename TQuotient>
571 template <
typename TInteger,
typename TQuotient>
583 template <
typename TInteger,
typename TQuotient>
591 template <
typename TInteger,
typename TQuotient>
606 template <
typename TInteger,
typename TQuotient>
612 if ( f.
null() ) out <<
"[Fraction null]";
615 out <<
"[Fraction f=" << f.
p()
620 std::vector<Quotient> quotients;
624 out <<
" [" << quotients[ 0 ];
625 for (
unsigned int i = 1; i < quotients.size(); ++i )
626 out <<
"," << quotients[ i ];
637 template <
typename TInteger,
typename TQuotient>
648 template <
typename TInteger,
typename TQuotient>
666 while ( ! ancestor.
equals( p, q ) )
668 ASSERT( ( p + q ) >= ( ancestor.
p() + ancestor.
q() )
669 &&
"[ImaGene::SternBrocot::fraction] bad ancestor." );
670 ancestor = ancestor.
lessThan( p, q )