Samchon Framework for CPP  1.0.0
Math.hpp
1 #pragma once
2 #include <samchon/API.hpp>
3 
4 #include <map>
5 #include <samchon/IndexPair.hpp>
6 
7 namespace samchon
8 {
9 namespace library
10 {
23  class SAMCHON_FRAMEWORK_API Math
24  {
25  public:
26  /* ========================================================
27  STATIC VARIABLES
28  ======================================================== */
36  static const double E;
37 
45  static const double PI;
46 
54  static const double LN2;
55 
63  static const double LN10;
64 
72  static const double LOG2E;
73 
83  static const double LOG10E;
84 
85 
93  static const double SQRT1_2;
94 
102  static const double SQRT2;
103 
104  /* ========================================================
105  RANDOM
106  ======================================================== */
113  static auto random() -> double;
114 
115  /* ========================================================
116  PIE
117  ======================================================== */
121  static auto degree_to_radian(double) -> double;
122 
126  static auto radian_to_degree(double) -> double;
127 
128  /* ========================================================
129  STATISTICS
130  ======================================================== */
142  template <typename _Cont, typename T = _Cont::value_type>
143  static auto minimum(const _Cont &container) -> IndexPair<T>
144  {
145  auto it = container.begin();
146  size_t i = 0, position = 0;
147 
148  const T *ptr = &(*it);
149 
150  for (; it != container.end(); it++)
151  {
152  if (*it < *ptr)
153  {
154  ptr = &(*it);
155  position = i;
156  }
157  i++;
158  }
159  return{ position, *ptr };
160  };
161 
173  template <typename _Cont, typename T = _Cont::value_type>
174  static auto maximum(const _Cont &container) -> IndexPair<T>
175  {
176  auto it = container.begin();
177  size_t i = 0, position = 0;
178 
179  const T *ptr = &(*it);
180 
181  for (; it != container.end(); it++)
182  {
183  if (*it > *ptr)
184  {
185  ptr = &(*it);
186  position = i;
187  }
188  i++;
189  }
190  return{ position, *ptr };
191  };
192 
204  template <typename _Cont, typename T = _Cont::value_type>
205  static auto mean(const _Cont &container) -> double
206  {
207  double val = 0.0;
208  for (auto it = container.begin(); it != container.end(); it++)
209  val += *it;
210 
211  return val / (double)container.size();
212  };
213 
225  template <typename _Cont, typename T = _Cont::value_type>
226  static auto median(const _Cont &container) -> double
227  {
228  auto it = container.begin();
229 
230  if (container.size() % 2 == 1)
231  {
232  advance(it, container.size() / 2);
233 
234  return (double)*it;
235  }
236  else
237  {
238  advance(it, container.size() / 2 - 1);
239  auto next_it = it;
240  next_it++;
241 
242  return ((double)*it + (double)*next_it) / 2.0;
243  }
244  };
245 
257  template <typename _Cont, typename T = _Cont::value_type>
258  static auto mode(const _Cont &container) -> T
259  {
260  std::map<T, size_t> frequencyMap;
261  for (auto it = container.begin(); it != container.end(); it++)
262  if (frequencyMap.find(*it) == frequencyMap.end())
263  frequencyMap[*it] = 1;
264  else
265  frequencyMap[*it]++;
266 
267  auto det_it = frequencyMap.begin();
268  for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++)
269  if (it->second > det_it->second)
270  det_it = it;
271 
272  return det_it->first;
273  };
274 
281  template <typename _Cont, typename T = _Cont::value_type>
282  static auto stdev_p(const _Cont &container) -> double
283  {
284  return sqrt(variance_p(contaier));
285  };
286 
292  template <typename _Cont, typename T = _Cont::value_type>
293  static auto stdev_s(const _Cont &container) -> double
294  {
295  return sql(variance_s(container));
296  };
297 
309  template <typename _Cont, typename T = _Cont::value_type>
310  static auto variance_p(const _Cont &container) -> double
311  {
312  double val = 0.0;
313  double mean = Math::mean(container);
314 
315  for (auto it = container.begin(); it != container.end(); it++)
316  val += *it - mean;
317 
318  return val / (double)container.size();
319  };
320 
332  template <typename _Cont, typename T = _Cont::value_type>
333  static auto variance_s(const _Cont &container) -> double
334  {
335  double val = 0.0;
336  double mean = Math::mean(container);
337 
338  for (auto it = container.begin(); it != container.end(); it++)
339  val += *it - mean;
340 
341  return val / (double)(container.size() - 1);
342  };
343  };
344 };
345 };
static const double PI
¥ð, Number Pi
Definition: Math.hpp:45
static auto stdev_s(const _Cont &container) -> double
Square root of a variance_s.
Definition: Math.hpp:293
static const double LOG2E
log2e
Definition: Math.hpp:72
static auto median(const _Cont &container) -> double
Get median value.
Definition: Math.hpp:226
static const double LOG10E
log10e
Definition: Math.hpp:83
Utility of Math.
Definition: Math.hpp:23
static auto mode(const _Cont &container) -> T
Find mode value.
Definition: Math.hpp:258
static auto minimum(const _Cont &container) -> IndexPair< T >
Calculate minimum value with its index.
Definition: Math.hpp:143
static const double LN10
loge10
Definition: Math.hpp:63
static auto variance_s(const _Cont &container) -> double
Calculate variance for sample.
Definition: Math.hpp:333
static const double E
Exponent.
Definition: Math.hpp:36
static const double LN2
loge2
Definition: Math.hpp:54
static const double SQRT1_2
Squart root of 0.5.
Definition: Math.hpp:93
static auto variance_p(const _Cont &container) -> double
Calculate standard deviation for all population.
Definition: Math.hpp:310
A pair of index and its value(T)
Definition: IndexPair.hpp:29
static auto maximum(const _Cont &container) -> IndexPair< T >
Calculate maximum value with its index.
Definition: Math.hpp:174
static auto stdev_p(const _Cont &container) -> double
Square root of a variance_p.
Definition: Math.hpp:282
static auto mean(const _Cont &container) -> double
Calculate average.
Definition: Math.hpp:205
static const double SQRT2
Squart root of 2.
Definition: Math.hpp:102
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7