MEPP2 Project
eigen_val_vect.hpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 #pragma once
12 
13 #include <Eigen/Dense>
14 #include <Eigen/Eigenvalues>
15 #include <cmath> // for std::abs()
16 #include <vector>
17 
18 
19 using EigenMatrix = Eigen::Matrix3d;
20 using EigenvectorsType = Eigen::Matrix3d;
21 using EigenvalueType = Eigen::Vector3d;
22 
23 /*
24  Compute eigenvalues and eigenvectors of a double symmetric matrix
25 
26  \param A (input) double symmetric matrix
27  \param U (ouput) eigenvectors
28  \param AD (ouput) eigenvalues, unsorted
29  \return 0 if ok
30 */
31 inline
32 int
34 {
35  Eigen::EigenSolver< EigenMatrix > solver(a, true);
36 
37  // check for error
38  if( solver.info() != Eigen::ComputationInfo::Success )
39  return -1;
40 
41  // ELO-note:
42  // solver.eigenvalues() and solver.eigenvectors() return
43  // matrices of std::complex values ; here we keep only
44  // the real part of this values
45  ad = solver.eigenvalues().real();
46  u = solver.eigenvectors().real();
47 
48  return 0;
49 }
50 
51 /*
52  Sort eigenvalues by descending order of their absolute values,
53  and sort eigenvectors accordingly
54 
55  \param AD eigenvalues ; at function return the values are sorted
56  by descending order of their absolute value
57  \param U eigenvectors ; at function return the vectors are sorted
58  in the same order as eigenvalues
59 */
60 inline
61 void
63 {
64  // sort eigenvalues indices by descending order of their absolute values
65  std::vector< size_t > sorted_indices;
66  sorted_indices.push_back(0);
67  for(int i = 1; i < 3; i++)
68  {
69  // find the place where to insert current eigenvalue
70  auto it = sorted_indices.begin();
71  for(; it != sorted_indices.end(); ++it)
72  {
73  if(std::abs(ad(*it)) < std::abs(ad(i)))
74  break;
75  }
76 
77  // insert current eigenvalue index here
78  sorted_indices.insert(it, i);
79  }
80 
81  // sort eigenvalues and eigenvectors
82  EigenvalueType sorted_eigen_val;
83  EigenvectorsType sorted_eigen_vect;
84 
85  for(int i = 0; i < 3; i++)
86  {
87  sorted_eigen_val(i) = ad(sorted_indices[i]);
88  sorted_eigen_vect.col(i) = u.col(sorted_indices[i]);
89  }
90 
91  ad = sorted_eigen_val;
92  u = sorted_eigen_vect;
93 }
EigenMatrix
Eigen::Matrix3d EigenMatrix
Definition: eigen_val_vect.hpp:19
EigenvectorsType
Eigen::Matrix3d EigenvectorsType
Definition: eigen_val_vect.hpp:20
EigenvalueType
Eigen::Vector3d EigenvalueType
Definition: eigen_val_vect.hpp:21
eigen_val_vect_sort
void eigen_val_vect_sort(EigenvalueType &ad, EigenvectorsType &u)
Definition: eigen_val_vect.hpp:62
eigen_val_vect_compute
int eigen_val_vect_compute(const EigenMatrix &a, EigenvectorsType &u, EigenvalueType &ad)
Definition: eigen_val_vect.hpp:33