WPILibC++ 2024.3.2
lmgamma.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 * log multivariate gamma function
23 */
24
25#ifndef _gcem_lmgamma_HPP
26#define _gcem_lmgamma_HPP
27
28namespace gcem
29{
30
31namespace internal
32{
33
34// see https://en.wikipedia.org/wiki/Multivariate_gamma_function
35
36template<typename T1, typename T2>
37constexpr
38T1
39lmgamma_recur(const T1 a, const T2 p)
40noexcept
41{
42 return( // NaN check
43 is_nan(a) ? \
45 //
46 p == T2(1) ? \
47 lgamma(a) :
48 p < T2(1) ? \
50 // else
51 T1(GCEM_LOG_PI) * (p - T1(1))/T1(2) \
52 + lgamma(a) + lmgamma_recur(a - T1(0.5),p-T2(1)) );
53}
54
55}
56
57/**
58 * Compile-time log multivariate gamma function
59 *
60 * @param a a real-valued input.
61 * @param p integral-valued input.
62 * @return computes log-multivariate gamma function via recursion
63 * \f[ \Gamma_p(a) = \pi^{(p-1)/2} \Gamma(a) \Gamma_{p-1}(a-0.5) \f]
64 * where \f$ \Gamma_1(a) = \Gamma(a) \f$.
65 */
66
67template<typename T1, typename T2>
68constexpr
69return_t<T1>
70lmgamma(const T1 a, const T2 p)
71noexcept
72{
73 return internal::lmgamma_recur(static_cast<return_t<T1>>(a),p);
74}
75
76}
77
78#endif
#define GCEM_LOG_PI
Definition: gcem_options.hpp:102
constexpr T1 lmgamma_recur(const T1 a, const T2 p) noexcept
Definition: lmgamma.hpp:39
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
Definition: is_even.hpp:29
constexpr return_t< T1 > lmgamma(const T1 a, const T2 p) noexcept
Compile-time log multivariate gamma function.
Definition: lmgamma.hpp:70
constexpr return_t< T > lgamma(const T x) noexcept
Compile-time log-gamma function.
Definition: lgamma.hpp:135
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