thanks a lot. do you know how I can have same response in matlab and python?
I think the question is: why do you want to do this at all? …given that you understood what eigenvalues and vectors are.
Again, everything is in this thread see the answer marked as solution: Eigenvalue calculation differs between Julia and MATLAB - #18 by GunnarFarneback
So… with matrix A\in \mathbb{C}^{n\times n} and eigenvalues \lambda \in \mathbb{C}^n, for the particular eigenvalue \lambda_j \in \mathbb{C}, an eigenvector v_j \in \mathbb{C}^n satisfies:
Here, eigenvalue \lambda_j by design ensures that A-\lambda_j I has rank loss: \det (A-\lambda_j I) \equiv 0. It follows that the eigenvector v_j lies in the non-trivial nullspace of A-\lambda_j I,
Because of the properties of the nullspace, if v_j is an eigenvector, then so is any other vector u that satisfies u \parallel v_j, in other words: u=c\cdot v_j where c is a complex number is also an eigenvector for eigenvalue \lambda_j.
It is common to scale v_j so that the length is unity, although this is not required for v_j to be an eigenvector. And even with unity length, the sign can differ.
In addition to this, the eigenvalues — and hence the corresponding eigenvectors can appear in any order. In principle. Unless a particular ordering of the eigenvalues is enforced.
So — MATLAB and Python (and Julia) may have different rules for ordering the eigenvalues \lambda_j, and they may also have different rules for choosing the sign of the eigenvectors v_j.
As tamasgal
indicates — is it important that the eigenvalues and eigenvectors appear in identical order and with identical value/sign?
yes I understood. but I am coding a matlab code to python and in this stage result for matlab and python is different. I don’t know what I have to for same result?
I think you would want to sort all the eigensolutions in the manner you prefer, e.g. absolute value, and normalize the eigenvectors in the same way, both in matlab and python. This should, in principle, give you comparable results.
So you need to figure out how MATLAB and Python and/or Julia sorts the eigenvalues. For your system:
julia> a = [1 2 1.5 2;2 2 0 0;1.5 0 1 0;2 0 0 3]
julia> lam = eigvals(a) |> show
[-1.7369215679264496, 1.276789240956863, 2.5000000000000004, 4.9601323269
69587]
julia> v = eigvecs(a)
4×4 Array{Float64,2}:
-0.752694 -0.159387 -0.171499 -0.615334
0.402842 0.440777 -0.685994 -0.415748
0.412522 -0.863765 -0.171499 -0.233073
0.317799 0.184989 0.685994 -0.627849
In this case, the eigenvalues are all real, and it seems like Julia sorts the eigenvalues in order of increasing value.
If you want to sort the eigenvalues in order of increasing absolute value of the eigenvalues, you would get eigenvalues:
Then you need to permute the columns of matrix v
correspondingly.
If you want to have a particular sign for the columns (eigenvectors), you can do that, too. Some possibilities:
- the first element of the eigenvectors should have positive value
- the majority of elements in the eigenvectors should have positive value
- etc.
In summary: to get correspondence between MATLAB and Julia (since this is a Julia forum), you need to figure out (i) MATLAB’s system of ordering the eigenvalues, and (ii) MATLAB’s system of choosing sign of eigenvectors.