-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.cpp
81 lines (72 loc) · 1.89 KB
/
temp.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back;
void print(auto a, auto b, string end="\n") {
for_each(a,b,[](auto & i){cout << i << " ";});
cout << end;
}
template <typename T>
class matrix{
public:
vector<vector<T> > vec;
// constructor
matrix(size_t i, size_t j = -1) {
if(j==-1) j = i;
vec.resize(i);
for(auto & v : vec) {
v.resize(j);
fill(v.begin(),v.end(),0);
}
}
matrix() {
}
vector<T> & operator[](size_t x) {
return vec[x];
}
// methods
void resize(size_t i, size_t j=-1) {
if(j==-1) j = i;
vec.resize(i);
for(auto & v : vec) {
v.resize(j);
}
}
size_t row_size() {
return vec.size();
}
size_t column_size() {
return vec[0].size();
}
void print(size_t i=-1, size_t j=-1) {
if(i==-1 && j==-1) { i = this->row_size(); j = this->column_size();}
else if(j==-1) {j = min(i,this->column_size());}
else {
i = min(i, this->row_size());
j = min(j, this->column_size());
}
for(size_t a=0; a<i; a++) {
for(size_t b=0; b<j; b++) cout << vec[a][b] << " ";
cout << endl;
}
}
// operator overloading for mathematical operations
matrix<T> operator*(matrix<T> & b) {
auto a = *this;
if(a.column_size() != b.row_size()) {
cout << "incompatible dimensions\n";
return matrix(0);
}
matrix<T> c(a.row_size(),b.column_size());
for(size_t i=0; i<a.row_size(); i++) {
for(size_t j=0; j<b.column_size(); j++) {
for(size_t k=0; k<a.column_size(); k++) {
c[i][j] = c[i][j] + (a[i][k])*(b[k][j]);
}
}
}
return c;
}
};
int main() {
}