14 #ifndef ANASAZI_BASIC_SORT_HPP
15 #define ANASAZI_BASIC_SORT_HPP
32 template<
class MagnitudeType>
48 BasicSort(
const std::string &which =
"LM" );
79 void sort(std::vector<MagnitudeType> &evals,
Teuchos::RCP<std::vector<int> > perm = Teuchos::null,
int n = -1)
const;
99 void sort(std::vector<MagnitudeType> &r_evals,
100 std::vector<MagnitudeType> &i_evals,
115 template <
class LTorGT>
118 bool operator()(MagnitudeType, MagnitudeType);
120 template <
class First,
class Second>
121 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
124 template <
class LTorGT>
127 bool operator()(std::pair<MagnitudeType,MagnitudeType>, std::pair<MagnitudeType,MagnitudeType>);
129 template <
class First,
class Second>
130 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
133 template <
class LTorGT>
136 bool operator()(MagnitudeType, MagnitudeType);
137 template <
class First,
class Second>
138 bool operator()(std::pair<First,Second>, std::pair<First,Second>);
141 template <
typename pair_type>
144 const typename pair_type::first_type &operator()(
const pair_type &v)
const;
147 template <
typename pair_type>
150 const typename pair_type::second_type &operator()(
const pair_type &v)
const;
159 template<
class MagnitudeType>
162 std::string which =
"LM";
163 which = pl.
get(
"Sort Strategy",which);
167 template<
class MagnitudeType>
173 template<
class MagnitudeType>
177 template<
class MagnitudeType>
181 std::string whichlc(which);
182 std::transform(which.begin(),which.end(),whichlc.begin(),(int(*)(int)) std::toupper);
183 if (whichlc ==
"LM") {
186 else if (whichlc ==
"SM") {
189 else if (whichlc ==
"LR") {
192 else if (whichlc ==
"SR") {
195 else if (whichlc ==
"LI") {
198 else if (whichlc ==
"SI") {
206 template<
class MagnitudeType>
214 std::invalid_argument,
"Anasazi::BasicSort::sort(r): eigenvalue vector size isn't consistent with n.");
215 if (perm != Teuchos::null) {
217 std::invalid_argument,
"Anasazi::BasicSort::sort(r): permutation vector size isn't consistent with n.");
220 typedef std::greater<MagnitudeType> greater_mt;
221 typedef std::less<MagnitudeType> less_mt;
223 if (perm == Teuchos::null) {
228 std::sort(evals.begin(),evals.begin()+n,compMag<greater_mt>());
230 else if (which_ == SM) {
231 std::sort(evals.begin(),evals.begin()+n,compMag<less_mt>());
233 else if (which_ == LR) {
234 std::sort(evals.begin(),evals.begin()+n,compAlg<greater_mt>());
236 else if (which_ == SR) {
237 std::sort(evals.begin(),evals.begin()+n,compAlg<less_mt>());
253 std::vector< std::pair<MagnitudeType,int> > pairs(n);
254 for (
int i=0; i<n; i++) {
255 pairs[i] = std::make_pair(evals[i],i);
260 std::sort(pairs.begin(),pairs.begin()+n,compMag<greater_mt>());
262 else if (which_ == SM) {
263 std::sort(pairs.begin(),pairs.begin()+n,compMag<less_mt>());
265 else if (which_ == LR) {
266 std::sort(pairs.begin(),pairs.begin()+n,compAlg<greater_mt>());
268 else if (which_ == SR) {
269 std::sort(pairs.begin(),pairs.begin()+n,compAlg<less_mt>());
276 std::transform(pairs.begin(),pairs.end(),evals.begin(),sel1st< std::pair<MagnitudeType,int> >());
277 std::transform(pairs.begin(),pairs.end(),perm->begin(),sel2nd< std::pair<MagnitudeType,int> >());
282 template<
class T1,
class T2>
285 std::pair<T1,T2> operator()(
const T1 &t1,
const T2 &t2 )
286 {
return std::make_pair(t1, t2); }
290 template<
class MagnitudeType>
292 std::vector<MagnitudeType> &i_evals,
302 n = r_evals.size() < i_evals.size() ? r_evals.size() : i_evals.size();
305 std::invalid_argument,
"Anasazi::BasicSort::sort(r,i): eigenvalue vector size isn't consistent with n.");
306 if (perm != Teuchos::null) {
308 std::invalid_argument,
"Anasazi::BasicSort::sort(r,i): permutation vector size isn't consistent with n.");
311 typedef std::greater<MagnitudeType> greater_mt;
312 typedef std::less<MagnitudeType> less_mt;
317 if (perm == Teuchos::null) {
321 std::vector< std::pair<MagnitudeType,MagnitudeType> > pairs(n);
325 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
327 r_evals.begin(), r_evals.begin()+n,
328 i_evals.begin(), pairs.begin(),
329 MakePairOp<MagnitudeType,MagnitudeType>());
333 i_evals.begin(), i_evals.begin()+n,
334 r_evals.begin(), pairs.begin(),
335 MakePairOp<MagnitudeType,MagnitudeType>());
338 if (which_ == LR || which_ == LI) {
339 std::sort(pairs.begin(),pairs.end(),compAlg<greater_mt>());
341 else if (which_ == SR || which_ == SI) {
342 std::sort(pairs.begin(),pairs.end(),compAlg<less_mt>());
344 else if (which_ == LM) {
345 std::sort(pairs.begin(),pairs.end(),compMag2<greater_mt>());
348 std::sort(pairs.begin(),pairs.end(),compMag2<less_mt>());
354 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
355 std::transform(pairs.begin(),pairs.end(),r_evals.begin(),sel1st< std::pair<MagnitudeType,MagnitudeType> >());
356 std::transform(pairs.begin(),pairs.end(),i_evals.begin(),sel2nd< std::pair<MagnitudeType,MagnitudeType> >());
359 std::transform(pairs.begin(),pairs.end(),r_evals.begin(),sel2nd< std::pair<MagnitudeType,MagnitudeType> >());
360 std::transform(pairs.begin(),pairs.end(),i_evals.begin(),sel1st< std::pair<MagnitudeType,MagnitudeType> >());
367 std::vector< std::pair< std::pair<MagnitudeType,MagnitudeType>,
int > > pairs(n);
371 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
372 for (
int i=0; i<n; i++) {
373 pairs[i] = std::make_pair(std::make_pair(r_evals[i],i_evals[i]),i);
377 for (
int i=0; i<n; i++) {
378 pairs[i] = std::make_pair(std::make_pair(i_evals[i],r_evals[i]),i);
382 if (which_ == LR || which_ == LI) {
383 std::sort(pairs.begin(),pairs.end(),compAlg<greater_mt>());
385 else if (which_ == SR || which_ == SI) {
386 std::sort(pairs.begin(),pairs.end(),compAlg<less_mt>());
388 else if (which_ == LM) {
389 std::sort(pairs.begin(),pairs.end(),compMag2<greater_mt>());
392 std::sort(pairs.begin(),pairs.end(),compMag2<less_mt>());
398 if (which_ == LR || which_ == SR || which_ == LM || which_ == SM) {
399 for (
int i=0; i<n; i++) {
400 r_evals[i] = pairs[i].first.first;
401 i_evals[i] = pairs[i].first.second;
402 (*perm)[i] = pairs[i].second;
406 for (
int i=0; i<n; i++) {
407 i_evals[i] = pairs[i].first.first;
408 r_evals[i] = pairs[i].first.second;
409 (*perm)[i] = pairs[i].second;
416 template<
class MagnitudeType>
417 template<
class LTorGT>
422 return comp( MTT::magnitude(v1), MTT::magnitude(v2) );
425 template<
class MagnitudeType>
426 template<
class LTorGT>
427 bool BasicSort<MagnitudeType>::compMag2<LTorGT>::operator()(std::pair<MagnitudeType,MagnitudeType> v1, std::pair<MagnitudeType,MagnitudeType> v2)
429 MagnitudeType m1 = v1.first*v1.first + v1.second*v1.second;
430 MagnitudeType m2 = v2.first*v2.first + v2.second*v2.second;
432 return comp( m1, m2 );
435 template<
class MagnitudeType>
436 template<
class LTorGT>
437 bool BasicSort<MagnitudeType>::compAlg<LTorGT>::operator()(MagnitudeType v1, MagnitudeType v2)
440 return comp( v1, v2 );
443 template<
class MagnitudeType>
444 template<
class LTorGT>
445 template<
class First,
class Second>
446 bool BasicSort<MagnitudeType>::compMag<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
447 return (*
this)(v1.first,v2.first);
450 template<
class MagnitudeType>
451 template<
class LTorGT>
452 template<
class First,
class Second>
453 bool BasicSort<MagnitudeType>::compMag2<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
454 return (*
this)(v1.first,v2.first);
457 template<
class MagnitudeType>
458 template<
class LTorGT>
459 template<
class First,
class Second>
460 bool BasicSort<MagnitudeType>::compAlg<LTorGT>::operator()(std::pair<First,Second> v1, std::pair<First,Second> v2) {
461 return (*
this)(v1.first,v2.first);
464 template <
class MagnitudeType>
465 template <
typename pair_type>
466 const typename pair_type::first_type &
467 BasicSort<MagnitudeType>::sel1st<pair_type>::operator()(
const pair_type &v)
const
472 template <
class MagnitudeType>
473 template <
typename pair_type>
474 const typename pair_type::second_type &
475 BasicSort<MagnitudeType>::sel2nd<pair_type>::operator()(
const pair_type &v)
const
482 #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...
T & get(const std::string &name, T def_value)
void setSortType(const std::string &which)
Set sort type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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...
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.
Anasazi's templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
virtual ~BasicSort()
Destructor.