GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Stat.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 #ifndef _GranOO_LibMath_Stat_hpp_
31 #define _GranOO_LibMath_Stat_hpp_
32 
33 #include <vector>
34 #include <cmath>
35 #include <limits>
36 #include <complex>
37 
38 #include "GranOO3/Core/Macro.hpp"
39 
40 namespace GranOO3
41 {
42  namespace Math
43  {
44 
45 
46 
47  class Stat
48  {
49  public:
50  template<typename T> static void All(const T&, double & ave, double & std, double & min, double & max);
51  template<typename T> static void AverageStdDev(const T&, double & ave, double & std);
52  template<typename T> static double Average(const T&);
53  template<typename T> static double StdDev(const T&);
54  template<typename T> static double Max(const T&);
55  template<typename T> static double Min(const T&);
56  template<typename T> static double Sum(const T&);
57 
58  public:
59  Stat();
60  ~Stat();
61  Stat(const Stat&);
62  Stat & operator=(const Stat &);
63 
64  GRANOO_ACCESS(value, std::vector<double>, _value);
65  GRANOO_ACCESS(average, double, _ave);
66  GRANOO_ACCESS(std, double, _std);
67  GRANOO_ACCESS(min, double, _min);
68  GRANOO_ACCESS(max, double, _max);
69 
70  void update();
71  size_t Size() const;
72  void add(double);
73  void clear();
74 
75  private:
76  std::vector<double> _value;
77  double _ave;
78  double _std;
79  double _min;
80  double _max;
81  };
82 
83  std::ostream& operator<< (std::ostream& os, const Stat& v);
84 
85 
86  template<typename T>
87  inline void
88  Stat::All(const T& container, double & ave, double & std, double & min, double & max) {
89  ave = Average(container);
90  std = 0.;
93 
94  typename T::const_iterator it;
95  for (it = container.begin(); it != container.end(); ++it) {
96  std += pow(*it - ave, 2);
97  if (*it > max)
98  max = *it;
99  if (*it < min)
100  min = *it;
101  }
102  std = sqrt(std/double(container.size()));
103  }
104 
105  template<typename T>
106  inline double
107  Stat::Average(const T& container) {
108  double val = 0.;
109  typename T::const_iterator it;
110  for (it = container.begin(); it != container.end(); ++it)
111  val += (*it);
112  val /= double(container.size());
113  return val;
114  }
115 
116  template<typename T>
117  inline double
118  Stat::StdDev(const T& container) {
119  const double average = Average(container);
120  double val = 0;
121  typename T::const_iterator it;
122  for (it = container.begin(); it != container.end(); ++it)
123  val += pow(*it - average, 2);
124  val = sqrt(val/double(container.size()));
125  return val;
126  }
127 
128  template<typename T>
129  void
130  Stat::AverageStdDev(const T& container, double& average, double& stdDev) {
131  average = Average(container);
132  stdDev = 0;
133  typename T::const_iterator it;
134  for (it = container.begin(); it != container.end(); ++it)
135  stdDev += pow(*it - average, 2);
136  stdDev = sqrt(stdDev/double(container.size()));
137  }
138 
139  template<typename T>
140  inline double
141  Stat::Max(const T& container) {
142  double val = std::numeric_limits<double>::min();
143  typename T::const_iterator it;
144  for (it = container.begin(); it != container.end(); ++it) {
145  if (*it > val)
146  val = *it;
147  }
148  return val;
149  }
150 
151  template<typename T>
152  inline double
153  Stat::Min(const T& container) {
154  double val = std::numeric_limits<double>::max();
155  typename T::const_iterator it;
156  for (it = container.begin(); it != container.end(); ++it) {
157  if (*it < val)
158  val = *it;
159  }
160  return val;
161  }
162 
163  template<typename T>
164  inline double
165  Stat::Sum(const T& container) {
166  double val = 0.;
167  typename T::const_iterator it;
168  for (it = container.begin(); it != container.end(); ++it)
169  val += *it;
170 
171  return val;
172  }
173 
174  }
175 }
176 #endif
177 
Definition: Stat.hpp:48
void update()
Definition: Stat.cpp:50
void clear()
Definition: Stat.cpp:65
void add(double)
Definition: Stat.cpp:60
~Stat()
Definition: Stat.cpp:46
static double StdDev(const T &)
Definition: Stat.hpp:118
static void AverageStdDev(const T &, double &ave, double &std)
Definition: Stat.hpp:130
Stat & operator=(const Stat &)
Definition: Stat.cpp:71
double _min
Definition: Stat.hpp:79
double _ave
Definition: Stat.hpp:77
Stat()
Definition: Stat.cpp:38
static double Sum(const T &)
Definition: Stat.hpp:165
double _max
Definition: Stat.hpp:80
std::vector< double > _value
Definition: Stat.hpp:76
static double Min(const T &)
Definition: Stat.hpp:153
double _std
Definition: Stat.hpp:78
static double Average(const T &)
Definition: Stat.hpp:107
static double Max(const T &)
Definition: Stat.hpp:141
static void All(const T &, double &ave, double &std, double &min, double &max)
Definition: Stat.hpp:88
size_t Size() const
Definition: Stat.cpp:55
std::ostream & operator<<(std::ostream &os, const Stat &v)
Definition: Stat.cpp:79
Definition: Common.hpp:198
T pow(const T v0, const T v1)
Definition: Exprtk.hpp:1491
T min(const T v0, const T v1)
Definition: Exprtk.hpp:1456
T max(const T v0, const T v1)
Definition: Exprtk.hpp:1463
T value(details::expression_node< T > *n)
Definition: Exprtk.hpp:15070