lambda-lanczos 2.1.1
Loading...
Searching...
No Matches
eigenpair_manager.hpp
Go to the documentation of this file.
1#ifndef LAMBDA_LANCZOS_EIGENPAIR_MANAGER_H_
2#define LAMBDA_LANCZOS_EIGENPAIR_MANAGER_H_
3
4#include <functional>
5#include <vector>
6
8
9namespace lambda_lanczos {
11
21template <typename T>
23 private:
24 template <typename n_type>
26
27 const bool find_maximum;
28 const size_t num_eigs;
29
30 std::multimap<real_t<T>, std::vector<T>, std::function<bool(real_t<T>, real_t<T>)>> eigenpairs;
31 // This super long definition is required because the default comparator type is std::less<?>.
32
33 public:
35 std::function<bool(real_t<T>, real_t<T>)> comp;
36 if (find_maximum) {
37 comp = std::greater<real_t<T>>();
38 } else {
39 comp = std::less<real_t<T>>();
40 }
41
42 std::vector<std::pair<real_t<T>, std::vector<T>>> dummy;
43 this->eigenpairs = std::multimap<real_t<T>, std::vector<T>, std::function<bool(real_t<T>, real_t<T>)>>(
44 dummy.begin(), dummy.end(), comp);
45 // This super long definition is required because the default comparator type is std::less<?>.
46 }
47
48 size_t size() const {
49 return eigenpairs.size();
50 }
51
52 bool insertEigenpairs(std::vector<real_t<T>>& eigenvalues, std::vector<std::vector<T>>& eigenvectors) {
53 assert(eigenvalues.size() == eigenvectors.size());
54
55 bool nothing_added = true;
56 for (size_t i = 0; i < eigenvalues.size(); ++i) {
57 auto inserted = eigenpairs.emplace(std::move(eigenvalues[i]), std::move(eigenvectors[i]));
58 auto last = eigenpairs.end();
59 last--;
60 if (eigenpairs.size() > num_eigs) {
61 if (inserted != last) { // If the eigenpair is not inserted to the tail
62 nothing_added = false;
63 }
64 eigenpairs.erase(last);
65 } else {
66 nothing_added = false;
67 }
68 }
69
70 return nothing_added;
71 }
72
76
78 return eigenpairs;
79 };
80};
81
82} /* namespace eigenpair_manager */
83} /* namespace lambda_lanczos */
84#endif /* LAMBDA_LANCZOS_EIGENPAIR_MANAGER_H_ */
std::multimap< real_t< T >, std::vector< T >, std::function< bool(real_t< T >, real_t< T >)> > eigenpairs
Definition eigenpair_manager.hpp:30
EigenPairManager(bool find_maximum, size_t num_eigs)
Definition eigenpair_manager.hpp:34
size_t size() const
Definition eigenpair_manager.hpp:48
const size_t num_eigs
Definition eigenpair_manager.hpp:28
const bool find_maximum
Definition eigenpair_manager.hpp:27
decltype(eigenpairs) & getEigenpairs()
Definition eigenpair_manager.hpp:77
lambda_lanczos::util::MapValueIterable< decltype(eigenpairs)> getEigenvectors() const
Definition eigenpair_manager.hpp:73
bool insertEigenpairs(std::vector< real_t< T > > &eigenvalues, std::vector< std::vector< T > > &eigenvectors)
Definition eigenpair_manager.hpp:52
util::real_t< n_type > real_t
Definition eigenpair_manager.hpp:25
Definition eigenpair_manager.hpp:10
typename realTypeMap< T >::type real_t
Type mapper from T to real type of T.
Definition common.hpp:102
Definition eigenpair_manager.hpp:9