MUQ  0.4.3
GeneralizedEigenSolver.cpp
Go to the documentation of this file.
2 
3 using namespace muq::Modeling;
4 
5 
6 
7 void GeneralizedEigenSolver::SortVec(std::vector<std::pair<int,int>> const& swapInds,
8  Eigen::Ref<Eigen::VectorXd> matrix)
9 {
10  for(auto& swap : swapInds)
11  std::swap(matrix(swap.first),matrix(swap.second));
12 }
13 
14 void GeneralizedEigenSolver::SortVec(std::vector<std::pair<int,int>> const& swapInds,
15  std::vector<bool> & vec)
16 {
17  for(auto& swap : swapInds)
18  std::vector<bool>::swap(vec[swap.first],vec[swap.second]);
19 }
20 
21 void GeneralizedEigenSolver::SortCols(std::vector<std::pair<int,int>> const& swapInds,
22  Eigen::Ref<Eigen::MatrixXd> matrix)
23 {
24  for(auto& swap : swapInds)
25  matrix.col(swap.first).swap(matrix.col(swap.second));
26 }
27 
28 std::vector<std::pair<int,int>> GeneralizedEigenSolver::GetSortSwaps(Eigen::Ref<const Eigen::VectorXd> const& residNorms)
29 {
30  std::vector<bool> allActive(residNorms.size(), true);
31  return GetSortSwaps(residNorms, allActive);
32 }
33 
34 std::vector<std::pair<int,int>> GeneralizedEigenSolver::GetSortSwaps(Eigen::Ref<const Eigen::VectorXd> const& resids,
35  std::vector<bool> const& isActive)
36 {
37  Eigen::VectorXd newResids = resids;
38  std::vector<bool> newIsActive = isActive;
39 
40  const unsigned int size = resids.size();
41 
42  std::vector<std::pair<int,int>> swaps;
43  unsigned int maxInd;
44 
45  // First, put all the active indices at the begining
46  int firstInactiveInd = 0;
47  while((firstInactiveInd<isActive.size())&&(newIsActive.at(firstInactiveInd)))
48  firstInactiveInd++;
49 
50  int lastActiveInd = isActive.size()-1;
51  while((lastActiveInd>=0)&&(!newIsActive.at(lastActiveInd)))
52  lastActiveInd--;
53 
54  while(firstInactiveInd<lastActiveInd){
55 
56  swaps.push_back(std::make_pair(firstInactiveInd,lastActiveInd));
57  std::vector<bool>::swap(newIsActive.at(firstInactiveInd), newIsActive.at(lastActiveInd));
58  std::swap(newResids(firstInactiveInd), newResids(lastActiveInd));
59 
60  while((firstInactiveInd<isActive.size())&&(newIsActive.at(firstInactiveInd)))
61  firstInactiveInd++;
62 
63  while((lastActiveInd>=0)&&(!newIsActive.at(lastActiveInd)))
64  lastActiveInd--;
65  }
66 
67  unsigned int numActive = lastActiveInd+1;
68 
69  // Now, sort all of the active residuals according to magnitude
70  for(unsigned int i=0; i<numActive; ++i)
71  {
72  // Find the maximum index
73  maxInd = std::distance(newResids.data(), std::max_element(&newResids(i), newResids.data()+numActive));
74 
75  // Swap indices if needed
76  if(maxInd!=i){
77  swaps.push_back( std::make_pair(i, maxInd) );
78  std::swap(newResids(i), newResids(maxInd));
79  }
80  }
81 
82  return swaps;
83 }
static std::vector< std::pair< int, int > > GetSortSwaps(Eigen::Ref< const Eigen::VectorXd > const &residNorms, std::vector< bool > const &isActive)
static void SortVec(std::vector< std::pair< int, int >> const &swapInds, Eigen::Ref< Eigen::VectorXd > matrix)
static void SortCols(std::vector< std::pair< int, int >> const &swapInds, Eigen::Ref< Eigen::MatrixXd > matrix)