As a current task, I need to calculate eigenvalues and eigenvectors for a 120*120 matrix. For start, I tested those calculations on a simple 2 by 2 matrix in both Java (Apache Commons Math library) and Python 2.7 (Numpy library). I have a problem with eigenvector values not matching, as show below :
//Java
import org.apache.commons.math3.linear.EigenDecomposition;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
public class TemporaryTest {
public static void main(String[] args) {
double[][] testArray = {{2, -1}, {1, 1}};
RealMatrix testMatrix = MatrixUtils.createRealMatrix(testArray);
EigenDecomposition decomposition = new EigenDecomposition (testMatrix);
System.out.println("eigenvector[0] = " + decomposition.getEigenvector(0));
System.out.println("eigenvector[1] = " + decomposition.getEigenvector(1));
}
Output of eigenvectors are shown as {real_value + imaginary_value; real_value + imaginary_value}:
//Java output
eigenvector[0] = {-0.8660254038; 0}
eigenvector[1] = {0.5; 1}
Same code in Python, but using Numpy library:
# Python
import numpy as np
from numpy import linalg as LA
w, v = LA.eig(np.array([[2, -1], [1, 1]]))
print (v[:, 0])
print (v[:, 1])
Output of eigenvectors in Python are shown similarly, [real+imag real+imag]:
[ 0.35355339+0.61237244j 0.70710678+0.j ]
[ 0.35355339-0.61237244j 0.70710678-0.j ]
My concern is, why are those vectors different ? Is there something that I am missing ? Ty for any kind of help or advice
getEigenvector(i)
returns aRealVector
, but your matrix has complex eigenvalues and eigenvectors. I don't know how the Apache Commons Math Library represents a complex eigenvector; hopefully someone familiar with the library will help you translate the real values returned by the Java functions into the actual complex eigenvectors. – EnumerationEigenDecomposition
methods returning only real values. – Enumerationnp.allclose(A, A.T)
. But you may know from the problem itself if the matrix should be symmetric. For example, a covariance matrix is symmetric by construction; there would be no need to check it in your code (except maybe to check for bugs in the code that generates the matrix). – Enumeration