From b7fd01a431da8f9e7fd79864e9ab31e58653be2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:25:26 +0100 Subject: [PATCH 01/12] Add StochasticMatrix utility class This class provides methods to check if a matrix is row-stochastic or column-stochastic, ensuring all elements are non-negative and the sums of rows or columns equal 1. --- .../matrix/StochasticMatrix.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/java/com/thealgorithms/matrix/StochasticMatrix.java diff --git a/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java new file mode 100644 index 000000000000..d5929ecfa0f1 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java @@ -0,0 +1,74 @@ +package com.thealgorithms.maths; + +/** + * Utility class to check whether a matrix is stochastic. + * A matrix is stochastic if all its elements are non-negative + * and the sum of each row or column is equal to 1. + */ +public final class StochasticMatrix { + + private static final double TOLERANCE = 1e-9; + + private StochasticMatrix() { + // Utility class + } + + /** + * Checks if a matrix is row-stochastic. + * + * @param matrix the matrix to check + * @return true if the matrix is row-stochastic + * @throws IllegalArgumentException if matrix is null or empty + */ + public static boolean isRowStochastic(double[][] matrix) { + validateMatrix(matrix); + + for (double[] row : matrix) { + double sum = 0.0; + for (double value : row) { + if (value < 0) { + return false; + } + sum += value; + } + if (Math.abs(sum - 1.0) > TOLERANCE) { + return false; + } + } + return true; + } + + /** + * Checks if a matrix is column-stochastic. + * + * @param matrix the matrix to check + * @return true if the matrix is column-stochastic + * @throws IllegalArgumentException if matrix is null or empty + */ + public static boolean isColumnStochastic(double[][] matrix) { + validateMatrix(matrix); + + int rows = matrix.length; + int cols = matrix[0].length; + + for (int j = 0; j < cols; j++) { + double sum = 0.0; + for (int i = 0; i < rows; i++) { + if (matrix[i][j] < 0) { + return false; + } + sum += matrix[i][j]; + } + if (Math.abs(sum - 1.0) > TOLERANCE) { + return false; + } + } + return true; + } + + private static void validateMatrix(double[][] matrix) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + throw new IllegalArgumentException("Matrix must not be null or empty"); + } + } +} From c2c0db719ab3e0f623ea2507d2d43c6630e939aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:26:52 +0100 Subject: [PATCH 02/12] Change package from maths to matrix Updated package declaration for StochasticMatrix class. --- src/main/java/com/thealgorithms/matrix/StochasticMatrix.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java index d5929ecfa0f1..dbb1f31a8f84 100644 --- a/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java @@ -1,9 +1,10 @@ -package com.thealgorithms.maths; +package com.thealgorithms.matrix; /** * Utility class to check whether a matrix is stochastic. * A matrix is stochastic if all its elements are non-negative * and the sum of each row or column is equal to 1. + *Reference: https://en.wikipedia.org/wiki/Stochastic_matrix */ public final class StochasticMatrix { From 5be7fc417517ece5be7d1db93f866ec74a35dcbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:28:53 +0100 Subject: [PATCH 03/12] Change package declaration in StochasticMatrixTest --- .../thealgorithms/StochasticMatrixTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/java/com/thealgorithms/StochasticMatrixTest.java diff --git a/src/test/java/com/thealgorithms/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/StochasticMatrixTest.java new file mode 100644 index 000000000000..808029671af8 --- /dev/null +++ b/src/test/java/com/thealgorithms/StochasticMatrixTest.java @@ -0,0 +1,35 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +class StochasticMatrixTest { + + @Test + void testRowStochasticMatrix() { + double[][] matrix = { + {0.2, 0.5, 0.3}, + {0.1, 0.6, 0.3} + }; + assertTrue(StochasticMatrix.isRowStochastic(matrix)); + assertFalse(StochasticMatrix.isColumnStochastic(matrix)); + } + + @Test + void testColumnStochasticMatrix() { + double[][] matrix = { + {0.4, 0.2}, + {0.6, 0.8} + }; + assertTrue(StochasticMatrix.isColumnStochastic(matrix)); + } + + @Test + void testInvalidMatrix() { + double[][] matrix = { + {0.5, -0.5}, + {0.5, 1.5} + }; + assertFalse(StochasticMatrix.isRowStochastic(matrix)); + } +} From e2d6da3d63639213bd791f2d275d7c3909ce322c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:33:58 +0100 Subject: [PATCH 04/12] Delete src/test/java/com/thealgorithms/StochasticMatrixTest.java --- .../thealgorithms/StochasticMatrixTest.java | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 src/test/java/com/thealgorithms/StochasticMatrixTest.java diff --git a/src/test/java/com/thealgorithms/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/StochasticMatrixTest.java deleted file mode 100644 index 808029671af8..000000000000 --- a/src/test/java/com/thealgorithms/StochasticMatrixTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.thealgorithms.matrix; - -import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; - -class StochasticMatrixTest { - - @Test - void testRowStochasticMatrix() { - double[][] matrix = { - {0.2, 0.5, 0.3}, - {0.1, 0.6, 0.3} - }; - assertTrue(StochasticMatrix.isRowStochastic(matrix)); - assertFalse(StochasticMatrix.isColumnStochastic(matrix)); - } - - @Test - void testColumnStochasticMatrix() { - double[][] matrix = { - {0.4, 0.2}, - {0.6, 0.8} - }; - assertTrue(StochasticMatrix.isColumnStochastic(matrix)); - } - - @Test - void testInvalidMatrix() { - double[][] matrix = { - {0.5, -0.5}, - {0.5, 1.5} - }; - assertFalse(StochasticMatrix.isRowStochastic(matrix)); - } -} From f88ace43412593dbbf282173728c4bbb56eac555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:35:21 +0100 Subject: [PATCH 05/12] Refactor matrix initialization for test cases --- .../matrix/StochasticMatrixTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java new file mode 100644 index 000000000000..e7b3f89b8637 --- /dev/null +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -0,0 +1,26 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +class StochasticMatrixTest { + + @Test + void testRowStochasticMatrix() { + double[][] matrix = {{0.2, 0.5, 0.3},{0.1, 0.6, 0.3}}; + assertTrue(StochasticMatrix.isRowStochastic(matrix)); + assertFalse(StochasticMatrix.isColumnStochastic(matrix)); + } + + @Test + void testColumnStochasticMatrix() { + double[][] matrix = {{0.4, 0.2},{0.6, 0.8}}; + assertTrue(StochasticMatrix.isColumnStochastic(matrix)); + } + + @Test + void testInvalidMatrix() { + double[][] matrix = {{0.5, -0.5},{0.5, 1.5}}; + assertFalse(StochasticMatrix.isRowStochastic(matrix)); + } +} From 2a421ac865a6677e18b75b03243c5d401b80806f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:37:54 +0100 Subject: [PATCH 06/12] Fix formatting of matrix initialization in tests --- .../java/com/thealgorithms/matrix/StochasticMatrixTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index e7b3f89b8637..4094930a01d9 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -7,20 +7,20 @@ class StochasticMatrixTest { @Test void testRowStochasticMatrix() { - double[][] matrix = {{0.2, 0.5, 0.3},{0.1, 0.6, 0.3}}; + double[][] matrix = {{0.2, 0.5, 0.3}, {0.1, 0.6, 0.3}}; assertTrue(StochasticMatrix.isRowStochastic(matrix)); assertFalse(StochasticMatrix.isColumnStochastic(matrix)); } @Test void testColumnStochasticMatrix() { - double[][] matrix = {{0.4, 0.2},{0.6, 0.8}}; + double[][] matrix = {{0.4, 0.2}, {0.6, 0.8}}; assertTrue(StochasticMatrix.isColumnStochastic(matrix)); } @Test void testInvalidMatrix() { - double[][] matrix = {{0.5, -0.5},{0.5, 1.5}}; + double[][] matrix = {{0.5, -0.5}, {0.5, 1.5}}; assertFalse(StochasticMatrix.isRowStochastic(matrix)); } } From af05c2448492e3dbe34d0780d34d7273da777b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:40:42 +0100 Subject: [PATCH 07/12] Remove unnecessary newline in StochasticMatrix --- src/main/java/com/thealgorithms/matrix/StochasticMatrix.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java index dbb1f31a8f84..8b071113f9cc 100644 --- a/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java +++ b/src/main/java/com/thealgorithms/matrix/StochasticMatrix.java @@ -13,7 +13,6 @@ public final class StochasticMatrix { private StochasticMatrix() { // Utility class } - /** * Checks if a matrix is row-stochastic. * From 5e76135808cada22e000714433e000962fdbfd58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:42:09 +0100 Subject: [PATCH 08/12] Add import statement for JUnit in StochasticMatrixTest --- src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index 4094930a01d9..79e86301a319 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.matrix; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; class StochasticMatrixTest { From 5627fd92d6ee8b6507493eacd3f8acf25108c0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:47:58 +0100 Subject: [PATCH 09/12] Add additional assertions to StochasticMatrixTest --- .../java/com/thealgorithms/matrix/StochasticMatrixTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index 79e86301a319..1a87573b7a8e 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -1,6 +1,10 @@ package com.thealgorithms.matrix; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; From 5c0454b2013adb44bd3a2f23aa2d97906d7e6773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:49:15 +0100 Subject: [PATCH 10/12] Clean up import statements in StochasticMatrixTest Removed unused import statements for cleaner code. --- .../java/com/thealgorithms/matrix/StochasticMatrixTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index 1a87573b7a8e..e2b458b45c15 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -1,9 +1,7 @@ package com.thealgorithms.matrix; import static org.junit.jupiter.api.Assertions.assertTrue; - import static org.junit.jupiter.api.Assertions.assertFalse; - import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; From 98ae6c04109f7c519239f5565f6d7ba5a7ba94c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:50:42 +0100 Subject: [PATCH 11/12] Reorder import statements in StochasticMatrixTest --- .../java/com/thealgorithms/matrix/StochasticMatrixTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index e2b458b45c15..532472a73ddd 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -1,8 +1,8 @@ package com.thealgorithms.matrix; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; From e085cfc0a30fb6b3d6664e499cb78e68566f8bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20S=C3=A1nchez=20N=C3=BA=C3=B1ez?= Date: Sun, 28 Dec 2025 00:54:06 +0100 Subject: [PATCH 12/12] Remove unused import assertThrows from StochasticMatrixTest Removed unused import for assertThrows. --- src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java index 532472a73ddd..1bba918dadac 100644 --- a/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java +++ b/src/test/java/com/thealgorithms/matrix/StochasticMatrixTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.matrix; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test;