-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.cpp
45 lines (42 loc) · 1.23 KB
/
matrix.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
#include <iostream>
#include <array>
template <typename T, std::size_t rows, std::size_t cols>
class Matrix
{
public:
Matrix(const T &default_value)
{
for (unsigned int i = 0; i < rows; i++)
{
for (unsigned int j = 0; j < cols; j++)
{
data_[i][j] = default_value;
}
}
}
template <std::size_t other_rows, std::size_t other_cols, std::size_t new_rows, std::size_t new_cols>
Matrix<T, new_rows, new_cols> dot(const Matrix<T, other_rows, other_cols> &other)
{
Matrix<T, rows, other_cols> data(0.0);
for (unsigned int row_index = 0; row_index < rows; row_index++)
{
for (unsigned int col_index = 0; col_index < other_cols; col_index++)
{
for (unsigned int index = 0; index < cols; index++)
{
data[row_index][col_index] += data_[row_index][index] * other[index][col_index];
}
}
}
return data;
}
private:
std::array<std::array<T, cols>, rows> data_;
};
int main(int argc, char const *argv[])
{
Matrix<double, 3, 3> a(0), b(1);
auto c = a.dot(b);
std::cout << c << std::endl;
return 0;
}