5#ifndef LINE_API_FJ_TYPES_H
6#define LINE_API_FJ_TYPES_H
116inline T num_exp(
const T& v) {
123inline T num_log(
const T& v) {
130inline T num_sqrt(
const T& v) {
137inline T num_pow(
const T& base,
const T& exponent) {
139 return pow(base, exponent);
145 return num_traits<T>::from_double(3.14159265358979323846264338327950288);
155inline T fj_binom(
unsigned n,
unsigned k) {
156 if (k > n)
return num_traits<T>::from_int(0);
157 const unsigned kk = (k > n - k) ? n - k : k;
158 T r = num_traits<T>::from_int(1);
159 for (
unsigned i = 1; i <= kk; ++i) {
160 r *= num_traits<T>::from_int(
static_cast<long>(n - kk + i));
161 r /= num_traits<T>::from_int(
static_cast<long>(i));
175template <
class T,
class F>
176inline T simpson(
const F& f,
const T& a,
const T& b,
unsigned n_intervals = 10000) {
177 if (n_intervals % 2 != 0 || n_intervals == 0)
178 throw InputError(
"simpson: the panel count must be a positive even number");
179 const T h = (b - a) / num_traits<T>::from_int(
static_cast<long>(n_intervals));
181 const T two = num_traits<T>::from_int(2), four = num_traits<T>::from_int(4);
182 for (
unsigned i = 1; i < n_intervals; ++i) {
183 const T x = a + h * num_traits<T>::from_int(
static_cast<long>(i));
184 acc += (i % 2 == 1 ? four : two) * f(x);
186 return acc * h / num_traits<T>::from_int(3);
198template <
class T,
class F>
199inline T bisect(
const F& f, T lo, T hi,
const char* fn,
unsigned max_iter = 200) {
200 const T zero = num_traits<T>::from_int(0);
201 T flo = f(lo), fhi = f(hi);
202 if ((flo > zero && fhi > zero) || (flo < zero && fhi < zero))
203 throw NumericError(std::string(fn) +
": the root is not bracketed by the initial interval");
204 for (
unsigned it = 0; it < max_iter; ++it) {
205 const T mid = (lo + hi) / num_traits<T>::from_int(2);
206 if (mid == lo || mid == hi)
break;
208 if (fm == zero)
return mid;
209 if ((fm > zero) == (flo > zero)) { lo = mid; flo = fm; }
210 else { hi = mid; fhi = fm; }
212 return (lo + hi) / num_traits<T>::from_int(2);
223inline double normal_quantile(
double u) {
224 if (!(u > 0.0) || !(u < 1.0))
225 throw InputError(
"normal_quantile: the probability must lie in (0,1)");
226 double lo = -40.0, hi = 40.0;
227 for (
int it = 0; it < 200; ++it) {
228 const double mid = 0.5 * (lo + hi);
229 if (mid == lo || mid == hi)
break;
231 const double phi = 0.5 * std::erfc(-mid / std::sqrt(2.0));
232 if (phi < u) lo = mid;
else hi = mid;
234 return 0.5 * (lo + hi);
238inline void require_positive_K(
unsigned K,
const char* fn) {
239 if (K < 1)
throw InputError(std::string(fn) +
": K must be a positive integer");
NumericError(const std::string &what)
The exception types the port throws.
FJNormalMethod
Bracketing methods for the normal-maximum approximation.
FJDistType
Distribution families for which a G(K) standardized-maximum factor exists.
Number-type abstraction for the templated API port.
Box constraint on one variable.
[Rmax, Rmin] of fj_bounds: pessimistic and optimistic response-time bounds.
[MK, mK] of fj_char_max: characteristic maximum and its threshold.
All four G(K) factors of fj_gk_bound in its 'all' mode.
[F_Yk, E_Yk] of fj_order_stat.
[m, v] of fj_quorum_moments: mean and variance of the k-of-n join time.
[Xmax, GK] of fj_xmax_approx.
[Xmax, Vmax] of fj_xmax_normal.
[Xmax, MK] of fj_xmax_pareto.