ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
fastor_matrix.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 "algebra/impl/fastor_types.hpp"
13#include "detray/definitions/detail/qualifiers.hpp"
14
15// Fastor include(s).
16#ifdef _MSC_VER
17#pragma warning(disable : 4244 4701 4702)
18#endif // MSVC
19#include <Fastor/Fastor.h>
20#ifdef _MSC_VER
21#pragma warning(default : 4244 4701 4702)
22#endif // MSVC
23
24namespace detray::algebra::fastor::math {
25
27template <concepts::matrix matrix_t>
28DETRAY_HOST_DEVICE constexpr matrix_t zero() {
29 return matrix_t(0);
30}
31
33template <concepts::matrix matrix_t>
34DETRAY_HOST_DEVICE constexpr matrix_t identity() {
35 using scalar_t = detray::traits::value_t<matrix_t>;
36 constexpr auto rows{detray::traits::rows<matrix_t>};
37 constexpr auto cols{detray::traits::columns<matrix_t>};
38
39 if constexpr (rows == cols) {
40 matrix_type<scalar_t, rows, cols> identity_matrix;
41 identity_matrix.eye2();
42 return identity_matrix;
43 } else if constexpr (rows > cols) {
44 matrix_type<scalar_t, rows, rows> identity_matrix;
45 identity_matrix.eye2();
46 return matrix_t(
47 identity_matrix(Fastor::fseq<0, rows>(), Fastor::fseq<0, cols>()));
48 } else {
49 matrix_type<scalar_t, cols, cols> identity_matrix;
50 identity_matrix.eye2();
51 return matrix_t(
52 identity_matrix(Fastor::fseq<0, rows>(), Fastor::fseq<0, cols>()));
53 }
54}
55
57template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
58DETRAY_HOST_DEVICE constexpr void set_zero(
59 matrix_type<scalar_t, ROWS, COLS> &m) {
60 m.zeros();
61}
62
64template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
65DETRAY_HOST_DEVICE constexpr void set_identity(
66 matrix_type<scalar_t, ROWS, COLS> &m) {
67 m = identity<matrix_type<scalar_t, ROWS, COLS>>();
68}
69
71template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
72DETRAY_HOST_DEVICE constexpr matrix_type<scalar_t, COLS, ROWS> transpose(
73 const matrix_type<scalar_t, ROWS, COLS> &m) {
74 return Fastor::transpose(m);
75}
76
79template <typename derived_1_t, typename derived_2_t>
80DETRAY_HOST_DEVICE constexpr auto column_wise_cross(
81 const Fastor::AbstractTensor<derived_1_t, 2> &m,
82 const Fastor::AbstractTensor<derived_2_t, 1> &v) {
83 using scalar_t = typename derived_1_t::scalar_type;
84
85 matrix_type<scalar_t, 3, 3> ret;
86 for (std::size_t j = 0u; j < 3; j++) {
87 ret(Fastor::all, j) =
88 Fastor::cross(static_cast<vector_type<scalar_t, 3>>(
89 Fastor::evaluate(m)(Fastor::all, j)),
90 Fastor::evaluate(v));
91 }
92
93 return ret;
94}
95
96template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
97DETRAY_HOST_DEVICE constexpr auto column_wise_cross(
98 const matrix_type<scalar_t, COLS, ROWS> &m,
99 const vector_type<scalar_t, ROWS> &v) {
100 matrix_type<scalar_t, ROWS, ROWS> ret;
101 for (std::size_t j = 0u; j < ROWS; j++) {
102 ret(Fastor::all, j) = Fastor::cross(
103 static_cast<vector_type<scalar_t, ROWS>>(m(Fastor::all, j)), v);
104 }
105
106 return ret;
107}
109
112template <typename derived_1_t, typename derived_2_t>
113DETRAY_HOST_DEVICE constexpr auto column_wise_multiply(
114 const Fastor::AbstractTensor<derived_1_t, 2> &m,
115 const Fastor::AbstractTensor<derived_2_t, 1> &v) {
116 using scalar_t = typename derived_1_t::scalar_type;
117 constexpr std::size_t N{derived_1_t::size()};
118
119 matrix_type<scalar_t, N, N> ret;
120 for (std::size_t j = 0u; j < N; j++) {
121 ret(Fastor::all, j) = m.self()(Fastor::all, j) * v;
122 }
123
124 return ret;
125}
126
127template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
128DETRAY_HOST_DEVICE constexpr auto column_wise_multiply(
129 const matrix_type<scalar_t, COLS, ROWS> &m,
130 const vector_type<scalar_t, ROWS> &v) {
131 matrix_type<scalar_t, ROWS, ROWS> ret;
132 for (std::size_t j = 0u; j < ROWS; j++) {
133 ret(Fastor::all, j) =
134 static_cast<vector_type<scalar_t, ROWS>>(m(Fastor::all, j)) * v;
135 }
136
137 return ret;
138}
140
142template <typename derived_1_t, typename derived_2_t>
143DETRAY_HOST_DEVICE constexpr auto outer_product(
144 const Fastor::AbstractTensor<derived_1_t, 1> &a,
145 const Fastor::AbstractTensor<derived_2_t, 1> &b) {
146 return Fastor::outer(a.self(), b.self());
147}
148
150template <std::size_t N, concepts::scalar scalar_t>
151DETRAY_HOST_DEVICE constexpr scalar_t determinant(
152 const matrix_type<scalar_t, N, N> &m) {
153 return Fastor::determinant(m);
154}
155
157template <std::size_t ROWS, std::size_t COLS, concepts::scalar scalar_t>
158DETRAY_HOST_DEVICE constexpr matrix_type<scalar_t, COLS, ROWS> inverse(
159 const matrix_type<scalar_t, ROWS, COLS> &m) {
160 return Fastor::inverse(m);
161}
162
163} // namespace detray::algebra::fastor::math
constexpr double m
Meter.
Definition Units.hpp:97