13 #include <type_traits>
39 static const T
e = exp( T( 1 ) );
49 static const T
pi = acos( T( -1 ) );
62 template<
class Field >
72 template <
class Mantissa,
class Exponent>
73 constexpr Mantissa
power(Mantissa m, Exponent p)
75 static_assert(std::numeric_limits<Exponent>::is_integer,
"Exponent must be an integer type!");
77 auto result = Mantissa(1);
78 auto absp = (p<0) ? -p : p;
79 for (Exponent i = Exponent(0); i<absp; i++)
83 result = Mantissa(1)/result;
108 constexpr
inline static T
factorial(
const T& n) noexcept
110 static_assert(std::numeric_limits<T>::is_integer,
"`factorial(n)` has to be called with an integer type.");
112 for(T k = 0; k < n; ++k)
118 template<
class T, T n>
119 constexpr
inline static auto factorial (std::integral_constant<T, n>) noexcept
121 return std::integral_constant<T,
factorial(n)>{};
128 constexpr
inline static T
binomial (
const T& n,
const T& k) noexcept
130 static_assert(std::numeric_limits<T>::is_integer,
"`binomial(n, k)` has to be called with an integer type.");
139 for(
auto i = n-k; i < n; ++i)
145 template<
class T, T n, T k>
146 constexpr
inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, k>) noexcept
148 return std::integral_constant<T,
binomial(n, k)>{};
151 template<
class T, T n>
152 constexpr
inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, n>) noexcept
154 return std::integral_constant<T, (n >= 0 ? 1 : 0)>{};
171 return std::complex<K>(c.real(),-c.imag());
179 return (val < 0 ? -1 : 1);
187 struct isComplexLike {
190 static auto test(U* u) -> decltype(u->real(), u->imag(), std::true_type());
193 static auto test(...) -> decltype(std::false_type());
196 static const bool value = decltype(test<T>(0))::value;
224 namespace MathOverloads {
229 #define DUNE_COMMON_MATH_ISFUNCTION(function, stdfunction) \
231 auto function(const T &t, PriorityTag<1>, ADLTag) \
232 -> decltype(function(t)) { \
233 return function(t); \
236 auto function(const T &t, PriorityTag<0>, ADLTag) { \
237 using std::stdfunction; \
238 return stdfunction(t); \
240 static_assert(true, "Require semicolon to unconfuse editors")
245 #undef DUNE_COMMON_MATH_ISFUNCTION
255 using std::isunordered;
256 return isunordered(t1, t2);
266 #define DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(function) \
267 struct function##Impl { \
269 constexpr auto operator()(const T &t) const { \
270 return function(t, PriorityTag<10>{}, MathOverloads::ADLTag{}); \
273 static_assert(true, "Require semicolon to unconfuse editors")
278 #undef DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR
300 static constexpr T value{};
304 constexpr T MathDummy<T>::value;
320 constexpr
auto const &
isNaN = Impl::MathDummy<MathImpl::isNaNImpl>::value;
327 constexpr
auto const &
isInf = Impl::MathDummy<MathImpl::isInfImpl>::value;
334 constexpr
auto const &
isFinite = Impl::MathDummy<MathImpl::isFiniteImpl>::value;
342 constexpr
auto const &
isUnordered = Impl::MathDummy<MathImpl::isUnorderedImpl>::value;
345 namespace MathOverloads {
347 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
352 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
357 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
Utilities for type computations, constraining overloads, ...
Dune namespace.
Definition: alignedallocator.hh:14
constexpr Mantissa power(Mantissa m, Exponent p)
Power method for integer exponents.
Definition: math.hh:73
constexpr static T factorial(const T &n) noexcept
calculate the factorial of n as a constexpr
Definition: math.hh:108
int sign(const T &val)
Return the sign of the value.
Definition: math.hh:177
constexpr static T binomial(const T &n, const T &k) noexcept
calculate the binomial coefficient n over k as a constexpr
Definition: math.hh:128
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:161
auto isNaN(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:348
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Definition: fvector.hh:613
DUNE_COMMON_MATH_ISFUNCTION(isNaN, isnan)
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Definition: fvector.hh:603
auto isUnordered(const T &t1, const T &t2, PriorityTag< 0 >, ADLTag)
Definition: math.hh:254
auto isInf(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:353
auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Definition: fvector.hh:593
bool isUnordered(const FieldVector< K, 1 > &b, const FieldVector< K, 1 > &c, PriorityTag< 2 >, ADLTag)
Definition: fvector.hh:623
auto isFinite(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:358
DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isNaN)
Standard implementation of MathematicalConstants.
Definition: math.hh:32
static const T e()
Euler's number.
Definition: math.hh:36
static const T pi()
Archimedes' constant.
Definition: math.hh:46
Provides commonly used mathematical constants.
Definition: math.hh:65
Calculates the factorial of m at compile time.
Definition: math.hh:91
@ factorial
Definition: math.hh:93
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:227
constexpr auto operator()(const T &t1, const T &t2) const
Definition: math.hh:282
Helper class for tagging priorities.
Definition: typeutilities.hh:71
Helper class for tagging priorities.
Definition: typeutilities.hh:85