oILAB
Loading...
Searching...
No Matches
Rational.h
Go to the documentation of this file.
1/* This file is part of gbLAB.
2 *
3 * gbLAB is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef gbLAB_Rational_h_
8#define gbLAB_Rational_h_
9
10#include <Eigen/Core>
11#include <IntegerMath.h>
12
13namespace gbLAB
14{
15 template<typename IntScalarType>
16 struct Rational
17 {
18
19 IntScalarType n; // numerator
20 IntScalarType d; // denominator
21
23 /* init */ n(0),
24 /* init */ d(1)
25 {
26
27
28 }
29
30 Rational(const IntScalarType& n_in) :
31 /* init */ n(n_in),
32 /* init */ d(1)
33 {
34
35
36 }
37
38 Rational(const IntScalarType& n_in, const IntScalarType& d_in) :
39 /* init */ n(IntegerMath<IntScalarType>::sgn(d_in)*n_in/IntegerMath<IntScalarType>::gcd(abs(n_in),abs(d_in))),
40 /* init */ d(IntegerMath<IntScalarType>::sgn(d_in)*d_in/IntegerMath<IntScalarType>::gcd(abs(n_in),abs(d_in)))
41 {
42
43
44 }
45
46 double asDouble() const
47 {
48 return double(n)/double(d);
49 }
50
51 bool operator==(const IntScalarType& other) const
52 {
53 switch (d)
54 {
55 case 0:
56 return false;
57 break;
58
59 case 1:
60 return n==other;
61 break;
62
63 default:
64 return n==0 && other==0;
65 break;
66 }
67 }
68
69 bool operator!=(const IntScalarType& other) const
70 {
71 return !(*this==other);
72 }
73
74 Rational operator*(const Rational& r2) const
75 {
76 return Rational(n*r2.n,d*r2.d);
77 }
78
79 Rational operator/(const Rational& r2) const
80 {
81 return Rational(n*r2.d,d*r2.n);
82 }
83
84 Rational operator+(const Rational& r2) const
85 {
86 return Rational(n*r2.d+r2.n*d,d*r2.d);
87 }
88
89 Rational operator-(const Rational& r2) const
90 {
91 return Rational(n*r2.d-r2.n*d,d*r2.d);
92 }
93
94 Rational operator*(const IntScalarType& i) const
95 {
96 return Rational(n*i,d);
97 }
98
99 Rational operator/(const IntScalarType& i) const
100 {
101 return Rational(n,d*i);
102 }
103
104 Rational operator+(const IntScalarType& i) const
105 {
106 return *this+Rational(i,1);
107 }
108
109 Rational operator-(const IntScalarType& i) const
110 {
111 return *this-Rational(i,1);
112 }
113
114 friend std::ostream& operator << (std::ostream& os, const Rational& r)
115 {
116 os<<r.n<<"/"<<r.d;
117 return os;
118 }
119
120 };
121
122
123
124
125} // end namespace
126
127
128namespace Eigen
129{
130 template<typename IntScalarType> struct NumTraits<gbLAB::Rational<IntScalarType>>
131 : NumTraits<IntScalarType> // permits to get the epsilon, dummy_precision, lowest, highest functions
132 {
133 typedef long int Real;
134 typedef long int NonInteger;
135 typedef long int Nested;
136 enum
137 {
138 IsComplex = 0,
139 IsInteger = 1,
140 IsSigned = 1,
141 RequireInitialization = 1,
142 ReadCost = int(NumTraits<IntScalarType>::ReadCost),
143 AddCost = int(NumTraits<IntScalarType>::AddCost),
144 MulCost = int(NumTraits<IntScalarType>::MulCost)
145 };
146 };
147}
148
149
150#endif
bool operator!=(const IntScalarType &other) const
Definition Rational.h:69
Rational operator-(const Rational &r2) const
Definition Rational.h:89
Rational operator-(const IntScalarType &i) const
Definition Rational.h:109
Rational operator*(const IntScalarType &i) const
Definition Rational.h:94
Rational operator*(const Rational &r2) const
Definition Rational.h:74
Rational operator/(const Rational &r2) const
Definition Rational.h:79
IntScalarType n
Definition Rational.h:19
friend std::ostream & operator<<(std::ostream &os, const Rational &r)
Definition Rational.h:114
bool operator==(const IntScalarType &other) const
Definition Rational.h:51
Rational(const IntScalarType &n_in, const IntScalarType &d_in)
Definition Rational.h:38
Rational operator+(const Rational &r2) const
Definition Rational.h:84
Rational operator/(const IntScalarType &i) const
Definition Rational.h:99
Rational operator+(const IntScalarType &i) const
Definition Rational.h:104
Rational(const IntScalarType &n_in)
Definition Rational.h:30
double asDouble() const
Definition Rational.h:46
IntScalarType d
Definition Rational.h:20