TheAlgorithms-C-Plus-Plus/others/Strassen Matrix Multiplication.cpp

56 lines
1005 B
C++
Raw Normal View History

2017-12-24 01:30:49 +08:00
#include <iostream>
using namespace std;
Multiply(int A[][], int B[][], int n)
{
2019-08-21 10:10:08 +08:00
if (n == 2)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
int p2 = (a[1][0] + a[1][1]) * b[0][0];
int p3 = a[0][0] * (b[0][1] - b[1][1]);
int p4 = a[1][1] * (b[1][0] - b[0][0]);
int p5 = (a[0][0] + a[0][1]) * b[1][1];
int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]);
int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]);
int c[n][n];
c[0][0] = p1 + p4 - p5 + p7;
c[0][1] = p3 + p5;
c[1][0] = p2 + p4;
c[1][1] = p1 - p2 + p3 + p6;
return c[][];
2017-12-24 01:30:49 +08:00
}
else
{
}
}
int main()
{
2019-08-21 10:10:08 +08:00
int p, q, r, s;
cout << "Enter the dimensions of Matrices";
cin >> n;
int A[n][n], ;
int B[n][n], ;
cout << "Enter the elements of Matrix A";
2017-12-24 01:30:49 +08:00
for (int i = 0; i < n; i++)
{
2019-08-21 10:10:08 +08:00
for (int j = 0; j < n; j++)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cin >> A[i][j];
2017-12-24 01:30:49 +08:00
}
}
2019-08-21 10:10:08 +08:00
cout << "Enter the elements of Matrix B";
2017-12-24 01:30:49 +08:00
for (int i = 0; i < n; i++)
{
2019-08-21 10:10:08 +08:00
for (int j = 0; j < n; j++)
2017-12-24 01:30:49 +08:00
{
2019-08-21 10:10:08 +08:00
cin >> B[i][j];
2017-12-24 01:30:49 +08:00
}
}
Multiply(A, B, n);
return 0;
}