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;
111 constexpr inline static T
factorial(
const T& n)
noexcept
113 static_assert(std::numeric_limits<T>::is_integer,
"`factorial(n)` has to be called with an integer type.");
115 for(T k = 0; k < n; ++k)
121 template<
class T, T n>
122 constexpr inline static auto factorial (std::integral_constant<T, n>)
noexcept
124 return std::integral_constant<T,
factorial(n)>{};
131 constexpr inline static T
binomial (
const T& n,
const T& k)
noexcept
133 static_assert(std::numeric_limits<T>::is_integer,
"`binomial(n, k)` has to be called with an integer type.");
142 for(
auto i = n-k; i < n; ++i)
148 template<
class T, T n, T k>
149 constexpr inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, k>)
noexcept
151 return std::integral_constant<T,
binomial(n, k)>{};
154 template<
class T, T n>
155 constexpr inline static auto binomial (std::integral_constant<T, n>, std::integral_constant<T, n>)
noexcept
157 return std::integral_constant<T, (n >= 0 ? 1 : 0)>{};
174 return std::complex<K>(c.real(),-c.imag());
182 return (val < 0 ? -1 : 1);
190 struct isComplexLike {
193 static auto test(U* u) ->
decltype(u->real(), u->imag(), std::true_type());
196 static auto test(...) ->
decltype(std::false_type());
199 static const bool value =
decltype(test<T>(0))::value;
227 namespace MathOverloads {
232#define DUNE_COMMON_MATH_ISFUNCTION(function, stdfunction) \
234 auto function(const T &t, PriorityTag<1>, ADLTag) \
235 -> decltype(function(t)) { \
236 return function(t); \
239 auto function(const T &t, PriorityTag<0>, ADLTag) { \
240 using std::stdfunction; \
241 return stdfunction(t); \
243 static_assert(true, "Require semicolon to unconfuse editors")
245 DUNE_COMMON_MATH_ISFUNCTION(isNaN,isnan);
246 DUNE_COMMON_MATH_ISFUNCTION(isInf,isinf);
247 DUNE_COMMON_MATH_ISFUNCTION(isFinite,isfinite);
248#undef DUNE_COMMON_MATH_ISFUNCTION
252 ->
decltype(isUnordered(t1, t2)) {
253 return isUnordered(t1, t2);
257 auto isUnordered(
const T &t1,
const T &t2,
PriorityTag<0>, ADLTag) {
258 using std::isunordered;
259 return isunordered(t1, t2);
269#define DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(function) \
270 struct function##Impl { \
272 constexpr auto operator()(const T &t) const { \
273 return function(t, PriorityTag<10>{}, MathOverloads::ADLTag{}); \
276 static_assert(true, "Require semicolon to unconfuse editors")
278 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isNaN);
279 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isInf);
280 DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR(isFinite);
281#undef DUNE_COMMON_MATH_ISFUNCTION_FUNCTOR
283 struct isUnorderedImpl {
285 constexpr auto operator()(
const T &t1,
const T &t2)
const {
286 return isUnordered(t1, t2, PriorityTag<10>{}, MathOverloads::ADLTag{});
303 static constexpr T value{};
307 constexpr T MathDummy<T>::value;
323 constexpr auto const &isNaN = Impl::MathDummy<MathImpl::isNaNImpl>::value;
330 constexpr auto const &isInf = Impl::MathDummy<MathImpl::isInfImpl>::value;
337 constexpr auto const &isFinite = Impl::MathDummy<MathImpl::isFiniteImpl>::value;
345 constexpr auto const &isUnordered = Impl::MathDummy<MathImpl::isUnorderedImpl>::value;
348 namespace MathOverloads {
350 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
351 auto isNaN(
const T &t, PriorityTag<2>, ADLTag) {
352 return Dune::isNaN(real(t)) || Dune::isNaN(imag(t));
355 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
356 auto isInf(
const T &t, PriorityTag<2>, ADLTag) {
357 return Dune::isInf(real(t)) || Dune::isInf(imag(t));
360 template<class T, class = std::enable_if_t<Impl::isComplexLike<T>::value> >
361 auto isFinite(
const T &t, PriorityTag<2>, ADLTag) {
362 return Dune::isFinite(real(t)) && Dune::isFinite(imag(t));
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:131
constexpr Base power(Base m, Exponent p)
Power method for integer exponents.
Definition: math.hh:75
static constexpr T factorial(const T &n) noexcept
calculate the factorial of n as a constexpr
Definition: math.hh:111
int sign(const T &val)
Return the sign of the value.
Definition: math.hh:180
K conjugateComplex(const K &x)
compute conjugate complex of x
Definition: math.hh:164
static constexpr int factorial
factorial stores m!
Definition: math.hh:96
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:230
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, ...