Matlab 和 Eigen 特征向量仅符号不同

Matlab and Eigen eigenvectors differ only by the sign

我正在处理一个项目,该项目包括将 Matlab 代码翻译成 C/C++。 有一次我必须计算矩阵的特征向量,在 Matlab 中,这是使用 eig 函数完成的,而在 C++ 中,我使用 Eigen 库中的 EigenSolver

问题是一些特征向量(显然是随机的)与等效的 Matlab 特征向量相比符号相反,这可能是我的应用程序中的一个问题。

我知道特征向量不是唯一的,那么有什么方法可以知道哪些特征向量会改变符号并简单地乘以 -1 来更正它?

根据 the documentation of eig,不保证特征向量的符号在使用的 MATLAB 版本 and/or 机器之间保持一致:

Different machines and releases of MATLAB® can produce different eigenvectors that are still numerically accurate:

  • For real eigenvectors, the sign of the eigenvectors can change.
  • For complex eigenvectors, the eigenvectors can be multiplied by any complex number of magnitude 1.

因此无法复制 MATLAB 所做的事情,因为它没有定义。

但是,如果您想同时修改 MATLAB 代码和 C++ 代码,则可以强制特征向量位于特定的 hemicircle/hemisphere 中,例如要求第一个分量为正(乘以如果 x 分量为负,则向量乘以 -1)。我认为这应该足以获得一致的结果。