ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
smatrix_getter.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/definitions/detail/qualifiers.hpp"
14
15// ROOT/Smatrix include(s).
16#include <Math/Expression.h>
17#include <Math/Functions.h>
18#include <Math/SMatrix.h>
19#include <Math/SVector.h>
20#include <TMath.h>
21
22// System include(s).
23#include <cassert>
24
25namespace detray::algebra::smatrix::storage {
26
34 template <unsigned int ROWS, unsigned int COLS, concepts::scalar scalar_t>
35 DETRAY_HOST_DEVICE constexpr scalar_t &operator()(
36 ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, unsigned int row,
37 unsigned int col) const {
38 assert(row < ROWS);
39 assert(col < COLS);
40 return m(row, col);
41 }
42
48 template <unsigned int ROWS, unsigned int COLS, concepts::scalar scalar_t>
49 DETRAY_HOST_DEVICE constexpr scalar_t operator()(
50 const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, unsigned int row,
51 unsigned int col) const {
52 assert(row < ROWS);
53 assert(col < COLS);
54 return m(row, col);
55 }
56
61 template <unsigned int N, concepts::scalar scalar_t>
62 DETRAY_HOST_DEVICE constexpr scalar_t &operator()(
63 ROOT::Math::SMatrix<scalar_t, N, 1> &m, unsigned int row) const {
64 assert(row < N);
65 return m(row);
66 }
67
72 template <unsigned int N, concepts::scalar scalar_t>
73 DETRAY_HOST_DEVICE constexpr scalar_t operator()(
74 const ROOT::Math::SMatrix<scalar_t, N, 1> &m, unsigned int row) const {
75 assert(row < N);
76 return m(row, 0);
77 }
78
83 template <concepts::scalar scalar_t, unsigned int N>
84 DETRAY_HOST_DEVICE constexpr scalar_t &operator()(
85 ROOT::Math::SVector<scalar_t, N> &m, unsigned int row) const {
86 assert(row < N);
87 return m(row);
88 }
89
94 template <unsigned int N, concepts::scalar scalar_t>
95 DETRAY_HOST_DEVICE constexpr scalar_t operator()(
96 const ROOT::Math::SVector<scalar_t, N> &m, unsigned int row) const {
97 assert(row < N);
98 return m(row);
99 }
100}; // element_getter
101
103template <concepts::scalar scalar_t, unsigned int ROWS, unsigned int COLS>
104DETRAY_HOST_DEVICE constexpr scalar_t element(
105 const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, std::size_t row,
106 std::size_t col) {
107 return element_getter()(m, static_cast<unsigned int>(row),
108 static_cast<unsigned int>(col));
109}
110
112template <concepts::scalar scalar_t, unsigned int ROWS, unsigned int COLS>
113DETRAY_HOST_DEVICE constexpr scalar_t &element(
114 ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, std::size_t row,
115 std::size_t col) {
116 return element_getter()(m, static_cast<unsigned int>(row),
117 static_cast<unsigned int>(col));
118}
119
121template <concepts::scalar scalar_t, unsigned int N>
122DETRAY_HOST_DEVICE constexpr scalar_t element(
123 const ROOT::Math::SMatrix<scalar_t, N, 1> &m, std::size_t row) {
124 return element_getter()(m, static_cast<unsigned int>(row));
125}
126
128template <concepts::scalar scalar_t, unsigned int N>
129DETRAY_HOST_DEVICE constexpr scalar_t &element(
130 ROOT::Math::SMatrix<scalar_t, N, 1> &m, std::size_t row) {
131 return element_getter()(m, static_cast<unsigned int>(row));
132}
133
135template <concepts::scalar scalar_t, unsigned int N>
136DETRAY_HOST_DEVICE constexpr scalar_t element(
137 const ROOT::Math::SVector<scalar_t, N> &m, std::size_t row) {
138 return element_getter()(m, static_cast<unsigned int>(row));
139}
140
142template <concepts::scalar scalar_t, unsigned int N>
143DETRAY_HOST_DEVICE constexpr scalar_t &element(
144 ROOT::Math::SVector<scalar_t, N> &m, std::size_t row) {
145 return element_getter()(m, static_cast<unsigned int>(row));
146}
147
148template <std::size_t I, std::size_t J, concepts::matrix M>
149DETRAY_HOST_DEVICE decltype(auto) element(M &matrix) {
150 if constexpr (concepts::has_compile_time_2d_access<M>) {
151 return matrix.template element<I, J>();
152 } else {
153 using index_t = detray::traits::index_t<std::decay_t<M>>;
154 return element(matrix, static_cast<index_t>(I), static_cast<index_t>(J));
155 }
156}
157
158template <std::size_t I, concepts::vector V>
159DETRAY_HOST_DEVICE decltype(auto) element(V &vector) {
160 if constexpr (concepts::has_compile_time_1d_access<V>) {
161 return vector.template element<I>();
162 } else {
163 using index_t = detray::traits::index_t<std::decay_t<V>>;
164 return element(vector, static_cast<index_t>(I));
165 }
166}
167
175 template <unsigned int ROWS, unsigned int COLS, unsigned int oROWS,
176 unsigned int oCOLS, concepts::scalar scalar_t>
177 DETRAY_HOST_DEVICE ROOT::Math::SMatrix<scalar_t, ROWS, COLS> operator()(
178 const ROOT::Math::SMatrix<scalar_t, oROWS, oCOLS> &m, unsigned int row,
179 unsigned int col) const {
180 return m.template Sub<ROOT::Math::SMatrix<scalar_t, ROWS, COLS>>(row, col);
181 }
182
188 template <unsigned int SIZE, unsigned int ROWS, unsigned int COLS,
189 concepts::scalar scalar_t>
190 DETRAY_HOST_DEVICE ROOT::Math::SVector<scalar_t, SIZE> vector(
191 const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, unsigned int row,
192 unsigned int col) const {
193 // TODO: SMatrix bug?
194 // return m.template SubCol<ROOT::Math::SVector<scalar_t, SIZE>>(col,
195 // row);
196
197 assert(col < COLS);
198 assert(row + SIZE <= ROWS);
199
200 ROOT::Math::SVector<scalar_t, SIZE> ret;
201
202 for (unsigned int irow = row; irow < row + SIZE; ++irow) {
203 ret[irow - row] = m(irow, col);
204 }
205
206 return ret;
207 }
208
214 template <unsigned int ROWS, unsigned int COLS, concepts::scalar scalar_t,
215 concepts::matrix input_matrix_type>
216 DETRAY_HOST_DEVICE void set(
217 input_matrix_type &m, const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &b,
218 std::size_t row, std::size_t col) const {
219 for (unsigned int i = 0; i < ROWS; ++i) {
220 for (unsigned int j = 0; j < COLS; ++j) {
221 m(i + static_cast<unsigned int>(row),
222 j + static_cast<unsigned int>(col)) = b(i, j);
223 }
224 }
225 }
226
232 template <unsigned int ROWS, concepts::scalar scalar_t,
233 concepts::matrix input_matrix_type>
234 DETRAY_HOST_DEVICE void set(input_matrix_type &m,
235 const ROOT::Math::SVector<scalar_t, ROWS> &b,
236 unsigned int row, unsigned int col) const {
237 for (unsigned int i = 0; i < ROWS; ++i) {
238 m(i + row, col) = b[i];
239 }
240 }
241
242}; // struct block_getter
243
245template <unsigned int ROWS, unsigned int COLS, unsigned int oROWS,
246 unsigned int oCOLS, concepts::scalar scalar_t>
247DETRAY_HOST_DEVICE ROOT::Math::SMatrix<scalar_t, ROWS, COLS> block(
248 const ROOT::Math::SMatrix<scalar_t, oROWS, oCOLS> &m, std::size_t row,
249 std::size_t col) {
250 return block_getter{}.template operator()<ROWS, COLS>(
251 m, static_cast<unsigned int>(row), static_cast<unsigned int>(col));
252}
253
256template <unsigned int SIZE, unsigned int ROWS, unsigned int COLS,
257 concepts::scalar scalar_t>
258DETRAY_HOST_DEVICE constexpr auto vector(
259 const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &m, std::size_t row,
260 std::size_t col) {
261 return block_getter{}.template vector<SIZE>(m, static_cast<unsigned int>(row),
262 static_cast<unsigned int>(col));
263}
264
266template <unsigned int ROWS, unsigned int COLS, concepts::scalar scalar_t,
267 concepts::matrix input_matrix_type>
268DETRAY_HOST_DEVICE void set_block(
269 input_matrix_type &m, const ROOT::Math::SMatrix<scalar_t, ROWS, COLS> &b,
270 std::size_t row, std::size_t col) {
271 block_getter{}.set(m, b, row, col);
272}
273
275template <unsigned int ROWS, concepts::scalar scalar_t,
276 concepts::matrix input_matrix_type>
277DETRAY_HOST_DEVICE void set_block(input_matrix_type &m,
278 const ROOT::Math::SVector<scalar_t, ROWS> &b,
279 unsigned int row, unsigned int col) {
280 block_getter{}.set(m, b, row, col);
281}
282
283} // namespace detray::algebra::smatrix::storage
Matrix concepts.
Definition concepts.hpp:83
Scalar concept: Elements of vectors/matrices (can be simd vectors).
Definition concepts.hpp:40
constexpr double m
Meter.
Definition Units.hpp:97
Functor used to extract a block from SMatrix matrices.
Definition smatrix_getter.hpp:169
void set(input_matrix_type &m, const ROOT::Math::SVector< scalar_t, ROWS > &b, unsigned int row, unsigned int col) const
Operator setting a block with a vector.
Definition smatrix_getter.hpp:234
ROOT::Math::SVector< scalar_t, SIZE > vector(const ROOT::Math::SMatrix< scalar_t, ROWS, COLS > &m, unsigned int row, unsigned int col) const
Extract a contiguous segment from one matrix column.
Definition smatrix_getter.hpp:190
ROOT::Math::SMatrix< scalar_t, ROWS, COLS > operator()(const ROOT::Math::SMatrix< scalar_t, oROWS, oCOLS > &m, unsigned int row, unsigned int col) const
Extract a rectangular block from a matrix.
Definition smatrix_getter.hpp:177
void set(input_matrix_type &m, const ROOT::Math::SMatrix< scalar_t, ROWS, COLS > &b, std::size_t row, std::size_t col) const
Operator setting a block with a matrix.
Definition smatrix_getter.hpp:216
Functor used to access elements of SMatrix matrices.
Definition smatrix_getter.hpp:28
constexpr scalar_t & operator()(ROOT::Math::SMatrix< scalar_t, N, 1 > &m, unsigned int row) const
Access a mutable element of a single-column matrix.
Definition smatrix_getter.hpp:62
constexpr scalar_t operator()(const ROOT::Math::SVector< scalar_t, N > &m, unsigned int row) const
Read a vector element.
Definition smatrix_getter.hpp:95
constexpr scalar_t & operator()(ROOT::Math::SMatrix< scalar_t, ROWS, COLS > &m, unsigned int row, unsigned int col) const
Access a mutable matrix element.
Definition smatrix_getter.hpp:35
constexpr scalar_t operator()(const ROOT::Math::SMatrix< scalar_t, ROWS, COLS > &m, unsigned int row, unsigned int col) const
Read a matrix element.
Definition smatrix_getter.hpp:49
constexpr scalar_t operator()(const ROOT::Math::SMatrix< scalar_t, N, 1 > &m, unsigned int row) const
Read an element of a single-column matrix.
Definition smatrix_getter.hpp:73
constexpr scalar_t & operator()(ROOT::Math::SVector< scalar_t, N > &m, unsigned int row) const
Access a mutable vector element.
Definition smatrix_getter.hpp:84