OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
mathutil.hpp
1 #ifndef MATHUTIL_HPP
2 #define MATHUTIL_HPP
3 
4 
5 namespace openfpm
6 {
7  namespace math
8  {
9 
21  static inline size_t factorial(size_t f)
22  {
23  size_t fc = 1;
24 
25  for (size_t s = 2 ; s <= f ; s++)
26  {
27  fc *= s;
28  }
29 
30  return fc;
31  }
32 
46  static inline size_t C(size_t n, size_t k)
47  {
48  return factorial(n)/(factorial(k)*factorial(n-k));
49  }
50 
61  static inline size_t round_big_2(size_t n)
62  {
63  n--;
64  n |= n >> 1; // Divide by 2^k for consecutive doublings of k up to 32,
65  n |= n >> 2; // and then or the results.
66  n |= n >> 4;
67  n |= n >> 8;
68  n |= n >> 16;
69  n++;
70 
71  return n;
72  }
73 
74 
87  template<class T>
88  inline constexpr T pow(const T base, unsigned const exponent)
89  {
90  // (parentheses not required in next line)
91  return (exponent == 0) ? 1 : (base * pow(base, exponent-1));
92  }
93 
94  /* \brief Return the positive modulo of a number
95  *
96  * # Example
97  * \snippet mathutil_unit_test.hpp positive modulo
98  *
99  * \param i number
100  * \param n modulo
101  *
102  */
103  static inline long int positive_modulo(long int i, long int n)
104  {
105  return (i % n + n) % n;
106  }
107 
123  template<typename T> static inline T periodic(const T & pos, const T & p2, const T & p1)
124  {
125  T pos_tmp;
126 
127  pos_tmp = pos - (p2 - p1) * (long int)( (pos -p1) / (p2 - p1));
128  pos_tmp += (pos < p1)?(p2 - p1):0;
129 
130  return pos_tmp;
131  }
132 
150  template<typename T> static inline T periodic_l(const T & pos, const T & p2, const T & p1)
151  {
152  T pos_tmp = pos;
153 
154  if (pos >= p2)
155  pos_tmp -= (p2 - p1);
156  else if (pos < p1)
157  pos_tmp += (p2 - p1);
158 
159 
160  return pos_tmp;
161  }
162  }
163 }
164 
165 #endif