-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixMultlipication.java
220 lines (165 loc) · 5.48 KB
/
matrixMultlipication.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package matrixMultlipication;
/*
* Algorithm Project
* Matrix Multiplication
*
* Name: Athoug Alsoughayer
* GWID: G38361988
*
*/
// for user input
import java.util.Scanner;
public class matrixMultlipication {
public int[][] mult(int[][] A, int[][] B){
int n = A.length;
int[][] C = new int[n][n]; // declaring the new array outcome of the multiplication
// base case
if(n == 1){
C[0][0] = A[0][0] * B[0][0];
} else {
int[][] A11 = new int[n/2][n/2];
int[][] A12 = new int[n/2][n/2];
int[][] A21 = new int[n/2][n/2];
int[][] A22 = new int[n/2][n/2];
int[][] B11 = new int[n/2][n/2];
int[][] B12 = new int[n/2][n/2];
int[][] B21 = new int[n/2][n/2];
int[][] B22 = new int[n/2][n/2];
// dividing the matrix into 4 halves
// splitting the first matrix
split(A, A11, 0, 0);
split(A, A12, 0, n/2);
split(A, A21, n/2, 0);
split(A, A22, n/2, n/2);
// splitting the second matrix
split(B, B11, 0, 0);
split(B, B12, 0, n/2);
split(B, B21, n/2, 0);
split(B, B22, n/2, n/2);
// applying Strassen's algorithm of dividing the multiplication into their complex form
// P1 =(A11 + A22)*(B11 + B22), P2 = (A21 + A22)*B11, P3 = A11*(B12 - B22), P4 = A22*(B21 - B11)
// P5 = (A11 + A12)*B22, P6 = (A21 - A11)*(B11 + B12), P7= (A12 - A22)*(B21 + B22)
int[][] P1 =mult(add(A11, A22), add(B11, B22));
int[][] P2 =mult(add(A21, A22) ,B11);
int[][] P3 =mult(A11, sub(B12, B22));
int[][] P4 =mult(A22, sub(B21, B11));
int[][] P5 =mult(add(A11, A12), B22);
int[][] P6 =mult(sub(A21, A11) ,add(B11, B12));
int[][] P7 =mult(sub(A12, A22), add(B21, B22));
// once we did the multiplication we then combine them using the addition that will make up
//the third matrix such that C11 = P1 + P4 - P5 + P7, C12 = P3 + P5, C21 = P2 + P4, C22 = P1 - P2 + P3 + P6
int[][] C11 = add(sub(add(P1, P4), P5), P7);
int[][] C12 = add(P3, P5);
int[][] C21 = add(P2, P4);
int[][] C22 = add(sub(add(P1, P3), P2), P6);
// the final step of divide and concur which is merging the results back together
merg(C11, C, 0, 0);
merg(C12, C, 0, n/2);
merg(C21, C, n/2, 0);
merg(C22, C, n/2, n/2);
}
//returning the result of the multiplication
return C;
}
// function to subtract two matrices
public int[][] sub(int[][] A, int[][] B){
int n = A.length;
int[][] C = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
C[i][j] = A[i][j] - B[i][j];
}
}
return C;
}
// function to add two matrices
public int[][] add(int[][] A, int[][] B){
int n = A.length;
int[][] C = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}
// splitting original matrix into leaf matrices
public void split(int[][] P, int[][] C, int iB, int jB){
for(int i1 = 0, i2 =iB; i1 < C.length; i1++, i2++){
for(int j1 = 0, j2 = jB; j1 < C.length; j1++, j2++){
C[i1][j1] = P[i2][j2];
}
}
}
// merging the matrices together
public void merg(int[][] C, int[][] P, int iB, int jB){
for(int i1=0, i2=iB; i1<C.length; i1++, i2++){
for(int j1=0, j2=jB; j1<C.length; j1++, j2++){
P[i2][j2] = C[i1][j1];
}
}
}
// main function for implementing the algorithm
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Strassen's Matrix Multiplication Algorithm");
System.out.println();
// make an object of the multiplication class
matrixMultlipication m = new matrixMultlipication();
System.out.println("Enter the order of matrix (N): ");
int n = scan.nextInt();
// take a 2D matrix
System.out.println("Randomly generated first matrix:\n");
int[][] A = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
A[i][j] = (int)(Math.random()*100);
System.out.print(A[i][j] + " ");
}
System.out.println();
}
System.out.println("Randomly generated second matrix:\n");
int[][] B = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n;j++){
B[i][j] = (int)(Math.random()*100);
System.out.print(B[i][j] + " ");
}
System.out.println();
}
// calculating matrix multiplication using brute force (naive) approach
int[][] bruteForce = new int[n][n];
// set starting time
long traditionalStartTime = System.nanoTime();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int k=0; k<n; k++){
bruteForce[i][j] += A[i][k] * B[k][j];
}
}
}
// set end time
long traditionalendTime = System.nanoTime();
// calculate the duration of multiplication
long traditionalduration = (traditionalendTime - traditionalStartTime)/ 1000000;
// calculating matrix multiplication using Strassen's Algorithm
// set starting time
long StartTime = System.nanoTime();
// Start multiplying
int[][] C = m.mult(A, B);
// set end time
long endTime = System.nanoTime();
// calculate the duration of multiplication
long duration = (endTime - StartTime)/ 1000000;
System.out.println("\nThe multiplication result of A * B is: ");
for(int i=0; i < n; i++){
for(int j=0; j < n; j++){
System.out.print(C[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.printf("It took %d miliseconds to calculate using Brute force approcah\nIt took %d miliseconds to calculate using Strassen's Algorithm", traditionalduration, duration);
System.out.println();
}
}