Skip to content

Commit 78fae04

Browse files
authored
Add Randomized Matrix Multiplication Verifier
This commit implements a Monte Carlo method to verify matrix multiplication for both integer and decimal valued matrices. The algorithm generates random binary vectors, multiplies them with the matrices, and compares the results. If any mismatch is found, the multiplication is deemed incorrect. This efficient verification avoids full matrix multiplication by using partial calculations. It also accounts for floating-point precision with a small tolerance and ensures matrix dimensions are consistent before verification.
1 parent b09766e commit 78fae04

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import java.util.Random;
2+
3+
/*
4+
This class implements the Randomized Matrix Multiplication Verification.
5+
It generates a random vector and performs verification using Freivalds' Algorithm.
6+
@author Menil-dev
7+
*/
8+
public class MatrixMultiplicationVerifier {
9+
10+
private MatrixMultiplicationVerifier() {
11+
throw new UnsupportedOperationException("Utility class");
12+
}
13+
14+
/*
15+
It multiplies input matrix with randomized vector.
16+
@params matrix which is being multiplied currently with random vector
17+
@params random vector generate for every iteration.
18+
19+
This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not.
20+
@returns matrix of calculated dot product.
21+
*/
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++)
26+
result[i]+=matrix[i][j]*vector[j];
27+
return result;
28+
}
29+
30+
/*
31+
@actual function that performs verification function
32+
@params, all three input matrices of int type, number of iterations
33+
*/
34+
public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) {
35+
if (A.length==0 || B.length==0 || C.length==0 || A[0].length==0 || B[0].length==0 || C[0].length==0) {
36+
return A.length==B[0].length && B.length==C.length && C[0].length==A[0].length; // Basic dimension consistency check
37+
}
38+
//Basic integrity checks on number of iterations.
39+
if (iterations<=0) {
40+
throw new IllegalArgumentException("Number of iterations must be positive");
41+
}
42+
int n = A.length;
43+
if (iterations>2*n) {
44+
throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size");
45+
}
46+
47+
// Actual logic to verify the multiplication
48+
int n=A.length; Random rand=new Random();
49+
for(int t=0;t<iterations;t++) {
50+
int[] randomizedVector=new int[n];
51+
//This generates a random binary vector of 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.
56+
}
57+
return true;
58+
}
59+
60+
61+
/*
62+
It multiplies input matrix of double type with randomized vector.
63+
@params matrix which is being multiplied currently with random vector.
64+
@params random vector generated for every iteration.
65+
66+
This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not.
67+
*/
68+
static double[] multiply(double[][] matrix,double[] vector) {
69+
int n=vector.length;
70+
double[] result = new double[n];
71+
for(int i=0;i<n;i++)
72+
for(int j=0;j<n;j++)
73+
result[i] += matrix[i][j] * vector[j];
74+
return result;
75+
}
76+
77+
/*
78+
Actual function that performs the verification.
79+
@params, all three input matrices of double type, number of iterations
80+
*/
81+
public static boolean verify(double[][] A,double[][] B,double[][] C,int iterations) {
82+
if (A.length==0 || B.length==0 || C.length==0 || A[0].length==0 || B[0].length==0 || C[0].length==0) {
83+
return A.length==B[0].length && B.length==C.length && C[0].length==A[0].length; // Basic dimension consistency check
84+
}
85+
// Basic integrity checks on number of iterations.
86+
if (iterations<=0) {
87+
throw new IllegalArgumentException("Number of iterations must be positive");
88+
}
89+
int n=A.length;
90+
if (iterations>2*n) {
91+
throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size");
92+
}
93+
94+
// Actual logic to verify the multiplication
95+
Random rand=new Random();
96+
for(int t=0;t<iterations;t++) {
97+
double[] randomizedVector=new double[n];
98+
// This generates a random binary vector of the first dimension of C matrix (Output Matrix).
99+
for(int i=0;i<n;i++)
100+
randomizedVector[i]=rand.nextInt(2); // Random binary values 0 or 1
101+
102+
double[] Br=multiply(B,randomizedVector);
103+
double[] ABr=multiply(A,Br);
104+
double[] Cr=multiply(C,randomizedVector);
105+
106+
for(int i=0;i<n;i++)
107+
if(Math.abs(ABr[i]-Cr[i])>1e-9) // Allowing a small tolerance for floating-point comparisons
108+
return false; // If any product mismatches, return false.
109+
}
110+
return true;
111+
}
112+
}

0 commit comments

Comments
 (0)