-
Notifications
You must be signed in to change notification settings - Fork 21
/
Strassen.cpp
78 lines (73 loc) · 3.37 KB
/
Strassen.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
#include<iostream>
using namespace std;
class Strassen
{
int i,j,a[2][2],b[2][2],c[2][2],p1,p2,p3,p4,p5,p6,p7;
public:
Strassen()
{
p1=0,p2=0,p3=0,p4=0,p5=0,p6=0,p7=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j] = 0;
b[i][j] = 0;
c[i][j] = 0;
}
}
}
void read()
{
cout<<"Matrix 1 : "<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Matrix 2 : "<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}
}
}
void cal()
{
p1 = (a[0][0] + a[1][1])*(b[0][0] + b[1][1]);
p2 = (a[1][0] + a[1][1])*b[0][0];
p3 = a[0][0]*(b[0][1] - b[1][1]);
p4 = a[1][1]*(b[1][0] - b[0][0]);
p5 = (a[0][0] + a[0][1])*b[1][1];
p6 = (a[1][0] - a[0][0])*(b[0][0] + b[0][1]);
p7 = (a[0][1] - a[1][1])*(b[1][0] + b[1][1]);
c[0][0] = p1 + p4 + - p5 + p7;
c[0][1] = p3 + p5;
c[1][0] = p2 + p4;
c[1][1] = p1 + p3 - p2 + p6;
}
void print()
{
cout<<"Resultant Matrix : "<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
}
};
main()
{
Strassen s;
cout<<"Enter the Matrix"<<endl;
s.read();
s.cal();
s.print();
}