ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
cofactor.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/determinant/cofactor.hpp"
14#include "detray/algebra/type_traits.hpp"
15#include "detray/definitions/detail/qualifiers.hpp"
16
17// System include(s)
18#include <cassert>
19#include <type_traits>
20
21namespace detray::algebra::generic::matrix {
22
23namespace adjoint {
24
26template <concepts::square_matrix matrix_t>
27struct cofactor {
29 using index_type = detray::traits::index_t<matrix_t>;
30
34 DETRAY_HOST_DEVICE constexpr matrix_t operator()(const matrix_t &m) const {
36 }
37
39 template <index_type N>
41
43 template <index_type N>
44 requires(N == 1)
48 DETRAY_HOST_DEVICE constexpr matrix_t operator()(
49 const matrix_t & /*m*/) const {
50 using element_getter_t = detray::traits::element_getter_t<matrix_t>;
51 constexpr element_getter_t elem{};
52
53 matrix_t ret;
54 elem(ret, 0, 0) = 1;
55
56 return ret;
57 }
58 };
59
61 template <index_type N>
62 requires(N != 1)
63 struct adjoint_getter_helper<N> {
66
70 DETRAY_HOST_DEVICE constexpr matrix_t operator()(const matrix_t &m) const {
71 using index_t = detray::traits::index_t<matrix_t>;
72 using element_getter_t = detray::traits::element_getter_t<matrix_t>;
73 constexpr element_getter_t elem{};
74
75 // temp is used to store cofactors of m
76 int sign = 1;
77
78 matrix_t temp; //< To store cofactors
79 matrix_t adj;
80
81 for (index_t i = 0; i < N; i++) {
82 for (index_t j = 0; j < N; j++) {
83 // Get cofactor of m[i][j]
84 typename determinant_getter::template determinant_getter_helper<N>()
85 .get_cofactor(m, temp, i, j);
86
87 // sign of adj[j][i] positive if sum of row
88 // and column indexes is even.
89 sign = ((i + j) % 2 == 0) ? 1 : -1;
90
91 // Interchanging rows and columns to get the
92 // transpose of the cofactor matrix
93 elem(adj, j, i) =
94 sign *
95 typename determinant_getter::template determinant_getter_helper<
96 N - 1>()(temp);
97 }
98 }
99
100 return adj;
101 }
102 };
103};
104
105} // namespace adjoint
106
107namespace inverse {
108
110template <concepts::square_matrix matrix_t>
111struct cofactor {
113 using scalar_t = detray::traits::value_t<matrix_t>;
115 using index_t = detray::traits::index_t<matrix_t>;
116
118 using element_getter_t = detray::traits::element_getter_t<matrix_t>;
123
127 DETRAY_HOST_DEVICE constexpr matrix_t operator()(const matrix_t &m) const {
128 constexpr element_getter_t elem{};
129 constexpr index_t N{detray::traits::max_rank<matrix_t>};
130
131 matrix_t ret;
132
133 // Find determinant of A
134 scalar_t det = determinant_getter()(m);
135
136 // TODO: handle singular matrix error
137 if (det == 0) {
138 assert(false && "Singular matrix during inversion (cofactor)");
139 return ret;
140 }
141
142 auto adj = adjoint_getter()(m);
143
144 // Find Inverse using formula "inverse(A) = adj(A)/det(A)"
145 for (index_t i = 0; i < N; i++) {
146 for (index_t j = 0; j < N; j++) {
147 elem(ret, j, i) = elem(adj, j, i) / det;
148 }
149 }
150
151 return ret;
152 }
153};
154
155} // namespace inverse
156
157} // namespace detray::algebra::generic::matrix
determinant::cofactor< matrix_t > determinant_getter
Determinant implementation used to evaluate each minor.
Definition cofactor.hpp:65
constexpr matrix_t operator()(const matrix_t &) const
Return the one-element identity matrix, the adjugate in rank one.
Definition cofactor.hpp:48
constexpr matrix_t operator()(const matrix_t &m) const
Form the adjugate by computing signed minors and transposing them.
Definition cofactor.hpp:70
Helper that computes the adjugate for a fixed matrix rank.
Definition cofactor.hpp:40
"Adjoint getter", assuming a N X N matrix
Definition cofactor.hpp:27
constexpr matrix_t operator()(const matrix_t &m) const
Return the adjugate of a square matrix.
Definition cofactor.hpp:34
detray::traits::index_t< matrix_t > index_type
Index type used to traverse matrix rows and columns.
Definition cofactor.hpp:29
"Determinant getter", assuming a N X N matrix
Definition cofactor.hpp:23
"inverse getter", assuming a N X N matrix
Definition cofactor.hpp:111
detray::traits::index_t< matrix_t > index_t
Index type used to traverse matrix rows and columns.
Definition cofactor.hpp:115
constexpr matrix_t operator()(const matrix_t &m) const
Invert a nonsingular square matrix.
Definition cofactor.hpp:127
detray::traits::value_t< matrix_t > scalar_t
Scalar type of matrix elements.
Definition cofactor.hpp:113
detray::traits::element_getter_t< matrix_t > element_getter_t
Function (object) used for accessing a matrix element.
Definition cofactor.hpp:118
adjoint::cofactor< matrix_t > adjoint_getter
Adjugate implementation used in the inverse formula.
Definition cofactor.hpp:122
determinant::cofactor< matrix_t > determinant_getter
Cofactor-expansion determinant implementation.
Definition cofactor.hpp:120