-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathlibrery.cpp
More file actions
executable file
·251 lines (228 loc) · 6.88 KB
/
mathlibrery.cpp
File metadata and controls
executable file
·251 lines (228 loc) · 6.88 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <pthread.h>
#include <cassert>
#include <vector>
template<class T, int row, int col>
class Matrix
{
public:
int matrixRow;
int matrixCol;
T matrix[row][col];
Matrix() : matrixRow(row), matrixCol(col)
{
}
Matrix(std::initializer_list<std::initializer_list<T>> list)
{
*this = list;
}
Matrix<T, col, row> matrixTranspose()
{
Matrix<T, col, row> tempTranceponentMatrix;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
tempTranceponentMatrix[j][i] = matrix[i][j];
}
return tempTranceponentMatrix;
}
template<class T2, int row2, int col2>
Matrix<T, row, col2> operator*(Matrix<T2, row2, col2>& matrix2);
T operator()(const int index1, const int index2);
T* operator[](const int index);
Matrix<T, row, col> operator-(Matrix<T, row, col>& matrix2);
Matrix<T, row, col> operator+(Matrix<T, row, col>& matrix2);
Matrix<T, row, col> operator*(T variable);
friend Matrix<T, row, col> operator*(T variable, Matrix<T, row, col>& matrix2);
void operator=(std::initializer_list<std::initializer_list<T>> list);
};
template<class T, int row, int col>
template<typename T2, int row2, int col2>
Matrix<T, row, col2> Matrix<T, row, col>::operator*(Matrix<T2, row2, col2>& matrix2)
{
assert(col == row2 && "Col of first matrix dont equal row of second matrix");
Matrix<T, row, col2> tempMatrix;
for (int x = 0; x < row; ++x)
for (int z = 0; z < col2; ++z) {
tempMatrix.matrix[x][z] = 0;
}
for(int i = 0; i < row; ++i)
for(int j = 0; j < col2; ++j)
for(int l = 0; l < col; ++l)
{
tempMatrix.matrix[i][j] += matrix[i][l] * matrix2.matrix[l][j];
}
/*for(int n = 0; n < row; ++n)
for(int m = 0; m < col2; ++m)
{
std::cout << tempMatrix.matrix[n][m] << std::endl;
}
*/
return tempMatrix;
}
template <class T, int row, int col>
T Matrix<T, row, col>::operator()(const int index1, const int index2)
{
return matrix[index1][index2];
}
template <class T, int row, int col>
T* Matrix<T, row, col>::operator[](const int index)
{
return matrix[index];
}
template<class T, int row, int col>
Matrix<T, row, col> Matrix<T, row, col>::operator-(Matrix<T, row, col>& matrix2)
{
//assert(matrix2.matrix == col && matrix2.matrixRow == row && "Sizes of matrix dont equal");
Matrix<T, row, col> tempMatrix;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
tempMatrix[i][j] = matrix[i][j] - matrix2.matrix[i][j];
}
return tempMatrix;
}
template<class T, int row, int col>
Matrix<T, row, col> Matrix<T, row, col>::operator+(Matrix<T, row, col>& matrix2)
{
//assert(matrix2.matrix == col && matrix2.matrixRow == row && "Sizes of matrix dont equal");
Matrix<T, row, col> tempMatrix;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
tempMatrix[i][j] = matrix[i][j] + matrix2.matrix[i][j];
}
return tempMatrix;
}
template<class T, int row, int col>
Matrix<T, row, col> Matrix<T, row, col>::operator*(T variable)
{
Matrix<T, row, col> tempMatrix;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
tempMatrix[i][j] = matrix[i][j] * variable;
}
return tempMatrix;
}
template<class T, int row, int col>
Matrix<T, row, col> operator*(T variable, Matrix<T, row, col>& matrix2)
{
Matrix<T, row, col> tempMatrix;
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
tempMatrix[i][j] = matrix2[i][j] * variable;
}
return tempMatrix;
}
template <class T, int row, int col>
std::ostream& operator<<(std::ostream &ostream,
Matrix<T, row, col>& matrix)
{
/*for (int (&array)[col] : matrix.matrix)
for(int n : array)
{
ostream << n << " ";
}*/
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
ostream << matrix.matrix[i][j] << " ";
}
return ostream;
}
template <typename X, int row, int col>
Matrix<X, 1, col> cutFunction(Matrix<X, row, col>& matrix, int iteration)
{
Matrix<X, 1, col> tempMatrix;
for(int i = 0; i < col; ++i)
{
tempMatrix[0][i] = matrix.matrix[iteration][i];
}
return tempMatrix;
}
template <class T, int row, int col>
void Matrix<T, row, col>::operator=(std::initializer_list<std::initializer_list<T>> list)
{
typename std::initializer_list<std::initializer_list<T>>::const_iterator iterator;
typename std::initializer_list<T>::const_iterator iterator2;
iterator = list.begin();
iterator2 = iterator->begin();
for(int i = 0; i < row; ++i)
{
iterator2 = iterator->begin();
++iterator;
for(int j = 0; j < col; ++j)
{
matrix[i][j] = *iterator2;
++iterator2;
}
}
}
/*template<class T, int row, int col>
void Matrix<T, row, col>::operator=(std::initializer_list<T> list)
{
typename std::initializer_list<T>::const_iterator iterator;
iterator = list.begin();
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
matrix[i][j] = *iterator;
++iterator;
}
}*/
// Return X if X > 0; else return 0
template <class T, int row, int col>
void relu(Matrix<T, row, col>& matrix)
{
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
if(matrix[i][j] > 0)
matrix[i][j] = matrix[i][j];
else
matrix[i][j] = 0;
}
//return (x > 0) * x;
}
// Return 1, if output > 0; else return 0
template<class T, int row, int col>
void relu2deriv(Matrix<T, row, col>& matrix, double* reluResult)
{
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
if(matrix[i][j] > 0)
reluResult[j] = 1;
else
reluResult[j] = 0;
}
}
double getRandomNumber(double min, double max)
{
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
// Равномерно распределяем рандомное число в нашем диапазоне
return static_cast<double>(rand() * fraction * (max - min + 1) + min)-1;
}
template <class T, int row, int col>
void weightsGenerator(Matrix<T, row, col>& matrix)
{
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col; ++j)
{
matrix[i][j] = getRandomNumber(0.0, 1.0);
}
}
}
/*
void weightsGeneratorVector(double* weightsVector, int range)
{
for(int i = 0; i < range; ++i)
{
weightsVector[i] = getRandomNumber(0.0, 1.0);
}
}*/