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
8
public class MatrixMultiplicationVerifier {
9
9
@@ -20,10 +20,13 @@ private MatrixMultiplicationVerifier() {
20
20
@returns matrix of calculated dot product.
21
21
*/
22
22
static int [] multiply (int [][] matrix , int [] vector ) {
23
- int n = vector .length , result [] = new int [n ];
24
- for (int i = 0 ; i < n ; i ++)
25
- for (int j = 0 ; j < n ; j ++)
23
+ int n = vector .length ;
24
+ int [] result = new int [n ];
25
+ for (int i = 0 ; i < n ; i ++) {
26
+ for (int j = 0 ; j < n ; j ++) {
26
27
result [i ] += matrix [i ][j ] * vector [j ];
28
+ }
29
+ }
27
30
return result ;
28
31
}
29
32
@@ -35,25 +38,34 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
35
38
if (A .length == 0 || B .length == 0 || C .length == 0 || A [0 ].length == 0 || B [0 ].length == 0 || C [0 ].length == 0 ) {
36
39
return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ; // Basic dimension consistency check
37
40
}
38
- // Basic integrity checks on number of iterations.
41
+
39
42
if (iterations <= 0 ) {
40
43
throw new IllegalArgumentException ("Number of iterations must be positive" );
41
44
}
45
+
42
46
int n = A .length ;
43
47
if (iterations > 2 * n ) {
44
48
throw new IllegalArgumentException ("Number of iterations should not exceed 2 * n where n is the matrix size" );
45
49
}
46
50
47
- // Actual logic to verify the multiplication
48
51
Random rand = new Random ();
49
52
for (int t = 0 ; t < iterations ; t ++) {
50
53
int [] r = new int [n ];
51
- // This generates a random binary vector of the first dimension of C matrix (Output Matrix).
52
- for (int i = 0 ; i < n ; i ++) r [i ] = rand .nextInt (2 );
53
- int [] Br = multiply (B , r ), ABr = multiply (A , Br ), Cr = multiply (C , r );
54
- for (int i = 0 ; i < n ; i ++)
55
- if (ABr [i ] != Cr [i ]) return false ; // If any product mismatches, return condition.
54
+ for (int i = 0 ; i < n ; i ++) {
55
+ r [i ] = rand .nextInt (2 );
56
+ }
57
+
58
+ int [] Br = multiply (B , r );
59
+ int [] ABr = multiply (A , Br );
60
+ int [] Cr = multiply (C , r );
61
+
62
+ for (int i = 0 ; i < n ; i ++) {
63
+ if (ABr [i ] != Cr [i ]) {
64
+ return false ;
65
+ }
66
+ }
56
67
}
68
+
57
69
return true ;
58
70
}
59
71
@@ -67,9 +79,11 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
67
79
static double [] multiply (double [][] matrix , double [] vector ) {
68
80
int n = vector .length ;
69
81
double [] result = new double [n ];
70
- for (int i = 0 ; i < n ; i ++)
71
- for (int j = 0 ; j < n ; j ++)
82
+ for (int i = 0 ; i < n ; i ++) {
83
+ for (int j = 0 ; j < n ; j ++) {
72
84
result [i ] += matrix [i ][j ] * vector [j ];
85
+ }
86
+ }
73
87
return result ;
74
88
}
75
89
@@ -79,33 +93,36 @@ static double[] multiply(double[][] matrix, double[] vector) {
79
93
*/
80
94
public static boolean verify (double [][] A , double [][] B , double [][] C , int iterations ) {
81
95
if (A .length == 0 || B .length == 0 || C .length == 0 || A [0 ].length == 0 || B [0 ].length == 0 || C [0 ].length == 0 ) {
82
- return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ; // Basic dimension consistency check
96
+ return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ;
83
97
}
84
- // Basic integrity checks on number of iterations.
98
+
85
99
if (iterations <= 0 ) {
86
100
throw new IllegalArgumentException ("Number of iterations must be positive" );
87
101
}
102
+
88
103
int m = A .length ;
89
104
if (iterations > 2 * m ) {
90
105
throw new IllegalArgumentException ("Number of iterations should not exceed 2 times m where n is the matrix size" );
91
106
}
92
107
93
- // Actual logic to verify the multiplication
94
108
Random rand = new Random ();
95
109
for (int t = 0 ; t < iterations ; t ++) {
96
110
double [] randomizedVector = new double [m ];
97
- // This generates a random binary vector of the first dimension of C matrix (Output Matrix).
98
- for (int i = 0 ; i < m ; i ++)
111
+ for (int i = 0 ; i < m ; i ++) {
99
112
randomizedVector [i ] = rand .nextInt (2 ); // Random binary values 0 or 1
113
+ }
100
114
101
115
double [] Br = multiply (B , randomizedVector );
102
116
double [] ABr = multiply (A , Br );
103
117
double [] Cr = multiply (C , randomizedVector );
104
118
105
- for (int i = 0 ; i < m ; i ++)
106
- if (Math .abs (ABr [i ] - Cr [i ]) > 1e-9 ) // Allowing a small tolerance for floating-point comparisons
107
- return false ; // If any product mismatches, return false.
119
+ for (int i = 0 ; i < m ; i ++) {
120
+ if (Math .abs (ABr [i ] - Cr [i ]) > 1e-9 ) {
121
+ return false ;
122
+ }
123
+ }
108
124
}
125
+
109
126
return true ;
110
127
}
111
128
}
0 commit comments