作为当前的任务,我需要计算120 * 120矩阵的特征值和特征向量。首先,我在Java(Apache Commons Math库)和Python 2.7(Numpy库)中以简单的2乘2矩阵测试了这些计算。我有本征矢量的值不匹配的问题,如示于下:使用Java和Python计算特征向量的差异
//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));
}
特征向量的输出被示为{real_value + imaginary_value; real_value + imaginary_value}:在Python
//Java output
eigenvector[0] = {-0.8660254038; 0}
eigenvector[1] = {0.5; 1}
相同的代码,但使用numpy的库:
# 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])
在Python特征向量的输出被类似地示出,[真实+ IMAG真实+ IMAG]:
[ 0.35355339+0.61237244j 0.70710678+0.j ]
[ 0.35355339-0.61237244j 0.70710678-0.j ]
我担心的是,为什么这些载体不同?有什么我失踪了吗? Ty的任何形式的帮助或建议
Java输出中的那些向量不是特征向量。 'getEigenvector(i)'返回一个'RealVector',但是你的矩阵有复杂的特征值和特征向量。我不知道Apache Commons Math Library是如何表示复杂的特征向量的;希望熟悉图书馆的人能够帮助你将由Java函数返回的实际值转化为实际复杂的特征向量。 –
我同意,但正如你所看到的,即使是真正的值不匹配: -/ – borgmater
Java的甚至不规范化.. – user3684792