WPILibC++ 2023.4.3-108-ge5452e3
tgamma.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * the ('true') gamma function
23 */
24
25#ifndef _gcem_tgamma_HPP
26#define _gcem_tgamma_HPP
27
28namespace internal
29{
30
31template<typename T>
32constexpr
33T
34tgamma_check(const T x)
35noexcept
36{
37 return( // NaN check
38 is_nan(x) ? \
39 GCLIM<T>::quiet_NaN() :
40 // indistinguishable from one or zero
41 GCLIM<T>::min() > abs(x - T(1)) ? \
42 T(1) :
43 GCLIM<T>::min() > abs(x) ? \
44 GCLIM<T>::infinity() :
45 // negative numbers
46 x < T(0) ? \
47 // check for integer
48 GCLIM<T>::min() > abs(x - find_whole(x)) ? \
49 GCLIM<T>::quiet_NaN() :
50 // else
51 tgamma_check(x+T(1)) / x :
52
53 // else
54 exp(lgamma(x)) );
55}
56
57}
58
59/**
60 * Compile-time gamma function
61 *
62 * @param x a real-valued input.
63 * @return computes the `true' gamma function
64 * \f[ \Gamma(x) = \int_0^\infty y^{x-1} \exp(-y) dy \f]
65 * using a polynomial form:
66 * \f[ \Gamma(x+1) \approx (x+g+0.5)^{x+0.5} \exp(-x-g-0.5) \sqrt{2 \pi} \left[ c_0 + \frac{c_1}{x+1} + \frac{c_2}{x+2} + \cdots + \frac{c_n}{x+n} \right] \f]
67 * where the value \f$ g \f$ and the coefficients \f$ (c_0, c_1, \ldots, c_n) \f$
68 * are taken from Paul Godfrey, whose note can be found here: http://my.fit.edu/~gabdo/gamma.txt
69 */
70
71template<typename T>
72constexpr
73return_t<T>
74tgamma(const T x)
75noexcept
76{
77 return internal::tgamma_check( static_cast<return_t<T>>(x) );
78}
79
80#endif
EIGEN_DEVICE_FUNC const LgammaReturnType lgamma() const
\cpp11
Definition: ArrayCwiseUnaryOps.h:620
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
Definition: Eigen_Colamd.h:50
constexpr T tgamma_check(const T x) noexcept
Definition: tgamma.hpp:34
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr llint_t find_whole(const T x) noexcept
Definition: find_whole.hpp:34
constexpr return_t< T > tgamma(const T x) noexcept
Compile-time gamma function.
Definition: tgamma.hpp:74