oILAB
Loading...
Searching...
No Matches
RationalApproximations.h
Go to the documentation of this file.
1
//
2
// Created by Nikhil Chandra Admal on 4/20/23.
3
//
4
5
#ifndef OILAB_RATIONALAPPROXIMATIONS_H
6
#define OILAB_RATIONALAPPROXIMATIONS_H
7
8
#include <
IntegerMath.h
>
9
#include <
Rational.h
>
10
#include <
Farey.h
>
11
12
namespace
gbLAB
{
13
template
<
typename
IntScalarType>
14
class
RationalApproximations
15
{
16
public
:
17
double
number
;
18
double
tolerance
;
19
IntScalarType
max_denominator
;
20
std::vector<Rational<IntScalarType>>
approximations
;
21
22
RationalApproximations
(
double
number
,
int
max_denominator
,
double
tolerance
):
23
/* init */
number
(
number
),
24
/* init */
max_denominator
(
max_denominator
),
25
/* init */
tolerance
(
tolerance
)
26
{
27
double
n=floor(
number
);
28
auto
seq =
farey
(
max_denominator
,
false
);
29
for
(
auto
p : seq) {
30
double
approx =
static_cast<
double
>
(p.first) /
static_cast<
double
>
(p.second);
31
if
(std::abs(
number
- n - approx) <=
tolerance
) {
32
p.first= p.first + n*p.second;
33
approximations
.push_back(
Rational<IntScalarType>
(p.first,p.second));
34
}
35
}
36
}
37
38
39
40
41
};
42
43
}
44
#endif
//OILAB_RATIONALAPPROXIMATIONS_H
Farey.h
IntegerMath.h
Rational.h
gbLAB::RationalApproximations
Definition
RationalApproximations.h:15
gbLAB::RationalApproximations::approximations
std::vector< Rational< IntScalarType > > approximations
Definition
RationalApproximations.h:20
gbLAB::RationalApproximations::RationalApproximations
RationalApproximations(double number, int max_denominator, double tolerance)
Definition
RationalApproximations.h:22
gbLAB::RationalApproximations::tolerance
double tolerance
Definition
RationalApproximations.h:18
gbLAB::RationalApproximations::number
double number
Definition
RationalApproximations.h:17
gbLAB::RationalApproximations::max_denominator
IntScalarType max_denominator
Definition
RationalApproximations.h:19
gbLAB
Definition
BiCrystal.cpp:13
gbLAB::farey
std::vector< std::pair< int, int > > farey(int limit, const bool firstQuadrant)
Definition
Farey.cpp:6
gbLAB::Rational
Definition
Rational.h:17
include
Math
RationalApproximations.h
Generated on Wed Aug 6 2025 23:06:44 for oILAB by
1.9.8