-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.h
More file actions
43 lines (37 loc) · 1.3 KB
/
Matrix.h
File metadata and controls
43 lines (37 loc) · 1.3 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
#pragma once
namespace AoC {
template<int R, int C, typename val_t = long, typename index_t = int>
requires (R > 0 && C > 0)
struct Matrix {
std::array<std::array<val_t, C>, R> m_values = {};
constexpr val_t& operator() (const index_t index_r, const index_t index_c) {
return m_values.at(index_r).at(index_c);
}
constexpr const val_t& operator() (const index_t index_r, const index_t index_c) const {
return m_values.at(index_r).at(index_c);
}
template<index_t P>
constexpr auto operator*(const Matrix<C, P>& right) const {
Matrix<R, P, val_t, index_t> result{};
for (index_t i = 0; i < R; ++i) {
for (index_t j = 0; j < P; ++j) {
val_t value{0};
for (index_t x = 0; x < C; ++x) {
value += (*this)(i, x) * right(x, j);
}
result(i, j) = value;
}
}
return result;
}
constexpr val_t sum() const {
val_t sum{0};
for (index_t i = 0; i < R; ++i) {
for (index_t j = 0; j < C; ++j) {
sum += (*this)(i, j);
}
}
return sum;
}
};
}