-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix multiplication.cpp
More file actions
69 lines (66 loc) · 1.51 KB
/
Copy pathMatrix multiplication.cpp
File metadata and controls
69 lines (66 loc) · 1.51 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
#include<bits/stdc++.h>
using namespace std;
using type=int;// Kieu du lieu cac phan tu trong ma tran
struct Matrix{
vector <vector <type> > data;
int row() const{return data.size();}
int col() const{ return data[0].size();}
auto & operator[] (int i) { return data[i];}
const auto & operator[] (int i) const {return data[i];}
Matrix()=default;
Matrix(int r, int c): data(r, vector<type> (c)){}
Matrix(const vector<vector <type> > &d): data(d){}
// in ra matran
friend ostream & operator<< (ostream &out, const Matrix &d){
for(auto x: d.data){
for(auto y: x) out<<y<<' ';
out<<'\n';
}
return out;
}
// ma tran don vi
static Matrix identity(long long n){
Matrix a= Matrix(n,n);
while(n--) a[n][n]=1;
return a;
}
// nhan ma tran
Matrix operator * (const Matrix &b){
Matrix a=*this;
assert(a.col()==b.row());
Matrix c(a.row(), b.col());
for(int i=0;i<a.row();i++)
for(int j=0;j<b.col();j++)
for(int k=0;k<a.col();k++)
c[i][j]+=a[i][k]*b[k][j];
return c;
}
// luy thua ma tran
Matrix pow(long long exp){
assert(row()==col());
Matrix base=*this,ans=identity(row());
for(;exp;exp>>=1,base=base*base)
if(exp&1) ans=ans*base;
return ans;
}
};
int main(){
Matrix a({
{1, 2},
{3, 4}}
);
Matrix b({
{0, 10, 100},
{1, 1, 10}
});
cout<<a*b<<'\n';
cout<<a.pow(3)<<'\n';
b=a;
cout<<b<<'\n';
b= Matrix::identity(3);
cout<<b<<'\n';
b=Matrix(2,3);
cout<<b<<'\n';
Matrix c(3,2);
cout<<c<<'\n';
}