-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
executable file
·231 lines (201 loc) · 5.21 KB
/
utils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include <string>
#include "config.hpp"
#include "utils.hpp"
#include <math.h>
using namespace std;
// FUNCTION FOR DECODING STATES INCOMING FROM UART COMMANDS
void Utils::stateDecoder(float ** state, string command)
{
for (int i=0; i<=Config::nStates; i++)
if(command[i] == '1')
state[0][i] = 1;
else
state[0][i] = 0;
}
// FUNCTION FOR CREATING AND INITIALIZING A 2D POINTER
float** Utils::create2DArray(int r, int c)
{
float** array2D = 0;
array2D = new float*[r];
for (int h = 0; h < r; h++)
{
array2D[h] = new float[c];
for (int w = 0; w < c; w++)
{
array2D[h][w] = 0;
}
}
return array2D;
}
// FUNCTION FOR CLEARING VALUES OF A 2D POINTER OF FLOATING POINTS
void Utils::clear(float** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
x[i][j] = 0;
}
void Utils::clearIntegers(int** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
x[i][j] = 0;
}
// FUNCTION FOR CLEARING VALUES OF A 2D POINTER OF INTEGERS
int** Utils::create2DArrayOfIntegers(int r, int c)
{
int** result = 0;
result = new int*[r];
for (int i = 0; i < r; i++)
{
result[i] = new int[c];
for (int j = 0; j < c; j++)
{
result[i][j] = 0;
}
}
return result;
}
// FUNCTION FOR CALCULATING DOT PRODUCT OF MATRICES
void Utils::dot(float** result, float ** x, float ** y, int m, int n, int p)
{
Utils::clear(result, m, p);
for(int i = 0 ; i < m ; i++)
for(int j = 0 ; j < p ; j++)
for(int k = 0 ; k < n ; k++)
{
result[i][j] += x[i][k] * y[k][j];
}
}
// FUNCTION FOR CALCULATING SUM OF MATRICES
void Utils::sum(float ** x, float ** y, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
x[i][j] += y[i][j];
}
// FUNCTION FOR COPYING MATRICES
void Utils::copy(float ** x, float ** y, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
x[i][j] = y[i][j];
}
// FUNCTION FOR CALCULATING DISTANCE OF MATRICES
void Utils::distance(float ** result, float ** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
result[i][j] = result[i][j] - x[i][j];
}
// FUNCTION FOR CALCULATING DISTANCE OF MATRICES
void Utils::relu(float ** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
if(x[i][j] <= 0)
x[i][j] = 0;
}
// FUNCTION FOR CALCULATING SCALAR MULTIPLICATION ON MATRICES
void Utils::scalar(float ** x, int r, int c , float alpha)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
x[i][j] = alpha*x[i][j];
}
// FUNCTION FOR CALCULATING ARGMAX OF MATRICES
void Utils::argMax(int** result, float **x, int r, int c, int axis)
{
if(axis==0)
{
for(int j = 0 ; j < c ; j++)
{
float max = x[0][j];
for(int i = 1 ; i < r ; i++)
if(x[i][j] > max)
{
max = x[i][j];
result[0][j] = i;
}
}
}
else
{
for(int i = 0 ; i < r ; i++)
{
float max = x[i][0];
for(int j = 1 ; j < c ; j++)
if(x[i][j] > max)
{
max = x[i][j];
result[i][0] = j;
}
}
}
}
// FUNCTION FOR CALCULATING TRANSPOSE OF MATRICES
void Utils::transpose(float ** result, float ** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
result[j][i] = x[i][j];
}
// FUNCTION FOR CALCULATING SUM OF MATRIX ROWS OR COLUMNS
void Utils::sigma(float** result, float ** x, int r, int c)
{
for(int j = 0 ; j < c ; j++)
for(int i = 0 ; i < r ; i++)
result[0][j] += x[i][j];
}
// FUNCTION FOR CALCULATING SOFTMAX FUNCTION
void Utils::softmax(float** result, float ** x, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
result[i][j] = exp(x[i][j]/Config::softmaxTemperature);
float sumExponents = 0;
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
sumExponents += result[i][j];
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
result[i][j] = result[i][j]/ sumExponents;
}
// FUNCTION FOR CALCULATING PARTIAL SUM
void Utils::partialSum(float** result, float** px, int r, int c)
{
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
result[i][j] += px[0][j];
}
void Utils::rewarder(float** reward, string command)
{
if(command == "1")
reward[0][0] = 1;
else
reward[0][0] = -1;
}
u32 Utils::float2fix(float input)
{
u32 temp = 0;
int fixedPoint[32];
for (int i = 0 ; i < 32 ; i++)
fixedPoint[i] = 0;
float test = input;
if (test < 0)
{
fixedPoint[Config::FP_Size-1] = 1;
test = test*-1;
}
for(int i = Config::FP_Size - Config::FR_Size - 2 ; i > -1*Config::FR_Size-1; i--)
{
if (test >= (pow(2,i)))
{
fixedPoint[Config::FR_Size+i]=1;
test = test - (pow(2,i));
}
}
for(int i = 0; i < 32; i++)
temp = temp+(pow(2,i))*fixedPoint[i];
return temp;
}