From f20ae062d38133e09679104c1e38857a080997c1 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Mon, 23 Jul 2012 20:19:52 +0000 Subject: [PATCH] [MATH-235] Added a hasComplexEigenvalues method. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1364783 13f79535-47bb-0310-9956-ffa450edef68 --- .../math3/linear/EigenDecomposition.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java b/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java index 78bc4d693..5f03a39f5 100644 --- a/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java +++ b/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java @@ -278,6 +278,24 @@ public class EigenDecomposition { return cachedVt; } + /** + * Returns whether the calculated eigen values are complex or real. + *

The method performs a zero check for each element of the + * {@link #getImagEigenvalues()} array and returns {@code true} if any + * element is not equal to zero. + * + * @return {@code true} if the eigen values are complex, {@code false} otherwise + * @since 3.1 + */ + public boolean hasComplexEigenvalues() { + for (int i = 0; i < imagEigenvalues.length; i++) { + if (!Precision.equals(imagEigenvalues[i], 0.0, epsilon)) { + return true; + } + } + return false; + } + /** * Gets a copy of the real parts of the eigenvalues of the original matrix. * @@ -374,10 +392,8 @@ public class EigenDecomposition { * complex eigenvalues */ public DecompositionSolver getSolver() { - for (int i = 0; i < imagEigenvalues.length; i++) { - if (imagEigenvalues[i] != 0.0) { - throw new MathUnsupportedOperationException(); - } + if (hasComplexEigenvalues()) { + throw new MathUnsupportedOperationException(); } return new Solver(realEigenvalues, imagEigenvalues, eigenvectors); }