|
A library for working with phylogenetic and population genetic data.
v0.27.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_MATH_COMMON_H_
2 #define GENESIS_UTILS_MATH_COMMON_H_
41 #include <type_traits>
55 constexpr
double PI = 3.141592653589793238463;
59 return 2.0 *
PI * radius;
115 if( ! std::isfinite(p) || p < 0.0 || p > 1.0 ) {
116 throw std::invalid_argument(
117 "Cannot compute binomial distribution with p outside of [ 0, 1 ]"
121 return coeff * std::pow( p, k ) * std::pow( 1.0 - p, n - k );
134 template<
typename T >
inline constexpr
137 return ((lhs > rhs) ? (lhs - rhs) : (rhs - lhs));
143 template <
typename T>
inline constexpr
155 template <
typename T>
inline constexpr
161 return (T(0) < x) - (x < T(0));
172 template <
typename T>
inline constexpr
175 return signum( x, std::is_signed<T>() );
184 double max_rel_diff = std::numeric_limits<double>::epsilon()
187 auto diff = std::abs( lhs - rhs );
190 auto largest = std::max( std::abs( lhs ), std::abs( rhs ));
193 return ( diff <= largest * max_rel_diff );
199 inline double round_to(
double x,
size_t accuracy_order )
201 double factor = std::pow( 10, accuracy_order );
202 return std::round( x * factor ) / factor;
215 inline size_t int_pow(
size_t base,
size_t exp )
237 return std::pow( base, exp ) <
static_cast<double>( std::numeric_limits<size_t>::max() );
259 inline constexpr
double cubed(
double x )
275 template <
class ForwardIteratorA,
class ForwardIteratorB>
277 ForwardIteratorA first_a, ForwardIteratorA last_a,
278 ForwardIteratorB first_b, ForwardIteratorB last_b
281 std::vector<double> vec_a;
282 std::vector<double> vec_b;
285 while( first_a != last_a && first_b != last_b ) {
286 if( std::isfinite( *first_a ) && std::isfinite( *first_b ) ) {
287 vec_a.push_back( *first_a );
288 vec_b.push_back( *first_b );
293 if( first_a != last_a || first_b != last_b ) {
294 throw std::runtime_error(
295 "Ranges need to have same length."
299 assert( vec_a.size() == vec_b.size() );
300 return { vec_a, vec_b };
309 template <
class ForwardIteratorA,
class ForwardIteratorB>
311 ForwardIteratorA first_a, ForwardIteratorA last_a,
312 ForwardIteratorB first_b, ForwardIteratorB last_b,
313 std::function<
void(
double value_a,
double value_b )> execute
316 while( first_a != last_a && first_b != last_b ) {
317 if( std::isfinite( *first_a ) && std::isfinite( *first_b ) ) {
318 execute( *first_a, *first_b );
325 if( first_a != last_a || first_b != last_b ) {
326 throw std::runtime_error(
"Ranges need to have same length." );
333 #endif // include guard
double log_factorial(size_t n)
Return the logarithm (base e) of the factorial of n, that is log(n!).
constexpr T abs_diff(T const &lhs, T const &rhs)
Calculate the absolute differenence between two values.
constexpr int signum(T x, std::false_type)
Implementation of signum(T x) for unsigned types. See there for details.
bool almost_equal_relative(double lhs, double rhs, double max_rel_diff=std::numeric_limits< double >::epsilon())
Check whether two doubles are almost equal, using a relative epsilon to compare them.
constexpr double cubed(double x)
Cube of a number.
double circumference(double radius)
size_t binomial_coefficient(size_t n, size_t k)
Compute the binomial coefficient, that is n choose k, for two integer numbers.
size_t int_pow(size_t base, size_t exp)
Calculate the power base^exp for positive integer values.
double binomial_distribution(size_t k, size_t n, double p, bool lenient=false)
Compute the probability mass function for a binomial distribution.
std::pair< std::vector< double >, std::vector< double > > finite_pairs(ForwardIteratorA first_a, ForwardIteratorA last_a, ForwardIteratorB first_b, ForwardIteratorB last_b)
Helper function that cleans two ranges of double of the same length from non-finite values.
constexpr double PI
Make the world go round.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
double binomial_coefficient_approx(size_t n, size_t k, bool lenient)
Compute the binomial coefficient, that is n choose k, for two integer numbers, for large numbers.
bool is_valid_int_pow(size_t base, size_t exp)
Return whether the given power can be stored within a size_t.
void for_each_finite_pair(ForwardIteratorA first_a, ForwardIteratorA last_a, ForwardIteratorB first_b, ForwardIteratorB last_b, std::function< void(double value_a, double value_b)> execute)
Iterate two ranges of double values in parallel, and execute a function for each pair of values from ...
constexpr double squared(double x)
Square of a number.
double round_to(double x, size_t accuracy_order)
Retun the value of x, rounded to the decimal digit given by accuracy_order.