33/*
44 This class implements the Randomized Matrix Multiplication Verification.
55 It generates a random vector and performs verification using Freivalds' Algorithm.
6- @author Menil-dev
6+ Author: Menil-dev
77 */
88public class MatrixMultiplicationVerifier {
99
@@ -20,10 +20,13 @@ private MatrixMultiplicationVerifier() {
2020 @returns matrix of calculated dot product.
2121 */
2222 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 ++) {
2627 result [i ] += matrix [i ][j ] * vector [j ];
28+ }
29+ }
2730 return result ;
2831 }
2932
@@ -35,25 +38,34 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
3538 if (A .length == 0 || B .length == 0 || C .length == 0 || A [0 ].length == 0 || B [0 ].length == 0 || C [0 ].length == 0 ) {
3639 return A .length == B [0 ].length && B .length == C .length && C [0 ].length == A [0 ].length ; // Basic dimension consistency check
3740 }
38- // Basic integrity checks on number of iterations.
41+
3942 if (iterations <= 0 ) {
4043 throw new IllegalArgumentException ("Number of iterations must be positive" );
4144 }
45+
4246 int n = A .length ;
4347 if (iterations > 2 * n ) {
4448 throw new IllegalArgumentException ("Number of iterations should not exceed 2 * n where n is the matrix size" );
4549 }
4650
47- // Actual logic to verify the multiplication
4851 Random rand = new Random ();
4952 for (int t = 0 ; t < iterations ; t ++) {
5053 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+ }
5667 }
68+
5769 return true ;
5870 }
5971
@@ -67,9 +79,11 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
6779 static double [] multiply (double [][] matrix , double [] vector ) {
6880 int n = vector .length ;
6981 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 ++) {
7284 result [i ] += matrix [i ][j ] * vector [j ];
85+ }
86+ }
7387 return result ;
7488 }
7589
@@ -79,33 +93,36 @@ static double[] multiply(double[][] matrix, double[] vector) {
7993 */
8094 public static boolean verify (double [][] A , double [][] B , double [][] C , int iterations ) {
8195 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 ;
8397 }
84- // Basic integrity checks on number of iterations.
98+
8599 if (iterations <= 0 ) {
86100 throw new IllegalArgumentException ("Number of iterations must be positive" );
87101 }
102+
88103 int m = A .length ;
89104 if (iterations > 2 * m ) {
90105 throw new IllegalArgumentException ("Number of iterations should not exceed 2 times m where n is the matrix size" );
91106 }
92107
93- // Actual logic to verify the multiplication
94108 Random rand = new Random ();
95109 for (int t = 0 ; t < iterations ; t ++) {
96110 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 ++) {
99112 randomizedVector [i ] = rand .nextInt (2 ); // Random binary values 0 or 1
113+ }
100114
101115 double [] Br = multiply (B , randomizedVector );
102116 double [] ABr = multiply (A , Br );
103117 double [] Cr = multiply (C , randomizedVector );
104118
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+ }
108124 }
125+
109126 return true ;
110127 }
111128}
0 commit comments