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

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

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


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

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#ifndef DOXYGEN
    namespace _aux
    {
      template <int> struct int_t {};
      template <bool> struct bool_t {};

      // forward declaration
      template <int p, class T> constexpr auto pow(int_t<p>, T v);

      template <int p, class T>
      constexpr auto pow_impl(int_t<p>, bool_t<true> /*p%2 == 0*/, T v)
      {
        return pow(int_t<p/2>{}, v*v);
      }

      template <int p, class T>
      constexpr auto pow_impl(int_t<p>, bool_t<false> /*p%2 != 0*/, T v)
      {
        return pow(int_t<p-1>{}, v) * v;
      }

      template <int p, class T>
      constexpr auto pow(int_t<p>, T v)
      {
        return pow_impl(int_t<p>{}, bool_t<p%2==0>{}, v);
      }

      template <class T>
      constexpr T pow(int_t<1>, T v) { return v; }

      template <class T>
      constexpr T pow(int_t<0>, T v) { return T(1); }

    } // end namespace _aux
#endif

    /// Implementation of the power \f$ v^p \f$ of arithmetic types `T`.
    template <int p, class T>
68
69
    constexpr auto pow(T v)
    {
70
71
      static_assert( p >= 0, "Exponent p in `pow<p>(v)` should be >= 0," );
      return _aux::pow(_aux::int_t<p>{}, v);
72
    }
73

74

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


84
    /// Implementation of the maximum of two values \f$ max(a,b)\f$ of any type
85
    /// supporting the `>` relation.
86
87
88
89
90
    template <class T0, class T1>
    constexpr auto max(T0 a, T1 b)
    {
      return a > b ? a : b;
    }
91

92
  } // end namespace Math
93
94


95
96
  template <class T>
  inline void nullify(T& a)
97
98
99
  {
    a = 0;
  }
100

101
102
103
104
  inline void nullify(std::string& s)
  {
    s = "";
  }
105

106
107
108
109
  template <class T>
  constexpr T threshold = T(1.e-16); //Math::sqr(std::numeric_limits<T>::epsilon());


110
  /// Calculates factorial of i
111
  constexpr std::uint64_t factorial(std::uint64_t i)
112
113
114
  {
    return i <= 1 ? 1 : i * factorial(i - 1);
  }
115

116
117
118
119
120
  /// check for inf and nan values
  inline bool isNumber(double val)
  {
    return !std::isnan(val) && !std::isinf(val);
  }
121

122
  // some predefined constants
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  constexpr double m_e = 	2.7182818284590452354;
  constexpr double m_log2e = 	1.4426950408889634074;
  constexpr double m_log10e = 	0.43429448190325182765;
  constexpr double m_ln2 = 	0.69314718055994530942;
  constexpr double m_ln10 = 	2.30258509299404568402;
  constexpr double m_pi = 	3.14159265358979323846;
  constexpr double m_pi_2 = 	1.57079632679489661923;
  constexpr double m_pi_4 = 	0.78539816339744830962;
  constexpr double m_1_pi = 	0.31830988618379067154;
  constexpr double m_2_pi = 	0.63661977236758134308;
  constexpr double m_2_sqrtpi = 1.12837916709551257390;
  constexpr double m_sqrt2 = 	1.41421356237309504880;
  constexpr double m_sqrt1_2 = 	0.70710678118654752440;

} // end namespace AMDiS