3
3
/*
4
4
This class implements the Randomized Matrix Multiplication Verification.
5
5
It generates a random vector and performs verification using Freivalds' Algorithm.
6
- @author: Menil-dev
6
+ @author Menil-dev
7
7
*/
8
- public class MatrixMultiplicationVerifier {
8
+ public final class MatrixMultiplicationVerifier {
9
9
10
10
private MatrixMultiplicationVerifier () {
11
11
throw new UnsupportedOperationException ("Utility class" );
@@ -22,28 +22,29 @@ private MatrixMultiplicationVerifier() {
22
22
static int [] multiply (int [][] matrix , int [] vector ) {
23
23
int n = vector .length ;
24
24
int [] result = new int [n ];
25
- for (int i = 0 ; i < n ; i ++) {
26
- for (int j = 0 ; j < n ; j ++) {
25
+ for (int i = 0 ; i < n ; i ++)
26
+ for (int j = 0 ; j < n ; j ++)
27
27
result [i ] += matrix [i ][j ] * vector [j ];
28
- }
29
- }
30
28
return result ;
31
29
}
32
30
33
31
/*
34
32
Actual function that performs verification function
35
33
@params, all three input matrices of int type, number of iterations
36
34
*/
37
- public static boolean verify (int [][] A , int [][] B , int [][] C , int iterations ) {
38
- if (A .length == 0 || B .length == 0 || C .length == 0 || A [0 ].length == 0 || B [0 ].length == 0 || C [0 ].length == 0 ) {
39
- return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ; // Basic dimension consistency check
35
+ public static boolean verify (int [][] matrixA , int [][] matrixB , int [][] matrixC , int iterations ) {
36
+ if (matrixA .length == 0 || matrixB .length == 0 || matrixC .length == 0
37
+ || matrixA [0 ].length == 0 || matrixB [0 ].length == 0 || matrixC [0 ].length == 0 ) {
38
+ return matrixA .length == matrixB [0 ].length
39
+ && matrixB .length == matrixC .length
40
+ && matrixC [0 ].length == matrixA [0 ].length ;
40
41
}
41
42
42
43
if (iterations <= 0 ) {
43
44
throw new IllegalArgumentException ("Number of iterations must be positive" );
44
45
}
45
46
46
- int n = A .length ;
47
+ int n = matrixA .length ;
47
48
if (iterations > 2 * n ) {
48
49
throw new IllegalArgumentException ("Number of iterations should not exceed 2 * n where n is the matrix size" );
49
50
}
@@ -55,17 +56,16 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
55
56
r [i ] = rand .nextInt (2 );
56
57
}
57
58
58
- int [] Br = multiply (B , r );
59
- int [] ABr = multiply (A , Br );
60
- int [] Cr = multiply (C , r );
59
+ int [] matrixBtimesR = multiply (matrixB , r );
60
+ int [] matrixAtimesBtimesR = multiply (matrixA , matrixBtimesR );
61
+ int [] matrixCtimesR = multiply (matrixC , r );
61
62
62
63
for (int i = 0 ; i < n ; i ++) {
63
- if (ABr [i ] != Cr [i ]) {
64
+ if (matrixAtimesBtimesR [i ] != matrixCtimesR [i ]) {
64
65
return false ;
65
66
}
66
67
}
67
68
}
68
-
69
69
return true ;
70
70
}
71
71
@@ -79,50 +79,50 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
79
79
static double [] multiply (double [][] matrix , double [] vector ) {
80
80
int n = vector .length ;
81
81
double [] result = new double [n ];
82
- for (int i = 0 ; i < n ; i ++) {
83
- for (int j = 0 ; j < n ; j ++) {
82
+ for (int i = 0 ; i < n ; i ++)
83
+ for (int j = 0 ; j < n ; j ++)
84
84
result [i ] += matrix [i ][j ] * vector [j ];
85
- }
86
- }
87
85
return result ;
88
86
}
89
87
90
88
/*
91
89
Actual function that performs the verification.
92
90
@params, all three input matrices of double type, number of iterations
93
91
*/
94
- public static boolean verify (double [][] A , double [][] B , double [][] C , int iterations ) {
95
- if (A .length == 0 || B .length == 0 || C .length == 0 || A [0 ].length == 0 || B [0 ].length == 0 || C [0 ].length == 0 ) {
96
- return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ;
92
+ public static boolean verify (double [][] matrixA , double [][] matrixB , double [][] matrixC , int iterations ) {
93
+ if (matrixA .length == 0 || matrixB .length == 0 || matrixC .length == 0
94
+ || matrixA [0 ].length == 0 || matrixB [0 ].length == 0 || matrixC [0 ].length == 0 ) {
95
+ return matrixA .length == matrixB [0 ].length
96
+ && matrixB .length == matrixC .length
97
+ && matrixC [0 ].length == matrixA [0 ].length ;
97
98
}
98
99
99
100
if (iterations <= 0 ) {
100
101
throw new IllegalArgumentException ("Number of iterations must be positive" );
101
102
}
102
103
103
- int m = A .length ;
104
+ int m = matrixA .length ;
104
105
if (iterations > 2 * m ) {
105
- throw new IllegalArgumentException ("Number of iterations should not exceed 2 times m where n is the matrix size" );
106
+ throw new IllegalArgumentException ("Number of iterations should not exceed 2 times m where m is the matrix size" );
106
107
}
107
108
108
109
Random rand = new Random ();
109
110
for (int t = 0 ; t < iterations ; t ++) {
110
111
double [] randomizedVector = new double [m ];
111
112
for (int i = 0 ; i < m ; i ++) {
112
- randomizedVector [i ] = rand .nextInt (2 ); // Random binary values 0 or 1
113
+ randomizedVector [i ] = rand .nextInt (2 );
113
114
}
114
115
115
- double [] Br = multiply (B , randomizedVector );
116
- double [] ABr = multiply (A , Br );
117
- double [] Cr = multiply (C , randomizedVector );
116
+ double [] matrixBtimesR = multiply (matrixB , randomizedVector );
117
+ double [] matrixAtimesBtimesR = multiply (matrixA , matrixBtimesR );
118
+ double [] matrixCtimesR = multiply (matrixC , randomizedVector );
118
119
119
120
for (int i = 0 ; i < m ; i ++) {
120
- if (Math .abs (ABr [i ] - Cr [i ]) > 1e-9 ) {
121
+ if (Math .abs (matrixAtimesBtimesR [i ] - matrixCtimesR [i ]) > 1e-9 ) {
121
122
return false ;
122
123
}
123
124
}
124
125
}
125
-
126
126
return true ;
127
127
}
128
128
}
0 commit comments