Rewriting Matlab eig(A,B) (Generalized eigenvalues/eigenvectors) to C/C++

后端 未结 2 922
旧时难觅i
旧时难觅i 2021-01-13 12:21

Do anyone have any idea how can I rewrite eig(A,B) from Matlab used to calculate generalized eigenvector/eigenvalues? I\'ve been struggling with this problem la

2条回答
  •  -上瘾入骨i
    2021-01-13 13:02

    There is no problem here with Eigen.

    In fact for the second example run, Matlab and Eigen produced the very same result. Please remember from basic linear algebra that eigenvector are determined up to an arbitrary scaling factor. (I.e. if v is an eigenvector the same holds for alpha*v, where alpha is a non zero complex scalar.)

    It is quite common that different linear algebra libraries compute different eigenvectors, but this does not mean that one of the two codes is wrong: it simply means that they choose a different scaling of the eigenvectors.

    EDIT

    The main problem with exactly replicating the scaling chosen by matlab is that eig(A,B) is a driver routine, which depending from the different properties of A and B may call different libraries/routines, and apply extra steps like balancing the matrices and so on. By quickly inspecting your example, I would say that in this case matlab is enforcing following condition:

    • all(imag(V(end,:))==0) (the last component of each eigenvector is real)

    but not imposing other constraints. This unfortunately means that the scaling is not unique, and probably depends on intermediate results of the generalised eigenvector algorithm used. In this case I'm not able to give you advice on how to exactly replicate matlab: knowledge of the internal working of matlab is required.

    As a general remark, in linear algebra usually one does not care too much about eigenvector scaling, since this is usually completely irrelevant for the problem solved, when the eigenvectors are just used as intermediate results.

    The only case in which the scaling has to be defined exactly, is when you are going to give a graphic representation of the eigenvalues.

提交回复
热议问题