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/type_traits.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15
16// System include(s)
17#include <type_traits>
18
19namespace detray::algebra::generic::matrix::determinant {
20
22template <concepts::square_matrix matrix_t>
23struct cofactor {
25 using scalar_type = detray::traits::value_t<matrix_t>;
27 using index_type = detray::traits::index_t<matrix_t>;
28
32 DETRAY_HOST_DEVICE constexpr scalar_type operator()(const matrix_t &m) const {
34 }
35
37 template <index_type N>
39
41 template <index_type N>
42 requires(N == 1)
47 template <class input_matrix_type>
48 DETRAY_HOST_DEVICE constexpr scalar_type operator()(
49 const input_matrix_type &m) const {
50 using element_getter_t =
51 detray::traits::element_getter_t<input_matrix_type>;
52
53 constexpr element_getter_t elem{};
54
55 return elem(m, 0, 0);
56 }
57 };
58
60 template <index_type N>
61 requires(N != 1)
62 struct determinant_getter_helper<N> {
66 template <class input_matrix_type>
67 DETRAY_HOST_DEVICE constexpr scalar_type operator()(
68 const input_matrix_type &m) const {
69 using scalar_t = detray::traits::value_t<input_matrix_type>;
70 using index_t = detray::traits::index_t<input_matrix_type>;
71 using element_getter_t =
72 detray::traits::element_getter_t<input_matrix_type>;
73
74 constexpr element_getter_t elem{};
75
76 scalar_t D = 0;
77
78 // To store cofactors
79 input_matrix_type temp;
80
81 // To store sign multiplier
82 int sign = 1;
83
84 // Iterate for each element of first row
85 for (index_t col = 0; col < N; col++) {
86 // Getting Cofactor of A[0][f]
87 this->get_cofactor(m, temp, index_t(0), col);
88 D += sign * elem(m, 0, col) * determinant_getter_helper<N - 1>()(temp);
89
90 // terms are to be added with alternate sign
91 sign = -sign;
92 }
93
94 return D;
95 }
96
102 template <class input_matrix_type>
103 DETRAY_HOST_DEVICE constexpr void get_cofactor(const input_matrix_type &m,
104 input_matrix_type &temp,
105 index_type p,
106 index_type q) const {
107 using index_t = detray::traits::index_t<input_matrix_type>;
108 using element_getter_t =
109 detray::traits::element_getter_t<input_matrix_type>;
110
111 constexpr element_getter_t elem{};
112
113 index_t i = 0;
114 index_t j = 0;
115
116 // Looping for each element of the matrix
117 for (index_t row = 0; row < N; row++) {
118 for (index_t col = 0; col < N; col++) {
119 // Copying into temporary matrix only those element
120 // which are not in given row and column
121 if (row != p && col != q) {
122 elem(temp, i, j++) = elem(m, row, col);
123
124 // Row is filled, so increase row index and
125 // reset col index
126 if (j == N - 1) {
127 j = 0;
128 i++;
129 }
130 }
131 }
132 }
133 }
134 };
135};
136
137} // namespace detray::algebra::generic::matrix::determinant
constexpr scalar_type operator()(const input_matrix_type &m) const
Return the sole element of the one-element matrix.
Definition cofactor.hpp:48
constexpr void get_cofactor(const input_matrix_type &m, input_matrix_type &temp, index_type p, index_type q) const
Fill the temporary matrix with the minor excluding one row and column.
Definition cofactor.hpp:103
Helper that evaluates the determinant for a fixed matrix rank.
Definition cofactor.hpp:38
"Determinant getter", assuming a N X N matrix
Definition cofactor.hpp:23
constexpr scalar_type operator()(const matrix_t &m) const
Compute the determinant of a square matrix.
Definition cofactor.hpp:32
detray::traits::index_t< matrix_t > index_type
Index type used to traverse the matrix.
Definition cofactor.hpp:27
detray::traits::value_t< matrix_t > scalar_type
Scalar type of the determinant.
Definition cofactor.hpp:25