From ed1ce82d822ffe185875b7b7d38352f20171c096 Mon Sep 17 00:00:00 2001 From: Gilles Date: Tue, 9 May 2017 21:31:24 +0200 Subject: [PATCH] MATH-1417: Incorrect loop start. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to Jean-François Lecomte for the report and the fix. --- src/changes/changes.xml | 3 +++ .../math4/linear/RRQRDecomposition.java | 7 +++---- .../math4/linear/RRQRDecompositionTest.java | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1b75585c9..622b44929 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + "RRQRDecomposition": bug in method "performHouseholderReflection". + BinomialTest P-value > 1 diff --git a/src/main/java/org/apache/commons/math4/linear/RRQRDecomposition.java b/src/main/java/org/apache/commons/math4/linear/RRQRDecomposition.java index 9f41ea62d..261ba09bd 100644 --- a/src/main/java/org/apache/commons/math4/linear/RRQRDecomposition.java +++ b/src/main/java/org/apache/commons/math4/linear/RRQRDecomposition.java @@ -68,7 +68,7 @@ public class RRQRDecomposition extends QRDecomposition { this(matrix, 0d); } - /** + /** * Calculates the QR-decomposition of the given matrix. * * @param matrix The matrix to decompose. @@ -92,18 +92,18 @@ public class RRQRDecomposition extends QRDecomposition { } /** Perform Householder reflection for a minor A(minor, minor) of A. + * * @param minor minor index * @param qrt transposed matrix */ @Override protected void performHouseholderReflection(int minor, double[][] qrt) { - double l2NormSquaredMax = 0; // Find the unreduced column with the greatest L2-Norm int l2NormSquaredMaxIndex = minor; for (int i = minor; i < qrt.length; i++) { double l2NormSquared = 0; - for (int j = 0; j < qrt[i].length; j++) { + for (int j = minor; j < qrt[i].length; j++) { l2NormSquared += qrt[i][j] * qrt[i][j]; } if (l2NormSquared > l2NormSquaredMax) { @@ -122,7 +122,6 @@ public class RRQRDecomposition extends QRDecomposition { } super.performHouseholderReflection(minor, qrt); - } diff --git a/src/test/java/org/apache/commons/math4/linear/RRQRDecompositionTest.java b/src/test/java/org/apache/commons/math4/linear/RRQRDecompositionTest.java index 01acaae2d..c2e9c3307 100644 --- a/src/test/java/org/apache/commons/math4/linear/RRQRDecompositionTest.java +++ b/src/test/java/org/apache/commons/math4/linear/RRQRDecompositionTest.java @@ -244,4 +244,22 @@ public class RRQRDecompositionTest { RRQRDecomposition qr = new RRQRDecomposition(m); Assert.assertEquals(2, qr.getRank(1e-14)); } + + // MATH-1417 + @Test + public void testRank3() { + double[][] d = { + {0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 1, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0} + }; + RealMatrix m = new Array2DRowRealMatrix(d); + RRQRDecomposition qr = new RRQRDecomposition(m.transpose()); + Assert.assertEquals(4, qr.getRank(1e-14)); + } }