-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathutils.h
92 lines (79 loc) · 1.73 KB
/
mathutils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MATHUTILS_H_
#define MATHUTILS_H_
#include <cmath>
namespace MathUtils
{
static int Sum(int *arr, int len)
{
int s = 0;
for (int i = 0; i < len; ++i)
s += arr[i];
return s;
}
static float DotProduct(float *vec0, float *vec1, int len)
{
float dot_prod = 0;
for (int i = 0; i < len; ++i)
dot_prod += vec0[i] * vec1[i];
return dot_prod;
}
static void ElementWiseDivide(float *vec, int len, float divisor)
{
for (int i = 0; i < len; ++i)
{
vec[i] /= divisor;
}
}
static float Sigma(float x)
{
return 1 / (1 + exp(-x));
}
static float Dist(float *vec0, float *vec1, int len)
{
float rslt = 0;
for (int i = 0; i < len; ++i)
{
rslt += (vec0[i] - vec1[i]) * (vec0[i] - vec1[i]);
}
return rslt;
}
static float Norm(float *vec, int len)
{
float val = 0;
for (int i = 0; i < len; ++i)
val += vec[i] * vec[i];
return sqrt(val);
}
static float Cosine(float *vec0, float *vec1, int len)
{
float dp = DotProduct(vec0, vec1, len);
return dp / Norm(vec0, len) / Norm(vec1, len);
}
// matrix: [ x11, x12, ..., x1n, x21, ..., xmn ]
static float XMY(float *vec0, int len0, float *vec1, int len1, float *matrix)
{
float result = 0;
for (int i = 0; i < len0; ++i)
for (int j = 0; j < len1; ++j)
result += vec0[i] * matrix[i * len1 + j] * vec1[j];
return result;
}
// matrix: [ x11, x12, ..., x1n, x21, ..., xmn ]
static void MY(float *matrix, float *vec, int m, int n, float *dst_vec)
{
for (int i = 0; i < m; ++i)
{
dst_vec[i] = 0;
for (int j = 0; j < n; ++j)
dst_vec[i] += matrix[i * n + j] * vec[j];
}
}
static float NormSqr(float *vec, int len)
{
float result = 0;
for (int i = 0; i < len; ++i)
result += vec[i] * vec[i];
return result;
}
}
#endif