Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid exception "org.hipparchus.exception.MathIllegalArgumentException: matrix is singular" for complex implementations #337

Open
axkr opened this issue May 2, 2024 · 2 comments

Comments

@axkr
Copy link
Contributor

axkr commented May 2, 2024

The EigenDecompositionNonSymmetricimplementation returns a result without an exception.

Can the exception org.hipparchus.exception.MathIllegalArgumentException: matrix is singular also be avoided for the "Complex implementations" ComplexEigenDecomposition and OrderedComplexEigenDecomposition?

@Test
  public void testEqualEigenValuesSingular() {
    Array2DRowRealMatrix matrix =
        new Array2DRowRealMatrix(new double[][] {{1, 0, 0}, {-2, 1, 0}, {0, 1, 1}});
    EigenDecompositionNonSymmetric ed = new EigenDecompositionNonSymmetric(matrix);
    for (int i = 0; i < 3; i++) {
      System.out.println(ed.getEigenvector(i));
    }

    try {
      ComplexEigenDecomposition complexEigenDecomposition = new ComplexEigenDecomposition(matrix);
      for (int i = 0; i < 3; i++) {
        System.out.println(complexEigenDecomposition.getEigenvector(i));
      }
    } catch (MathRuntimeException mre) {
      mre.printStackTrace();
    }

    try {
      ComplexEigenDecomposition ced = new OrderedComplexEigenDecomposition(matrix, //
          ComplexEigenDecomposition.DEFAULT_EIGENVECTORS_EQUALITY, //
          ComplexEigenDecomposition.DEFAULT_EPSILON, //
          ComplexEigenDecomposition.DEFAULT_EPSILON_AV_VD_CHECK, //
          (c1, c2) -> Double.compare(c2.norm(), c1.norm()));
      for (int i = 0; i < 3; i++) {
        System.out.println(ced.getEigenvector(i));
      }
    } catch (MathRuntimeException mre) {
      mre.printStackTrace();
    }
  }

See:

@axkr
Copy link
Contributor Author

axkr commented May 27, 2024

This input:

Eigenvalues({{1,0,0,0,0},{3,1,0,0,0},{6,3,2,0,0},{10,6,3,2,0},{15,10,6,3,2}})

raises a "failed decomposition of a 5x5 matrix" exception for the OrderedComplexEigenDecomposition implementation:

org.hipparchus.exception.MathRuntimeException: failed decomposition of a 5x5 matrix
	at org.hipparchus.linear.ComplexEigenDecomposition.checkDefinition(ComplexEigenDecomposition.java:455)
	at org.hipparchus.linear.ComplexEigenDecomposition.<init>(ComplexEigenDecomposition.java:167)
	at org.hipparchus.linear.OrderedComplexEigenDecomposition.<init>(OrderedComplexEigenDecomposition.java:98)
	at org.matheclipse.core.builtin.LinearAlgebra$Eigenvalues.realMatrixEval(LinearAlgebra.java:2204)

if I set epsilonAVVDCheck = 1e-4 I get a result.

Thereas the EigenDecompositionNonSymmetric implementation doesn't need this adjustment:

      EigenDecompositionNonSymmetric edns = new EigenDecompositionNonSymmetric(matrix);
      Complex[] eigenvalues = edns.getEigenvalues();
      return F.mapRange(0, eigenvalues.length, (int i) -> {
        if (F.isZero(eigenvalues[i].getImaginary())) {
          return F.num(eigenvalues[i].getReal());
        }
        return F.complexNum(eigenvalues[i].getReal(), eigenvalues[i].getImaginary());
      });

returns the result:
{2.00008+I*0.000132429,2.00008+I*(-0.000132429),1.99985,1.0,1.0}

axkr added a commit to axkr/symja_android_library that referenced this issue May 27, 2024
- Hipparchus-Math/hipparchus#337
- lower the epsilon criteria for final AV=VD check in Eigen
decompositions
  Config.DEFAULT_EPSILON_AV_VD_CHECK = 1.0e-4
@maisonobe
Copy link
Contributor

I confirm the problem.
Already worked a few hours on it. I am still invetigating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants