Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-25 07:55:29

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cmath>
0012 
0013 namespace Acts {
0014 
0015 /// @brief Returns the absolute of a number
0016 ///        (Can be removed for c++ 23)
0017 /// @param n The number to take absolute value of
0018 /// @return The absolute value of the input
0019 template <typename T>
0020 constexpr T abs(const T n) {
0021   if constexpr (std::is_signed_v<T>) {
0022     if (n < 0) {
0023       return -n;
0024     }
0025   }
0026   return n;
0027 }
0028 
0029 /// @brief Calculates the ordinary power of the number x.
0030 /// @param x: Number to take the power from
0031 /// @param p: Power to take
0032 /// @return x raised to the power p
0033 template <typename T, std::integral P>
0034 constexpr T pow(T x, P p) {
0035   constexpr T one = 1;
0036   if constexpr (std::is_signed_v<P>) {
0037     if (p < 0 && abs(x) > std::numeric_limits<T>::epsilon()) {
0038       x = one / x;
0039       p = -p;
0040     }
0041   }
0042   using unsigned_p = std::make_unsigned_t<P>;
0043   return p == 0 ? one : x * pow(x, static_cast<unsigned_p>(p) - 1);
0044 }
0045 
0046 /// @brief Returns the square of the passed number
0047 /// @param x The number to square
0048 /// @return The square of the input
0049 template <typename T>
0050 constexpr auto square(T x) {
0051   return x * x;
0052 }
0053 
0054 /// @brief Calculates the sum of squares of arguments
0055 /// @param args Variable number of arguments to square and sum
0056 /// @return Sum of squares of all arguments
0057 template <typename... T>
0058 constexpr auto hypotSquare(T... args) {
0059   return (square(args) + ...);
0060 }
0061 
0062 /// @brief Fast hypotenuse calculation for multiple arguments
0063 /// @param args Variable number of arguments
0064 /// @return Square root of sum of squares of arguments
0065 template <typename... T>
0066 constexpr auto fastHypot(T... args) {
0067   return std::sqrt(hypotSquare(args...));
0068 }
0069 
0070 /// @brief Calculates the sum of 1 + 2 + 3+ ... + N using the
0071 ///        Gaussian sum formula
0072 /// @param N: Number until which the sum runs
0073 /// @return Sum of integers from 1 to N
0074 template <std::integral T>
0075 constexpr T sumUpToN(const T N) {
0076   return N * (N + 1) / 2;
0077 }
0078 /// @brief Calculates the factorial of a number
0079 ///        N!= N*(N-1)....*3*2*1
0080 /// @param upperN: Upper factor until which the factorial is calculated
0081 /// @param lowerN: Optional argument to remove the first factors from the calculation
0082 /// @return Factorial result
0083 template <std::integral T>
0084 constexpr T factorial(const T upperN, const T lowerN = 1) {
0085   constexpr T one = 1;
0086   const T& limit = std::max(one, lowerN);
0087   return upperN >= limit ? upperN * factorial(upperN - 1, limit) : one;
0088 }
0089 /// @brief Calculate the binomial coefficient
0090 ///              n        n!
0091 ///                 =  --------
0092 ///              k     k!(n-k)!
0093 /// @param n Upper value in binomial coefficient
0094 /// @param k Lower value in binomial coefficient
0095 /// @return Binomial coefficient n choose k
0096 template <std::integral T>
0097 constexpr T binomial(const T n, const T k) {
0098   return factorial<T>(n, n - k + 1) / factorial<T>(k);
0099 }
0100 
0101 }  // namespace Acts