J'e
J'e

Reputation: 3716

Java Hipparchus Eigenvector and C++ Eigen Eigenvector have different results

While comparing the eigen vector results from Java Hipparchus and C++ Eigen, I'm getting different results. They are transposed for the most part as well as 2 elements not matching. Why are libraries returning different values?

Java Hipparchus

import org.hipparchus.linear.Array2DRowRealMatrix;
import org.hipparchus.linear.EigenDecomposition;
import org.hipparchus.linear.RealVector;
...
double[][] example_matrix = {
  { 1, 2, 3 },
  { 3, 2, 1 },
  { 2, 1, 3 }
};
RealMatrix P = new Array2DRowRealMatrix(example_matrix , true);
EigenDecomposition eigenDecomposition = new EigenDecomposition(P);
RealVector[] eigenvectors = new RealVector[3];
for (int i = 0; i < 3; i++) {
  System.out.println(eigenDecomposition.getEigenvector(i));
}

// prints:
// {-0.7641805281; 0.6105725033; 0.2079166628}
// {0.5776342875; 0.5776342875; 0.5776342875}
// {-0.0235573892; -0.9140029063; 0.6060826741}

C++ Eigen

Eigen::Matrix<double, 3, 3> matrix;
matrix <<
    1, 2, 3,
    3, 2, 1,
    2, 1, 3;
Eigen::EigenSolver<Eigen::Matrix<double, 3, 3>> eigen_decomposition{ matrix };
eigen_decomposition.compute(matrix, true);
const auto eigen_vectors = eigen_decomposition.eigenvectors().real();
std::cout << eigen_vectors.matrix() << "\n"

// prints:
// -0.764181    0.57735 -0.0214754
//  0.610573    0.57735  -0.833224
//  0.207917    0.57735   0.552518

Upvotes: 0

Views: 155

Answers (1)

2b-t
2b-t

Reputation: 2564

While not identical the results of the two are actually both correct:

  • In Java Hipparchus the eigenvectors are not normalised (the vector norm is not 1!): This is in particular apparent for the last one where the norm is approximately 1.10. If you normalise it then you will see that it corresponds to the last column of the results Eigen gives you. Physically this does not matter as any scalar multiple of an eigenvector is again an eigenvector. Instead the library seems to normalise the matrix spanned by the eigenvectors: The magnitude of the determinant seems to correspond to unity. A matrix with determinant 1 preserves volume so it seems like a logical choice.

  • The Eigen eigenvectors on the other hand seem to be normalised. The matrix you plot gives you the eigenvectors as the columns. Normalising the individual eigenvectors seems like an equally reasonable choice to me.

Upvotes: 1

Related Questions