-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
105 lines (86 loc) · 2.3 KB
/
Copy pathmatrix.cpp
File metadata and controls
105 lines (86 loc) · 2.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
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
#include "matrix.h"
Matrix::Matrix(size_t n, size_t m) : _n(n), _m(m)
{
_rows = std::vector<std::vector<float>>(_n);
for (unsigned int i = 0; i < _n; ++i) {
_rows[i] = std::vector<float>(m, 0);
}
}
Matrix::Matrix(std::initializer_list<std::initializer_list<float>> list) :
_rows(list.begin(),list.end())
{
_n = _rows.size();
_m = _rows[0].size();
}
Matrix::Matrix(const Matrix &other) : _n(other._n), _m(other._m), _rows(other._rows)
{
}
Matrix::Matrix(Matrix &&other) : _n(other._n), _m(other._m), _rows(other._rows)
{
}
Matrix& Matrix::operator=(const Matrix &right)
{
this->_n = right._n;
this->_m = right._m;
this->_rows = right._rows;
return *this;
}
Matrix& Matrix::operator=(Matrix &&right)
{
this->_n = right._n;
this->_m = right._m;
this->_rows = right._rows;
return *this;
}
Matrix Matrix::operator*(const Matrix &right) const
{
Matrix res(this->_n, right._m);
// TODO: переписать на Алгоритм Винограда
for (int i = 0; i < _n; ++i) {
for (int j = 0; j < right._m; ++j) {
for (int k = 0; k < _m; ++k) {
res._rows[i][j] += this->_rows[i][k] * right._rows[k][j];
}
}
}
return res;
}
Matrix Matrix::operator+(const Matrix &right) const
{
if (this->_n != right._n || this->_m != right._m) {
throw MatrixException("Sizes of matrix are not equal");
}
Matrix res(this->_n, this->_m);
for (int i = 0; i < _n; ++i) {
for (int j = 0; j < _m; ++j) {
res._rows[i][j] = this->_rows[i][j] + right._rows[i][j];
}
}
return res;
}
Matrix &Matrix::operator+=(const Matrix &right)
{
if (this->_n != right._n || this->_m != right._m) {
throw MatrixException("Sizes of matrix are not equal");
}
for (int i = 0; i < _n; ++i) {
for (int j = 0; j < _m; ++j) {
this->_rows[i][j] = this->_rows[i][j] + right._rows[i][j];
}
}
return *this;
}
std::vector<float> &Matrix::operator[](size_t i)
{
if (i >= _n) {
throw MatrixException("Reading besides the border");
}
return _rows[i];
}
const std::vector<float> &Matrix::operator[](size_t i) const
{
if (i >= _n) {
throw MatrixException("Reading besides the border");
}
return _rows[i];
}