From 78fae04773f8e8043c2462a2aed1b85f2de51a75 Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 01:34:53 +0530 Subject: [PATCH 1/2] 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. --- .../MatrixMultiplicationVerifier.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java new file mode 100644 index 000000000000..33cefa8c6752 --- /dev/null +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -0,0 +1,112 @@ +import java.util.Random; + +/* + This class implements the Randomized Matrix Multiplication Verification. + It generates a random vector and performs verification using Freivalds' Algorithm. + @author Menil-dev + */ +public class MatrixMultiplicationVerifier { + + private MatrixMultiplicationVerifier() { + throw new UnsupportedOperationException("Utility class"); + } + + /* + It multiplies input matrix with randomized vector. + @params matrix which is being multiplied currently with random vector + @params random vector generate for every iteration. + + This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not. + @returns matrix of calculated dot product. + */ + static int[] multiply(int[][] matrix, int[] vector) { + int n=vector.length, result[]=new int[n]; + for(int i=0;i2*n) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); + } + + // Actual logic to verify the multiplication + int n=A.length; Random rand=new Random(); + for(int t=0;t2*n) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); + } + + // Actual logic to verify the multiplication + Random rand=new Random(); + for(int t=0;t1e-9) // Allowing a small tolerance for floating-point comparisons + return false; // If any product mismatches, return false. + } + return true; +} +} From a9b98d22e975cfc4350f398fc714c6686a4f7314 Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 01:45:31 +0530 Subject: [PATCH 2/2] Update MatrixMultiplicationVerifier.java --- .../MatrixMultiplicationVerifier.java | 159 +++++++++--------- 1 file changed, 79 insertions(+), 80 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java index 33cefa8c6752..fc6ccffa1fed 100644 --- a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -7,106 +7,105 @@ */ public class MatrixMultiplicationVerifier { - private MatrixMultiplicationVerifier() { + private MatrixMultiplicationVerifier() { throw new UnsupportedOperationException("Utility class"); } - /* - It multiplies input matrix with randomized vector. - @params matrix which is being multiplied currently with random vector - @params random vector generate for every iteration. + /* + It multiplies input matrix with randomized vector. + @params matrix which is being multiplied currently with random vector + @params random vector generated for every iteration. - This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not. - @returns matrix of calculated dot product. - */ + This basically calculates dot product for every row, which is used to verify whether the product of matrices is valid or not. + @returns matrix of calculated dot product. + */ static int[] multiply(int[][] matrix, int[] vector) { - int n=vector.length, result[]=new int[n]; - for(int i=0;i2*n) { - throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); - } + int n = A.length; + if (iterations > 2 * n) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); + } - // Actual logic to verify the multiplication - int n=A.length; Random rand=new Random(); - for(int t=0;t 2 * m) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 times m where n is the matrix size"); + } -/* - Actual function that performs the verification. - @params, all three input matrices of double type, number of iterations -*/ -public static boolean verify(double[][] A,double[][] B,double[][] C,int iterations) { - if (A.length==0 || B.length==0 || C.length==0 || A[0].length==0 || B[0].length==0 || C[0].length==0) { - return A.length==B[0].length && B.length==C.length && C[0].length==A[0].length; // Basic dimension consistency check - } - // Basic integrity checks on number of iterations. - if (iterations<=0) { - throw new IllegalArgumentException("Number of iterations must be positive"); - } - int n=A.length; - if (iterations>2*n) { - throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); - } + // Actual logic to verify the multiplication + Random rand = new Random(); + for (int t = 0; t < iterations; t++) { + double[] randomizedVector = new double[m]; + // This generates a random binary vector of the first dimension of C matrix (Output Matrix). + for (int i = 0; i < m; i++) + randomizedVector[i] = rand.nextInt(2); // Random binary values 0 or 1 - // Actual logic to verify the multiplication - Random rand=new Random(); - for(int t=0;t1e-9) // Allowing a small tolerance for floating-point comparisons - return false; // If any product mismatches, return false. + double[] Br = multiply(B, randomizedVector); + double[] ABr = multiply(A, Br); + double[] Cr = multiply(C, randomizedVector); + + for (int i = 0; i < m; i++) + if (Math.abs(ABr[i] - Cr[i]) > 1e-9) // Allowing a small tolerance for floating-point comparisons + return false; // If any product mismatches, return false. + } + return true; } - return true; -} }