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")
227 DUNE_COMMON_MATH_ISFUNCTION(
isNaN,isnan);
228 DUNE_COMMON_MATH_ISFUNCTION(
isInf,isinf);
229 DUNE_COMMON_MATH_ISFUNCTION(
isFinite,isfinite);
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")
260 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isNaN);
261 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isInf);
262 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isFinite);
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> >
333 auto isNaN(
const T &t, PriorityTag<2>, ADLTag) {
334 return Dune::isNaN(real(t)) || Dune::isNaN(imag(t));
337 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
338 auto isInf(
const T &t, PriorityTag<2>, ADLTag) {
339 return Dune::isInf(real(t)) || Dune::isInf(imag(t));
342 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
343 auto isFinite(
const T &t, PriorityTag<2>, ADLTag) {
344 return Dune::isFinite(real(t)) && Dune::isFinite(imag(t));
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is NaN.
Definition: fvector.hh:458
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is infinite.
Definition: fvector.hh:446
auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether all entries are finite.
Definition: fvector.hh:435
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
Dune namespace.
Definition: alignedallocator.hh:13
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
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
constexpr int sign(const T &val)
Return the sign of the value.
Definition: math.hh:162
static constexpr T factorial(const T &n) noexcept
calculate the factorial of n as a constexpr
Definition: math.hh:93
constexpr K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:146
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:212
Provides commonly used mathematical constants.
Definition: math.hh:67
Helper class for tagging priorities.
Definition: typeutilities.hh:87
Helper class for tagging priorities.
Definition: typeutilities.hh:73
Standard implementation of MathematicalConstants.
Definition: math.hh:34
static const T e()
Euler's number.
Definition: math.hh:38
static const T pi()
Archimedes' constant.
Definition: math.hh:48
Utilities for type computations, constraining overloads, ...