69 static_assert(std::is_default_constructible_v<T>,
70 "Accumulator type parameters must be default constructible.");
73 void add(
const T& val) {
78 Avg_ += (val - Avg_) /
static_cast<RealType
>(Count_);
79 Avg2_ += (val * val - Avg2_) /
static_cast<RealType
>(Count_);
85 Max_ = val > Max_ ? val : Max_;
86 Min_ = val < Min_ ? val : Min_;
90 std::size_t getCount()
const {
return Count_; }
92 T getLastValue()
const {
return Val_; }
99 RealType getMax()
const {
100 static_assert(std::is_arithmetic<T>::value,
101 "getMax() requires a RealType Accumulator.");
106 RealType getMin()
const {
107 static_assert(std::is_arithmetic<T>::value,
108 "getMin() requires a RealType Accumulator.");
113 RealType getAverage()
const {
118 RealType getVariance()
const {
120 T var = (Avg2_ - Avg_ * Avg_);
121 if (var < 0) var = 0;
126 RealType getStdDev()
const {
128 T sd = std::sqrt(this->getVariance());
133 RealType get95percentConfidenceInterval()
const {
136 1.960 * this->getStdDev() / std::sqrt(
static_cast<RealType
>(Count_));
142 std::size_t Count_ {};
143 T Val_ {}, Total_ {};
144 RealType Max_ {}, Min_ {}, Avg_ {}, Avg2_ {};
153 constexpr static RealType BinEmptyFlag =
154 std::numeric_limits<RealType>::max();
156 void add(
const std::vector<RealType>& val) {
157 if (val.empty() || (val.size() != Avg_.size() && !Avg_.empty())) {
158 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
159 "Size of vector passed to add() did not "
160 "match the size of the StaticAccumulator.");
161 painCave.isFatal = 1;
168 Val_.resize(val.size());
169 Total_.resize(val.size());
170 Avg_.resize(val.size());
171 Avg2_.resize(val.size());
174 for (std::size_t i = 0; i < val.size(); i++) {
177 if (val[i] == BinEmptyFlag)
continue;
180 Avg_[i] += (val[i] - Avg_[i]) /
static_cast<RealType
>(Count_);
182 (val[i] * val[i] - Avg2_[i]) /
static_cast<RealType
>(Count_);
186 std::size_t getCount()
const {
return Count_; }
188 std::vector<RealType> getLastValue()
const {
return Val_; }
190 std::vector<RealType> getTotal()
const {
return Total_; }
192 std::vector<RealType> getAverage()
const {
return Avg_; }
194 std::vector<RealType> getVariance()
const {
195 std::vector<RealType> var(Avg_.size());
197 for (std::size_t i = 0; i < Avg_.size(); i++) {
198 var[i] = (Avg2_[i] - Avg_[i] * Avg_[i]);
199 if (var[i] < 0) var[i] = 0;
205 std::vector<RealType> getStdDev()
const {
206 std::vector<RealType> sd(Avg_.size());
207 std::vector<RealType> variance = this->getVariance();
209 for (std::size_t i = 0; i < variance.size(); i++) {
210 sd[i] = std::sqrt(variance[i]);
216 std::vector<RealType> get95percentConfidenceInterval()
const {
217 std::vector<RealType> ci(Avg_.size());
218 std::vector<RealType> stdDev = this->getStdDev();
220 for (std::size_t i = 0; i < stdDev.size(); i++) {
221 ci[i] = 1.960 * stdDev[i] / std::sqrt(
static_cast<RealType
>(Count_));
228 std::size_t Count_ {};
229 std::vector<RealType> Val_ {}, Total_ {}, Avg_ {}, Avg2_ {};
238 for (std::size_t i = 0; i < Dim; i++) {
241 Avg_[i] += (val[i] - Avg_[i]) /
static_cast<RealType
>(Count_);
243 (val[i] * val[i] - Avg2_[i]) /
static_cast<RealType
>(Count_);
247 std::size_t getCount()
const {
return Count_; }
268 for (std::size_t i = 0; i < Dim; i++) {
269 var[i] = (Avg2_[i] - Avg_[i] * Avg_[i]);
270 if (var[i] < 0) var[i] = 0;
282 for (std::size_t i = 0; i < Dim; i++) {
283 sd[i] = std::sqrt(variance[i]);
295 for (std::size_t i = 0; i < Dim; i++) {
296 ci[i] = 1.960 * stdDev[i] / std::sqrt(
static_cast<RealType
>(Count_));
303 std::size_t Count_ {};
310 void add(
const Mat3x3d& val) {
313 for (std::size_t i = 0; i < 3; i++) {
314 for (std::size_t j = 0; j < 3; j++) {
315 Val_(i, j) = val(i, j);
316 Total_(i, j) += val(i, j);
318 (val(i, j) - Avg_(i, j)) /
static_cast<RealType
>(Count_);
319 Avg2_(i, j) += (val(i, j) * val(i, j) - Avg2_(i, j)) /
320 static_cast<RealType
>(Count_);
325 std::size_t getCount()
const {
return Count_; }
327 Mat3x3d getLastValue()
const {
return Val_; }
329 Mat3x3d getTotal()
const {
335 Mat3x3d getAverage()
const {
341 Mat3x3d getVariance()
const {
346 for (std::size_t i = 0; i < 3; i++) {
347 for (std::size_t j = 0; j < 3; j++) {
348 var(i, j) = (Avg2_(i, j) - Avg_(i, j) * Avg_(i, j));
349 if (var(i, j) < 0) var(i, j) = 0;
356 Mat3x3d getStdDev()
const {
360 Mat3x3d variance = this->getVariance();
362 for (std::size_t i = 0; i < 3; i++) {
363 for (std::size_t j = 0; j < 3; j++) {
364 sd(i, j) = std::sqrt(variance(i, j));
371 Mat3x3d get95percentConfidenceInterval()
const {
375 Mat3x3d stdDev = this->getStdDev();
377 for (std::size_t i = 0; i < 3; i++) {
378 for (std::size_t j = 0; j < 3; j++) {
380 1.960 * stdDev(i, j) / std::sqrt(
static_cast<RealType
>(Count_));
388 std::size_t Count_ {};
389 Mat3x3d Val_ {}, Total_ {}, Avg_ {}, Avg2_ {};