|
A library for working with phylogenetic and population genetic data.
v0.32.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;
69 template <
typename T>
inline
80 template <
typename T>
inline
94 template<
typename T >
inline constexpr
97 return ((lhs > rhs) ? (lhs - rhs) : (rhs - lhs));
103 template <
typename T>
inline constexpr
115 template <
typename T>
inline constexpr
121 return (T(0) < x) - (x < T(0));
132 template <
typename T>
inline constexpr
135 return signum( x, std::is_signed<T>() );
144 template <
typename T,
typename U>
149 static_assert(
static_cast<int>(
false ) == 0,
"static_cast<int>( false ) != 0" );
150 static_assert(
static_cast<int>(
true ) == 1,
"static_cast<int>( true ) != 1" );
151 return static_cast<int>( lhs > rhs ) -
static_cast<int>( lhs < rhs );
160 double max_rel_diff = std::numeric_limits<double>::epsilon()
163 auto diff = std::abs( lhs - rhs );
166 auto largest = std::max( std::abs( lhs ), std::abs( rhs ));
169 return ( diff <= largest * max_rel_diff );
175 inline double round_to(
double x,
size_t accuracy_order )
177 double factor = std::pow( 10, accuracy_order );
178 return std::round( x * factor ) / factor;
191 inline size_t int_pow(
size_t base,
size_t exp )
213 return std::pow( base, exp ) <
static_cast<double>( std::numeric_limits<size_t>::max() );
235 inline constexpr
double cubed(
double x )
251 template <
class ForwardIteratorA,
class ForwardIteratorB>
253 ForwardIteratorA first_a, ForwardIteratorA last_a,
254 ForwardIteratorB first_b, ForwardIteratorB last_b
257 std::vector<double> vec_a;
258 std::vector<double> vec_b;
261 while( first_a != last_a && first_b != last_b ) {
262 if( std::isfinite( *first_a ) && std::isfinite( *first_b ) ) {
263 vec_a.push_back( *first_a );
264 vec_b.push_back( *first_b );
269 if( first_a != last_a || first_b != last_b ) {
270 throw std::runtime_error(
271 "Ranges need to have same length."
275 assert( vec_a.size() == vec_b.size() );
276 return { vec_a, vec_b };
285 template <
class ForwardIteratorA,
class ForwardIteratorB>
287 ForwardIteratorA first_a, ForwardIteratorA last_a,
288 ForwardIteratorB first_b, ForwardIteratorB last_b,
289 std::function<
void(
double value_a,
double value_b )> execute
292 while( first_a != last_a && first_b != last_b ) {
293 if( std::isfinite( *first_a ) && std::isfinite( *first_b ) ) {
294 execute( *first_a, *first_b );
301 if( first_a != last_a || first_b != last_b ) {
302 throw std::runtime_error(
"Ranges need to have same length." );
309 #endif // include guard
void swap(Sample &lhs, Sample &rhs)
constexpr int compare_threeway(T lhs, U rhs)
Three-way comparison (spaceship operator) for C++ <20.
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 int_pow(size_t base, size_t exp)
Calculate the power base^exp for positive integer values.
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.
void descending(T &f, T &s)
Sort two values in descending order, inplace.
void ascending(T &f, T &s)
Sort two values in ascending order, inplace.
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.