-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.txt
More file actions
144 lines (123 loc) · 3.8 KB
/
Copy pathMatrix.txt
File metadata and controls
144 lines (123 loc) · 3.8 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
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
#include <iostream>
#include <vector>
using std::vector;
using std::cout;
using std::endl;
template <typename T>
class Matrix {
private:
vector<T> data;
public:
Matrix(const vector<vector<T>>& vec) {
for (int i = 0; i != vec.size(); ++i) {
for (int j = 0; j != vec[0].size(); ++j) {
data.push_back(vec[i][j]);
}
}
}
/*Matrix(const vector<vector<T>>& d)
: data(d) {
}
const T& operator() (size_t i, size_t j) const {
return data[i][j];
}
std::pair<size_t, size_t> size() const {
return std::pair<size_t, size_t> (data.size(), data[0].size());
}*/
typename vector<T>::iterator begin() {
return data.begin();
}
typename vector<T>::iterator end() {
return data.end();
}
typename vector<T>::const_iterator begin() const {
return data.begin();
}
typename vector<T>::const_iterator end() const {
return data.end();
}
/*T& operator() (size_t i, size_t j) {
return data[i][j];
}
Matrix& operator += (const Matrix& other) {
for (size_t i = 0; i != data.size(); ++i)
for (size_t j = 0; j != data[0].size(); ++j)
data[i][j] += other.data[i][j];
return *this;
}
Matrix operator + (const Matrix& other) const {
auto C = *this;
C += other;
return C;
}
template <typename M>
Matrix& operator *= (const M& other) {
for (size_t i = 0; i != data.size(); ++i)
for (size_t j = 0; j != data[0].size(); ++j)
data[i][j] *= other;
return *this;
}
template <typename M>
Matrix operator * (const M& other) const {
auto C = *this;
C *= other;
return C;
}
Matrix operator * (const Matrix& other) {
vector<vector<T>> new_m((*this).size().first, vector<T>(other.size().second, 0));
Matrix<T> new_mat = Matrix<T>(new_m);
for (size_t i = 0; i != this -> size().first; ++i) {
for (size_t j = 0; j != other.size().second; ++j) {
for (size_t p = 0; p != this -> size().second; ++p) {
new_mat(i, j) += (*this)(i, p) * other(p, j);
}
}
}
*this = new_mat;
return *this;
}
Matrix operator *= (const Matrix& other) {
vector<vector<T>> new_m((*this).size().first, vector<T>(other.size().second, 0));
Matrix<T> new_mat = Matrix<T>(new_m);
new_mat = *this * other;
return new_mat;
}
Matrix& transpose() {
vector<vector<T>> new_m(size().second, vector<T>(size().first, 0));
Matrix<T> new_mat = Matrix<T>(new_m);
for (size_t i = 0; i != this -> size().first; ++i)
for (size_t j = 0; j != this -> size().second; ++j) {
new_mat(j, i) = (*this)(i, j);
}
*this = new_mat;
return *this;
}
Matrix transposed() const {
Matrix m = *this;
m.transpose();
return m;
}*/
};
template <typename T>
std::ostream& operator << (std::ostream& out, const Matrix<T>& mat) {
for (size_t i = 0; i != mat.size().first; ++i) {
for (size_t j = 0; j != mat.size().second; ++j) {
out << mat(i, j);
if (j != mat.size().second - 1) {
out << "\t";
}
}
if (i != mat.size().first - 1) {
out << endl;
}
}
return out;
}
/*
int main() {
std::vector< std::vector<int>> vec {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
Matrix<int> mat(vec);
for (auto t = mat.begin(); t != mat.end(); ++t) {
cout << *t << " ";
}
}*/