Ordering of eigenvectors when calculating eigenvectors using LAPACK's ssteqr - lapack

I am using LAPACK's ssteqr function to calculate eigenvalues/eigenvectors. The documentation for ssteqr says that the eigenvalues are sorted "in ascending order". Is it reasonable to assume that the list of eigenvectors is also sorted in ascending order?

Yes, it is reasonable to assume that the eigenvectors are ordered so that the i-th eigenvector corresponds to the i-th eigenvalue.
Nevertheless, if I were you, I would check for each eigenvalue the result of the multiplication of the eigenvector by the matrix. This way you are sure that you interpret the output right, and you see explicitly the accuracy of the calculations.

An old question, this, but I struggled with this recently, so am adding this for current and future readers.
The basic answer is that, yes, the eigenvectors are sorted such that the ith eigenvector corresponds to the ith eigenvalue. However, note that the eigenvectors thus obtained may not be the actual eigenvectors you want. This is so because of the following.
Since the ?steqr functions work only on tridiagonal matrices, one typically uses LAPACK's ?sytrd functions to first transform one's original symmetric matrix, call it M, to a tridiagonal form, call it T, such that M = QTQT where Q is an orthogonal matrix (and QT denotes its transpose). One then applies the ?steqr function on this tridiagonal matrix T to find its eigenvalues and eigenvectors. Now the eigenvalues thus obtained (of T) are exactly the same as the eigenvalues of M, so if one only wants the eigenvalues one can stop here. But if one is interested in the eigenvectors, like the OP, then one needs to bear in mind that the eigenvectors of T and M are different. To find the eigenvectors of the original matrix M, one needs to left-multiply the obtained eigenvectors of T by Q. This is very easily done by using the LAPACK functions orgtr or ormtr. See here for a clear explanation: https://software.intel.com/en-us/mkl-developer-reference-fortran-sytrd.

Related

how does Matlab normalize generalized eigenvectors?

I know that the eigenvectors produced by eig(A) have 2-norm 1. But what about the vectors produced in the generalized eigenvalue problem eig(A,B)? A natural conjecture is that such a vector v should satisfy v'Bv=1. When B is the identity matrix, then v'Bv is exactly the square of the 2-norm. I ran the following test for various matrices A and B:
[p,d]=eig(A,B);
v=p(:,1);
v'*B*v
I always choose B to be diagonal. I noticed that v'Bv is not always 1. However, it is indeed 1 when A is symmetric. Does anyone know the rule for the way that Matlab normalizes the generalized eigenvectors? I can't find it in the document.
According to the documentation (emphasis mine):
The form and normalization of V depends on the combination of input arguments:
[...]
[V,D] = eig(A,B) and [V,D] = eig(A,B,algorithm) returns V as a matrix whose columns are the generalized right eigenvectors that satisfy A*V = B*V*D. The 2-norm of each eigenvector is not necessarily 1. In this case, D contains the generalized eigenvalues of the pair, (A,B), along the main diagonal.
When eig uses the 'chol' algorithm with symmetric (Hermitian) A and symmetric (Hermitian) positive definite B, it normalizes the eigenvectors in V so that the B-norm of each is 1.
This means that, unless you are using the 'chol' algorithm, V is not normalized.
If I get you correctly, you are looking for a way to generalize a vector then given a vector you can divide it by its norm to obtain a secondary vector whose norm is 1.
If you are looking for the mathematical background, then Eigendecomposition of a matrix contains a good introduction.

Eigenvalues are always 1

When I get the eigenvalues of the diagonal of a PCA transformed image, I always get 1, whatever the image. What's the reason behind this?
I used the following code.
coeff = pca(pmap);
disp(coeff);
[V,L]=eig (coeff'*coeff);
Lamda = diag(L);
disp(Lamda);
The coeff which pca outputs are already eigenvectors, which are all orthogonal. They are even orthonormal, since MATLAB normalises them. Relative weight is in the explained output parameter of pca.
So transpose(coeff)*coeff gives you the identity matrix, which just contains ones and the eigenvectors of the identity matrix are, obviously, all just 1 in a single dimension.
The reason is thus because that's how linear algebra works.

Do we need to normalize the eigen values in Matlab?

When using eig function in Matlab, it seems that this function has already normalize the values of the eigenvalues. Do we need to write some lines of code to normalize the eigenvalues after using the eig function.
The function eig in MATLAB normalizes the eigenvectors (not the eigenvalues).
See the following from the documentation:
[V,D] = eig(A) returns matrix V, whose columns are the right
eigenvectors of A such that AV = VD. The eigenvectors in V are
normalized so that the 2-norm of each is 1.
Eigenvectors can vary by a scalar, so a computation algorithm has to choose a particular scaled value of an eigenvector to show you. eig chooses 2-norm = 1. Just look at the eigenvector definition to see why: AV=VD. V shows up on both sides, so you can multiple V by anything without affecting the equation.
Eigenvalues do not vary. Look again at AV=VD. D is only on one side, so it can't be scaled.

Are the largest eigenvectors sorted by absolute eigenvalue?

Say I have a real symmetric matrix, and I want to extract the k largest eigenvectors using eig.
I know I can use eigs instead but that's not the point of my question.
I read that they use different algorithms, and the documentation for eigs states explicitly "largest magnitude", which seems to imply that the eigenvalues would be sorted in absolute value, especially because apparently the sign of the eigenvector/values does not matter.
However I also read that ordering the eigenvectors should be done according to the ranking of the eigenvalues, with sort(diag(D)); no absolute value here (and no assumption about positiveness for the matrix).
I think that either the latter post is wrong, or Matlab's documentation for eigs is wrong or misleading when using the words "largest magnitude", is that right? Or are they both right and I misunderstood something?
To clarify then, the "largest" eigenvectors should be sorted according to the absolute eigenvalue, correct?
The latter question you reference is discussing eig, which uses direct methods intended for general, dense matrices; you are discussing eigs, which uses iterative methods intended for general, sparse matrices.
eig will return the eigenvalues in the order found by the direct method. In general, this means random ordering. For real symmetric matrices, the ordering from the direct method appears to generate the eigenvalues from most negative to most positive.
eigs will return k eigenvalues and vectors with the largest/smallest magnitude/real part/imaginary part (user-specified). However, as noted, "eigs does not always return sorted eigenvalues and eigenvectors. Use sort to explicitly sort the output eigenvalues and eigenvectors in cases where their order is important."
Typically, yes, largest (absolute) magnitude. Though sort can be used to permute the eigenvalues into any required order.

Octave/Matlab: PCA on sparse matrix: how to get only the most important eigenvectors?

I am using Octave and have a huge sparse matrix that I have to get the eigenvalues of. However, if I just use a function to get all eigenvalues and eigenvectors, the result will take up way too much space, since the input matrix is sparse for a reason.
How can I get only a limited number of the most important eigenvectors?
Use eigs instead of eig:
D = eigs(A,k);
This returns the k largest eigenvalues of the matrix A. According to this page, Octave does support eigs for sparse matrices. eigs uses different techniques than eig, is slower overall, and shouldn't generally be used except in the cases such as the one you describe.
Be sure to check out the options for the sigma argument in case you want the largest eigenvalues with respect to their real parts only, for example.
The Matlab documentation for eigs is here.