-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathFunc.h
62 lines (55 loc) · 1.64 KB
/
MathFunc.h
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
#ifndef MATH_FUNC_H
#define MATH_FUNC_H
#include <vector>
// Whenever you define a template class or (as in this case) a namespace of template functions
// YOU CANNOT SEPARATE THE HEADER AND IMPLEMENTATION FILES because you will have link errors (unresolved externals).
// Therefore, both the declarations and the definitions should be included in the header file.
namespace mfnc
{
template <typename T>
std::vector<T> multiplyVectorByConstant(const std::vector<T>& v, const T& c)
{
// TODO: consider changing the return type std::vector<T> (e.g., to something like a reference to std::vector<T>)
std::vector<T> result;
for (size_t i = 0; i < v.size(); ++i)
{
result.push_back(c * v[i]);
}
return result;
}
template <typename T>
std::vector<T> addVectors(const std::vector<T>& v_1, const std::vector<T>& v_2)
{
// TODO: consider changing the return type std::vector<T> (e.g., to something like a reference to std::vector<T>)
// TODO: add check for vectors with different sizes
std::vector<T> result;
for (size_t i = 0; i < v_1.size(); ++i)
{
result.push_back(v_1[i] + v_2[i]);
}
return result;
}
template <typename T>
void addToVector(std::vector<T>& v, const std::vector<T>& other_v)
{
for (size_t i = 0; i < v.size(); ++i)
{
v[i] += other_v[i];
}
}
template <typename T>
double computeEuclideanDistance(const std::vector<T>& v_1, const std::vector<T>& v_2)
{
double dist = 0.0;
size_t s_1 = v_1.size();
size_t s_2 = v_2.size();
size_t s = s_1 < s_2 ? s_1 : s_2;
for (size_t i = 0; i < s; ++i)
{
dist += std::pow(v_1[i] - v_2[i], 2.0);
}
dist = sqrt(dist);
return dist;
}
}
#endif // MATH_FUNC_H