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 oILAB {
14template <typename IntScalarType> struct Rational {
15
16 IntScalarType n; // numerator
17 IntScalarType d; // denominator
18
20 : /* init */ n(0),
21 /* init */ d(1) {}
22
23 Rational(const IntScalarType &n_in)
24 : /* init */ n(n_in),
25 /* init */ d(1) {}
26
27 Rational(const IntScalarType &n_in, const IntScalarType &d_in)
28 : /* init */ n(IntegerMath<IntScalarType>::sgn(d_in) * n_in /
29 IntegerMath<IntScalarType>::gcd(abs(n_in), abs(d_in))),
30 /* init */ d(IntegerMath<IntScalarType>::sgn(d_in) * d_in /
31 IntegerMath<IntScalarType>::gcd(abs(n_in), abs(d_in))) {}
32
33 double asDouble() const { return double(n) / double(d); }
34
35 bool operator==(const IntScalarType &other) const {
36 switch (d) {
37 case 0:
38 return false;
39 break;
40
41 case 1:
42 return n == other;
43 break;
44
45 default:
46 return n == 0 && other == 0;
47 break;
48 }
49 }
50
51 bool operator!=(const IntScalarType &other) const {
52 return !(*this == other);
53 }
54
55 Rational operator*(const Rational &r2) const {
56 return Rational(n * r2.n, d * r2.d);
57 }
58
59 Rational operator/(const Rational &r2) const {
60 return Rational(n * r2.d, d * r2.n);
61 }
62
63 Rational operator+(const Rational &r2) const {
64 return Rational(n * r2.d + r2.n * d, d * r2.d);
65 }
66
67 Rational operator-(const Rational &r2) const {
68 return Rational(n * r2.d - r2.n * d, d * r2.d);
69 }
70
71 Rational operator*(const IntScalarType &i) const {
72 return Rational(n * i, d);
73 }
74
75 Rational operator/(const IntScalarType &i) const {
76 return Rational(n, d * i);
77 }
78
79 Rational operator+(const IntScalarType &i) const {
80 return *this + Rational(i, 1);
81 }
82
83 Rational operator-(const IntScalarType &i) const {
84 return *this - Rational(i, 1);
85 }
86
87 friend std::ostream &operator<<(std::ostream &os, const Rational &r) {
88 os << r.n << "/" << r.d;
89 return os;
90 }
91};
92
93} // namespace oILAB
94
95namespace Eigen
96{
97template <typename IntScalarType>
98struct NumTraits<oILAB::Rational<IntScalarType>>
99 : NumTraits<IntScalarType> // permits to get the epsilon, dummy_precision,
100 // lowest, highest functions
101{
102 typedef long int Real;
103 typedef long int NonInteger;
104 typedef long int Nested;
105 enum {
106 IsComplex = 0,
107 IsInteger = 1,
108 IsSigned = 1,
109 RequireInitialization = 1,
110 ReadCost = int(NumTraits<IntScalarType>::ReadCost),
111 AddCost = int(NumTraits<IntScalarType>::AddCost),
112 MulCost = int(NumTraits<IntScalarType>::MulCost)
113 };
114};
115}
116
117
118#endif
IntScalarType d
Definition Rational.h:17
Rational(const IntScalarType &n_in, const IntScalarType &d_in)
Definition Rational.h:27
Rational operator-(const IntScalarType &i) const
Definition Rational.h:83
Rational operator/(const Rational &r2) const
Definition Rational.h:59
Rational operator*(const IntScalarType &i) const
Definition Rational.h:71
Rational operator*(const Rational &r2) const
Definition Rational.h:55
IntScalarType n
Definition Rational.h:16
friend std::ostream & operator<<(std::ostream &os, const Rational &r)
Definition Rational.h:87
bool operator==(const IntScalarType &other) const
Definition Rational.h:35
Rational operator+(const Rational &r2) const
Definition Rational.h:63
Rational(const IntScalarType &n_in)
Definition Rational.h:23
Rational operator-(const Rational &r2) const
Definition Rational.h:67
Rational operator/(const IntScalarType &i) const
Definition Rational.h:75
Rational operator+(const IntScalarType &i) const
Definition Rational.h:79
bool operator!=(const IntScalarType &other) const
Definition Rational.h:51
double asDouble() const
Definition Rational.h:33