Re: [eigen] ordering of eigenvalues of EigenSolver

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Susane,

This is the code I've been using for a while to do the sorting.
In its context it's intended to live as an extension to EigenBase, but
you could easily adapt it to your needs:

Perhaps it could be optimized, but it's tested and works ;-)



template <class Derived>
template <class MATRIX1,class VECTOR1>
EIGEN_STRONG_INLINE void Eigen::MatrixBase<Derived>::eigenVectorsVec(
MATRIX1 & eVecs, VECTOR1 & eVals ) const
{
	Eigen::EigenSolver< Derived > es(*this, true);
	eVecs = es.eigenvectors().real(); // Keep only the real part of complex matrix
	eVals = es.eigenvalues().real(); // Keep only the real part of complex matrix

	// Sort by ascending eigenvalues:
	std::vector<std::pair<Scalar,Index> > D;
	D.reserve(eVals.size());
	for (Index i=0;i<eVals.size();i++)
		D.push_back(std::make_pair<Scalar,Index>(eVals.coeff(i,0),i));
	std::sort(D.begin(),D.end());
	MATRIX1 sortedEigs;
	sortedEigs.resizeLike(eVecs);
	for (int i=0;i<eVals.size();i++)
	{
		eVals.coeffRef(i,0)=D[i].first;
		sortedEigs.col(i)=eVecs.col(D[i].second);
	}
	eVecs = sortedEigs;
}



Best,
Jose Luis


On Tue, Feb 8, 2011 at 6:08 PM, FMDSPAM <fmdspam@xxxxxxxxx> wrote:
> Am 08.02.2011 17:36, schrieb Milian Wolff:
>>
>> as I was teached: the ordering of eigen values is totally arbitrary and
>> depends on the implemented algorithm. Meaning: You have to sort them to
>> your
>> expectations.
>>
> fair enough.
> What is the most elegant way to do so in eigen(3)?
> --Frank
>
>
>



-- 
___________________________________________________________

Dr. Jose-Luis Blanco-Claraco
Dpt. Ing. Civil, Mat. y Fabric - Phone: +34 951 952435
E.T.S.I. Industriales - Despacho 2.037
Universidad de Malaga - Campus Universitario de Teatinos
29071 Malaga, Spain
https://sites.google.com/site/jlblancosite/
___________________________________________________________



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/