WPILibC++ 2024.3.2
tgamma.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 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
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37template<typename T>
38constexpr
39T
40tgamma_check(const T x)
41noexcept
42{
43 return( // NaN check
44 is_nan(x) ? \
46 // indistinguishable from one or zero
47 GCLIM<T>::min() > abs(x - T(1)) ? \
48 T(1) :
49 GCLIM<T>::min() > abs(x) ? \
51 // negative numbers
52 x < T(0) ? \
53 // check for integer
54 GCLIM<T>::min() > abs(x - find_whole(x)) ? \
56 // else
57 tgamma_check(x+T(1)) / x :
58
59 // else
60 exp(lgamma(x)) );
61}
62
63}
64
65/**
66 * Compile-time gamma function
67 *
68 * @param x a real-valued input.
69 * @return computes the `true' gamma function
70 * \f[ \Gamma(x) = \int_0^\infty y^{x-1} \exp(-y) dy \f]
71 * using a polynomial form:
72 * \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]
73 * where the value \f$ g \f$ and the coefficients \f$ (c_0, c_1, \ldots, c_n) \f$
74 * are taken from Paul Godfrey, whose note can be found here: http://my.fit.edu/~gabdo/gamma.txt
75 */
76
77template<typename T>
78constexpr
79return_t<T>
80tgamma(const T x)
81noexcept
82{
84 return internal::tgamma_check( static_cast<return_t<T>>(x) );
85 } else {
86 return std::tgamma(x);
87 }
88}
89
90}
91
92#endif
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr T tgamma_check(const T x) noexcept
Definition: tgamma.hpp:40
constexpr llint_t find_whole(const T x) noexcept
Definition: find_whole.hpp:37
Definition: is_even.hpp:29
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition: abs.hpp:40
constexpr return_t< T > lgamma(const T x) noexcept
Compile-time log-gamma function.
Definition: lgamma.hpp:135
constexpr return_t< T > tgamma(const T x) noexcept
Compile-time gamma function.
Definition: tgamma.hpp:80
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
typename std::conditional< std::is_integral< T >::value, double, T >::type return_t
Definition: gcem_options.hpp:77