-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cc
277 lines (238 loc) · 5.64 KB
/
matrix.cc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "matrix.h"
#include <cmath>
#include <iostream>
#include <random>
#include <string>
Matrix::Matrix(){};
Matrix::~Matrix(){};
Matrix::Matrix(vector2d& input) : data(input){};
Matrix::Matrix(int x, int y) {
for (int i = 0; i < x; i++) {
vector1d row;
for (int j = 0; j < y; j++) {
row.push_back(1.0);
}
this->data.push_back(row);
}
}
void Matrix::to_arrays(){};
void Matrix::init_grad() {
this->requires_grad = true;
int x = this->size()[0];
int y = this->size()[1];
Matrix* grad = new Matrix(x, y);
this->grad = grad;
}
void Matrix::uniform(float a, float b) {
int x = this->size()[0];
int y = this->size()[1];
// random_device rand_dev;
// default_random_engine generator(rand_dev());
default_random_engine generator;
uniform_real_distribution<float> uniform(a, b);
for (int i = 0; i < x; i++) {
vector<float> row;
for (int j = 0; j < y; j++) {
this->data[i][j] = uniform(generator);
}
}
}
void Matrix::ones() {
int x = this->size()[0];
int y = this->size()[1];
Matrix result(x, y);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
this->data[i][j] = 1.0;
}
}
};
Matrix Matrix::tanh() {
int x = this->size()[0];
int y = this->size()[1];
Matrix result(x, y);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
result.data[i][j] = std::tanh(this->data[i][j]);
}
}
return result;
};
Matrix Matrix::square() {
int x = this->size()[0];
int y = this->size()[1];
Matrix result(x, y);
if (this->requires_grad) {
result.init_grad();
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
result.data[i][j] = this->data[i][j] * this->data[i][j];
}
}
return result;
};
Matrix Matrix::mul(float other) {
int x = this->size()[0];
int y = this->size()[1];
Matrix result(x, y);
if (this->requires_grad) {
result.init_grad();
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
result.data[i][j] = this->data[i][j] * other;
}
}
return result;
};
void Matrix::mulip(Matrix* other) {
int x = this->size()[0];
int y = this->size()[1];
if (this->requires_grad || other->requires_grad) {
if (!this->requires_grad) {
this->init_grad();
}
if (!other->requires_grad) {
other->init_grad();
}
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
this->data[i][j] *= other->data[i][j];
}
}
};
Matrix Matrix::transpose() {
int x = this->size()[0];
int y = this->size()[1];
Matrix result = Matrix(y, x);
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
result.data[i][j] = this->data[j][i];
}
}
return result;
};
float Matrix::sum() {
int x = this->size()[0];
int y = this->size()[1];
float sum = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
sum += data[i][j];
}
}
return sum;
};
Matrix Matrix::cols(int a, int b) {
int x = this->size()[0];
Matrix result = Matrix(x, b - a);
for (int i = 0; i < x; i++) {
for (int j = a; j < b; j++) {
result.data[i][j - a] = this->data[i][j];
}
}
return result;
};
// Core operations
Matrix Matrix::matmul(Matrix& other) {
if (size()[1] != other.size()[0]) {
cout << "Sizes of matrices should be compatible. Got " << size_str()
<< " and " << other.size_str() << endl;
exit(EXIT_FAILURE);
}
int x = this->size()[0];
int y = other.size()[1];
int z = size()[1];
Matrix result = Matrix(x, y);
if (this->requires_grad || other.requires_grad) {
if (!this->requires_grad) {
this->init_grad();
}
if (!other.requires_grad) {
other.init_grad();
}
result.init_grad();
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
result.data[i][j] = 0;
for (int k = 0; k < z; k++) {
result.data[i][j] += data[i][k] * other.data[k][j];
}
}
}
return result;
};
Matrix Matrix::add(Matrix other) {
if (this->size()[1] != other.size()[1]) {
cout << "Sizes of matrices should be compatible. Got " << size_str()
<< " and " << other.size_str() << endl;
exit(EXIT_FAILURE);
}
int x = this->size()[0];
int y = this->size()[1];
if (other.size()[0] == 1) {
Matrix interm = Matrix(x, y);
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
interm.data[i][j] = other.data[0][j];
}
}
other = interm;
}
if (this->size()[0] != other.size()[0]) {
cout << "Sizes of matrices should be compatible. Got " << size_str()
<< " and " << other.size_str() << endl;
exit(EXIT_FAILURE);
}
Matrix result = Matrix(x, y);
if (this->requires_grad || other.requires_grad) {
if (!this->requires_grad) {
this->init_grad();
}
if (!other.requires_grad) {
other.init_grad();
}
result.init_grad();
}
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
result.data[i][j] = this->data[i][j] + other.data[i][j];
}
}
return result;
};
// Helper functions
void Matrix::print_data() {
for (vector1d row : data) {
for (float col : row) {
printf("%10.4f ", col);
}
cout << endl;
}
};
vector<int> Matrix::size() {
vector<int> size;
size.push_back(data.size());
size.push_back(data[0].size());
return size;
};
string Matrix::size_str() {
vector<int> size_vec = size();
string size_str;
size_str = "(";
for (size_t i = 0; i < size_vec.size(); i++) {
size_str += to_string(size_vec[i]);
if (i < (size_vec.size() - 1)) {
size_str += ", ";
}
}
size_str += ")";
return size_str;
};
void Matrix::print_size() {
// useless comment
cout << size_str() << endl;
};