Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
mathdef.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File mathdef.h
5 // General maths types and macros
6 // ==========================================================================
7 
8 #ifndef __MATHDEF_H
9 #define __MATHDEF_H
10 
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 #include <stdlib.h>
15 #include <math.h>
16 #include <algorithm>
17 #include <complex>
18 
19 #ifdef FEM_DEBUG
20 #define MATH_DEBUG // switch on error checks
21 #define NO_TEMPLATE_INLINE // switch off inlines in template classes
22 #endif
23 
24 //#define COMPUTE_FLOPS // record floating point operations for
25  // selected calculations
26 
27 // matrix/vector index types
28 typedef int idxtype;
29 //typedef long idxtype;
30 
31 // some constants ===========================================================
32 
33 const double Pi = 3.1415926535897932384626433832795;
34 const double Pi2 = Pi*2.0;
35 const double Pi05 = Pi*0.5;
36 const double Rad = Pi/180.0;
37 const double Deg = 180.0/Pi;
38 
39 // type definitions =========================================================
40 
41 typedef unsigned char BYTE;
42 
43 // macros ===================================================================
44 
45 //#ifndef __MINMAX_DEFINED
46 //namespace toast {
47 // template<class T>
48 // inline T min (const T x, const T y) { return (x < y) ? x : y; }
49 // template<class T>
50 // inline T max (const T x, const T y) { return (x > y) ? x : y; }
51 //}
52 //#endif
53 
54 typedef enum {
55  DEEP_COPY,
56  SHALLOW_COPY
57 } CopyMode;
58 
59 template<class T>
60 inline T sqr (const T a) { return (a == 0.0 ? (T)0 : a*a); }
61 
62 inline double sign (const double a, const double b)
63 { return (b >= 0 ? fabs(a) : -fabs(a)); }
64 // return a with the sign of b
65 
66 inline double norm (double f)
67 { return fabs (f); }
68 
69 inline double norm2 (double f)
70 { return f*f; }
71 
72 inline int norm (int i)
73 { return abs (i); }
74 
75 inline bool pow (bool base, bool e)
76 { return base; }
77 
78 inline int pow (int base, int e)
79 { return (int)pow ((double)base, (double)e); }
80 
81 // inline float pow (float base, float e)
82 // { return (float)pow ((double)base, (double)e); }
83 
84 inline float conj(float a)
85 { return a; }
86 
87 inline double conj(double a)
88 { return a; }
89 
90 inline bool iszero (double d)
91 { return d == 0.0; }
92 
93 inline bool iszero (std::complex<double> d)
94 {
95  return d.real() == 0 && d.imag() == 0;
96 }
97 
98 inline bool operator> (const std::complex<double> &z1,
99  const std::complex<double> &z2)
100 {
101  return std::norm(z1) > std::norm(z2);
102 }
103 
104 inline bool operator< (const std::complex<double> &z1,
105  const std::complex<double> &z2)
106 {
107  return std::norm(z1) < std::norm(z2);
108 }
109 
110 inline bool operator! (const std::complex<double> &z)
111 {
112  return z.real() == 0 && z.imag() == 0;
113 }
114 
115 inline std::complex<double> hadamard (const std::complex<double>& a,
116  const std::complex<double>& b)
117 {
118  return std::complex<double> (a.real()*b.real(), a.imag()*b.imag());
119 }
120 
121 inline int binomial_coeff (int n, int r)
122 {
123  int i, sum = 1;
124  for (i = n; i > std::max (r, (n-r)); i--) sum *= i;
125  for (i = std::min (r, (n-r)); i > 1; i--) sum /= i;
126  return sum;
127 }
128 
129 inline double rad (double deg) { return deg*Rad; }
130 inline double deg (double rad) { return rad*Deg; }
131 
132 #ifdef __BORLANDC__ // BorlandC doesn't know about atanh
133 inline double atanh (double x)
134 { return 0.5 * log ((1.0+x) / (1.0-x)); } // valid for |x| < 1
135 #endif
136 
137 #endif