32#ifndef RCSC_MATH_UTIL_H
33#define RCSC_MATH_UTIL_H
41constexpr double EPS = 1.0e-10;
51template <
typename T >
53bound(
const T & low,
const T & x,
const T & high )
55 return std::min( std::max( low, x ), high );
66template <
typename T >
68min_max(
const T & low,
const T & x,
const T & high )
70 return std::min( std::max( low, x ), high );
78template <
typename T >
96 return x > 0.0 ? 1.0 : -1.0;
109 const double & prec )
111 return rint( value / prec ) * prec;
132 double d = b * b - 4.0 * a * c;
135 if ( std::fabs( d ) < 0.001 )
137 if ( sol1 ) *sol1 = -b / ( 2.0 * a );
147 if ( sol1 ) *sol1 = (-b + d) / (2.0 * a);
148 if ( sol2 ) *sol2 = (-b - d) / (2.0 * a);
173 return first_term * ( ( std::pow( r, len ) - 1.0 ) / ( r - 1.0 ) );
192 if ( r < 0.0 || 1.0 <= r )
198 return first_term / ( 1.0 - r );
219 return sum * ( 1.0 - r ) / ( 1.0 - std::pow( r, len ) );
239 return sum * ( 1.0 - r );
260 if ( std::fabs( last_term ) < 0.001 )
262 return sum * ( 1.0 - r );
269 double inverse = 1.0 / r;
270 double tmp = 1.0 + sum * (inverse - 1.0) / last_term;
278 return last_term * std::pow( inverse, std::log( tmp ) / std::log( inverse ) );
297 if ( first_term <=
EPS
318 double tmp = 1.0 + sum * ( r - 1.0 ) / first_term;
323 return std::log( tmp ) / std::log( r );
double calc_sum_inf_geom_series(const double &first_term, const double &r)
the sum of a infinity geometric series
Definition: math_util.h:189
double calc_length_geom_series(const double &first_term, const double &sum, const double &r)
caluculate the length of a geometric series
Definition: math_util.h:293
double calc_first_term_inf_geom_series(const double &sum, const double &r)
caluculate the first term value of a infinity geometric series.
Definition: math_util.h:235
double calc_first_term_geom_series(const double &sum, const double &r, const int len)
caluculate the first term value of a geometric series.
Definition: math_util.h:213
double round(const double &value, const double &prec)
round a floating point number with specified precision
Definition: math_util.h:108
constexpr double EPS
epsilon value
Definition: math_util.h:41
const T & bound(const T &low, const T &x, const T &high)
bound value within the range [low, high]
Definition: math_util.h:53
const T & min_max(const T &low, const T &x, const T &high)
bound value within the range [low, high] (same as rcsc::bound())
Definition: math_util.h:68
double sign(const double &x)
get a sign value
Definition: math_util.h:94
double calc_first_term_geom_series_last(const double &last_term, const double &sum, const double &r)
caluculate the first term value of a geometric series.
Definition: math_util.h:256
T square(const T &x)
calculate squared value.
Definition: math_util.h:80
double calc_sum_geom_series(const double &first_term, const double &r, const int len)
calculate the sum of a geometric series
Definition: math_util.h:165
int quadratic_formula(const double &a, const double &b, const double &c, double *sol1, double *sol2)
Quadratic Formulation (ax^2 + bx + c = 0)
Definition: math_util.h:126