Repository navigation
Expand file tree
/
Copy pathMatrix.cpp
More file actions
244 lines (191 loc) · 5.72 KB
/
Copy pathMatrix.cpp
File metadata and controls
244 lines (191 loc) · 5.72 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "Matrix.h"
#include <stdexcept>
Matrix::Matrix() : rows(0), cols(0) {}
Matrix::Matrix(int r, int c) : rows(r), cols(c) {
if (rows <= 0 || cols <= 0)
throw invalid_argument("Matrix dimensions must be positive and not zero");
data.resize(rows, std::vector<float>(cols, 0.0f));
}
Matrix::Matrix(const vector<vector<float>>& d, const int r, const int c) {
if (d.size() != r || d[0].size() != c)
throw invalid_argument("Invalid Matrix dimensions");
rows = r;
cols = c;
data = d;
}
/*Matrix::Matrix(const vector<float>& d, const int r, const int c) {
if (d.size() != r || c != 0)
throw invalid_argument("vector is empty");
*this = toMatrix(d);
}*/
Matrix::Matrix(const Matrix& other)
: rows(other.rows), cols(other.cols), data(other.data) {}
Matrix Matrix::identity(int size) {
Matrix identityMatrix(size, size);
for (int i = 0; i < size; i++) {
identityMatrix.data[i][i] = 1.0f;
}
return identityMatrix;
}
Matrix Matrix::zeroMatrix(int r, int c) {
return Matrix(r, c);
}
Matrix Matrix::toMatrix(const vector<float>& vec, bool asRow) {
if (vec.empty())
throw invalid_argument("vector is empty");
int rows = asRow ? 1 : vec.size();
int cols = asRow ? vec.size() : 1;
Matrix m(rows, cols);
for (int i = 0; i < vec.size(); i++) {
if (asRow)
m.data[0][i] = vec[i]; // row vector
else
m.data[i][0] = vec[i]; // column vector
}
return m;
}
vector<float> Matrix::toVector() const {
vector<float> vec;
for (int i = 0; i < rows; i++) {
vec.push_back(data[i][0]);
}
return vec;
}
Matrix Matrix::operator+(Matrix const& obj) {
/*if (rows != obj.rows || cols != obj.cols)
throw std::invalid_argument("Matrix dimensions must match for addition");*/
Matrix m(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m.data[i][j] = data[i][j] + obj.data[i][j];
}
}
return m;
}
Matrix Matrix::operator-(Matrix const& obj) {
if (rows != obj.rows || cols != obj.cols)
throw std::invalid_argument("Matrix dimensions must match for subtraction");
Matrix m(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
m.data[i][j] = data[i][j] - obj.data[i][j];
}
}
return m;
}
Matrix Matrix::operator*(Matrix const& obj) {
if (cols != obj.rows)
throw std::invalid_argument("cols num of first matrix is not equal to rows num of second matrix");
Matrix result(rows, obj.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < obj.cols; j++) {
float summ = 0.0f;
for (int x = 0; x < cols; x++) {
summ += (data[i][x] * obj.data[x][j]);
}
result.data[i][j] = summ;
}
}
return result;
}
Matrix& Matrix::operator+=(const Matrix& obj) {
if (rows != obj.rows || cols != obj.cols)
throw std::invalid_argument("Matrix dimensions must match for addition");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] += obj.data[i][j];
}
}
return *this;
}
Matrix& Matrix::operator-=(const Matrix& obj) {
if (rows != obj.rows || cols != obj.cols)
throw std::invalid_argument("Matrix dimensions must match for subtraction");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] -= obj.data[i][j];
}
}
return *this;
}
Matrix& Matrix::operator*=(const Matrix& obj) {
if (cols != obj.rows)
throw std::invalid_argument("Number of columns of the first matrix must be equal to number of rows of the second matrix");
Matrix result(rows, obj.cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < obj.cols; j++) {
float summ = 0.0f;
for (int x = 0; x < cols; x++) {
summ += (data[i][x] * obj.data[x][j]);
}
result.data[i][j] = summ;
}
}
rows = result.rows;
cols = result.cols;
data = result.data;
return *this;
}
Matrix Matrix::operator*(float scalar) {
Matrix result(rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result.data[i][j] = data[i][j] * scalar;
}
}
return result;
}
Matrix Matrix::operator*=(float scalar) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] *= scalar;
}
}
return *this;
}
Matrix& Matrix::operator=(const Matrix& other) {
if (this == &other) return *this;
this->rows = other.rows;
this->cols = other.cols;
this->data = other.data;
return *this;
}
bool Matrix::operator==(const Matrix& other) const {
if (rows != other.rows || cols != other.cols)
return false;
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (data[i][j] != other.data[i][j])
return false;
return true;
}
Matrix Matrix::transpose() const {
Matrix transposed(cols, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed.data[j][i] = data[i][j];
}
}
return transposed;
}
float& Matrix::in(int i, int j) {
return data[i][j];
}
float Matrix::at(int i, int j)const {
return data[i][j];
}
int Matrix::getRows() const {
return rows;
}
int Matrix::getCols() const {
return cols;
}
ostream& operator<<(ostream& os, const Matrix& m) {
for (int i = 0; i < m.rows; ++i) {
for (int j = 0; j < m.cols; ++j) {
os << m.data[i][j] << " ";
}
os << endl;
}
return os;
}