WPILibC++ 2024.3.2
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_binomial_coef_HPP
22#define _gcem_binomial_coef_HPP
23
24namespace gcem
25{
26
27namespace internal
28{
29
30template<typename T>
31constexpr
32T
33binomial_coef_recur(const T n, const T k)
34noexcept
35{
36 return( // edge cases
37 (k == T(0) || n == k) ? T(1) : // deals with 0 choose 0 case
38 n == T(0) ? T(0) :
39 // else
40 binomial_coef_recur(n-1,k-1) + binomial_coef_recur(n-1,k) );
41}
42
43template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
44constexpr
45T
46binomial_coef_check(const T n, const T k)
47noexcept
48{
49 return binomial_coef_recur(n,k);
50}
51
52template<typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
53constexpr
54T
55binomial_coef_check(const T n, const T k)
56noexcept
57{
58 return( // NaN check; removed due to MSVC problems; template not being ignored in <int> cases
59 // (is_nan(n) || is_nan(k)) ? GCLIM<T>::quiet_NaN() :
60 //
61 static_cast<T>(binomial_coef_recur(static_cast<ullint_t>(n),static_cast<ullint_t>(k))) );
62}
63
64template<typename T1, typename T2, typename TC = common_t<T1,T2>>
65constexpr
66TC
67binomial_coef_type_check(const T1 n, const T2 k)
68noexcept
69{
70 return binomial_coef_check(static_cast<TC>(n),static_cast<TC>(k));
71}
72
73}
74
75/**
76 * Compile-time binomial coefficient
77 *
78 * @param n integral-valued input.
79 * @param k integral-valued input.
80 * @return computes the Binomial coefficient
81 * \f[ \binom{n}{k} = \frac{n!}{k!(n-k)!} \f]
82 * also known as '\c n choose \c k '.
83 */
84
85template<typename T1, typename T2>
86constexpr
87common_t<T1,T2>
88binomial_coef(const T1 n, const T2 k)
89noexcept
90{
92}
93
94}
95
96#endif
type
Definition: core.h:556
constexpr T binomial_coef_recur(const T n, const T k) noexcept
Definition: binomial_coef.hpp:33
constexpr T binomial_coef_check(const T n, const T k) noexcept
Definition: binomial_coef.hpp:46
constexpr TC binomial_coef_type_check(const T1 n, const T2 k) noexcept
Definition: binomial_coef.hpp:67
Definition: is_even.hpp:29
unsigned long long int ullint_t
Definition: gcem_options.hpp:69
constexpr common_t< T1, T2 > binomial_coef(const T1 n, const T2 k) noexcept
Compile-time binomial coefficient.
Definition: binomial_coef.hpp:88