Class template used to approximate decimal numbers by rationals.
More...
#include <agrum/core/math/rational.h>
|
|
static void | farey (int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000L, const GUM_SCALAR &zero=1e-6) |
| Find the rational close enough to a given ( decimal ) number in [-1,1] and whose denominator is not higher than a given integer number. More...
|
|
static void | continuedFracFirst (int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const double &zero=1e-6) |
| Find the first best rational approximation. More...
|
|
static void | continuedFracBest (int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000) |
| Find the best rational approximation. More...
|
|
template<typename GUM_SCALAR>
class gum::Rational< GUM_SCALAR >
Class template used to approximate decimal numbers by rationals.
- Template Parameters
-
GUM_SCALAR | The floating type ( float, double, long double ... ) of the number. |
Definition at line 57 of file rational.h.
◆ continuedFracBest()
template<typename GUM_SCALAR >
void gum::Rational< GUM_SCALAR >::continuedFracBest |
( |
int64_t & |
numerator, |
|
|
int64_t & |
denominator, |
|
|
const GUM_SCALAR & |
number, |
|
|
const int64_t & |
den_max = 1000000 |
|
) |
| |
|
static |
Find the best rational approximation.
Not the first, to a given ( decimal) number ( ANY number ) and whose denominator is not higher than a given integer number.
In this case, we look for semi-convergents at the right of the last admissible convergent, if any. They are better approximations, but have higher denominators.
- Parameters
-
numerator | The numerator of the rational. |
denominator | The denominator of the rational. |
number | The constant number we want to approximate using rationals. |
den_max | The constant highest authorized denominator. 1000000 by default. |
Definition at line 190 of file rational_tpl.h.
194 const GUM_SCALAR pnumber = (number > 0) ? number : -number;
196 const uint64_t denMax =
200 GUM_SCALAR rnumber = pnumber;
203 std::vector< uint64_t > p({0, 1});
204 std::vector< uint64_t > q({1, 0});
207 std::vector< uint64_t > a;
209 uint64_t p_tmp, q_tmp;
212 double delta, delta_tmp;
216 a.push_back(std::lrint(std::floor(rnumber)));
218 p_tmp = a.back() * p.back() + p[p.size() - 2];
219 q_tmp = a.back() * q.back() + q[q.size() - 2];
221 if (q_tmp > denMax || p_tmp > denMax)
break;
226 if (std::fabs(rnumber - a.back()) < 1e-6)
break;
228 rnumber = GUM_SCALAR(1.) / (rnumber - a.back());
231 if (a.size() < 2 || q.back() == denMax || p.back() == denMax) {
232 numerator = p.back();
233 if (number < 0) numerator = -numerator;
234 denominator = q.back();
241 Idx i =
Idx(p.size() - 1);
246 for (n = a[i - 1] - 1; n >= (a[i - 1] + 2) / 2; --n) {
247 p_tmp = n * p[i] + p[i - 1];
248 q_tmp = n * q[i] + q[i - 1];
250 if (q_tmp > denMax || p_tmp > denMax)
continue;
252 numerator = (int64_t)p_tmp;
253 if (number < 0) numerator = -numerator;
260 p_tmp = n * p[i] + p[i - 1];
261 q_tmp = n * q[i] + q[i - 1];
263 delta_tmp = std::fabs(pnumber - ((
double)p_tmp) / q_tmp);
264 delta = std::fabs(pnumber - ((
double)p[i]) / q[i]);
266 if (delta_tmp < delta && q_tmp <= denMax && p_tmp <= denMax) {
267 numerator = (int64_t)p_tmp;
268 if (number < 0) numerator = -numerator;
271 numerator = (int64_t)p[i];
272 if (number < 0) numerator = -numerator;
Size Idx
Type for indexes.
◆ continuedFracFirst()
template<typename GUM_SCALAR >
void gum::Rational< GUM_SCALAR >::continuedFracFirst |
( |
int64_t & |
numerator, |
|
|
int64_t & |
denominator, |
|
|
const GUM_SCALAR & |
number, |
|
|
const double & |
zero = 1e-6 |
|
) |
| |
|
static |
Find the first best rational approximation.
end of farey func
The one with the smallest denominator such that no other rational with smaller denominator is a better approx, within precision zero to a given ( decimal ) number ( ANY number).
It gives the same answer than farey assuming zero
is the same and den_max is infinite. Use this functions because you are sure to get an approx within zero
of number
.
We look at the semi-convergents left of the last admissible convergent, if any. They may be within the same precision and have a smaller denominator.
- Parameters
-
numerator | The numerator of the rational. |
denominator | The denominator of the rational. |
number | The constant number we want to approximate using rationals. |
zero | The positive value below which a number is considered zero. 1e-6 by default. |
Definition at line 93 of file rational_tpl.h.
Referenced by gum::credal::LRSWrapper< GUM_SCALAR >::__fill().
97 const GUM_SCALAR pnumber = (number > 0) ? number : -number;
100 GUM_SCALAR rnumber = pnumber;
103 std::vector< uint64_t > p({0, 1});
104 std::vector< uint64_t > q({1, 0});
107 std::vector< uint64_t > a;
109 uint64_t p_tmp, q_tmp;
112 double delta, delta_tmp;
120 a.push_back(std::lrint(std::floor(rnumber)));
121 p.push_back(a.back() * p.back() + p[p.size() - 2]);
122 q.push_back(a.back() * q.back() + q[q.size() - 2]);
124 delta = std::fabs(pnumber - (GUM_SCALAR)p.back() / q.back());
127 numerator = (int64_t)p.back();
128 if (number < 0) numerator = -numerator;
129 denominator = q.back();
133 if (std::abs(rnumber - a.back()) < 1e-6)
break;
135 rnumber = GUM_SCALAR(1.) / (rnumber - a.back());
138 if (a.size() < 2)
return;
143 Idx i =
Idx(p.size() - 2);
149 p_tmp = n * p[i] + p[i - 1];
150 q_tmp = n * q[i] + q[i - 1];
152 delta = std::fabs(pnumber - ((
double)p[i]) / q[i]);
153 delta_tmp = std::fabs(pnumber - ((
double)p_tmp) / q_tmp);
156 numerator = (int64_t)p[i];
157 if (number < 0) numerator = -numerator;
162 if (delta_tmp < zero) {
163 numerator = (int64_t)p_tmp;
164 if (number < 0) numerator = -numerator;
172 for (n = (a[i - 1] + 2) / 2; n < a[i - 1]; ++n) {
173 p_tmp = n * p[i] + p[i - 1];
174 q_tmp = n * q[i] + q[i - 1];
176 delta_tmp = std::fabs(pnumber - ((
double)p_tmp) / q_tmp);
178 if (delta_tmp < zero) {
179 numerator = (int64_t)p_tmp;
180 if (number < 0) numerator = -numerator;
Size Idx
Type for indexes.
◆ farey()
template<typename GUM_SCALAR >
void gum::Rational< GUM_SCALAR >::farey |
( |
int64_t & |
numerator, |
|
|
int64_t & |
denominator, |
|
|
const GUM_SCALAR & |
number, |
|
|
const int64_t & |
den_max = 1000000L , |
|
|
const GUM_SCALAR & |
zero = 1e-6 |
|
) |
| |
|
static |
Find the rational close enough to a given ( decimal ) number in [-1,1] and whose denominator is not higher than a given integer number.
Because of the double constraint on precision and size of the denominator, there is no guarantee on the precision of the approximation if den_max
is low and zero
is high. Prefer the use of continued fractions.
- Parameters
-
numerator | The numerator of the rational. |
denominator | The denominator of the rational. |
number | The constant number we want to approximate using rationals. |
den_max | The constant highest authorized denominator. 1000000 by default. |
zero | The positive value below which a number is considered zero. 1e-6 by default. |
Definition at line 34 of file rational_tpl.h.
Referenced by gum::credal::CredalNet< GUM_SCALAR >::__H2Vlrs().
39 bool isNegative = (number < 0) ?
true :
false;
40 GUM_SCALAR pnumber = (isNegative) ? -number : number;
42 if (std::abs(pnumber - GUM_SCALAR(1.)) < zero) {
43 numerator = (isNegative) ? -1 : 1;
46 }
else if (pnumber < zero) {
52 int64_t a(0), b(1), c(1), d(1);
55 while (b <= den_max && d <= den_max) {
56 mediant = (GUM_SCALAR)(a + c) / (GUM_SCALAR)(b + d);
58 if (std::fabs(pnumber - mediant) < zero) {
59 if (b + d <= den_max) {
60 numerator = (isNegative) ? -(a + c) : (a + c);
64 numerator = (isNegative) ? -c : c;
68 numerator = (isNegative) ? -a : a;
72 }
else if (pnumber > mediant) {
82 numerator = (isNegative) ? -c : c;
86 numerator = (isNegative) ? -a : a;
The documentation for this class was generated from the following files: