Samchon Framework for CPP  1.0.0
Math.hpp
1 #pragma once
2 
3 #include <cmath>
4 #include <random>
5 #include <map>
6 #include <samchon/IndexPair.hpp>
7 
8 namespace samchon
9 {
10 namespace library
11 {
22  class Math
23  {
24  public:
25  /* ========================================================
26  STATIC VARIABLES
27  ======================================================== */
35  static auto E() -> double
36  {
37  return exp(1.0);
38  };
39 
47  static auto PI() -> double
48  {
49  return 3.141592653589793;
50  }
51 
59  static auto LN2() -> double
60  {
61  return 1.0 / log2(E());
62  }
63 
71  static auto LN10() -> double
72  {
73  return 1.0 / log10(E());
74  }
75 
83  static auto LOG2E() -> double
84  {
85  return log2(E());
86  }
87 
97  static auto LOG10E() -> double
98  {
99  return log10(E());
100  }
101 
109  static auto SQRT1_2() -> double
110  {
111  return sqrt(.5);
112  }
113 
121  static auto SQRT2() -> double
122  {
123  return sqrt(2.0);
124  }
125 
126  /* ========================================================
127  RANDOM
128  ======================================================== */
135  static auto random() -> double
136  {
137  static std::random_device device;
138  static std::uniform_real_distribution<double> distribution(0.0, 1.0);
139 
140  return distribution(device);
141  };
142 
143  /* ========================================================
144  PIE
145  ======================================================== */
149  static auto degree_to_radian(double val) -> double
150  {
151  return val * PI() / 180.0;
152  }
153 
157  static auto radian_to_degree(double val) -> double
158  {
159  return val * 180.0 / PI();
160  }
161 
162  /* ========================================================
163  STATISTICS
164  ======================================================== */
176  template <typename _Cont, typename T = _Cont::value_type>
177  static auto minimum(const _Cont &container) -> IndexPair<T>
178  {
179  auto it = container.begin();
180  size_t i = 0, position = 0;
181 
182  const T *ptr = &(*it);
183 
184  for (; it != container.end(); it++)
185  {
186  if (*it < *ptr)
187  {
188  ptr = &(*it);
189  position = i;
190  }
191  i++;
192  }
193  return{ position, *ptr };
194  };
195 
207  template <typename _Cont, typename T = _Cont::value_type>
208  static auto maximum(const _Cont &container) -> IndexPair<T>
209  {
210  auto it = container.begin();
211  size_t i = 0, position = 0;
212 
213  const T *ptr = &(*it);
214 
215  for (; it != container.end(); it++)
216  {
217  if (*it > *ptr)
218  {
219  ptr = &(*it);
220  position = i;
221  }
222  i++;
223  }
224  return{ position, *ptr };
225  };
226 
238  template <typename _Cont, typename T = _Cont::value_type>
239  static auto mean(const _Cont &container) -> double
240  {
241  double val = 0.0;
242  for (auto it = container.begin(); it != container.end(); it++)
243  val += *it;
244 
245  return val / (double)container.size();
246  };
247 
259  template <typename _Cont, typename T = _Cont::value_type>
260  static auto median(const _Cont &container) -> double
261  {
262  auto it = container.begin();
263 
264  if (container.size() % 2 == 1)
265  {
266  advance(it, container.size() / 2);
267 
268  return (double)*it;
269  }
270  else
271  {
272  advance(it, container.size() / 2 - 1);
273  auto next_it = it;
274  next_it++;
275 
276  return ((double)*it + (double)*next_it) / 2.0;
277  }
278  };
279 
291  template <typename _Cont, typename T = _Cont::value_type>
292  static auto mode(const _Cont &container) -> T
293  {
294  std::map<T, size_t> frequencyMap;
295  for (auto it = container.begin(); it != container.end(); it++)
296  if (frequencyMap.find(*it) == frequencyMap.end())
297  frequencyMap[*it] = 1;
298  else
299  frequencyMap[*it]++;
300 
301  auto det_it = frequencyMap.begin();
302  for (auto it = frequencyMap.begin(); it != frequencyMap.end(); it++)
303  if (it->second > det_it->second)
304  det_it = it;
305 
306  return det_it->first;
307  };
308 
315  template <typename _Cont, typename T = _Cont::value_type>
316  static auto stdev_p(const _Cont &container) -> double
317  {
318  return sqrt(variance_p(contaier));
319  };
320 
326  template <typename _Cont, typename T = _Cont::value_type>
327  static auto stdev_s(const _Cont &container) -> double
328  {
329  return sql(variance_s(container));
330  };
331 
343  template <typename _Cont, typename T = _Cont::value_type>
344  static auto variance_p(const _Cont &container) -> double
345  {
346  double val = 0.0;
347  double mean = Math::mean(container);
348 
349  for (auto it = container.begin(); it != container.end(); it++)
350  val += *it - mean;
351 
352  return val / (double)container.size();
353  };
354 
366  template <typename _Cont, typename T = _Cont::value_type>
367  static auto variance_s(const _Cont &container) -> double
368  {
369  double val = 0.0;
370  double mean = Math::mean(container);
371 
372  for (auto it = container.begin(); it != container.end(); it++)
373  val += *it - mean;
374 
375  return val / (double)(container.size() - 1);
376  };
377  };
378 };
379 };
static auto LN10() -> double
loge10
Definition: Math.hpp:71
static auto stdev_s(const _Cont &container) -> double
Square root of a variance_s.
Definition: Math.hpp:327
static auto PI() -> double
¥ð, Number Pi
Definition: Math.hpp:47
static auto radian_to_degree(double val) -> double
Convert radian to degree.
Definition: Math.hpp:157
static auto median(const _Cont &container) -> double
Get median value.
Definition: Math.hpp:260
static auto LOG2E() -> double
log2e
Definition: Math.hpp:83
static auto E() -> double
Exponent.
Definition: Math.hpp:35
Utility of Math.
Definition: Math.hpp:22
static auto LN2() -> double
loge2
Definition: Math.hpp:59
static auto mode(const _Cont &container) -> T
Find mode value.
Definition: Math.hpp:292
static auto SQRT1_2() -> double
Squart root of 0.5.
Definition: Math.hpp:109
static auto minimum(const _Cont &container) -> IndexPair< T >
Calculate minimum value with its index.
Definition: Math.hpp:177
static auto variance_s(const _Cont &container) -> double
Calculate variance for sample.
Definition: Math.hpp:367
static auto SQRT2() -> double
Squart root of 2.
Definition: Math.hpp:121
static auto variance_p(const _Cont &container) -> double
Calculate standard deviation for all population.
Definition: Math.hpp:344
A pair of index and its value(T)
Definition: IndexPair.hpp:30
static auto maximum(const _Cont &container) -> IndexPair< T >
Calculate maximum value with its index.
Definition: Math.hpp:208
static auto stdev_p(const _Cont &container) -> double
Square root of a variance_p.
Definition: Math.hpp:316
static auto random() -> double
Get a random value.
Definition: Math.hpp:135
static auto mean(const _Cont &container) -> double
Calculate average.
Definition: Math.hpp:239
static auto degree_to_radian(double val) -> double
Convert degree to radian.
Definition: Math.hpp:149
static auto LOG10E() -> double
log10e
Definition: Math.hpp:97