ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
fastor_vector.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/common/math.hpp"
13#include "detray/algebra/concepts.hpp"
14#include "detray/definitions/detail/qualifiers.hpp"
15
16// Fastor include(s).
17#ifdef _MSC_VER
18#pragma warning(disable : 4244 4701 4702)
19#endif // MSVC
20#include <Fastor/Fastor.h>
21#ifdef _MSC_VER
22#pragma warning(default : 4244 4701 4702)
23#endif // MSVC
24
25namespace detray::algebra::fastor::math {
26
27// Note that for all `Fastor::AbstractTensors`, the `N` template parameters
28// refers to the number of dimensions, not the number of elements.
29
33template <typename Derived, auto N>
34DETRAY_HOST_DEVICE constexpr auto phi(
35 const Fastor::AbstractTensor<Derived, N> &a) {
36 // we first force evaluation of whatever was passed in.
37 auto v = Fastor::evaluate(a);
38 // using `auto` relieves us from having to extract the exact dimension of
39 // the vector from somewhere. For all intents and purposes, we can consider
40 // the type to be `Fastor::Tensor<scalar_t, SIZE>`.
41
42 // we use the cmath version of `atan2` because Fastor's `atan2` works on
43 // `Fastor::Tensor`s element-wise, which we don't want.
44 return algebra::math::atan2(v[1], v[0]);
45}
46
51template <typename Derived, auto N>
52DETRAY_HOST constexpr auto theta(
53 const Fastor::AbstractTensor<Derived, N> &a) noexcept {
54 // we first force evaluation of whatever was passed in.
55 auto v = Fastor::evaluate(a);
56 // using `auto` relieves us from having to extract the exact dimension of
57 // the vector from somewhere. For all intents and purposes, we can consider
58 // the type to be `Fastor::Tensor<scalar_t, SIZE>`.
59
60 // we use the cmath version of `atan2` because Fastor's `atan2` works on
61 // `Fastor::Tensor`s element-wise, which we don't want.
62 return algebra::math::atan2(Fastor::norm(v(Fastor::fseq<0, 2>())), v[2]);
63}
64
69template <typename Derived, auto N>
70DETRAY_HOST constexpr auto perp(
71 const Fastor::AbstractTensor<Derived, N> &a) noexcept {
72 // we first force evaluation of whatever was passed in.
73 // using `auto` relieves us from having to extract the exact dimension of
74 // the vector from somewhere. For all intents and purposes, we can consider
75 // the type to be `Fastor::Tensor<scalar_t, SIZE>`.
76 auto v = Fastor::evaluate(a);
77
78 // we use the cmath version of `sqrt` because Fastor's `sqrt` works on
79 // `Fastor::Tensor`s element-wise, which we don't want.
81 Fastor::inner(v(Fastor::fseq<0, 2>()), v(Fastor::fseq<0, 2>())));
82}
83
88template <typename Derived, auto N>
89DETRAY_HOST constexpr auto norm(const Fastor::AbstractTensor<Derived, N> &v) {
90 return Fastor::norm(v);
91}
92
98template <typename Derived, auto N>
99DETRAY_HOST constexpr auto eta(
100 const Fastor::AbstractTensor<Derived, N> &a) noexcept {
101 auto v = Fastor::evaluate(a);
102
103 // we use the cmath version of `atanh` because Fastor's `atanh` works on
104 // `Fastor::Tensor`s element-wise, which we don't want.
105 return algebra::math::atanh(v[2] / Fastor::norm(v));
106}
107
111template <typename Derived, auto N>
112DETRAY_HOST constexpr auto normalize(
113 const Fastor::AbstractTensor<Derived, N> &v) {
114 return v / Fastor::norm(v);
115}
116
123template <typename Derived0, auto N0, typename Derived1, auto N1>
124DETRAY_HOST constexpr auto dot(const Fastor::AbstractTensor<Derived0, N0> &a,
125 const Fastor::AbstractTensor<Derived1, N1> &b) {
126 return Fastor::inner(a, b);
127}
128
135template <concepts::scalar scalar_t, auto N>
136DETRAY_HOST_DEVICE constexpr scalar_t dot(
137 const Fastor::Tensor<scalar_t, N> &a,
138 const Fastor::Tensor<scalar_t, N> &b) {
139 return Fastor::inner(a, b);
140}
141
148template <concepts::scalar scalar_t, auto N>
149DETRAY_HOST constexpr scalar_t dot(const Fastor::Tensor<scalar_t, N> &a,
150 const Fastor::Tensor<scalar_t, N, 1> &b) {
151 // We need to specify the type of the Tensor slice because Fastor by default
152 // is lazy, so it returns an intermediate type which does not play well with
153 // the Fastor::inner function.
154 return Fastor::inner(a,
155 Fastor::Tensor<scalar_t, N>(b(Fastor::fseq<0, N>(), 0)));
156}
157
164template <concepts::scalar scalar_t, auto N>
165DETRAY_HOST constexpr scalar_t dot(const Fastor::Tensor<scalar_t, N, 1> &a,
166 const Fastor::Tensor<scalar_t, N> &b) {
167 return Fastor::inner(Fastor::Tensor<scalar_t, N>(a(Fastor::fseq<0, N>(), 0)),
168 b);
169}
170
177template <concepts::scalar scalar_t, auto N>
178DETRAY_HOST constexpr scalar_t dot(const Fastor::Tensor<scalar_t, N, 1> &a,
179 const Fastor::Tensor<scalar_t, N, 1> &b) {
180 return Fastor::inner(Fastor::Tensor<scalar_t, N>(a(Fastor::fseq<0, 3>(), 0)),
181 Fastor::Tensor<scalar_t, N>(b(Fastor::fseq<0, 3>(), 0)));
182}
183
190template <typename Derived0, auto N0, typename Derived1, auto N1>
191DETRAY_HOST constexpr auto cross(
192 const Fastor::AbstractTensor<Derived0, N0> &a,
193 const Fastor::AbstractTensor<Derived1, N1> &b) {
194 return Fastor::cross(a, b);
195}
196
203template <concepts::scalar scalar_t>
204DETRAY_HOST constexpr auto cross(const Fastor::Tensor<scalar_t, 3> &a,
205 const Fastor::Tensor<scalar_t, 3> &b) {
206 return Fastor::cross(a, b);
207}
208
215template <concepts::scalar scalar_t>
216DETRAY_HOST constexpr auto cross(const Fastor::Tensor<scalar_t, 3> &a,
217 const Fastor::Tensor<scalar_t, 3, 1> &b) {
218 // We need to specify the type of the Tensor slice because Fastor by default
219 // is lazy, so it returns an intermediate type which does not play well with
220 // the Fastor::cross function.
221 return Fastor::cross(a,
222 Fastor::Tensor<scalar_t, 3>(b(Fastor::fseq<0, 3>(), 0)));
223}
224
231template <concepts::scalar scalar_t>
232DETRAY_HOST constexpr auto cross(const Fastor::Tensor<scalar_t, 3, 1> &a,
233 const Fastor::Tensor<scalar_t, 3> &b) {
234 return Fastor::cross(Fastor::Tensor<scalar_t, 3>(a(Fastor::fseq<0, 3>(), 0)),
235 b);
236}
237
244template <concepts::scalar scalar_t>
245DETRAY_HOST constexpr auto cross(const Fastor::Tensor<scalar_t, 3, 1> &a,
246 const Fastor::Tensor<scalar_t, 3, 1> &b) {
247 return Fastor::cross(Fastor::Tensor<scalar_t, 3>(a(Fastor::fseq<0, 3>(), 0)),
248 Fastor::Tensor<scalar_t, 3>(b(Fastor::fseq<0, 3>(), 0)));
249}
250
251} // namespace detray::algebra::fastor::math
constexpr decltype(auto) sqrt(T &&vec)
Definition vc_soa_math.hpp:78