-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathnumeric.h
186 lines (168 loc) · 4.6 KB
/
numeric.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef MIR_NUMERIC
#define MIR_NUMERIC
#include <functional>
#include <limits>
enum class mir_find_root_status
{
/// Success
success,
///
badBounds,
///
nanX,
///
nanY,
};
template<class T>
struct mir_find_root_result
{
/// Left bound
T ax = 0;
/// Rifht bound
T bx = 0;
/// `f(ax)` or `f(axfabs()).fmin(T.max / 2).copysign(f(ax))`.
T ay = 0;
/// `f(bx)` or `f(bxfabs()).fmin(T.max / 2).copysign(f(bx))`.
T by = 0;
/// Amount of target function calls.
unsigned int iterations = 0;
/**
Returns: self
Required_versions:`D_Exceptions`
Throws: `Exception` if $(LREF FindRootResult.status) isn't $(LREF mir_find_root_status.success).
*/
const mir_find_root_result& validate() const
{
switch(status())
{
case mir_find_root_status::success: return *this;
case mir_find_root_status::badBounds: throw std::domain_error("findRoot: f(ax) and f(bx) must have opposite signs to bracket the root.");
case mir_find_root_status::nanX: throw std::domain_error("findRoot: ax or bx is NaN.");
case mir_find_root_status::nanY: throw std::domain_error("findRoot: f(x) returned NaN.");
default: throw std::domain_error("findRoot: unknown error.");
}
}
/**
Returns: $(LREF mir_find_root_status)
*/
mir_find_root_status status() const noexcept;
/**
A bound that corresponds to the minimal absolute function value.
Returns: `!(fabs(ay) > fabs(by)) ? ax : bx`
*/
T x() const noexcept;
/**
The minimal of absolute function values.
Returns: `!(fabs(ay) > fabs(by)) ? ay : by`
*/
T y() const noexcept;
};
template<class T>
T mir_internal_find_root_f(const void* ctx, T x)
{
return (*((const std::function<T(T)>*)ctx))(x);
}
template<class T>
bool mir_internal_find_root_tolerance(const void* ctx, T a, T b)
{
return (*((const std::function<bool(T, T)>*)ctx))(a, b);
}
mir_find_root_result<float> mir_find_root(
float ax,
float bx,
float fax,
float fbx,
float lower_bound,
float upper_bound,
unsigned int maxIterations,
unsigned int steps,
float (*f)(const void* ctx, float x),
const void* f_ctx = NULL,
bool (*tolerance)(const void* ctx, float a, float b) = NULL,
const void* tolerance_ctx = NULL
);
mir_find_root_result<double> mir_find_root(
double ax,
double bx,
double fax,
double fbx,
double lower_bound,
double upper_bound,
unsigned int maxIterations,
unsigned int steps,
double (*f)(const void* ctx, double x),
const void* f_ctx = NULL,
bool (*tolerance)(const void* ctx, double a, double b) = NULL,
const void* tolerance_ctx = NULL
);
mir_find_root_result<long double> mir_find_root(
long double ax,
long double bx,
long double fax,
long double fbx,
long double lower_bound,
long double upper_bound,
unsigned int maxIterations,
unsigned int steps,
long double (*f)(const void* ctx, long double x),
const void* f_ctx = NULL,
bool (*tolerance)(const void* ctx, long double a, long double b) = NULL,
const void* tolerance_ctx = NULL
);
template<class T>
mir_find_root_result<T> mir_find_root(
const std::function<T(T)>& f,
const std::function<bool(T, T)>& tolerance,
const T a,
const T b,
const T fa = std::numeric_limits<T>::quiet_NaN(),
const T fb = std::numeric_limits<T>::quiet_NaN(),
const T lower_bound = std::numeric_limits<T>::quiet_NaN(),
const T upper_bound = std::numeric_limits<T>::quiet_NaN(),
unsigned int maxIterations = sizeof(T) * 16,
unsigned int steps = 0
)
{
return mir_find_root(
a,
b,
fa,
fb,
lower_bound,
upper_bound,
maxIterations,
steps,
&mir_internal_find_root_f<T>,
&f,
&mir_internal_find_root_tolerance<T>,
&tolerance
);
}
template<class T>
mir_find_root_result<T> mir_find_root(
const std::function<T(T)>& f,
const T absoluteTolerance,
const T a,
const T b,
const T fa = std::numeric_limits<T>::quiet_NaN(),
const T fb = std::numeric_limits<T>::quiet_NaN(),
const T lower_bound = std::numeric_limits<T>::quiet_NaN(),
const T upper_bound = std::numeric_limits<T>::quiet_NaN(),
unsigned int maxIterations = sizeof(T) * 16,
unsigned int steps = 0
)
{
return mir_find_root<T>(
f,
[absoluteTolerance](T a, T b) { return b - a < absoluteTolerance; },
a,
b,
fa,
fb,
lower_bound,
upper_bound,
maxIterations,
steps
);
}
#endif