-
Notifications
You must be signed in to change notification settings - Fork 0
/
12 - Mathimp.cpp
81 lines (72 loc) · 2.31 KB
/
12 - Mathimp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cmath>
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <algorithm>
#pragma once
namespace algMath
{
template <typename T>
T Math_Mean(std::vector<T>);
template <typename M>
double Math_Var(std::vector<M> &);
double calc_prob(double value, double mean, double stdev);
template <typename T>
std::vector<std::vector<T>> vect_Transpose(std::vector<std::vector<T>> &input_Vector);
template <typename T>
std::vector<T> vect_Multiply(std::vector<T> &input_Vector1, std::vector<T> &input_Vector2);
}
template <typename T>
T algMath::Math_Mean(std::vector<T> Data)
{
T mean = std::accumulate(std::begin(Data), std::end(Data), 0.0) / Data.size();
return mean;
}
template <typename M>
double algMath::Math_Var(std::vector<M> &Data)
{
M mean = std::accumulate(std::begin(Data), std::end(Data), 0.0) / Data.size();
double accum = 0.0;
std::for_each(std::begin(Data), std::end(Data), [&](const double d)
{ accum += (d - mean) * (d - mean); });
double stdev = sqrt(accum / (Data.size() - 1));
return stdev;
}
template <typename T>
std::vector<std::vector<T>> algMath::vect_Transpose(std::vector<std::vector<T>> &input_Vector)
{
if (input_Vector.size() > 0)
{
std::vector<std::vector<T>> out_Vector(input_Vector[0].size(), std::vector<T>(input_Vector.size()));
for (std::size_t i = 0; i < input_Vector.size(); i++)
{
for (std::size_t j = 0; j < input_Vector[i].size(); j++)
{
out_Vector[j][i] = input_Vector[i][j];
}
}
return out_Vector;
}
return input_Vector;
}
template <typename T>
std::vector<T> algMath::vect_Multiply(std::vector<T> &input_Vector1, std::vector<T> &input_Vector2)
{
if (input_Vector1.size() == input_Vector2.size() && input_Vector1.size() != 0)
{
std::vector<T> out_Vector(input_Vector1.size());
for (int i = 0; i < input_Vector1.size(); i++)
{
out_Vector[i] = input_Vector1[i] * input_Vector2[i];
}
return out_Vector;
}
return input_Vector1;
}
double algMath::calc_prob(double x, double m, double s)
{
static const double inv_sqrt_2pi = 0.3989422804014327;
double a = (x - m) / s;
return inv_sqrt_2pi / s * std::exp(-0.5f * a * a);
}