48#ifndef VIGRA_RATIONAL_HPP
49#define VIGRA_RATIONAL_HPP
55#include "mathutil.hxx"
56#include "numerictraits.hxx"
57#include "metaprogramming.hxx"
80template <
typename IntType>
81IntType
gcd(IntType n, IntType m)
121template <
typename IntType>
122IntType
lcm(IntType n, IntType m)
127 if (n == zero || m == zero)
140class bad_rational :
public std::domain_error
143 explicit bad_rational() : std::domain_error(
"bad rational: zero denominator") {}
146template <
typename IntType>
149template <
typename IntType>
150Rational<IntType>
abs(
const Rational<IntType>& r);
151template <
typename IntType>
152Rational<IntType> pow(
const Rational<IntType>& r,
int n);
153template <
typename IntType>
154Rational<IntType>
floor(
const Rational<IntType>& r);
155template <
typename IntType>
156Rational<IntType>
ceil(
const Rational<IntType>& r);
192template <
typename IntType>
203 typedef typename If<typename TypeTraits<IntType>::isBuiltinType,
249 : num(IntType(v < 0.0 ?
252 den(IntType(1.0/epsilon + 0.5))
346 return den == zero && num > zero;
354 return den == zero && num < zero;
362 return den == zero && num != zero;
372 return num == zero ? 0 : num < zero ? -1 : 1;
389template <
typename IntType>
390inline Rational<IntType>&
401template <
typename IntType>
409 if(r.den == zero &&
sign()*r.
sign() < 0)
410 throw bad_rational();
415 assign(r.num, zero,
false);
438 IntType r_num = r.num;
439 IntType r_den = r.den;
441 IntType g =
gcd(den, r_den);
443 num = num * (r_den / g) + r_num * den;
451template <
typename IntType>
459 if(r.den == zero &&
sign()*r.
sign() > 0)
460 throw bad_rational();
465 assign(-r.num, zero,
false);
470 IntType r_num = r.num;
471 IntType r_den = r.den;
475 IntType g =
gcd(den, r_den);
477 num = num * (r_den / g) - r_num * den;
485template <
typename IntType>
494 throw bad_rational();
501 throw bad_rational();
502 num = r.num *
sign();
508 IntType r_num = r.num;
509 IntType r_den = r.den;
512 IntType gcd1 = gcd<IntType>(num, r_den);
513 IntType gcd2 = gcd<IntType>(r_num, den);
514 num = (num/gcd1) * (r_num/gcd2);
515 den = (den/gcd2) * (r_den/gcd1);
519template <
typename IntType>
528 throw bad_rational();
536 throw bad_rational();
537 num = IntType(
sign());
546 IntType r_num = r.num;
547 IntType r_den = r.den;
550 IntType gcd1 = gcd<IntType>(num, r_num);
551 IntType gcd2 = gcd<IntType>(r_den, den);
552 num = (num/gcd1) * (r_den/gcd2);
553 den = (den/gcd2) * (r_num/gcd1);
563template <
typename IntType>
571template <
typename IntType>
579template <
typename IntType>
590 throw bad_rational();
600 IntType g =
gcd(i, den);
606template <
typename IntType>
617 throw bad_rational();
618 num = IntType(
sign());
623 IntType g =
gcd(i, num);
638template <
typename IntType>
646template <
typename IntType>
655template <
typename IntType>
664 throw bad_rational();
678 IntType g = gcd<IntType>(num, den);
738#ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
741struct NumericTraits<Rational<T> >
743 typedef Rational<T> Type;
744 typedef Rational<typename NumericTraits<T>::Promote> Promote;
745 typedef Rational<typename NumericTraits<T>::RealPromote> RealPromote;
746 typedef std::complex<Rational<RealPromote> > ComplexPromote;
749 typedef typename NumericTraits<T>::isIntegral isIntegral;
750 typedef VigraTrueType isScalar;
751 typedef typename NumericTraits<T>::isSigned isSigned;
752 typedef VigraTrueType isOrdered;
753 typedef VigraFalseType isComplex;
755 static Type zero() {
return Type(0); }
756 static Type one() {
return Type(1); }
757 static Type nonZero() {
return one(); }
759 static Promote toPromote(Type
const & v)
760 {
return Promote(v.numerator(), v.denominator(),
false); }
761 static RealPromote toRealPromote(Type
const & v)
762 {
return RealPromote(v.numerator(), v.denominator(),
false); }
763 static Type fromPromote(Promote
const & v)
764 {
return Type(NumericTraits<T>::fromPromote(v.numerator()),
765 NumericTraits<T>::fromPromote(v.denominator()),
false); }
766 static Type fromRealPromote(RealPromote
const & v)
767 {
return Type(NumericTraits<T>::fromRealPromote(v.numerator()),
768 NumericTraits<T>::fromRealPromote(v.denominator()),
false); }
772struct NormTraits<Rational<T> >
774 typedef Rational<T> Type;
775 typedef typename NumericTraits<Type>::Promote SquaredNormType;
776 typedef Type NormType;
780struct PromoteTraits<Rational<T>, Rational<T> >
782 typedef Rational<typename PromoteTraits<T, T>::Promote> Promote;
783 static Promote toPromote(Rational<T>
const & v) {
return v; }
786template <
class T1,
class T2>
787struct PromoteTraits<Rational<T1>, Rational<T2> >
789 typedef Rational<typename PromoteTraits<T1, T2>::Promote> Promote;
790 static Promote toPromote(Rational<T1>
const & v) {
return v; }
791 static Promote toPromote(Rational<T2>
const & v) {
return v; }
794template <
class T1,
class T2>
795struct PromoteTraits<Rational<T1>, T2 >
797 typedef Rational<typename PromoteTraits<T1, T2>::Promote> Promote;
798 static Promote toPromote(Rational<T1>
const & v) {
return v; }
799 static Promote toPromote(T2
const & v) {
return Promote(v); }
802template <
class T1,
class T2>
803struct PromoteTraits<T1, Rational<T2> >
805 typedef Rational<typename PromoteTraits<T1, T2>::Promote> Promote;
806 static Promote toPromote(T1
const & v) {
return Promote(v); }
807 static Promote toPromote(Rational<T2>
const & v) {
return v; }
837template <
typename IntType>
844template <
typename IntType>
851template <
typename IntType>
858template <
typename IntType>
865template <
typename IntType>
872template <
typename IntType>
879template <
typename IntType>
880inline Rational<IntType>
887template <
typename IntType>
888inline Rational<IntType>
895template <
typename IntType>
896inline Rational<IntType>
903template <
typename IntType>
904inline Rational<IntType>
911template <
typename IntType>
912inline Rational<IntType>
919template <
typename IntType>
920inline Rational<IntType>
927template <
typename IntType>
928inline Rational<IntType>
935template <
typename IntType>
936inline Rational<IntType>
953template <
typename IntType1,
typename IntType2>
963template <
typename IntType1,
typename IntType2>
971template <
typename IntType1,
typename IntType2>
979template <
typename IntType1,
typename IntType2>
989template <
typename IntType1,
typename IntType2>
997template <
typename IntType1,
typename IntType2>
1005template <
typename IntType1,
typename IntType2>
1010 typedef typename PromoteTraits<IntType1, IntType2>::Promote IntType;
1045template <
typename IntType1,
typename IntType2>
1050 typedef typename PromoteTraits<IntType1, IntType2>::Promote IntType;
1077template <
typename IntType1,
typename IntType2>
1085template <
typename IntType1,
typename IntType2>
1093template <
typename IntType1,
typename IntType2>
1106template <
typename IntType1,
typename IntType2>
1114template <
typename IntType1,
typename IntType2>
1122template <
typename IntType1,
typename IntType2>
1130template <
typename IntType1,
typename IntType2>
1138template <
typename IntType1,
typename IntType2>
1146template <
typename IntType1,
typename IntType2>
1154template <
typename IntType1,
typename IntType2>
1168template <
typename IntType>
1169inline Rational<IntType>
1179template <
typename IntType>
1180inline Rational<IntType>
1187template <
typename IntType>
1188inline typename NormTraits<Rational<IntType> >::SquaredNormType
1198template <
typename IntType>
1207 throw bad_rational();
1226 nnew = IntType(1), dnew = IntType(1);
1227 for(; ae != 0; ae >>= 1, nold *= nold, dold *= dold)
1247template <
typename IntType>
1251 IntType zero(0), one(1);
1260template <
typename IntType>
1264 IntType zero(0), one(1);
1288template <
typename T,
typename IntType>
1302template <
typename IntType>
Definition: rational.hxx:194
Rational(param_type n, param_type d, bool doNormalize=true)
Definition: rational.hxx:236
Rational(param_type n)
Definition: rational.hxx:223
param_type numerator() const
Definition: rational.hxx:269
Rational & operator=(param_type n)
Definition: rational.hxx:261
Rational(double v, double epsilon=1e-4)
Definition: rational.hxx:248
bool is_ninf() const
Definition: rational.hxx:351
bool operator!() const
Definition: rational.hxx:339
Rational & operator/=(const Rational &r)
Definition: rational.hxx:520
param_type denominator() const
Definition: rational.hxx:273
bool is_pinf() const
Definition: rational.hxx:343
Rational operator++(int)
Definition: rational.hxx:332
Rational & operator--()
Definition: rational.hxx:647
bool is_inf() const
Definition: rational.hxx:359
IntType value_type
Definition: rational.hxx:198
Rational & operator+=(const Rational &r)
Definition: rational.hxx:402
Rational & operator*=(const Rational &r)
Definition: rational.hxx:486
Rational operator--(int)
Definition: rational.hxx:335
Rational(Rational< U > const &r)
Definition: rational.hxx:216
Rational()
Definition: rational.hxx:208
If< typenameTypeTraits< IntType >::isBuiltinType, IntType, IntTypeconst & >::type param_type
Definition: rational.hxx:204
Rational & operator++()
Definition: rational.hxx:639
Rational & assign(param_type n, param_type d, bool doNormalize=true)
Definition: rational.hxx:391
Rational & operator-=(const Rational &r)
Definition: rational.hxx:452
int sign() const
Definition: rational.hxx:369
int floor(FixedPoint< IntBits, FracBits > v)
rounding down.
Definition: fixedpoint.hxx:667
Diff2D operator+(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:739
bool operator<=(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
less or equal
Definition: fixedpoint.hxx:521
FFTWComplex< R >::NormType norm(const FFTWComplex< R > &a)
norm (= magnitude)
Definition: fftw3.hxx:1037
NumericTraits< T >::Promote sq(T t)
The square function.
Definition: mathutil.hxx:382
bool operator>=(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
greater or equal
Definition: fixedpoint.hxx:539
IntType lcm(IntType n, IntType m)
Definition: rational.hxx:122
IntType gcd(IntType n, IntType m)
Definition: rational.hxx:81
T sign(T t)
The sign function.
Definition: mathutil.hxx:591
FFTWComplex< R >::NormType abs(const FFTWComplex< R > &a)
absolute value (= magnitude)
Definition: fftw3.hxx:1002
bool operator>(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
greater
Definition: fixedpoint.hxx:530
FFTWComplex< R >::SquaredNormType squaredNorm(const FFTWComplex< R > &a)
squared norm (= squared magnitude)
Definition: fftw3.hxx:1044
bool operator<(FixedPoint< IntBits1, FracBits1 > l, FixedPoint< IntBits2, FracBits2 > r)
less than
Definition: fixedpoint.hxx:512
T rational_cast(const Rational< IntType > &src)
Definition: rational.hxx:1289
bool operator==(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
equal
Definition: fftw3.hxx:825
int ceil(FixedPoint< IntBits, FracBits > v)
rounding up.
Definition: fixedpoint.hxx:675
bool operator!=(FFTWComplex< R > const &a, const FFTWComplex< R > &b)
not equal
Definition: fftw3.hxx:841
Diff2D operator-(Diff2D const &a, Diff2D const &b)
Definition: diff2d.hxx:711