AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Math.hpp
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <limits>
7#include <type_traits>
8
9#include <dune/common/math.hh>
10
11namespace AMDiS
12{
13 namespace Math
14 {
16 template <class T>
17 constexpr T abs(T const& a)
18 {
19 return a < 0 ? -a : a;
20 }
21
22
24 template <class T>
25 constexpr auto sqr(T const& a)
26 {
27 return a*a;
28 }
29
31 template <int p, class T>
32 constexpr auto pow(T const& v)
33 {
34 static_assert( p >= 0, "Exponent p in `pow<p>(v)` should be >= 0," );
35 return Dune::power(v,p);
36 }
37
38
42
43 template <class T0, class T1>
44 constexpr auto min(T0 a, T1 b)
45 {
46 using T = std::common_type_t<T0,T1>;
47 return T(a) < T(b) ? a : b;
48 }
49
50 template <class T0>
51 constexpr T0 min(T0 a)
52 {
53 return a;
54 }
55
56 template <class T0, class... Ts>
57 constexpr auto min(T0 a, Ts... ts)
58 {
59 return min(a, min(ts...));
60 }
61
63
64
68
69 template <class T0, class T1>
70 constexpr auto max(T0 a, T1 b)
71 {
72 using T = std::common_type_t<T0,T1>;
73 return T(a) < T(b) ? b : a;
74 }
75
76 template <class T0>
77 constexpr T0 max(T0 a)
78 {
79 return a;
80 }
81
82 template <class T0, class... Ts>
83 constexpr auto max(T0 a, Ts... ts)
84 {
85 return max(a, max(ts...));
86 }
87
89
90 // sum over empty set is zero
91 constexpr double sum()
92 {
93 return 0.0;
94 }
95
96 template <class... Ts>
97 constexpr auto sum(Ts const&... ts)
98 {
99 return (ts + ...);
100 }
101
102 } // end namespace Math
103
104 template <class T>
105 constexpr T threshold = std::numeric_limits<T>::epsilon();
106
107} // end namespace AMDiS