From bda25b40295b13782c488fd94f0abd091da296bd Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Thu, 31 Oct 2013 20:06:15 +0000 Subject: [PATCH] [MATH-1051] Fix EigenDecomposition for certain non-symmetric matrices. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1537611 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 4 ++++ .../math3/linear/SchurTransformer.java | 12 +++++----- .../math3/linear/EigenDecompositionTest.java | 22 ++++++++++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d13244cd5..b14ec3620 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces! + + "EigenDecomposition" may have failed to compute the decomposition for certain + non-symmetric matrices. Port of the respective bugfix in Jama-1.0.3. + Check for overflow in methods "pow" (class "o.a.c.m.util.ArithmeticUtils"). diff --git a/src/main/java/org/apache/commons/math3/linear/SchurTransformer.java b/src/main/java/org/apache/commons/math3/linear/SchurTransformer.java index a2ea88ee4..b566de766 100644 --- a/src/main/java/org/apache/commons/math3/linear/SchurTransformer.java +++ b/src/main/java/org/apache/commons/math3/linear/SchurTransformer.java @@ -364,14 +364,12 @@ class SchurTransformer { q = matrixT[k + 1][k - 1]; r = notlast ? matrixT[k + 2][k - 1] : 0.0; shift.x = FastMath.abs(p) + FastMath.abs(q) + FastMath.abs(r); - if (!Precision.equals(shift.x, 0.0, epsilon)) { - p = p / shift.x; - q = q / shift.x; - r = r / shift.x; + if (Precision.equals(shift.x, 0.0, epsilon)) { + continue; } - } - if (shift.x == 0.0) { - break; + p = p / shift.x; + q = q / shift.x; + r = r / shift.x; } double s = FastMath.sqrt(p * p + q * q + r * r); if (p < 0.0) { diff --git a/src/test/java/org/apache/commons/math3/linear/EigenDecompositionTest.java b/src/test/java/org/apache/commons/math3/linear/EigenDecompositionTest.java index 0a1577c6a..ce0a6d04a 100644 --- a/src/test/java/org/apache/commons/math3/linear/EigenDecompositionTest.java +++ b/src/test/java/org/apache/commons/math3/linear/EigenDecompositionTest.java @@ -436,7 +436,27 @@ public class EigenDecompositionTest { checkUnsymmetricMatrix(m); } } - + + /** + * Tests the porting of a bugfix in Jama-1.0.3 (from changelog): + * + * Patched hqr2 method in Jama.EigenvalueDecomposition to avoid infinite loop; + * Thanks Frederic Devernay + */ + @Test + public void testMath1051() { + double[][] data = { + {0,0,0,0,0}, + {0,0,0,0,1}, + {0,0,0,1,0}, + {1,1,0,0,1}, + {1,0,1,0,1} + }; + + RealMatrix m = MatrixUtils.createRealMatrix(data); + checkUnsymmetricMatrix(m); + } + @Test @Ignore public void testNormalDistributionUnsymmetricMatrix() {