5 #ifndef COTILA_SCALAR_MATH_H_ 6 #define COTILA_SCALAR_MATH_H_ 8 #include <cotila/detail/type_traits.h> 9 #include <cotila/detail/assert.h> 10 #include <type_traits> 24 constexpr
double sqrt(
double x) {
26 throw "sqrt argument must be positive";
28 double est = (1 + x) / 2;
31 est = (est + x / est) / 2;
42 constexpr
float sqrt(
float x) {
return float(
sqrt(
double(x))); }
50 template <
typename T> constexpr detail::remove_complex_t<T>
abs(T x) {
51 COTILA_DETAIL_ASSERT_ARITHMETIC(T);
52 if constexpr (detail::is_complex_v<T>)
53 return sqrt(x.real() * x.real() + x.imag() * x.imag());
55 return x > 0 ? x : -x;
92 constexpr
double nthroot(
double x,
int n) {
94 throw "nth root argument must be positive";
99 double dxk = 1. / n * (x /
exponentiate(prev, n - 1) - prev);
111 template <
typename T> constexpr T
conj(T x) {
112 COTILA_DETAIL_ASSERT_ARITHMETIC(T);
113 if constexpr (detail::is_complex_v<T>)
114 return {x.real(), -x.imag()};
123 #endif // COTILA_SCALAR_MATH_H_ constexpr vector< detail::remove_complex_t< T >, N > abs(const vector< T, N > &v)
computes the elementwise absolute value
Definition: math.h:73
constexpr double nthroot(double x, int n)
computes the th root
Definition: math.h:92
constexpr double exponentiate(double x, int n)
computes exponents
Definition: math.h:65
constexpr vector< T, N > sqrt(const vector< T, N > &v)
computes the elementwise square root
Definition: math.h:38
constexpr vector< T, N > conj(const vector< T, N > &v)
computes the elementwise complex conjugate
Definition: math.h:27