33 #ifndef ANASAZI_BASIC_SORT_HPP 34 #define ANASAZI_BASIC_SORT_HPP 45 #include "Teuchos_LAPACK.hpp" 46 #include "Teuchos_ScalarTraits.hpp" 47 #include "Teuchos_ParameterList.hpp" 51 template<
class MagnitudeType>
67 BasicSort(
const std::string &which =
"LM" );
98 void sort(std::vector<MagnitudeType> &evals, Teuchos::RCP<std::vector<int> > perm = Teuchos::null,
int n = -1)
const;
118 void sort(std::vector<MagnitudeType> &r_evals,
119 std::vector<MagnitudeType> &i_evals,
120 Teuchos::RCP<std::vector<int> > perm = Teuchos::null,
134 template <
class LTorGT>
137 bool operator()(MagnitudeType, MagnitudeType);
139 template <
class First,
class Second>
140 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
143 template <
class LTorGT>
146 bool operator()(std::pair<MagnitudeType,MagnitudeType>, std::pair<MagnitudeType,MagnitudeType>);
148 template <
class First,
class Second>
149 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
152 template <
class LTorGT>
155 bool operator()(MagnitudeType, MagnitudeType);
156 template <
class First,
class Second>
157 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
160 template <
typename pair_type>
163 const typename pair_type::first_type &operator()(
const pair_type &v)
const;
166 template <
typename pair_type>
169 const typename pair_type::second_type &operator()(
const pair_type &v)
const;
178 template<
class MagnitudeType>
181 std::string which =
"LM";
182 which = pl.get(
"Sort Strategy",which);
186 template<
class MagnitudeType>
192 template<
class MagnitudeType>
196 template<
class MagnitudeType>
200 std::string whichlc(which);
201 std::transform(which.begin(),which.end(),whichlc.begin(),(int(*)(int)) std::toupper);
202 if (whichlc ==
"LM") {
205 else if (whichlc ==
"SM") {
208 else if (whichlc ==
"LR") {
211 else if (whichlc ==
"SR") {
214 else if (whichlc ==
"LI") {
217 else if (whichlc ==
"SI") {
221 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
"Anasazi::BasicSort::setSortType(): sorting order is not valid");
225 template<
class MagnitudeType>
228 TEUCHOS_TEST_FOR_EXCEPTION(n < -1, std::invalid_argument,
"Anasazi::BasicSort::sort(r): n must be n >= 0 or n == -1.");
232 TEUCHOS_TEST_FOR_EXCEPTION(evals.size() < (
unsigned int) n,
233 std::invalid_argument,
"Anasazi::BasicSort::sort(r): eigenvalue vector size isn't consistent with n.");
234 if (perm != Teuchos::null) {
235 TEUCHOS_TEST_FOR_EXCEPTION(perm->size() < (
unsigned int) n,
236 std::invalid_argument,
"Anasazi::BasicSort::sort(r): permutation vector size isn't consistent with n.");
239 typedef std::greater<MagnitudeType> greater_mt;
240 typedef std::less<MagnitudeType> less_mt;
242 if (perm == Teuchos::null) {
247 std::sort(evals.begin(),evals.begin()+n,compMag<greater_mt>());
249 else if (which_ == SM) {
250 std::sort(evals.begin(),evals.begin()+n,compMag<less_mt>());
252 else if (which_ == LR) {
253 std::sort(evals.begin(),evals.begin()+n,compAlg<greater_mt>());
255 else if (which_ == SR) {
256 std::sort(evals.begin(),evals.begin()+n,compAlg<less_mt>());
259 TEUCHOS_TEST_FOR_EXCEPTION(
true,
SortManagerError,
"Anasazi::BasicSort::sort(r): LI or SI sorting invalid for real scalar types." );
272 std::vector< std::pair<MagnitudeType,int> > pairs(n);
273 for (
int i=0; i<n; i++) {
274 pairs[i] = std::make_pair(evals[i],i);
279 std::sort(pairs.begin(),pairs.begin()+n,compMag<greater_mt>());
281 else if (which_ == SM) {
282 std::sort(pairs.begin(),pairs.begin()+n,compMag<less_mt>());
284 else if (which_ == LR) {
285 std::sort(pairs.begin(),pairs.begin()+n,compAlg<greater_mt>());
287 else if (which_ == SR) {
288 std::sort(pairs.begin(),pairs.begin()+n,compAlg<less_mt>());
291 TEUCHOS_TEST_FOR_EXCEPTION(
true,
SortManagerError,
"Anasazi::BasicSort::sort(r): LI or SI sorting invalid for real scalar types." );
295 std::transform(pairs.begin(),pairs.end(),evals.begin(),sel1st< std::pair<MagnitudeType,int> >());
296 std::transform(pairs.begin(),pairs.end(),perm->begin(),sel2nd< std::pair<MagnitudeType,int> >());
301 template<
class T1,
class T2>
304 std::pair<T1,T2> operator()(
const T1 &t1,
const T2 &t2 )
305 {
return std::make_pair(t1, t2); }
309 template<
class MagnitudeType>
311 std::vector<MagnitudeType> &i_evals,
312 Teuchos::RCP< std::vector<int> > perm,
319 TEUCHOS_TEST_FOR_EXCEPTION(n < -1, std::invalid_argument,
"Anasazi::BasicSort::sort(r,i): n must be n >= 0 or n == -1.");
321 n = r_evals.size() < i_evals.size() ? r_evals.size() : i_evals.size();
323 TEUCHOS_TEST_FOR_EXCEPTION(r_evals.size() < (
unsigned int) n || i_evals.size() < (
unsigned int) n,
324 std::invalid_argument,
"Anasazi::BasicSort::sort(r,i): eigenvalue vector size isn't consistent with n.");
325 if (perm != Teuchos::null) {
326 TEUCHOS_TEST_FOR_EXCEPTION(perm->size() < (
unsigned int) n,
327 std::invalid_argument,
"Anasazi::BasicSort::sort(r,i): permutation vector size isn't consistent with n.");
330 typedef std::greater<MagnitudeType> greater_mt;
331 typedef std::less<MagnitudeType> less_mt;
336 if (perm == Teuchos::null) {
340 std::vector< std::pair<MagnitudeType,MagnitudeType> > pairs(n);
344 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
346 r_evals.begin(), r_evals.begin()+n,
347 i_evals.begin(), pairs.begin(),
348 MakePairOp<MagnitudeType,MagnitudeType>());
352 i_evals.begin(), i_evals.begin()+n,
353 r_evals.begin(), pairs.begin(),
354 MakePairOp<MagnitudeType,MagnitudeType>());
357 if (which_ == LR || which_ == LI) {
358 std::sort(pairs.begin(),pairs.end(),compAlg<greater_mt>());
360 else if (which_ == SR || which_ == SI) {
361 std::sort(pairs.begin(),pairs.end(),compAlg<less_mt>());
363 else if (which_ == LM) {
364 std::sort(pairs.begin(),pairs.end(),compMag2<greater_mt>());
367 std::sort(pairs.begin(),pairs.end(),compMag2<less_mt>());
373 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
374 std::transform(pairs.begin(),pairs.end(),r_evals.begin(),sel1st< std::pair<MagnitudeType,MagnitudeType> >());
375 std::transform(pairs.begin(),pairs.end(),i_evals.begin(),sel2nd< std::pair<MagnitudeType,MagnitudeType> >());
378 std::transform(pairs.begin(),pairs.end(),r_evals.begin(),sel2nd< std::pair<MagnitudeType,MagnitudeType> >());
379 std::transform(pairs.begin(),pairs.end(),i_evals.begin(),sel1st< std::pair<MagnitudeType,MagnitudeType> >());
386 std::vector< std::pair< std::pair<MagnitudeType,MagnitudeType>,
int > > pairs(n);
390 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
391 for (
int i=0; i<n; i++) {
392 pairs[i] = std::make_pair(std::make_pair(r_evals[i],i_evals[i]),i);
396 for (
int i=0; i<n; i++) {
397 pairs[i] = std::make_pair(std::make_pair(i_evals[i],r_evals[i]),i);
401 if (which_ == LR || which_ == LI) {
402 std::sort(pairs.begin(),pairs.end(),compAlg<greater_mt>());
404 else if (which_ == SR || which_ == SI) {
405 std::sort(pairs.begin(),pairs.end(),compAlg<less_mt>());
407 else if (which_ == LM) {
408 std::sort(pairs.begin(),pairs.end(),compMag2<greater_mt>());
411 std::sort(pairs.begin(),pairs.end(),compMag2<less_mt>());
417 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
418 for (
int i=0; i<n; i++) {
419 r_evals[i] = pairs[i].first.first;
420 i_evals[i] = pairs[i].first.second;
421 (*perm)[i] = pairs[i].second;
425 for (
int i=0; i<n; i++) {
426 i_evals[i] = pairs[i].first.first;
427 r_evals[i] = pairs[i].first.second;
428 (*perm)[i] = pairs[i].second;
435 template<
class MagnitudeType>
436 template<
class LTorGT>
439 typedef Teuchos::ScalarTraits<MagnitudeType> MTT;
441 return comp( MTT::magnitude(v1), MTT::magnitude(v2) );
444 template<
class MagnitudeType>
445 template<
class LTorGT>
446 bool BasicSort<MagnitudeType>::compMag2<LTorGT>::operator()(std::pair<MagnitudeType,MagnitudeType> v1, std::pair<MagnitudeType,MagnitudeType> v2)
448 MagnitudeType m1 = v1.first*v1.first + v1.second*v1.second;
449 MagnitudeType m2 = v2.first*v2.first + v2.second*v2.second;
451 return comp( m1, m2 );
454 template<
class MagnitudeType>
455 template<
class LTorGT>
456 bool BasicSort<MagnitudeType>::compAlg<LTorGT>::operator()(MagnitudeType v1, MagnitudeType v2)
459 return comp( v1, v2 );
462 template<
class MagnitudeType>
463 template<
class LTorGT>
464 template<
class First,
class Second>
465 bool BasicSort<MagnitudeType>::compMag<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
466 return (*
this)(v1.first,v2.first);
469 template<
class MagnitudeType>
470 template<
class LTorGT>
471 template<
class First,
class Second>
472 bool BasicSort<MagnitudeType>::compMag2<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
473 return (*
this)(v1.first,v2.first);
476 template<
class MagnitudeType>
477 template<
class LTorGT>
478 template<
class First,
class Second>
479 bool BasicSort<MagnitudeType>::compAlg<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
480 return (*
this)(v1.first,v2.first);
483 template <
class MagnitudeType>
484 template <
typename pair_type>
485 const typename pair_type::first_type &
486 BasicSort<MagnitudeType>::sel1st<pair_type>::operator()(
const pair_type &v)
const 491 template <
class MagnitudeType>
492 template <
typename pair_type>
493 const typename pair_type::second_type &
494 BasicSort<MagnitudeType>::sel2nd<pair_type>::operator()(
const pair_type &v)
const 501 #endif // ANASAZI_BASIC_SORT_HPP BasicSort(Teuchos::ParameterList &pl)
Parameter list driven constructor.
An implementation of the Anasazi::SortManager that performs a collection of common sorting techniques...
void sort(std::vector< MagnitudeType > &evals, Teuchos::RCP< std::vector< int > > perm=Teuchos::null, int n=-1) const
Sort real eigenvalues, optionally returning the permutation vector.
void setSortType(const std::string &which)
Set sort type.
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
SortManagerError is thrown when the Anasazi::SortManager is unable to sort the numbers, due to some failure of the sort method or error in calling it.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
Virtual base class which defines the interface between an eigensolver and a class whose job is the so...
Anasazi's templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
virtual ~BasicSort()
Destructor.