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 */
8- public class MatrixMultiplicationVerifier {
8+ public final class MatrixMultiplicationVerifier {
99
1010 private MatrixMultiplicationVerifier () {
1111 throw new UnsupportedOperationException ("Utility class" );
@@ -22,28 +22,29 @@ private MatrixMultiplicationVerifier() {
2222 static int [] multiply (int [][] matrix , int [] vector ) {
2323 int n = vector .length ;
2424 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 ++)
2727 result [i ] += matrix [i ][j ] * vector [j ];
28- }
29- }
3028 return result ;
3129 }
3230
3331 /*
3432 Actual function that performs verification function
3533 @params, all three input matrices of int type, number of iterations
3634 */
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 ;
4041 }
4142
4243 if (iterations <= 0 ) {
4344 throw new IllegalArgumentException ("Number of iterations must be positive" );
4445 }
4546
46- int n = A .length ;
47+ int n = matrixA .length ;
4748 if (iterations > 2 * n ) {
4849 throw new IllegalArgumentException ("Number of iterations should not exceed 2 * n where n is the matrix size" );
4950 }
@@ -55,17 +56,16 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
5556 r [i ] = rand .nextInt (2 );
5657 }
5758
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 );
6162
6263 for (int i = 0 ; i < n ; i ++) {
63- if (ABr [i ] != Cr [i ]) {
64+ if (matrixAtimesBtimesR [i ] != matrixCtimesR [i ]) {
6465 return false ;
6566 }
6667 }
6768 }
68-
6969 return true ;
7070 }
7171
@@ -79,50 +79,50 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
7979 static double [] multiply (double [][] matrix , double [] vector ) {
8080 int n = vector .length ;
8181 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 ++)
8484 result [i ] += matrix [i ][j ] * vector [j ];
85- }
86- }
8785 return result ;
8886 }
8987
9088 /*
9189 Actual function that performs the verification.
9290 @params, all three input matrices of double type, number of iterations
9391 */
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 ;
9798 }
9899
99100 if (iterations <= 0 ) {
100101 throw new IllegalArgumentException ("Number of iterations must be positive" );
101102 }
102103
103- int m = A .length ;
104+ int m = matrixA .length ;
104105 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" );
106107 }
107108
108109 Random rand = new Random ();
109110 for (int t = 0 ; t < iterations ; t ++) {
110111 double [] randomizedVector = new double [m ];
111112 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 );
113114 }
114115
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 );
118119
119120 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 ) {
121122 return false ;
122123 }
123124 }
124125 }
125-
126126 return true ;
127127 }
128128}
0 commit comments