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// Eigen's matrix exponential lives in the unsupported MatrixFunctions module.
23#include "itkeigen/unsupported/Eigen/MatrixFunctions"
24
25namespace itk
26{
27namespace Math
28{
29
37namespace detail
38{
39// VNL/itk::Matrix storage is row-major and contiguous, so map the input/output
40// blocks directly instead of copying element-by-element. Compile-time
41// dimensions let Eigen size its exp() temporaries on the stack.
42template <unsigned int VRows, unsigned int VColumns, typename TReal>
43void
44MatrixExponentialEigen(const TReal * inData, TReal * outData)
45{
46 using RowMajorMatrix = Eigen::Matrix<TReal, VRows, VColumns, Eigen::RowMajor>;
47 Eigen::Map<const RowMajorMatrix> inMap(inData);
48 Eigen::Map<RowMajorMatrix> outMap(outData);
49 outMap = inMap.exp();
50}
51
52template <typename TReal>
53void
54MatrixExponentialEigen(const TReal * inData, TReal * outData, unsigned int n)
55{
56 using RowMajorMatrix = Eigen::Matrix<TReal, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
57 Eigen::Map<const RowMajorMatrix> inMap(inData, n, n);
58 Eigen::Map<RowMajorMatrix> outMap(outData, n, n);
59 outMap = inMap.exp();
60}
61} // namespace detail
62
64template <typename T, unsigned int VRows, unsigned int VColumns>
67{
68 static_assert(VRows == VColumns, "MatrixExponential requires a square matrix");
70 detail::MatrixExponentialEigen<VRows, VColumns>(A.GetVnlMatrix().data_block(), result.GetVnlMatrix().data_block());
71 return result;
72}
73
75template <typename T, unsigned int VRows, unsigned int VColumns>
76vnl_matrix_fixed<T, VRows, VColumns>
77MatrixExponential(const vnl_matrix_fixed<T, VRows, VColumns> & A)
78{
79 static_assert(VRows == VColumns, "MatrixExponential requires a square matrix");
80 vnl_matrix_fixed<T, VRows, VColumns> result;
81 detail::MatrixExponentialEigen<VRows, VColumns>(A.data_block(), result.data_block());
82 return result;
83}
84
86template <typename T>
87vnl_matrix<T>
88MatrixExponential(const vnl_matrix<T> & A)
89{
90 itkAssertOrThrowMacro(A.rows() == A.cols(), "MatrixExponential requires a square matrix");
91 const unsigned int n = A.rows();
92 vnl_matrix<T> result(n, n);
93 detail::MatrixExponentialEigen<T>(A.data_block(), result.data_block(), n);
94 return result;
95}
96
97} // namespace Math
98} // namespace itk
99
100#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....