From cec75d74f8ae7d0c6d37a9e1605e1920fb545726 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sat, 13 Aug 2011 07:45:42 +0000 Subject: [PATCH] MATH-602 Inverse condition number. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1157345 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/linear/SingularValueDecompositionImpl.java | 11 +++++++++++ .../linear/SingularValueDecompositionImplTest.java | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java b/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java index b7a3feff4..6682df2b0 100644 --- a/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java +++ b/src/main/java/org/apache/commons/math/linear/SingularValueDecompositionImpl.java @@ -545,6 +545,17 @@ public class SingularValueDecompositionImpl implements SingularValueDecompositio return singularValues[0] / singularValues[n - 1]; } + /** + * Computes the inverse of the condition number. + * In cases of rank deficiency, the {@link #getConditionNumber() condition + * number} will become undefined. + * + * @return the inverse of the condition number. + */ + public double getInverseConditionNumber() { + return singularValues[n - 1] / singularValues[0]; + } + /** {@inheritDoc} */ public int getRank() { int r = 0; diff --git a/src/test/java/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java b/src/test/java/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java index 55578b3c6..cbac286b0 100644 --- a/src/test/java/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java +++ b/src/test/java/org/apache/commons/math/linear/SingularValueDecompositionImplTest.java @@ -298,6 +298,13 @@ public class SingularValueDecompositionImplTest { Assert.assertEquals(3.0, svd.getConditionNumber(), 1.5e-15); } + @Test + public void testInverseConditionNumber() { + SingularValueDecompositionImpl svd = + new SingularValueDecompositionImpl(MatrixUtils.createRealMatrix(testSquare)); + Assert.assertEquals(1.0/3.0, svd.getInverseConditionNumber(), 1.5e-15); + } + private RealMatrix createTestMatrix(final Random r, final int rows, final int columns, final double[] singularValues) { final RealMatrix u =