-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
66 lines (59 loc) · 1.54 KB
/
matrix.hpp
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
#pragma once
#include <vector>
#include "jio.hpp"
#include <iomanip>
namespace matrix {
struct Matrix {
int rows;
int cols;
mutable float* data;
Matrix(const int rows, const int cols) {
this->rows = rows;
this->cols = cols;
data = static_cast<float*>(_mm_malloc(sizeof(float) * rows * cols, 32));
std::fill(data, data + rows * cols, 0.f);
}
~Matrix() {
_mm_free(data);
}
auto at(const int i, const int j) const -> float& {
return data[i * cols + j];
}
};
inline auto toStr(Matrix& a) -> std::string {
auto ss = std::stringstream();
auto s = std::string();
s.resize(10);
ss << "Matrix {\n";
for (auto i = 0; i < a.rows; ++i) {
ss << " ";
for (auto j = 0; j < a.cols; ++j) {
std::fill(begin(s), end(s), '0');
s = std::to_string(a.at(i, j));
ss << s.substr(0, 10);
if (j < a.cols - 1) {
ss << ", ";
}
}
ss << '\n';
}
ss << '}';
return ss.str();
}
inline auto transpose(Matrix& a, Matrix& b) -> void {
b.rows = a.cols;
b.cols = a.rows;
for (auto i = 0; i < a.rows; ++i) {
for (auto j = 0; j < a.cols; ++j) {
b.at(j, i) = a.at(i, j);
}
}
}
inline auto transpose(const int rows, const int cols, std::vector<float> a, const Matrix& b) -> void {
for (auto i = 0; i < rows; ++i) {
for (auto j = 0; j < cols; ++j) {
b.at(j, i) = a[i * cols + j];
}
}
}
}