15 #include <type_traits> 41 static const T
e = exp( T( 1 ) );
51 static const T
pi = acos( T( -1 ) );
64 template<
class Field >
74 template <
class Base,
class Exponent>
75 constexpr Base
power(Base m, Exponent p)
77 static_assert(std::numeric_limits<Exponent>::is_integer,
"Exponent must be an integer type!");
79 auto result = Base(1);
80 auto absp = (p<0) ? -p : p;
81 for (Exponent
i = Exponent(0);
i<absp;
i++)
85 result = Base(1)/result;
93 constexpr
inline static T
factorial(
const T& n) noexcept
95 static_assert(std::numeric_limits<T>::is_integer,
"`factorial(n)` has to be called with an integer type.");
97 for(T k = 0; k < n; ++k)
103 template<
class T, T n>
104 constexpr
inline static auto factorial (std::integral_constant<T, n>) noexcept
106 return std::integral_constant<T, factorial(n)>{};
113 constexpr
inline static T
binomial (
const T& n,
const T& k) noexcept
115 static_assert(std::numeric_limits<T>::is_integer,
"`binomial(n, k)` has to be called with an integer type.");
124 for(
auto i = n-k;
i < n; ++
i)
130 template<
class T, T n, T k>
131 constexpr
inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, k>) noexcept
133 return std::integral_constant<T, binomial(n, k)>{};
136 template<
class T, T n>
137 constexpr
inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, n>) noexcept
139 return std::integral_constant<T, (n >= 0 ? 1 : 0)>{};
156 return std::complex<K>(c.real(),-c.imag());
162 constexpr
int sign(
const T& val)
164 return (val < 0 ? -1 : 1);
172 struct isComplexLike {
175 static auto test(U* u) -> decltype(u->real(), u->imag(), std::true_type());
178 static auto test(...) -> decltype(std::false_type());
181 static const bool value = decltype(test<T>(0))::value;
209 namespace MathOverloads {
214 #define DUNE_COMMON_MATH_ISFUNCTION(function, stdfunction) \ 216 auto function(const T &t, PriorityTag<1>, ADLTag) \ 217 -> decltype(function(t)) { \ 218 return function(t); \ 221 auto function(const T &t, PriorityTag<0>, ADLTag) { \ 222 using std::stdfunction; \ 223 return stdfunction(t); \ 225 static_assert(true, "Require semicolon to unconfuse editors") 230 #undef DUNE_COMMON_MATH_ISFUNCTION 240 using std::isunordered;
241 return isunordered(t1, t2);
251 #define DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(function) \ 252 struct function##Impl { \ 254 constexpr auto operator()(const T &t) const { \ 255 return function(t, PriorityTag<10>{}, MathOverloads::ADLTag{}); \ 258 static_assert(true, "Require semicolon to unconfuse editors") 263 #undef DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR 265 struct isUnorderedImpl {
267 constexpr
auto operator()(
const T &t1,
const T &t2)
const {
268 return isUnordered(t1, t2, PriorityTag<10>{}, MathOverloads::ADLTag{});
285 static constexpr T value{};
289 constexpr T MathDummy<T>::value;
305 constexpr
auto const &
isNaN = Impl::MathDummy<MathImpl::isNaNImpl>::value;
312 constexpr
auto const &
isInf = Impl::MathDummy<MathImpl::isInfImpl>::value;
319 constexpr
auto const &
isFinite = Impl::MathDummy<MathImpl::isFiniteImpl>::value;
327 constexpr
auto const &
isUnordered = Impl::MathDummy<MathImpl::isUnorderedImpl>::value;
330 namespace MathOverloads {
332 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
337 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
342 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
349 #endif // #ifndef DUNE_MATH_HH auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether all entries are finite.
Definition: fvector.hh:435
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is NaN.
Definition: fvector.hh:458
bool isUnordered(const FieldVector< K, 1 > &b, const FieldVector< K, 1 > &c, PriorityTag< 2 >, ADLTag)
Returns true if either b or c is NaN.
Definition: fvector.hh:470
constexpr K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:146
auto isNaN(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:333
auto isInf(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:338
static const T pi()
Archimedes' constant.
Definition: math.hh:48
auto isUnordered(const T &t1, const T &t2, PriorityTag< 0 >, ADLTag)
Definition: math.hh:239
static const T e()
Euler's number.
Definition: math.hh:38
I i
Definition: hybridmultiindex.hh:328
auto isFinite(const T &t, PriorityTag< 2 >, ADLTag)
Definition: math.hh:343
constexpr int sign(const T &val)
Return the sign of the value.
Definition: math.hh:162
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:212
Dune namespace
Definition: alignedallocator.hh:12
Utilities for type computations, constraining overloads, ...
DUNE_COMMON_MATH_ISFUNCTION(isNaN, isnan)
Helper class for tagging priorities.
Definition: typeutilities.hh:86
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is infinite.
Definition: fvector.hh:446
Helper class for tagging priorities.
Definition: typeutilities.hh:72
static constexpr T factorial(const T &n) noexcept
calculate the factorial of n as a constexpr
Definition: math.hh:93
#define DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(function)
Definition: math.hh:251
Provides commonly used mathematical constants.
Definition: math.hh:65
static constexpr T binomial(const T &n, const T &k) noexcept
calculate the binomial coefficient n over k as a constexpr
Definition: math.hh:113
Standard implementation of MathematicalConstants.
Definition: math.hh:33