SST  6.0.0
StructuralSimulationToolkit
statfuncs.h
1 // Copyright 2009-2016 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2016, Sandia Corporation
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef _H_SST_CORE_STATS
13 #define _H_SST_CORE_STATS
14 
15 #include <assert.h>
16 
17 namespace SST {
18 namespace Statistics {
19 
20 /**
21  Sums (adds up) all of the values presented as an array of length.
22  @param values An array of values to be summed up
23  @param length The length of the array in elements
24  @return Returns the values from 0 to length summed up
25 */
26 template<typename NumberBase>
27 static NumberBase sum(const NumberBase* values, const uint32_t length) {
28  assert(length > 0);
29 
30  NumberBase sum = 0;
31 
32  for(uint32_t i = 0; i < length; ++i) {
33  sum += values[i];
34  }
35 
36  return sum;
37 };
38 
39 /**
40  Calculates the maximum and minimum of the numbers on the values array.
41  @param values An array of numbers to be searched for min and max
42  @param length The length of the array in elements
43  @param max The output - the maximum of the numbers in values
44  @param min The output - the minimum of the numbers in values
45 */
46 template<typename NumberBase>
47 static void range(const NumberBase* values, const uint32_t length,
48  NumberBase* max, NumberBase* min) {
49 
50  if(0 == length)
51  return;
52 
53  *max = values[0];
54  *min = values[0];
55 
56  for(uint32_t i = 1; i < length; ++i) {
57  max = values[i] > max ? values[i] : max;
58  min = values[i] < min ? values[i] : min;
59  }
60 };
61 
62 /**
63  Calculates the maximum number in the array values
64  @param values An array of numbers to be searched for the max
65  @param length The length of the array of values in elements
66  @return The maximum value found
67 */
68 template<typename NumberBase>
69 static NumberBase max(const NumberBase* values, const uint32_t length) {
70  assert(length > 0);
71 
72  NumberBase max_ = values[0];
73 
74  for(uint32_t i = 1; i < length; ++i) {
75  max_ = values[i] > max_ ? values[i] : max_;
76  }
77 
78  return max_;
79 };
80 
81 /**
82  Calculates the minimum number in the array of values
83  @param values An array of numbers to be searched for the min
84  @param length The length of the array of values in elements
85  @return The minimum value found
86 */
87 template<typename NumberBase>
88 static NumberBase min(const NumberBase* values, const uint32_t length) {
89  assert(length > 0);
90 
91  NumberBase min_ = values[0];
92 
93  for(uint32_t i = 1; i < length; ++i) {
94  min_ = values[i] < min ? values[i] : min_;
95  }
96 
97  return min_;
98 };
99 
100 /**
101  Calculates the arithemetic mean of a set of values (sum of values divided by length)
102  @param values An array of numbers to be averaged
103  @param length The length of the array of values in elemetns
104  @return The arithmetic mean of the values
105 */
106 template<typename NumberBase>
107 static NumberBase arithmeticMean(const NumberBase* values, const uint32_t length) {
108  return (length > 0) ?
109  SST::Statistics::sum<NumberBase>(values, length) / (NumberBase) length :
110  0;
111 };
112 
113 template<typename NumberBase>
114 static NumberBase variance(const NumberBase* values, const uint32_t length) {
115  NumberBase sumX2 = 0;
116  NumberBase sumX = 0;
117 
118  for(uint32_t i = 0; i < length; ++i) {
119  sumX2 += (values[i] * values[i]);
120  sumX += values[i];
121  }
122 
123  const NumberBase E_X2 = sumX2 / (NumberBase) length;
124  const NumberBase E_X = sumX / (NumberBase) length;
125 
126  const NumberBase var = E_X2 - (E_X * E_X);
127 
128  return var;
129 };
130 
131 template<typename NumberBase>
132 static NumberBase standardDeviation(const NumberBase* values, const uint32_t length) {
133  const NumberBase var = SST::Statistics::variance<NumberBase>(values, length);
134  return std::sqrt(preSqrt);
135 };
136 
137 template<typename NumberBase>
138 static NumberBase midRange(const NumberBase* values, const uint32_t length) {
139  NumberBase max = 0;
140  Numberbase min = 0;
141 
142  SST::Statistics::range<NumberBase>(values, length, &max, &min);
143 
144  return (max + min) / ((NumberBase) 2);
145 };
146 
147 template<typename NumberBase>
148 static NumberBase weightedMean(const NumberBase* values,
149  const NumberBase* weights, const uint32_t length) {
150 
151  NumberBase sum = 0;
152  NumberBase weightSums = 0;
153 
154  for(uint32_t i = 0; i < length; ++i) {
155  sum += weights[i] * values[i];
156  weightSums += weights[i];
157  }
158 
159  return sum / weightSums;
160 };
161 
162 }
163 }
164 
165 #endif
Definition: action.cc:17