File indexing completed on 2025-09-17 08:02:04
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cmath>
0012
0013 namespace Acts {
0014
0015
0016
0017 template <typename T>
0018 constexpr T abs(const T n) {
0019 if constexpr (std::is_signed_v<T>) {
0020 if (n < 0) {
0021 return -n;
0022 }
0023 }
0024 return n;
0025 }
0026
0027
0028
0029
0030 template <typename T, std::integral P>
0031 constexpr T pow(T x, P p) {
0032 constexpr T one = 1;
0033 if constexpr (std::is_signed_v<P>) {
0034 if (p < 0 && abs(x) > std::numeric_limits<T>::epsilon()) {
0035 x = one / x;
0036 p = -p;
0037 }
0038 }
0039 using unsigned_p = std::make_unsigned_t<P>;
0040 return p == 0 ? one : x * pow(x, static_cast<unsigned_p>(p) - 1);
0041 }
0042
0043
0044 template <typename T>
0045 constexpr auto square(T x) {
0046 return x * x;
0047 }
0048
0049 template <typename... T>
0050 constexpr auto hypotSquare(T... args) {
0051 return (square(args) + ...);
0052 }
0053
0054 template <typename... T>
0055 constexpr auto fastHypot(T... args) {
0056 return std::sqrt(hypotSquare(args...));
0057 }
0058
0059
0060
0061
0062 template <std::integral T>
0063 constexpr T sumUpToN(const T N) {
0064 return N * (N + 1) / 2;
0065 }
0066
0067
0068
0069
0070 template <std::integral T>
0071 constexpr T factorial(const T upperN, const T lowerN = 1) {
0072 constexpr T one = 1;
0073 const T& limit = std::max(one, lowerN);
0074 return upperN >= limit ? upperN * factorial(upperN - 1, limit) : one;
0075 }
0076
0077
0078
0079
0080 template <std::integral T>
0081 constexpr T binomial(const T n, const T k) {
0082 return factorial<T>(n, n - k + 1) / factorial<T>(k);
0083 }
0084
0085 }