source: trunk/src/Utils/getStats.cc @ 258

Last change on this file since 258 was 222, checked in by Matthew Whiting, 17 years ago

Large commit, but mostly documentation-oriented.

Only non-doc-related changes are:

  • To remove two deprecated files and any declarations of the functions in them
  • To move drawBlankEdges to the Cubes/ directory
  • Some small changes to the implementation of the StatsContainer? functions.
  • Creation of Utils/devel.hh to hide functions not used in Duchamp
  • To move the trimmedHist stats functions to their own file, again to hide them.
File size: 11.1 KB
Line 
1#include <iostream>
2#include <algorithm>
3#include <math.h>
4#include <Utils/utils.hh>
5
6template <class T> T absval(T value)
7{
8  /**
9   * Type-independent way of getting the absolute value.
10   */
11  if( value > 0) return value;
12  else return 0-value;
13}
14template int absval<int>(int value);
15template long absval<long>(long value);
16template float absval<float>(float value);
17template double absval<double>(double value);
18//--------------------------------------------------------------------
19
20template <class T> void findMinMax(const T *array, const int size,
21                                   T &min, T &max)
22{
23  /**
24   * A function to find the minimum and maximum values of a set of numbers.
25   * \param array The array of data values. Type independent.
26   * \param size The length of the array
27   * \param min The returned value of the minimum value in the array.
28   * \param max The returned value of the maximum value in the array.
29   */
30  min = max = array[0];
31  for(int i=1;i<size;i++) {
32    if(array[i]<min) min=array[i];
33    if(array[i]>max) max=array[i];
34  }
35}
36template void findMinMax<int>(const int *array, const int size,
37                                 int &min, int &max);
38template void findMinMax<long>(const long *array, const int size,
39                                  long &min, long &max);
40template void findMinMax<float>(const float *array, const int size,
41                                   float &min, float &max);
42template void findMinMax<double>(const double *array, const int size,
43                                    double &min, double &max);
44//--------------------------------------------------------------------
45
46template <class T> float findMean(T *array, int size)
47{
48  /**
49   * Find the mean of an array of numbers. Type independent.
50   * \param array The array of numbers.
51   * \param size The length of the array.
52   * \return The mean value of the array, returned as a float
53   */
54  float mean = array[0];
55  for(int i=1;i<size;i++) mean += array[i];
56  mean /= float(size);
57  return mean;
58}
59template float findMean<int>(int *array, int size);
60template float findMean<long>(long *array, int size);
61template float findMean<float>(float *array, int size);
62template float findMean<double>(double *array, int size);
63//--------------------------------------------------------------------
64
65template <class T> float findStddev(T *array, int size)
66{
67  /**
68   * Find the rms or standard deviation of an array of numbers. Type independent.
69   * \param array The array of numbers.
70   * \param size The length of the array.
71   * \return The rms value of the array, returned as a float
72   */
73  float mean = findMean(array,size);
74  float stddev = (array[0]-mean) * (array[0]-mean);
75  for(int i=1;i<size;i++) stddev += (array[i]-mean)*(array[i]-mean);
76  stddev = sqrt(stddev/float(size-1));
77  return stddev;
78}
79template float findStddev<int>(int *array, int size);
80template float findStddev<long>(long *array, int size);
81template float findStddev<float>(float *array, int size);
82template float findStddev<double>(double *array, int size);
83//--------------------------------------------------------------------
84
85template <class T> T findMedian(T *array, int size)
86{
87  /**
88   * Find the median value of an array of numbers. Type independent.
89   * \param array The array of numbers.
90   * \param size The length of the array.
91   * \return The median value of the array, returned as the same type as the array.
92   */
93  T *newarray = new T[size];
94  T median;
95  for(int i=0;i<size;i++) newarray[i] = array[i];
96  std::sort(newarray,newarray+size);
97  if((size%2)==0) median = (newarray[size/2-1]+newarray[size/2])/2;
98  else median = newarray[size/2];
99  delete [] newarray;
100  return median;
101}
102template int findMedian<int>(int *array, int size);
103template long findMedian<long>(long *array, int size);
104template float findMedian<float>(float *array, int size);
105template double findMedian<double>(double *array, int size);
106//--------------------------------------------------------------------
107
108template <class T> T findMADFM(T *array, int size)
109{
110  /**
111   * Find the median absolute deviation from the median value of an
112   * array of numbers. Type independent.
113   *
114   * \param array The array of numbers.
115   * \param size The length of the array.
116   * \return The median absolute deviation from the median value of
117   * the array, returned as the same type as the array.
118   */
119  T *newarray = new T[size];
120  T median = findMedian<T>(array,size);
121  T madfm;
122  for(int i=0;i<size;i++) newarray[i] = absval(array[i]-median);
123  std::sort(newarray,newarray+size);
124  if((size%2)==0) madfm = (newarray[size/2-1]+newarray[size/2])/2;
125  else madfm = newarray[size/2];
126  delete [] newarray;
127  return madfm;
128}
129template int findMADFM<int>(int *array, int size);
130template long findMADFM<long>(long *array, int size);
131template float findMADFM<float>(float *array, int size);
132template double findMADFM<double>(double *array, int size);
133//--------------------------------------------------------------------
134
135template <class T> void findMedianStats(T *array, int size,
136                                        T &median, T &madfm)
137{
138  /**
139   * Find the median and the median absolute deviation from the median
140   * value of an array of numbers. Type independent.
141   *
142   * \param array The array of numbers.
143   * \param size The length of the array.
144   * \param median The median value of the array, returned as the same type as the array.
145   * \param madfm The median absolute deviation from the median value of
146   * the array, returned as the same type as the array.
147   */
148  if(size==0){
149    std::cerr << "Error in findMedianStats: zero sized array!\n";
150    return;
151  }
152  T *newarray = new T[size];
153
154  for(int i=0;i<size;i++) newarray[i] = array[i];
155  std::sort(newarray,newarray+size);
156  if((size%2)==0) median = (newarray[size/2-1]+newarray[size/2])/2;
157  else median = newarray[size/2];
158
159  for(int i=0;i<size;i++) newarray[i] = absval(array[i]-median);
160  std::sort(newarray,newarray+size);
161  if((size%2)==0) madfm = (newarray[size/2-1]+newarray[size/2])/2;
162  else madfm = newarray[size/2];
163
164  delete [] newarray;
165}
166template void findMedianStats<int>(int *array, int size,
167                                      int &median, int &madfm);
168template void findMedianStats<long>(long *array, int size,
169                                       long &median, long &madfm);
170template void findMedianStats<float>(float *array, int size,
171                                        float &median, float &madfm);
172template void findMedianStats<double>(double *array, int size,
173                                         double &median, double &madfm);
174//--------------------------------------------------------------------
175
176template <class T> void findMedianStats(T *array, int size, bool *isGood,
177                                        T &median, T &madfm)
178{
179  /**
180   * Find the median and the median absolute deviation from the median
181   * value of a subset of an array of numbers. The subset is defined
182   * by an array of bool variables. Type independent.
183   *
184   * \param array The array of numbers.
185   * \param size The length of the array.
186   * \param isGood An array of the same length that says whether to
187   * include each member of the array in the calculations.
188   * \param median The median value of the array, returned as the same type as the array.
189   * \param madfm The median absolute deviation from the median value of
190   * the array, returned as the same type as the array.
191   */
192  int goodSize=0;
193  for(int i=0;i<size;i++) if(isGood[i]) goodSize++;
194  if(goodSize==0){
195    std::cerr << "Error in findMedianStats: no good values!\n";
196    return;
197  }
198  T *newarray = new T[goodSize];
199  for(int i=0;i<size;i++) if(isGood[i]) newarray[goodSize++] = array[i];
200  std::sort(newarray,newarray+goodSize);
201  if((goodSize%2)==0)
202    median = (newarray[goodSize/2-1]+newarray[goodSize/2])/2;
203  else
204    median = newarray[goodSize/2];
205
206  for(int i=0;i<goodSize;i++) newarray[i] = absval(newarray[i]-median);
207  std::sort(newarray,newarray+goodSize);
208  if((goodSize%2)==0)
209    madfm = (newarray[goodSize/2-1]+newarray[goodSize/2])/2;
210  else
211    madfm = newarray[goodSize/2];
212
213  delete [] newarray;
214}
215template void findMedianStats<int>(int *array, int size, bool *isGood,
216                                      int &median, int &madfm);
217template void findMedianStats<long>(long *array, int size, bool *isGood,
218                                       long &median, long &madfm);
219template void findMedianStats<float>(float *array, int size, bool *isGood,
220                                        float &median, float &madfm);
221template void findMedianStats<double>(double *array, int size, bool *isGood,
222                                         double &median, double &madfm);
223//--------------------------------------------------------------------
224 
225
226template <class T> void findNormalStats(T *array, int size,
227                                        float &mean, float &stddev)
228{
229  /**
230   * Find the mean and rms or standard deviation of an array of numbers. Type independent.
231   *
232   * \param array The array of numbers.
233   * \param size The length of the array.
234   * \param mean The mean value of the array, returned as a float.
235   * \param stddev The rms or standard deviation of the array, returned as a float.
236   */
237  if(size==0){
238    std::cerr << "Error in findNormalStats: zero sized array!\n";
239    return;
240  }
241  mean = array[0];
242  for(int i=1;i<size;i++){
243    mean += array[i];
244  }
245  mean /= float(size);
246
247  stddev = (array[0]-mean) * (array[0]-mean);
248  for(int i=1;i<size;i++){
249    float sqdiff = (array[i]-mean)*(array[i]-mean);
250    stddev += sqdiff;
251  }
252  stddev = sqrt(stddev/float(size-1));
253
254}
255template void findNormalStats<int>(int *array, int size,
256                                      float &mean, float &stddev);
257template void findNormalStats<long>(long *array, int size,
258                                       float &mean, float &stddev);
259template void findNormalStats<float>(float *array, int size,
260                                        float &mean, float &stddev);
261template void findNormalStats<double>(double *array, int size,
262                                         float &mean, float &stddev);
263//--------------------------------------------------------------------
264
265template <class T> void findNormalStats(T *array, int size, bool *isGood,
266                                        float &mean, float &stddev)
267{
268  /**
269   * Find the mean and rms or standard deviation of a subset of an
270   * array of numbers. The subset is defined by an array of bool
271   * variables. Type independent.
272   *
273   * \param array The array of numbers.
274   * \param size The length of the array.
275   * \param isGood An array of the same length that says whether to
276   * include each member of the array in the calculations.
277   * \param mean The mean value of the array, returned as a float.
278   * \param stddev The rms or standard deviation of the array, returned as a float.
279   */
280  int goodSize=0;
281  for(int i=0;i<size;i++) if(isGood[i]) goodSize++;
282  if(goodSize==0){
283    std::cerr << "Error in findNormalStats: no good values!\n";
284    return;
285  }
286  int start=0;
287  while(!isGood[start]){start++;}
288  mean = array[start];
289  for(int i=start+1;i<size;i++){
290    if(isGood[i]) mean += array[i];
291  }
292  mean /= float(goodSize);
293
294  stddev = (array[start]-mean) * (array[start]-mean);
295  for(int i=1;i<size;i++){
296    if(isGood[i]) stddev += (array[i]-mean)*(array[i]-mean);
297  }
298  stddev = sqrt(stddev/float(goodSize-1));
299
300}
301template void findNormalStats<int>(int *array, int size, bool *isGood,
302                                      float &mean, float &stddev);
303template void findNormalStats<long>(long *array, int size, bool *isGood,
304                                       float &mean, float &stddev);
305template void findNormalStats<float>(float *array, int size, bool *isGood,
306                                        float &mean, float &stddev);
307template void findNormalStats<double>(double *array, int size, bool *isGood,
308                                         float &mean, float &stddev);
309//--------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.