QR decomposition can compute pseudo-inverses for tall matrices.

Thanks to Sean Owen for the patch.

JIRA: MATH-1053

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1570566 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2014-02-21 13:18:35 +00:00
parent ed7ac7cf74
commit 927361d787
3 changed files with 19 additions and 1 deletions

View File

@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="3.3" date="TBD" description="TBD">
<action dev="luc" type="add" issue="MATH-1053" due-to="Sean Owen">
QR decomposition can compute pseudo-inverses for tall matrices.
</action>
<action dev="luc" type="add" issue="MATH-820">
Field vectors now implement the visitor pattern just like real vectors.
</action>

View File

@ -463,7 +463,7 @@ public class QRDecomposition {
* @throws SingularMatrixException if the decomposed matrix is singular.
*/
public RealMatrix getInverse() {
return solve(MatrixUtils.createRealIdentityMatrix(rDiag.length));
return solve(MatrixUtils.createRealIdentityMatrix(qrt[0].length));
}
}
}

View File

@ -248,6 +248,21 @@ public class QRDecompositionTest {
qr.getSolver().getInverse();
}
@Test
public void testInvertTallSkinny() {
RealMatrix a = MatrixUtils.createRealMatrix(testData4x3);
RealMatrix pinv = new QRDecomposition(a).getSolver().getInverse();
Assert.assertEquals(0, pinv.multiply(a).subtract(MatrixUtils.createRealIdentityMatrix(3)).getNorm(), 1.0e-6);
}
@Test
public void testInvertShortWide() {
RealMatrix a = MatrixUtils.createRealMatrix(testData3x4);
RealMatrix pinv = new QRDecomposition(a).getSolver().getInverse();
Assert.assertEquals(0, a.multiply(pinv).subtract(MatrixUtils.createRealIdentityMatrix(3)).getNorm(), 1.0e-6);
Assert.assertEquals(0, pinv.multiply(a).getSubMatrix(0, 2, 0, 2).subtract(MatrixUtils.createRealIdentityMatrix(3)).getNorm(), 1.0e-6);
}
private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) {
RealMatrix m = MatrixUtils.createRealMatrix(rows, columns);
m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){