ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkSymmetricEigenDecomposition.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkSymmetricEigenDecomposition_h
19#define itkSymmetricEigenDecomposition_h
20
21#include "vnl/vnl_matrix.h"
22#include "vnl/vnl_vector.h"
23#include "vnl/vnl_diag_matrix.h"
26#include "itkMacro.h"
27#include "itk_eigen.h"
28#include ITK_EIGEN(Dense)
29
30namespace itk
31{
32
59template <typename T>
61{
62public:
67 explicit SymmetricEigenDecomposition(const vnl_matrix<T> & M, bool canonicalizeSigns = true)
68 : V(M.rows(), M.cols())
69 , D(M.rows())
70 {
71 using RowMajor = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
72 using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
73 const unsigned int n = M.rows();
74
75 Eigen::Map<const RowMajor> mMap(M.data_block(), n, n);
76 const Eigen::SelfAdjointEigenSolver<RowMajor> solver(mMap);
77
78 if (solver.info() != Eigen::Success)
79 {
80 itkGenericExceptionMacro(<< "SymmetricEigenDecomposition: Eigen SelfAdjointEigenSolver failed: "
81 << detail::EigenComputationInfoString(solver.info()) << '.');
82 }
83
84 Eigen::Map<RowMajor>(V.data_block(), n, n) = solver.eigenvectors();
85 Eigen::Map<Vector>(D.data_block(), n) = solver.eigenvalues();
86
87 if (canonicalizeSigns)
88 {
90 }
91 }
92
94 vnl_matrix<T> V;
95
97 vnl_diag_matrix<T> D;
98
100 vnl_vector<T>
101 get_eigenvector(int i) const
102 {
103 return V.get_column(i);
104 }
105
107 T
108 get_eigenvalue(int i) const
109 {
110 return D(i, i);
111 }
112
114 vnl_vector<T>
116 {
117 return V.get_column(0);
118 }
119
121 vnl_matrix<T>
122 recompose() const
123 {
124 return V * D * V.transpose();
125 }
126};
127
128} // namespace itk
129
130#endif // itkSymmetricEigenDecomposition_h
SymmetricEigenDecomposition(const vnl_matrix< T > &M, bool canonicalizeSigns=true)
A templated class holding a n-Dimensional vector.
Definition itkVector.h:63
const char * EigenComputationInfoString(Eigen::ComputationInfo info)
void CanonicalizeEigenvectorColumnSigns(vnl_matrix< T > &V)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....