ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
partial_pivot_lud.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "detray/algebra/concepts.hpp"
13#include "detray/algebra/generic/algorithms/matrix/decomposition/partial_pivot_lud.hpp"
14#include "detray/algebra/type_traits.hpp"
15#include "detray/definitions/detail/qualifiers.hpp"
16
17namespace detray::algebra::generic::matrix::inverse {
18
20template <concepts::square_matrix matrix_t>
23 using scalar_t = detray::traits::value_t<matrix_t>;
25 using index_t = detray::traits::index_t<matrix_t>;
26
28 using element_getter_t = detray::traits::element_getter_t<matrix_t>;
29
33 matrix_t>;
34
38 DETRAY_HOST_DEVICE constexpr matrix_t operator()(const matrix_t& m) const {
39 constexpr element_getter_t elem{};
40 constexpr index_t N{detray::traits::max_rank<matrix_t>};
41
42 const typename decomposition_t::template lud<N> decomp_res =
43 decomposition_t()(m);
44
45 // Get the LU decomposition matrix equal to (L - I) + U
46 const auto& lu = decomp_res.lu;
47
48 // Permutation vector
49 const auto& P = decomp_res.P;
50
51 // Inverse matrix
52 matrix_t inv{};
53
54 // Calculate inv(A) = inv(U) * inv(L) * P;
55 for (index_t j = 0; j < N; j++) {
56 for (index_t i = 0; i < N; i++) {
57 elem(inv, i, j) = static_cast<index_t>(P[i]) == j
58 ? static_cast<scalar_t>(1.0)
59 : static_cast<scalar_t>(0.0);
60
61 for (index_t k = 0; k < i; k++) {
62 elem(inv, i, j) -= elem(lu, i, k) * elem(inv, k, j);
63 }
64 }
65
66 for (index_t i = N - 1; int(i) >= 0; i--) {
67 for (index_t k = i + 1; k < N; k++) {
68 elem(inv, i, j) -= elem(lu, i, k) * elem(inv, k, j);
69 }
70 elem(inv, i, j) /= elem(lu, i, i);
71 }
72 }
73
74 return inv;
75 }
76};
77
78} // namespace detray::algebra::generic::matrix::inverse
"Partial Pivot LU Decomposition", assuming a N X N matrix
Definition partial_pivot_lud.hpp:23
Invert a square matrix using LU decomposition with partial pivoting.
Definition partial_pivot_lud.hpp:21
detray::traits::index_t< matrix_t > index_t
Matrix index type.
Definition partial_pivot_lud.hpp:25
typename algebra::generic::matrix::decomposition::partial_pivot_lud< matrix_t > decomposition_t
LU decomposition implementation used to factor the matrix.
Definition partial_pivot_lud.hpp:31
constexpr matrix_t operator()(const matrix_t &m) const
Compute the inverse from the pivoted LU factors.
Definition partial_pivot_lud.hpp:38
detray::traits::value_t< matrix_t > scalar_t
Matrix element type.
Definition partial_pivot_lud.hpp:23
detray::traits::element_getter_t< matrix_t > element_getter_t
Function (object) used for accessing a matrix element.
Definition partial_pivot_lud.hpp:28