STRASSEN’S MATRIX MULTIPLICATION

Example 1:

Complexity Analysis:

Strassen’s Matrix Multiplication

  1. Input the rows and columns of both the elements
  2. Check if the number of columns of first matrix is same as the rows of second matrix(condition for matrix multiplication).
  3. Use the Strassen’s formulae.
  4. Keep values in the final matrix.
  5. Next, display the final matrix.
#include<iostream>   
using namespace std;
double A[4][4]; #input the A and B matrices
double B[4][4];

void insert(double a[4][4])
{
double val;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cin>>val;
a[i][j]=val;
}
}
}
double cal11(double a[4][4])
{
return (a[1][1] * a[1][2])+ (a[1][2] * a[2][1]);
}
double cal21(double a[4][4])
{
return (a[3][1] * a[4][2])+ (a[3][2] * a[4][1]);
}

double cal12(double a[4][4])
{
return (a[1][3] * a[2][4])+ (a[1][4] * a[2][3]);
}

double cal22(double a[4][4])
{
return (a[2][3] * a[1][4])+ (a[2][4] * a[1][3]);
}

int main()
{
double a11,a12,a22,a21,b11,b12,b21,b22,a[4][4],b[4][4];
double p,q,r,s,t,u,v,c11,c12,c21,c22;
cout<<"\n a: \n";
insert(a);
cout<<"\n b: \n";
insert(b);

a11=cal11(a);
a12=cal12(a);
a21=cal21(a);
a22=cal22(a);
b11=cal11(b);
b12=cal12(b);
b21=cal21(b);
b22=cal22(b);

p=(a11+a22)*(b11+b22);
q=(a21+a22)*b11;
r=a11*(b12-b22);
s=a22*(b21-b11);
t=(a11+a12)*b22;
u=(a11-a21)*(b11+b12);
v=(a12-a22)*(b21+b22);
cout<<"\n final matrix";
cout<<"\n"<<p+s-t+v<<" "<<r+t;
cout<<"\n"<<q+s<<" "<<p+r-q+u;
return 0;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store