All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MathUtil.h
Go to the documentation of this file.
1 #ifndef MATHUTIL_H
2 #define MATHUTIL_H
3 
4 //////////////////////////////////////////////////////////////////////////
5 // //
6 // \file MathUtil.h //
7 // //
8 // Simple mathematical functions, initially to prevent the abuse of //
9 // pow() //
10 // <bckhouse@caltech.edu> //
11 // //
12 //////////////////////////////////////////////////////////////////////////
13 
14 #include <cmath>
15 #include <vector>
16 
17 namespace util{
18 
19  /// \name Simple mathematical functions
20  //@{
21 
22  /// More efficient square function than pow(x,2)
23  template<class T> inline T sqr(T x){return x*x;}
24 
25  /// More efficient cube function than pow(x,3)
26  template<class T> inline T cube(T x){return x*x*x;}
27 
28  /// More efficient exponentiation function than pow(x,n) for small n
29  template<class T> inline T ipow(T x, unsigned int n)
30  {
31  T ret = 1;
32  if (n == 0) return ret;
33  for(unsigned int i = 1; i <= n; ++i) ret *= x;
34  return ret;
35  }
36 
37 
38  /// 2D Euclidean distance
39  inline double pythag(double x, double y)
40  {
41  return sqrt(sqr(x)+sqr(y));
42  }
43 
44  /// 3D Euclidean distance
45  inline double pythag(double x, double y, double z)
46  {
47  return sqrt(sqr(x)+sqr(y)+sqr(z));
48  }
49 
50 } // end namespace
51 
52 #endif // MATHUTIL_H
process_name opflash particleana ie ie ie z
process_name opflash particleana ie x
T cube(T x)
More efficient cube function than pow(x,3)
Definition: MathUtil.h:26
T sqr(T x)
More efficient square function than pow(x,2)
Definition: MathUtil.h:23
process_name opflash particleana ie ie y
T ipow(T x, unsigned int n)
More efficient exponentiation function than pow(x,n) for small n.
Definition: MathUtil.h:29
double pythag(double x, double y)
2D Euclidean distance
Definition: MathUtil.h:39