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