From 78fae04773f8e8043c2462a2aed1b85f2de51a75 Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 01:34:53 +0530 Subject: [PATCH 1/6] 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/6] 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; -} } From 029ddaac9ad6b56502846c81c63eb094ba43e4ce Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 01:58:30 +0530 Subject: [PATCH 3/6] Update-2 MatrixMultiplicationVerifier.java I had used wrong format in for loop, changed that in my implementation --- .../MatrixMultiplicationVerifier.java | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java index fc6ccffa1fed..71d7315b3f16 100644 --- a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -3,7 +3,7 @@ /* This class implements the Randomized Matrix Multiplication Verification. It generates a random vector and performs verification using Freivalds' Algorithm. - @author Menil-dev + Author: Menil-dev */ public class MatrixMultiplicationVerifier { @@ -20,10 +20,13 @@ private MatrixMultiplicationVerifier() { @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; i < n; i++) - for (int j = 0; j < n; j++) + int n = vector.length; + int[] result = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { result[i] += matrix[i][j] * vector[j]; + } + } return result; } @@ -35,25 +38,34 @@ public static boolean verify(int[][] A, int[][] B, int[][] 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++) { int[] r = new int[n]; - // This generates a random binary vector of the first dimension of C matrix (Output Matrix). - for (int i = 0; i < n; i++) r[i] = rand.nextInt(2); - int[] Br = multiply(B, r), ABr = multiply(A, Br), Cr = multiply(C, r); - for (int i = 0; i < n; i++) - if (ABr[i] != Cr[i]) return false; // If any product mismatches, return condition. + for (int i = 0; i < n; i++) { + r[i] = rand.nextInt(2); + } + + int[] Br = multiply(B, r); + int[] ABr = multiply(A, Br); + int[] Cr = multiply(C, r); + + for (int i = 0; i < n; i++) { + if (ABr[i] != Cr[i]) { + return false; + } + } } + return true; } @@ -67,9 +79,11 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) { static double[] multiply(double[][] matrix, double[] vector) { int n = vector.length; double[] result = new double[n]; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { result[i] += matrix[i][j] * vector[j]; + } + } return result; } @@ -79,33 +93,36 @@ static double[] multiply(double[][] matrix, double[] vector) { */ 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 + return A.length == B[0].length && B.length == C.length && C[0].length == A[0].length; } - // Basic integrity checks on number of iterations. + if (iterations <= 0) { throw new IllegalArgumentException("Number of iterations must be positive"); } + int m = A.length; if (iterations > 2 * m) { throw new IllegalArgumentException("Number of iterations should not exceed 2 times m 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++) + for (int i = 0; i < m; i++) { randomizedVector[i] = rand.nextInt(2); // Random binary values 0 or 1 + } 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. + for (int i = 0; i < m; i++) { + if (Math.abs(ABr[i] - Cr[i]) > 1e-9) { + return false; + } + } } + return true; } } From 8bdebc08964fef4721cec890b7584e191cd0f18e Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 02:03:14 +0530 Subject: [PATCH 4/6] Update MatrixMultiplicationVerifier.java --- .../thealgorithms/randomized/MatrixMultiplicationVerifier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java index 71d7315b3f16..7f47cd95c567 100644 --- a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -3,7 +3,7 @@ /* This class implements the Randomized Matrix Multiplication Verification. It generates a random vector and performs verification using Freivalds' Algorithm. - Author: Menil-dev + @author: Menil-dev */ public class MatrixMultiplicationVerifier { From 5d375d7faf177e5e43ac1debe18e2fd17cbd2b4e Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 02:08:29 +0530 Subject: [PATCH 5/6] Update MatrixMultiplicationVerifier.java --- .../MatrixMultiplicationVerifier.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java index 7f47cd95c567..62318f5f16c5 100644 --- a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -3,9 +3,9 @@ /* This class implements the Randomized Matrix Multiplication Verification. It generates a random vector and performs verification using Freivalds' Algorithm. - @author: Menil-dev + @author Menil-dev */ -public class MatrixMultiplicationVerifier { +public final class MatrixMultiplicationVerifier { private MatrixMultiplicationVerifier() { throw new UnsupportedOperationException("Utility class"); @@ -22,11 +22,9 @@ private MatrixMultiplicationVerifier() { static int[] multiply(int[][] matrix, int[] vector) { int n = vector.length; int[] result = new int[n]; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) result[i] += matrix[i][j] * vector[j]; - } - } return result; } @@ -34,16 +32,19 @@ static int[] multiply(int[][] matrix, int[] vector) { Actual function that performs verification function @params, all three input matrices of int type, number of iterations */ - public static boolean verify(int[][] A, int[][] B, int[][] 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 + public static boolean verify(int[][] matrixA, int[][] matrixB, int[][] matrixC, int iterations) { + if (matrixA.length == 0 || matrixB.length == 0 || matrixC.length == 0 + || matrixA[0].length == 0 || matrixB[0].length == 0 || matrixC[0].length == 0) { + return matrixA.length == matrixB[0].length + && matrixB.length == matrixC.length + && matrixC[0].length == matrixA[0].length; } if (iterations <= 0) { throw new IllegalArgumentException("Number of iterations must be positive"); } - int n = A.length; + int n = matrixA.length; if (iterations > 2 * n) { throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); } @@ -55,17 +56,16 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) { r[i] = rand.nextInt(2); } - int[] Br = multiply(B, r); - int[] ABr = multiply(A, Br); - int[] Cr = multiply(C, r); + int[] matrixBtimesR = multiply(matrixB, r); + int[] matrixAtimesBtimesR = multiply(matrixA, matrixBtimesR); + int[] matrixCtimesR = multiply(matrixC, r); for (int i = 0; i < n; i++) { - if (ABr[i] != Cr[i]) { + if (matrixAtimesBtimesR[i] != matrixCtimesR[i]) { return false; } } } - return true; } @@ -79,11 +79,9 @@ public static boolean verify(int[][] A, int[][] B, int[][] C, int iterations) { static double[] multiply(double[][] matrix, double[] vector) { int n = vector.length; double[] result = new double[n]; - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) result[i] += matrix[i][j] * vector[j]; - } - } return result; } @@ -91,38 +89,40 @@ static double[] multiply(double[][] matrix, double[] vector) { 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; + public static boolean verify(double[][] matrixA, double[][] matrixB, double[][] matrixC, int iterations) { + if (matrixA.length == 0 || matrixB.length == 0 || matrixC.length == 0 + || matrixA[0].length == 0 || matrixB[0].length == 0 || matrixC[0].length == 0) { + return matrixA.length == matrixB[0].length + && matrixB.length == matrixC.length + && matrixC[0].length == matrixA[0].length; } if (iterations <= 0) { throw new IllegalArgumentException("Number of iterations must be positive"); } - int m = A.length; + int m = matrixA.length; if (iterations > 2 * m) { - throw new IllegalArgumentException("Number of iterations should not exceed 2 times m where n is the matrix size"); + throw new IllegalArgumentException("Number of iterations should not exceed 2 times m where m is the matrix size"); } Random rand = new Random(); for (int t = 0; t < iterations; t++) { double[] randomizedVector = new double[m]; for (int i = 0; i < m; i++) { - randomizedVector[i] = rand.nextInt(2); // Random binary values 0 or 1 + randomizedVector[i] = rand.nextInt(2); } - double[] Br = multiply(B, randomizedVector); - double[] ABr = multiply(A, Br); - double[] Cr = multiply(C, randomizedVector); + double[] matrixBtimesR = multiply(matrixB, randomizedVector); + double[] matrixAtimesBtimesR = multiply(matrixA, matrixBtimesR); + double[] matrixCtimesR = multiply(matrixC, randomizedVector); for (int i = 0; i < m; i++) { - if (Math.abs(ABr[i] - Cr[i]) > 1e-9) { + if (Math.abs(matrixAtimesBtimesR[i] - matrixCtimesR[i]) > 1e-9) { return false; } } } - return true; } } From 1767c7f1bb13c4fbc1e05c33d8544567c3d5b09a Mon Sep 17 00:00:00 2001 From: Menil Dhameliya Date: Mon, 12 May 2025 02:12:41 +0530 Subject: [PATCH 6/6] Update MatrixMultiplicationVerifier.java --- .../MatrixMultiplicationVerifier.java | 144 +++++++++++++++++- 1 file changed, 140 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java index 62318f5f16c5..db51cb64a9e5 100644 --- a/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java +++ b/src/main/java/com/thealgorithms/randomized/MatrixMultiplicationVerifier.java @@ -22,9 +22,143 @@ private MatrixMultiplicationVerifier() { static int[] multiply(int[][] matrix, int[] vector) { int n = vector.length; int[] result = new int[n]; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { result[i] += matrix[i][j] * vector[j]; + } + } + return result; + } + + /* + Actual function that performs verification function + @params, all three input matrices of int type, number of iterations + */ + public static boolean verify(int[][] matrixA, int[][] matrixB, int[][] matrixC, int iterations) { + if (matrixA.length == 0 || matrixB.length == 0 || matrixC.length == 0 + || matrixA[0].length == 0 || matrixB[0].length == 0 || matrixC[0].length == 0) { + return matrixA.length == matrixB[0].length + && matrixB.length == matrixC.length + && matrixC[0].length == matrixA[0].length; + } + + if (iterations <= 0) { + throw new IllegalArgumentException("Number of iterations must be positive"); + } + + int n = matrixA.length; + if (iterations > 2 * n) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 * n where n is the matrix size"); + } + + Random rand = new Random(); + for (int t = 0; t < iterations; t++) { + int[] r = new int[n]; + for (int i = 0; i < n; i++) { + r[i] = rand.nextInt(2); + } + + int[] matrixBtimesR = multiply(matrixB, r); + int[] matrixAtimesBtimesR = multiply(matrixA, matrixBtimesR); + int[] matrixCtimesR = multiply(matrixC, r); + + for (int i = 0; i < n; i++) { + if (matrixAtimesBtimesR[i] != matrixCtimesR[i]) { + return false; + } + } + } + return true; + } + + /* + It multiplies input matrix of double type 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. + */ + static double[] multiply(double[][] matrix, double[] vector) { + int n = vector.length; + double[] result = new double[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + result[i] += matrix[i][j] * vector[j]; + } + } + return result; + } + + /* + Actual function that performs the verification. + @params, all three input matrices of double type, number of iterations + */ + public static boolean verify(double[][] matrixA, double[][] matrixB, double[][] matrixC, int iterations) { + if (matrixA.length == 0 || matrixB.length == 0 || matrixC.length == 0 + || matrixA[0].length == 0 || matrixB[0].length == 0 || matrixC[0].length == 0) { + return matrixA.length == matrixB[0].length + && matrixB.length == matrixC.length + && matrixC[0].length == matrixA[0].length; + } + + if (iterations <= 0) { + throw new IllegalArgumentException("Number of iterations must be positive"); + } + + int m = matrixA.length; + if (iterations > 2 * m) { + throw new IllegalArgumentException("Number of iterations should not exceed 2 times m where m is the matrix size"); + } + + Random rand = new Random(); + for (int t = 0; t < iterations; t++) { + double[] randomizedVector = new double[m]; + for (int i = 0; i < m; i++) { + randomizedVector[i] = rand.nextInt(2); + } + + double[] matrixBtimesR = multiply(matrixB, randomizedVector); + double[] matrixAtimesBtimesR = multiply(matrixA, matrixBtimesR); + double[] matrixCtimesR = multiply(matrixC, randomizedVector); + + for (int i = 0; i < m; i++) { + if (Math.abs(matrixAtimesBtimesR[i] - matrixCtimesR[i]) > 1e-9) { + return false; + } + } + } + return true; + } +} +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 final 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 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. + */ + static int[] multiply(int[][] matrix, int[] vector) { + int n = vector.length; + int[] result = new int[n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + result[i] += matrix[i][j] * vector[j]; + } + } return result; } @@ -79,9 +213,11 @@ public static boolean verify(int[][] matrixA, int[][] matrixB, int[][] matrixC, static double[] multiply(double[][] matrix, double[] vector) { int n = vector.length; double[] result = new double[n]; - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { result[i] += matrix[i][j] * vector[j]; + } + } return result; }