Skip to content

Commit 4ced569

Browse files
committed
Added program to implement Strassens Multiplication
1 parent fed3fd9 commit 4ced569

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Programs/StrassensMultiplication.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Program to multiply two square matrices using Strassen's Multiplication
2+
import java.util.*;
3+
public class StrassensMultiplication
4+
{
5+
public static void main(String[] args)
6+
{
7+
int n,m;
8+
Scanner sc = new Scanner(System.in);
9+
System.out.println("Enter the number of rows and columns of the first matrix : ");
10+
n = sc.nextInt();
11+
System.out.println("Enter the number of rows and columns of the second matrix : ");
12+
m = sc.nextInt();
13+
int[][] a = new int[n][n];
14+
int[][] b = new int[m][m];
15+
int[][] c = new int[2][2];
16+
int i, j, m1, m2, m3, m4, m5, m6, m7;
17+
18+
//Taking input from the user
19+
System.out.println("Enter all the elemens of the first matrix : ");
20+
for(i=0; i<n; i++)
21+
{
22+
for(j=0; j<n; j++)
23+
{
24+
a[i][j] = sc.nextInt();
25+
}
26+
}
27+
System.out.println("Enter all the elemens of the second matrix : ");
28+
for(i=0; i<m; i++)
29+
{
30+
for(j=0; j<m; j++)
31+
{
32+
b[i][j] = sc.nextInt();
33+
}
34+
}
35+
36+
//Strassen's multiplication
37+
m1= (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
38+
m2= (a[1][0] + a[1][1]) * b[0][0];
39+
m3= a[0][0] * (b[0][1] - b[1][1]);
40+
m4= a[1][1] * (b[1][0] - b[0][0]);
41+
m5= (a[0][0] + a[0][1]) * b[1][1];
42+
m6= (a[1][0] - a[0][0]) * (b[0][0]+b[0][1]);
43+
m7= (a[0][1] - a[1][1]) * (b[1][0]+b[1][1]);
44+
45+
c[0][0] = m1 + m4- m5 + m7;
46+
c[0][1] = m3 + m5;
47+
c[1][0] = m2 + m4;
48+
c[1][1] = m1 - m2 + m3 + m6;
49+
50+
//Display the output matrix
51+
System.out.println("Ouput Matrix : ");
52+
for(i=0; i<n; i++)
53+
{
54+
for(j=0; j<m; j++)
55+
{
56+
System.out.print(c[i][j]+"\t");
57+
}
58+
System.out.println();
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)