MathLib
Loading...
Searching...
No Matches
Combinatorics.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5namespace Math
6{
7
8namespace Combinatorics
9{
10
11inline float factorial_f(float input)
12{
13 return std::tgammaf(input);
14}
15
16inline double factorial_d(double input)
17{
18 return std::tgamma(input);
19}
20
21inline long double factorial_ld(long double input)
22{
23 return std::tgammal(input);
24}
25
26constexpr inline int factorial_i(int input)
27{
28 assert( input >= 0 );
29
30 if ((input == 0) || (input == 1))
31 return 1;
32 return input * factorial_i( input - 1 );
33}
34
35constexpr inline long factorial_l(long input)
36{
37 assert( input >= 0 );
38
39 if ((input == 0) || (input == 1))
40 return 1;
41 return input * factorial_l( input - 1 );
42}
43
44constexpr inline long long factorial_ll(long long input)
45{
46 assert( input >= 0 );
47
48 if ((input == 0) || (input == 1))
49 return 1;
50 return input * factorial_ll( input - 1 );
51}
52
53template <class Type>
54constexpr inline Type factorial(Type input)
55{
56 if constexpr (std::is_integral<Type>::value)
57 {
58 if constexpr (std::is_same<Type, long long>::value)
59 return factorial_ll(input);
60 else if constexpr (std::is_same<Type, long>::value)
61 return factorial_l(input);
62 else if constexpr (std::is_same<Type, int>::value)
63 return factorial_i(input);
64 else
65 return factorial_i(input);
66 }
67 else if constexpr (std::is_floating_point<Type>::value)
68 {
69 if constexpr (std::is_same<Type, long double>::value)
70 return factorial_ld(input);
71 if constexpr (std::is_same<Type, double>::value)
72 return factorial_d(input);
73 if constexpr (std::is_same<Type, float>::value)
74 return factorial_f(input);
75 else
76 return factorial_f(input);
77 }
78 else
79 static_assert(false);
80}
81
82template <class Type>
83constexpr inline Type n_choose_k(Type n, Type k)
84{
85 assert(k >= 0);
86 assert(k <= n);
87
88 return factorial(n) / (factorial(k) * factorial(n - k));
89}
90
91constexpr inline int binomial_coefficient(int row, int column)
92{
93 if (column == 0) // Top of triangle AND left side
94 return 1;
95 else if (row == column) // Right side of triangle
96 return 1;
97 else
98 return n_choose_k(row, column);
99}
100
101constexpr inline std::vector<int> pascal_triangle_row(int row)
102{
103 assert(row >= 0);
104
105 std::vector<int> values;
106
107 values.reserve( static_cast<size_t>(row + 1 ) );
108 for (int iCurrentColumn = 0; iCurrentColumn <= row; ++iCurrentColumn)
109 values.push_back( binomial_coefficient(row, iCurrentColumn) );
110
111 return values;
112}
113
114} // Combinatorics
115} // Math
float factorial_f(float input)
double factorial_d(double input)
long double factorial_ld(long double input)
constexpr long factorial_l(long input)
constexpr int factorial_i(int input)
constexpr long long factorial_ll(long long input)
constexpr std::vector< int > pascal_triangle_row(int row)
constexpr int binomial_coefficient(int row, int column)
constexpr Type n_choose_k(Type n, Type k)
constexpr Type factorial(Type input)
Definition Angle.hpp:16