This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Ordering the eigenvalues and print them to file

Tags: None
(comma "," separated)
EigenRobert
Registered Member
Posts
7
Karma
0
Dear All,

I got 2 question :
1.) I kindly would like to know which property should I set to get the eigenvalues ordered, because they are presented at the arbitrary order, but I want them to be from smallest to biggest?
2.) Probably it is not totally eigen library issue but I want to write those alpha values to be written to file, since alphas() or betas() are of ComplexVectorType I don't know how to convert them to simple float or double type in order to be able to write them with following code printf(myfile, "Eigen value is : %f.8", ges.alphas() ) .

Your helps will be appreciated,

Regards,
jitseniesen
Registered Member
Posts
204
Karma
2
I assume you want to solve a generalized eigenvalue problem solved using GeneralizedEigenSolver. In this case, the eigenvalues are complex numbers, even if all the inputs are real. Thus, for sorting the eigenvalues, you need to specify how you want to sort complex numbers (is 2+3i bigger or smaller than 3?). To print the eigenvalues, you need to do something like
Code: Select all
for (int i=0; i < /* size of matrix */; ++i)
   fprintf(myfile, "Eigenvalue %d is : %f.8 + %f.8 i\n", i, ges.alphas()(i).real(),  ges.alphas()(i).imag());

If your matrix is actually symmetric, then the eigenvalues are always real and you can use GeneralizedSelfAdjointEigenSolver. This will avoid all issues with complex numbers.
EigenRobert
Registered Member
Posts
7
Karma
0
Thank you in advance,

Thus, for sorting the eigenvalues, you need to specify how you want to sort complex numbers (is 2+3i bigger or smaller than 3?).

Where actually do we specify the soting method for both type of solvers, of course with the same entity complex/complex or real/real?

Regards,
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You have to do it manually, there is no sorting facilities yet.
jitseniesen
Registered Member
Posts
204
Karma
2
EigenRobert wrote:Where actually do we specify the soting method for both type of solvers, of course with the same entity complex/complex or real/real?

I should have added that there is no functionality provided in the Eigen library for this. The easiest is to copy the eigenvalues to a new vector and then use std::sort() for the sorting. You need to copy the eigenvalues first because you cannot change the eigenvalues in the GeneralizedEigenSolver object. Something like the following (code not tested):
Code: Select all
VectorXcd alphas = ges.alphas(); // assuming you are using doubles
std::sort(alphas.data(), alphas.data() + alphas.size(), sorting_function);
where the sorting function is something like:
Code: Select all
bool sorting_function(std::complex<double> x, std::complex<double> y)
{
   return x.real() < y.real() || (x.real() == y.real() && x.imag() < y.imag());
   // sort by real part; use imaginary part for tie breaks
}


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell