Line data Source code
1 : #ifndef A000E27D_C91E_4768_A8FD_B292AB7B986A
2 : #define A000E27D_C91E_4768_A8FD_B292AB7B986A
3 :
4 : #include "conversions.h"
5 : #include "math/linalg/diagonal_matrix.hpp" // IWYU pragma: keep
6 : #include "math/linalg/square_matrix.hpp" // IWYU pragma: keep
7 :
8 : namespace tracking
9 : {
10 : namespace math
11 : {
12 : namespace conversions
13 : {
14 :
15 : /// \brief Creates a DiagonalMatrix from the diagonal elements of a SquareMatrix
16 : ///
17 : /// This function extracts the diagonal elements from a square matrix to create a diagonal matrix.
18 : /// All off-diagonal elements are discarded, preserving only the main diagonal values.
19 : ///
20 : /// \tparam ValueType_ The atomic data type of internal elements
21 : /// \tparam Size_ The size of the square matrix and resulting diagonal matrix
22 : /// \tparam IsRowMajor_ The storage layout of the source matrix
23 : /// \param[in] mat The source square matrix
24 : /// \return DiagonalMatrix containing the diagonal elements of the input matrix
25 : /// \see DiagonalFromList() for creating diagonal matrixes from initializer lists
26 : /// \see SquareFromDiagonal() for the reverse conversion
27 : template <typename ValueType_, sint32 Size_, bool IsRowMajor_>
28 2 : inline auto DiagonalFromSquare(const SquareMatrix<ValueType_, Size_, IsRowMajor_>& mat) -> DiagonalMatrix<ValueType_, Size_>
29 : {
30 2 : DiagonalMatrix<ValueType_, Size_> result;
31 8 : for (sint32 i = 0; i < Size_; ++i)
32 : {
33 6 : result.at_unsafe(i) = mat.at_unsafe(i, i);
34 : }
35 2 : return result;
36 : }
37 :
38 : } // namespace conversions
39 : } // namespace math
40 : } // namespace tracking
41 :
42 : #endif // A000E27D_C91E_4768_A8FD_B292AB7B986A
|