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;
93 enum { factorial = m *
Factorial<m-1>::factorial };
101 enum { factorial = 1 };
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")
242 DUNE_COMMON_MATH_ISFUNCTION(isNaN,isnan);
243 DUNE_COMMON_MATH_ISFUNCTION(isInf,isinf);
244 DUNE_COMMON_MATH_ISFUNCTION(isFinite,isfinite);
245#undef DUNE_COMMON_MATH_ISFUNCTION
249 ->
decltype(isUnordered(t1, t2)) {
250 return isUnordered(t1, t2);
254 auto isUnordered(
const T &t1,
const T &t2,
PriorityTag<0>, ADLTag) {
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")
275 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isNaN);
276 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isInf);
277 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isFinite);
278#undef DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR
280 struct isUnorderedImpl {
282 constexpr auto operator()(
const T &t1,
const T &t2)
const {
283 return isUnordered(t1, t2, PriorityTag<10>{}, MathOverloads::ADLTag{});
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> >
348 auto isNaN(
const T &t, PriorityTag<2>, ADLTag) {
349 return Dune::isNaN(real(t)) || Dune::isNaN(imag(t));
352 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
353 auto isInf(
const T &t, PriorityTag<2>, ADLTag) {
354 return Dune::isInf(real(t)) || Dune::isInf(imag(t));
357 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
358 auto isFinite(
const T &t, PriorityTag<2>, ADLTag) {
359 return Dune::isFinite(real(t)) && Dune::isFinite(imag(t));
Dune namespace.
Definition: alignedallocator.hh:11
static constexpr T binomial(const T &n, const T &k) noexcept
calculate the binomial coefficient n over k as a constexpr
Definition: math.hh:128
constexpr Mantissa power(Mantissa m, Exponent p)
Power method for integer exponents.
Definition: math.hh:73
static constexpr 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
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:161
Calculates the factorial of m at compile time.
Definition: math.hh:91
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:227
Provides commonly used mathematical constants.
Definition: math.hh:65
Helper class for tagging priorities.
Definition: typeutilities.hh:85
Helper class for tagging priorities.
Definition: typeutilities.hh:71
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
Utilities for type computations, constraining overloads, ...