WPILibC++ 2024.3.2
log_binomial_coef.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#ifndef _gcem_log_binomial_coef_HPP
22#define _gcem_log_binomial_coef_HPP
23
24namespace gcem
25{
26
27namespace internal
28{
29
30template<typename T>
31constexpr
32T
33log_binomial_coef_compute(const T n, const T k)
34noexcept
35{
36 return( lgamma(n+1) - (lgamma(k+1) + lgamma(n-k+1)) );
37}
38
39template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
40constexpr
41TC
42log_binomial_coef_type_check(const T1 n, const T2 k)
43noexcept
44{
45 return log_binomial_coef_compute(static_cast<TC>(n),static_cast<TC>(k));
46}
47
48}
49
50/**
51 * Compile-time log binomial coefficient
52 *
53 * @param n integral-valued input.
54 * @param k integral-valued input.
55 * @return computes the log Binomial coefficient
56 * \f[ \ln \frac{n!}{k!(n-k)!} = \ln \Gamma(n+1) - [ \ln \Gamma(k+1) + \ln \Gamma(n-k+1) ] \f]
57 */
58
59template<typename T1, typename T2>
60constexpr
61common_return_t<T1,T2>
62log_binomial_coef(const T1 n, const T2 k)
63noexcept
64{
66}
67
68}
69
70#endif
constexpr TC log_binomial_coef_type_check(const T1 n, const T2 k) noexcept
Definition: log_binomial_coef.hpp:42
constexpr T log_binomial_coef_compute(const T n, const T k) noexcept
Definition: log_binomial_coef.hpp:33
Definition: is_even.hpp:29
constexpr return_t< T > lgamma(const T x) noexcept
Compile-time log-gamma function.
Definition: lgamma.hpp:135
constexpr common_return_t< T1, T2 > log_binomial_coef(const T1 n, const T2 k) noexcept
Compile-time log binomial coefficient.
Definition: log_binomial_coef.hpp:62