Skip to content

Commit a9b98d2

Browse files
authored
Update MatrixMultiplicationVerifier.java
1 parent 78fae04 commit a9b98d2

File tree

1 file changed

+79
-80
lines changed

1 file changed

+79
-80
lines changed

src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,106 +7,105 @@
77
*/
88
public class MatrixMultiplicationVerifier {
99

10-
private MatrixMultiplicationVerifier() {
10+
private MatrixMultiplicationVerifier() {
1111
throw new UnsupportedOperationException("Utility class");
1212
}
1313

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.
14+
/*
15+
It multiplies input matrix with randomized vector.
16+
@params matrix which is being multiplied currently with random vector
17+
@params random vector generated for every iteration.
1818
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-
*/
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+
*/
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++)
26-
result[i]+=matrix[i][j]*vector[j];
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];
2727
return result;
2828
}
2929

30-
/*
31-
@actual function that performs verification function
32-
@params, all three input matrices of int type, number of iterations
33-
*/
30+
/*
31+
Actual function that performs verification function
32+
@params, all three input matrices of int type, number of iterations
33+
*/
3434
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
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
3737
}
38-
//Basic integrity checks on number of iterations.
39-
if (iterations<=0) {
38+
// Basic integrity checks on number of iterations.
39+
if (iterations <= 0) {
4040
throw new IllegalArgumentException("Number of iterations must be positive");
4141
}
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-
}
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+
}
4646

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.
47+
// Actual logic to verify the multiplication
48+
Random rand = new Random();
49+
for (int t = 0; t < iterations; t++) {
50+
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.
5656
}
5757
return true;
5858
}
5959

60+
/*
61+
It multiplies input matrix of double type with randomized vector.
62+
@params matrix which is being multiplied currently with random vector.
63+
@params random vector generated for every iteration.
6064
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+
This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not.
66+
*/
67+
static double[] multiply(double[][] matrix, double[] vector) {
68+
int n = vector.length;
69+
double[] result = new double[n];
70+
for (int i = 0; i < n; i++)
71+
for (int j = 0; j < n; j++)
72+
result[i] += matrix[i][j] * vector[j];
73+
return result;
74+
}
6575

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+
Actual function that performs the verification.
78+
@params, all three input matrices of double type, number of iterations
79+
*/
80+
public static boolean verify(double[][] A, double[][] B, double[][] C, int iterations) {
81+
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
83+
}
84+
// Basic integrity checks on number of iterations.
85+
if (iterations <= 0) {
86+
throw new IllegalArgumentException("Number of iterations must be positive");
87+
}
88+
int m = A.length;
89+
if (iterations > 2 * m) {
90+
throw new IllegalArgumentException("Number of iterations should not exceed 2 times m where n is the matrix size");
91+
}
7692

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+
// Actual logic to verify the multiplication
94+
Random rand = new Random();
95+
for (int t = 0; t < iterations; t++) {
96+
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++)
99+
randomizedVector[i] = rand.nextInt(2); // Random binary values 0 or 1
93100

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.
101+
double[] Br = multiply(B, randomizedVector);
102+
double[] ABr = multiply(A, Br);
103+
double[] Cr = multiply(C, randomizedVector);
104+
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.
108+
}
109+
return true;
109110
}
110-
return true;
111-
}
112111
}

0 commit comments

Comments
 (0)