-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
54 lines (47 loc) · 1.32 KB
/
Copy pathMatrix.cpp
File metadata and controls
54 lines (47 loc) · 1.32 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
#include <iostream>
#include <vector>
class Matrix{
public:
std::vector<std::vector<double>> data;
int rows, cols;
Matrix(int rows, int cols) : rows(rows), cols(cols){
data.resize(rows, std::vector<double>(cols, 0.0)); /*0.0 initialized the values to zero*/
}
void set2zero(){
for(int i=0; i<rows; ++i){
for(int ii=0; ii<cols; ++ii){
data[i][ii]=0;
}
}
}
Matrix operator+(const Matrix& other) const {
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] + other.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix& other) const {
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] - other.data[i][j];
}
}
return result;
}
Matrix operator*(float scalar) const {
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;
}
};
// int main(){
// return 0;
// }