ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkMatrixExponential.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 itkMatrixExponential_h
19#define itkMatrixExponential_h
20
21#include "itkMatrix.h"
22#include "itk_eigen.h"
23// Eigen's matrix exponential lives in the unsupported MatrixFunctions module.
24#include ITK_EIGEN_UNSUPPORTED(MatrixFunctions)
25
26namespace itk
27{
28namespace Math
29{
30
38namespace detail
39{
40// VNL/itk::Matrix storage is row-major and contiguous, so map the input/output
41// blocks directly instead of copying element-by-element. Compile-time
42// dimensions let Eigen size its exp() temporaries on the stack.
43template <unsigned int VRows, unsigned int VColumns, typename TReal>
44void
45MatrixExponentialEigen(const TReal * inData, TReal * outData)
46{
47 using RowMajorMatrix = Eigen::Matrix<TReal, VRows, VColumns, Eigen::RowMajor>;
48 Eigen::Map<const RowMajorMatrix> inMap(inData);
49 Eigen::Map<RowMajorMatrix> outMap(outData);
50 outMap = inMap.exp();
51}
52
53template <typename TReal>
54void
55MatrixExponentialEigen(const TReal * inData, TReal * outData, unsigned int n)
56{
57 using RowMajorMatrix = Eigen::Matrix<TReal, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
58 Eigen::Map<const RowMajorMatrix> inMap(inData, n, n);
59 Eigen::Map<RowMajorMatrix> outMap(outData, n, n);
60 outMap = inMap.exp();
61}
62} // namespace detail
63
65template <typename T, unsigned int VRows, unsigned int VColumns>
68{
69 static_assert(VRows == VColumns, "MatrixExponential requires a square matrix");
71 detail::MatrixExponentialEigen<VRows, VColumns>(A.GetVnlMatrix().data_block(), result.GetVnlMatrix().data_block());
72 return result;
73}
74
76template <typename T, unsigned int VRows, unsigned int VColumns>
77vnl_matrix_fixed<T, VRows, VColumns>
78MatrixExponential(const vnl_matrix_fixed<T, VRows, VColumns> & A)
79{
80 static_assert(VRows == VColumns, "MatrixExponential requires a square matrix");
81 vnl_matrix_fixed<T, VRows, VColumns> result;
82 detail::MatrixExponentialEigen<VRows, VColumns>(A.data_block(), result.data_block());
83 return result;
84}
85
87template <typename T>
88vnl_matrix<T>
89MatrixExponential(const vnl_matrix<T> & A)
90{
91 itkAssertOrThrowMacro(A.rows() == A.cols(), "MatrixExponential requires a square matrix");
92 const unsigned int n = A.rows();
93 vnl_matrix<T> result(n, n);
94 detail::MatrixExponentialEigen<T>(A.data_block(), result.data_block(), n);
95 return result;
96}
97
98} // namespace Math
99} // namespace itk
100
101#endif // itkMatrixExponential_h
A templated class holding a M x N size Matrix.
Definition itkMatrix.h:53
InternalMatrixType & GetVnlMatrix()
Definition itkMatrix.h:214
Cholesky-based linear algebra for symmetric matrices, backed by Eigen.
void MatrixExponentialEigen(const TReal *inData, TReal *outData)
Matrix< T, VRows, VColumns > MatrixExponential(const Matrix< T, VRows, VColumns > &A)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....