Math.hpp 2.67 KB
Newer Older
1
2
#pragma once

3
4
5
6
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
7
8
9
#include <string>
#include <type_traits>

10
11
#include <dune/common/power.hh>

12
13
namespace AMDiS
{
14
  namespace Math
15
  {
16
    /// Implementation of the absolute value \f$|a|\f$ of arithmetic types.
17
    template <class T>
18
    constexpr T abs(T const& a)
19
    {
20
21
      return  a >= 0 ? a : -a;
    }
22
23


24
    /// Implementation of the square \f$ a^2 \f$ of arithmetic types.
25
    template <class T>
26
    constexpr auto sqr(T const& a)
27
28
29
    {
      return a*a;
    }
30

31
32
    /// Implementation of the power \f$ v^p \f$ of arithmetic types `T`.
    template <int p, class T>
33
    constexpr auto pow(T const& v)
34
    {
35
      static_assert( p >= 0, "Exponent p in `pow<p>(v)` should be >= 0," );
36
      return Dune::Power<p>::eval(v);
37
    }
38

39

40
    /// Implementation of the minimum of two values \f$ min(a,b)\f$ of any type
41
42
43
    /// supporting the `<` relation.
    /// @{

44
45
46
    template <class T0, class T1>
    constexpr auto min(T0 a, T1 b)
    {
47
48
49
50
51
52
53
54
      return a < b ? a : b;
    }

#ifndef AMDIS_HAS_CXX_FOLD_EXPRESSIONS
    template <class T0>
    constexpr T0 min(T0 a)
    {
      return a;
55
    }
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    template <class T0, class... Ts>
    constexpr auto min(T0 a, Ts... ts)
    {
      return min(a, min(ts...));
    }
#else
    template <class T0, class... Ts>
    constexpr auto min(T0 a, Ts... ts)
    {
      std::common_type_t<T0,Ts...> result = a;
      return (result = min(result, ts), ...);
    }
#endif

    /// @}

73

74
    /// Implementation of the maximum of two values \f$ max(a,b)\f$ of any type
75
    /// supporting the `>` relation.
76
    /// @{
77
78
79
    template <class T0, class T1>
    constexpr auto max(T0 a, T1 b)
    {
80
81
82
83
84
85
86
87
      return a < b ? b : a;
    }

#ifndef AMDIS_HAS_CXX_FOLD_EXPRESSIONS
    template <class T0>
    constexpr T0 max(T0 a)
    {
      return a;
88
    }
89

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    template <class T0, class... Ts>
    constexpr auto max(T0 a, Ts... ts)
    {
      return max(a, max(ts...));
    }
#else
    template <class T0, class... Ts>
    constexpr auto max(T0 a, Ts... ts)
    {
      std::common_type_t<T0,Ts...> result = a;
      return (result = max(result, ts), ...);
    }
#endif

    /// @}

106
  } // end namespace Math
107
108


109
110
  template <class T>
  inline void nullify(T& a)
111
112
113
  {
    a = 0;
  }
114

115
116
117
118
  inline void nullify(std::string& s)
  {
    s = "";
  }
119

120
121
122
123
  template <class T>
  constexpr T threshold = T(1.e-16); //Math::sqr(std::numeric_limits<T>::epsilon());


124
  /// Calculates factorial of i
125
  constexpr std::uint64_t factorial(std::uint64_t i)
126
127
128
  {
    return i <= 1 ? 1 : i * factorial(i - 1);
  }
129

130
131
132
133
134
  /// check for inf and nan values
  inline bool isNumber(double val)
  {
    return !std::isnan(val) && !std::isinf(val);
  }
135
136

} // end namespace AMDiS