-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.h
More file actions
90 lines (82 loc) · 2.41 KB
/
Copy pathmatrix.h
File metadata and controls
90 lines (82 loc) · 2.41 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
#include<iostream>
#include<vector>
using namespace std;
class Matrix {
int r, c;
vector<vector<int>> mat;
public:
Matrix(int noOfRows=0, int noOfColumns=0) {
r = noOfRows;
c = noOfColumns;
mat.resize(noOfRows, vector<int>(noOfColumns));
}
Matrix(vector<vector<int>> const &obj) {
r = obj.size();
c = obj[0].size();
mat = obj;
}
void fill_matrix(vector<vector<int>> const &obj) {
int i, j;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
mat[i][j] = obj[i][j];
}
Matrix operator+(Matrix const &obj) {
Matrix res;
if (r != obj.r or c != obj.c)
{
cout << "Matrix addition is not possible!\n";
return res;
}
res = Matrix(r, c);
int i, j;
for (i = 0; i < obj.r; ++i)
for (j = 0; j < obj.c; ++j)
res.mat[i][j] = mat[i][j] + obj.mat[i][j];
return res;
}
Matrix operator-(Matrix const &obj) {
Matrix res;
if (r != obj.r or c != obj.c)
{
cout << "Matrix subtraction is not possible!\n";
return res;
}
res = Matrix(r, c);
int i, j;
for (i = 0; i < obj.r; ++i)
for (j = 0; j < obj.c; ++j)
res.mat[i][j] = mat[i][j] - obj.mat[i][j];
return res;
}
Matrix operator*(Matrix const &obj) {
Matrix res;
if (c != obj.r)
{
cout << "Matrix multiplication is not possible!\n";
return res;
}
res = Matrix(r, obj.c);
int i, j, k;
for (i = 0; i < r; ++i)
for (j = 0; j < obj.c; ++j)
{
int s = 0;
for (k = 0; k < c; ++k)
s += mat[i][k] * obj.mat[k][j];
res.mat[i][j] = s;
}
return res;
}
void print() {
int i, j;
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
cout << mat[i][j] << ' ';
}
cout << '\n';
}
}
};