mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
* 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.
29 lines
810 B
Java
29 lines
810 B
Java
package com.thealgorithms.matrix;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
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));
|
|
}
|
|
}
|