Skip to content

Commit d382e65

Browse files
authored
Adding method to check wether a matrix is stochastic (#7184)
* 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. * Change package from maths to matrix Updated package declaration for StochasticMatrix class. * Change package declaration in StochasticMatrixTest * Delete src/test/java/com/thealgorithms/StochasticMatrixTest.java * Refactor matrix initialization for test cases * Fix formatting of matrix initialization in tests * Remove unnecessary newline in StochasticMatrix * Add import statement for JUnit in StochasticMatrixTest * Add additional assertions to StochasticMatrixTest * Clean up import statements in StochasticMatrixTest Removed unused import statements for cleaner code. * Reorder import statements in StochasticMatrixTest * Remove unused import assertThrows from StochasticMatrixTest Removed unused import for assertThrows.
1 parent 51335cc commit d382e65

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.matrix;
2+
3+
/**
4+
* Utility class to check whether a matrix is stochastic.
5+
* A matrix is stochastic if all its elements are non-negative
6+
* and the sum of each row or column is equal to 1.
7+
*Reference: https://en.wikipedia.org/wiki/Stochastic_matrix
8+
*/
9+
public final class StochasticMatrix {
10+
11+
private static final double TOLERANCE = 1e-9;
12+
13+
private StochasticMatrix() {
14+
// Utility class
15+
}
16+
/**
17+
* Checks if a matrix is row-stochastic.
18+
*
19+
* @param matrix the matrix to check
20+
* @return true if the matrix is row-stochastic
21+
* @throws IllegalArgumentException if matrix is null or empty
22+
*/
23+
public static boolean isRowStochastic(double[][] matrix) {
24+
validateMatrix(matrix);
25+
26+
for (double[] row : matrix) {
27+
double sum = 0.0;
28+
for (double value : row) {
29+
if (value < 0) {
30+
return false;
31+
}
32+
sum += value;
33+
}
34+
if (Math.abs(sum - 1.0) > TOLERANCE) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
/**
42+
* Checks if a matrix is column-stochastic.
43+
*
44+
* @param matrix the matrix to check
45+
* @return true if the matrix is column-stochastic
46+
* @throws IllegalArgumentException if matrix is null or empty
47+
*/
48+
public static boolean isColumnStochastic(double[][] matrix) {
49+
validateMatrix(matrix);
50+
51+
int rows = matrix.length;
52+
int cols = matrix[0].length;
53+
54+
for (int j = 0; j < cols; j++) {
55+
double sum = 0.0;
56+
for (int i = 0; i < rows; i++) {
57+
if (matrix[i][j] < 0) {
58+
return false;
59+
}
60+
sum += matrix[i][j];
61+
}
62+
if (Math.abs(sum - 1.0) > TOLERANCE) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
69+
private static void validateMatrix(double[][] matrix) {
70+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
71+
throw new IllegalArgumentException("Matrix must not be null or empty");
72+
}
73+
}
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.matrix;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class StochasticMatrixTest {
9+
10+
@Test
11+
void testRowStochasticMatrix() {
12+
double[][] matrix = {{0.2, 0.5, 0.3}, {0.1, 0.6, 0.3}};
13+
assertTrue(StochasticMatrix.isRowStochastic(matrix));
14+
assertFalse(StochasticMatrix.isColumnStochastic(matrix));
15+
}
16+
17+
@Test
18+
void testColumnStochasticMatrix() {
19+
double[][] matrix = {{0.4, 0.2}, {0.6, 0.8}};
20+
assertTrue(StochasticMatrix.isColumnStochastic(matrix));
21+
}
22+
23+
@Test
24+
void testInvalidMatrix() {
25+
double[][] matrix = {{0.5, -0.5}, {0.5, 1.5}};
26+
assertFalse(StochasticMatrix.isRowStochastic(matrix));
27+
}
28+
}

0 commit comments

Comments
 (0)