Throw "SingularMatrixException" instead of computing a meaningless value.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1483319 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2013-05-16 12:29:37 +00:00
parent 51fe6ff3e4
commit d62f6d9a56
2 changed files with 32 additions and 4 deletions

View File

@ -1023,18 +1023,34 @@ public class MatrixUtils {
final RealMatrix d = m.getSubMatrix(splitIndex1, n - 1, splitIndex1, n - 1);
final SingularValueDecomposition aDec = new SingularValueDecomposition(a);
final RealMatrix aInv = aDec.getSolver().getInverse();
final DecompositionSolver aSolver = aDec.getSolver();
if (!aSolver.isNonSingular()) {
throw new SingularMatrixException();
}
final RealMatrix aInv = aSolver.getInverse();
final SingularValueDecomposition dDec = new SingularValueDecomposition(d);
final RealMatrix dInv = dDec.getSolver().getInverse();
final DecompositionSolver dSolver = dDec.getSolver();
if (!dSolver.isNonSingular()) {
throw new SingularMatrixException();
}
final RealMatrix dInv = dSolver.getInverse();
final RealMatrix tmp1 = a.subtract(b.multiply(dInv).multiply(c));
final SingularValueDecomposition tmp1Dec = new SingularValueDecomposition(tmp1);
final RealMatrix result00 = tmp1Dec.getSolver().getInverse();
final DecompositionSolver tmp1Solver = tmp1Dec.getSolver();
if (!tmp1Solver.isNonSingular()) {
throw new SingularMatrixException();
}
final RealMatrix result00 = tmp1Solver.getInverse();
final RealMatrix tmp2 = d.subtract(c.multiply(aInv).multiply(b));
final SingularValueDecomposition tmp2Dec = new SingularValueDecomposition(tmp2);
final RealMatrix result11 = tmp2Dec.getSolver().getInverse();
final DecompositionSolver tmp2Solver = tmp2Dec.getSolver();
if (!tmp2Solver.isNonSingular()) {
throw new SingularMatrixException();
}
final RealMatrix result11 = tmp2Solver.getInverse();
final RealMatrix result01 = aInv.multiply(b).multiply(result11).scalarMultiply(-1);
final RealMatrix result10 = dInv.multiply(c).multiply(result00).scalarMultiply(-1);

View File

@ -364,6 +364,18 @@ public final class MatrixUtilsTest {
}
}
@Test(expected=SingularMatrixException.class)
public void testBlockInverseNonInvertible() {
final double[][] data = {
{ -1, 0, 123, 4 },
{ -56, 78.9, -0.1, -23.4 },
{ 5.67, 8, -9, 1011 },
{ 5.67, 8, -9, 1011 },
};
MatrixUtils.blockInverse(new Array2DRowRealMatrix(data), 2);
}
@Test
public void testIsSymmetric() {
final double eps = Math.ulp(1d);