DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
misc.h
Go to the documentation of this file.
1 
12 #ifndef MISC_H
13 #define MISC_H
14 
15 #include <cstdlib>
16 #include <cstdio>
17 #include <cmath>
18 #include "utils.h"
19 
20 #ifdef WINDOWS
21 #define isnan _isnan
22 #define isinf !_finite
23 #endif
24 
25 namespace spams
26 {
27 
28 using namespace std;
29 
30 
32 static inline void stop();
34 static int seed = 0;
36 template <typename T> static inline T ran1();
38 template <typename T> static inline T ran1b();
40 template <typename T> static inline T normalDistrib();
43 template <typename T>
44 static void sort(int* irOut, T* prOut,int beg, int end);
45 template <typename T>
46 static void quick_sort(int* irOut, T* prOut,const int beg, const int end, const bool incr);
48 template <typename T>
49 T power(const T x, const T y);
51 template <typename T>
52 T abs(const T x);
54 template <typename T>
55 T sqr(const T x);
56 template <typename T>
57 T sqr_alt(const T x);
59 template <typename T>
60 T sqr(const int x) {
61  return sqr<T>(static_cast<T>(x));
62 }
63 
64 template <typename T>
65 T exp_alt(const T x);
66 template <typename T>
67 T log_alt(const T x);
68 
70 /*static inline void stop() {
71  cout << "Appuyez sur entrĂ©e pour continuer...";
72  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
73 };*/
74 static inline void stop() {
75  printf("Appuyez sur une touche pour continuer\n");
76  getchar();
77 }
78 
80 template <typename T> static inline T ran1() {
81  const int IA=16807,IM=2147483647,IQ=127773,IR=2836,NTAB=32;
82  const int NDIV=(1+(IM-1)/NTAB);
83  const T EPS=3.0e-16,AM=1.0/IM,RNMX=(1.0-EPS);
84  static int iy=0;
85  static int iv[NTAB];
86  int j,k;
87  T temp;
88 
89  if (seed <= 0 || !iy) {
90  if (-seed < 1) seed=1;
91  else seed = -seed;
92  for (j=NTAB+7;j>=0;j--) {
93  k=seed/IQ;
94  seed=IA*(seed-k*IQ)-IR*k;
95  if (seed < 0) seed += IM;
96  if (j < NTAB) iv[j] = seed;
97  }
98  iy=iv[0];
99  }
100  k=seed/IQ;
101  seed=IA*(seed-k*IQ)-IR*k;
102  if (seed < 0) seed += IM;
103  j=iy/NDIV;
104  iy=iv[j];
105  iv[j] = seed;
106  if ((temp=AM*iy) > RNMX) return RNMX;
107  else return temp;
108 };
109 
111 template <typename T> T ran1b() {
112  return static_cast<T>(rand())/RAND_MAX;
113 }
114 
116 template <typename T>
117 static inline T normalDistrib() {
118  static bool iset = true;
119  static T gset;
120 
121  T fac,rsq,v1,v2;
122  if (iset) {
123  do {
124  v1 = 2.0*ran1<T>()-1.0;
125  v2 = 2.0*ran1<T>()-1.0;
126  rsq = v1*v1+v2*v2;
127  } while (rsq >= 1.0 || rsq == 0.0);
128  fac = sqrt(-2.0*log(rsq)/rsq);
129  gset = v1*fac;
130  iset = false;
131  return v2*fac;
132  } else {
133  iset = true;
134  return gset;
135  }
136 };
137 
140 template <typename T>
141 static void sort(int* irOut, T* prOut,int beg, int end) {
142  int i;
143  if (end <= beg) return;
144  int pivot=beg;
145  for (i = beg+1; i<=end; ++i) {
146  if (irOut[i] < irOut[pivot]) {
147  if (i == pivot+1) {
148  int tmp = irOut[i];
149  T tmpd = prOut[i];
150  irOut[i]=irOut[pivot];
151  prOut[i]=prOut[pivot];
152  irOut[pivot]=tmp;
153  prOut[pivot]=tmpd;
154  } else {
155  int tmp = irOut[pivot+1];
156  T tmpd = prOut[pivot+1];
157  irOut[pivot+1]=irOut[pivot];
158  prOut[pivot+1]=prOut[pivot];
159  irOut[pivot]=irOut[i];
160  prOut[pivot]=prOut[i];
161  irOut[i]=tmp;
162  prOut[i]=tmpd;
163  }
164  ++pivot;
165  }
166  }
167  sort(irOut,prOut,beg,pivot-1);
168  sort(irOut,prOut,pivot+1,end);
169 }
170 template <typename T>
171 static void quick_sort(int* irOut, T* prOut,const int beg, const int end, const bool incr) {
172  if (end <= beg) return;
173  int pivot=beg;
174  if (incr) {
175  const T val_pivot=prOut[pivot];
176  const int key_pivot=irOut[pivot];
177  for (int i = beg+1; i<=end; ++i) {
178  if (prOut[i] < val_pivot) {
179  prOut[pivot]=prOut[i];
180  irOut[pivot]=irOut[i];
181  prOut[i]=prOut[++pivot];
182  irOut[i]=irOut[pivot];
183  prOut[pivot]=val_pivot;
184  irOut[pivot]=key_pivot;
185  }
186  }
187  } else {
188  const T val_pivot=prOut[pivot];
189  const int key_pivot=irOut[pivot];
190  for (int i = beg+1; i<=end; ++i) {
191  if (prOut[i] > val_pivot) {
192  prOut[pivot]=prOut[i];
193  irOut[pivot]=irOut[i];
194  prOut[i]=prOut[++pivot];
195  irOut[i]=irOut[pivot];
196  prOut[pivot]=val_pivot;
197  irOut[pivot]=key_pivot;
198  }
199  }
200  }
201  quick_sort(irOut,prOut,beg,pivot-1,incr);
202  quick_sort(irOut,prOut,pivot+1,end,incr);
203 }
204 
205 
207 template <>
208 inline double power(const double x, const double y) {
209  return pow(x,y);
210 };
211 template <>
212 inline float power(const float x, const float y) {
213  return powf(x,y);
214 };
215 
217 template <>
218 inline double abs(const double x) {
219  return fabs(x);
220 };
221 template <>
222 inline float abs(const float x) {
223  return fabsf(x);
224 };
225 
227 template <>
228 inline double sqr(const double x) {
229  return sqrt(x);
230 };
231 template <>
232 inline float sqr(const float x) {
233  return sqrtf(x);
234 };
235 
236 template <>
237 inline double exp_alt(const double x) {
238  return exp(x);
239 };
240 template <>
241 inline float exp_alt(const float x) {
242  return expf(x);
243 };
244 
245 template <>
246 inline double log_alt(const double x) {
247  return log(x);
248 };
249 template <>
250 inline float log_alt(const float x) {
251  return logf(x);
252 };
253 
254 
255 template <>
256 inline double sqr_alt(const double x) {
257  return sqrt(x);
258 };
259 template <>
260 inline float sqr_alt(const float x) {
261  return sqrtf(x);
262 };
263 
264 static inline int init_omp(const int numThreads) {
265  int NUM_THREADS;
266 #ifdef _OPENMP
267  NUM_THREADS = (numThreads == -1) ? MIN(MAX_THREADS,omp_get_num_procs()) : numThreads;
268  omp_set_nested(0);
269  omp_set_dynamic(0);
270  omp_set_num_threads(NUM_THREADS);
271 #else
272  NUM_THREADS = 1;
273 #endif
274  return NUM_THREADS;
275 }
276 
277 }
278 
279 #endif
Definition: dag.h:26
T sqr(const T x)
template version of the fabs function
T sqr_alt(const T x)
STL namespace.
static void sort(int *irOut, T *prOut, int beg, int end)
Definition: misc.h:141
static void stop()
a useful debugging function
Definition: misc.h:74
Contains various variables and class timer.
#define MIN(a, b)
Definition: utils.h:47
T abs(const T x)
template version of the fabs function
#define MAX_THREADS
Definition: utils.h:42
static char incr
static void quick_sort(int *irOut, T *prOut, const int beg, const int end, const bool incr)
Definition: misc.h:171
static int seed
seed for random number generation
Definition: misc.h:34
T exp_alt(const T x)
static T normalDistrib()
random sampling from the normal distribution
Definition: misc.h:117
static int init_omp(const int numThreads)
Definition: misc.h:264
T power(const T x, const T y)
template version of the power function
static T ran1()
first random number generator from Numerical Recipe
Definition: misc.h:80
T log_alt(const T x)
static T ran1b()
standard random number generator
Definition: misc.h:111