AbstractMatrixDecomposer implements MatrixDecomposer<E> { @Override public final MatrixDecomposition<E> decompose(Matrix<E> matrix) { Objects.requireNonNull(matrix); if (matrix.getRows() != matrix.getColumns()) { throw new IllegalArgumentException("Cannot decompose a matrix that is not square"); } final MatrixDecomposition<E> result = doDecompose(MatrixUtils.copyOf(matrix)); Objects.requireNonNull(result); Objects.requireNonNull(result.getLower()); Objects.requireNonNull(result.getUpper()); Objects.requireNonNull(result.getPermutation()); if (matrix.getColumns() != result.getLower().getColumns() || matrix.getRows() != result.getLower().getRows()) { throw new IllegalStateException("Resulting lower matrix is incompatible with the original matrix"); } if (matrix.getColumns() != result.getUpper().getColumns() || matrix.getRows() != result.getUpper().getRows()) { throw new IllegalStateException("Resulting upper matrix is incompatible with the original matrix"); } if (matrix.getColumns() != result.getPermutation().size()) { throw new IllegalStateException("Resulting permutation is incompatible with the original matrix"); } return result; } AbstractMatrixDecomposer(Class<E> type); Class<E> getType(); @Override final MatrixDecomposition<E> decompose(Matrix<E> matrix);  }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*permutation.*incompatible.*") public void testThatItIsUnhappyWhenResultPermutationIsIncompatible() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3, 4); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test public void testThatItDoesNotPassTheOriginalMatrixDown() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); assertThat(decomposer.getMatrix() == matrix, is(false)); }
@Test public void testItReturnsTheResultAsIs() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final MatrixDecomposition<Integer> decomposition = new MatrixDecomposition<>(upper, lower, permutation); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, decomposition); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); final MatrixDecomposition<Integer> result = decomposer.decompose(matrix); assertThat(result, is(decomposition)); assertThat(result == decomposition, is(true)); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyIfTheMatrixIsNull() throws Exception { new SampleAbstractMatrixDecomposer<>(Integer.class, null).decompose(null); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ".*square.*") public void testThatItIsUnhappyIfTheMatrixIsNotSquare() throws Exception { new SampleAbstractMatrixDecomposer<>(Integer.class, null).decompose(new ArrayMatrix<Integer>(3, 4)); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenResultIsNull() throws Exception { final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, null); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenResultLowerIsNull() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = null; final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenResultUpperIsNull() throws Exception { final Matrix<Integer> upper = null; final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = NullPointerException.class) public void testThatItIsUnhappyWhenResultPermutationIsNull() throws Exception { final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final List<Integer> permutation = null; final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*lower.*incompatible.*") public void testThatItIsUnhappyWhenResultLowerHasIncompatibleRows() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = new ArrayMatrix<>(4, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*lower.*incompatible.*") public void testThatItIsUnhappyWhenResultLowerHasIncompatibleColumns() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 3); final Matrix<Integer> lower = new ArrayMatrix<>(3, 4); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*upper.*incompatible.*") public void testThatItIsUnhappyWhenResultUpperHasIncompatibleRows() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(4, 3); final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }
@Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = ".*upper.*incompatible.*") public void testThatItIsUnhappyWhenResultUpperHasIncompatibleColumns() throws Exception { final Matrix<Integer> upper = new ArrayMatrix<>(3, 4); final Matrix<Integer> lower = new ArrayMatrix<>(3, 3); final List<Integer> permutation = Arrays.asList(1, 2, 3); final SampleAbstractMatrixDecomposer<Integer> decomposer = new SampleAbstractMatrixDecomposer<>(Integer.class, new MatrixDecomposition<>(upper, lower, permutation)); final Matrix<Integer> matrix = new ArrayMatrix<>(3, 3); decomposer.decompose(matrix); }