-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
77 lines (59 loc) · 1.23 KB
/
functions.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
#pragma once
#include <cmath>
#define M_PI 3.14159265358979323846
inline float crossent(float predicted, float actual)
{
return -actual * log2(predicted);
}
inline float clamp(float val, float min, float max)
{
return std::fmin(std::fmax(val, min), max);
}
inline float factor_eps(float a, float b, float eps)
{
float dist = a - b;
if (dist > eps) {
return 1.f;
} else if (dist < -eps) {
return 0.f;
} else {
return (dist + eps) / (2.f * eps);
}
}
inline float factor_eps_top(float a, float b, float eps)
{
float dist = a - b;
if (dist > eps) {
return 1.f;
} else if (dist < 0.f) {
return 0.f;
} else {
return dist / eps;
}
}
inline float gaussian_blur(int x, int y, float std_dev)
{
float a = 1.f / (2.f * M_PI * std_dev * std_dev);
float b = (x*x + y*y) / (2.f * std_dev * std_dev);
return clamp(a * exp(-b), 0.f, 1.f);
}
inline float logisticize(float f)
{
float sig_ff = 0.1f;
float sig_f = 1.f / (1.f - 2.f * sig_ff);
f -= 0.5f;
f *= 4.38f;
f = (1.f / (1.f + exp(-f))) * sig_f - sig_ff;
return clamp(f, 0.f, 1.f);
}
inline float logisticize_full(float f)
{
f -= 0.5f;
f *= 12.f;
f = (1.f / (1.f + exp(-f)));
return clamp(f, 0.f, 1.f);
}
inline int modulo(int a, int b)
{
return (a%b + b) % b;
}