openGPMP
Open Source Mathematics Package
|
A class providing various combinatorial functions and algorithms. More...
#include <comb.hpp>
Public Member Functions | |
int64_t | permutation (int n, int r) |
Calculate the number of permutations (nPr) More... | |
int64_t | combination (int n, int r) |
Calculate the number of combinations (nCr) More... | |
int64_t | binom_coeff (int n, int r) |
Calculate the binomial coefficient (n choose r) More... | |
int64_t | factorial (int n) |
Calculate the factorial of a number. More... | |
std::vector< std::vector< int > > | partitions (int n) |
Generate all partitions of a positive integer n. More... | |
std::vector< int64_t > | bell_num (int n) |
Calculate the Bell numbers. More... | |
int64_t | stirling_num (int n, int k) |
Calculate the Stirling number of the second kind. More... | |
std::vector< int > | gray_code (int n) |
Generate the Gray code sequence of length n. More... | |
int64_t | subfactorial (int n) |
Calculate the subfactorial (!n) or derangement of n. More... | |
int64_t | subsequences (int n, int k) |
Calculate the number of subsequences of length k from n items. More... | |
std::vector< std::vector< int > > | partition_distinct (int n) |
Generate all distinct partitions of a positive integer n. More... | |
std::vector< std::vector< int > > | compositions (int n) |
Generate all compositions of a positive integer n. More... | |
int64_t | dyck_words (int n) |
Calculate the number of Dyck words of length 2n. More... | |
std::vector< std::string > | gen_dyck_words_until (int n) |
Generate Dyck words up to length n. More... | |
int64_t | necklaces (int n, int k) |
Calculate the number of necklaces of length n with k colors. More... | |
int | phi (int n) |
Calculate Euler's totient function (φ) More... | |
std::vector< std::vector< int > > | generateNecklaces (int n, int k) |
Generate all necklaces of length n with k colors. More... | |
int64_t | lyndonWords (int n) |
Calculate the number of Lyndon words of length n. More... | |
std::vector< std::string > | generateLyndonWords (int n) |
Generate all Lyndon words of length n. More... | |
void | generatePartitions (int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result) |
Generate partitions of a positive integer. More... | |
void | gen_partition_distinct (int n, int max, std::vector< int > &partition, std::vector< std::vector< int >> &result) |
Generate distinct partitions of a positive integer. More... | |
void | gen_compositions (int n, int max, std::vector< int > &composition, std::vector< std::vector< int >> &result) |
Generate compositions of a positive integer. More... | |
void | gen_dyck_words (std::string prefix, int open, int close, std::vector< std::string > &dyck_words) |
Generate Dyck words. More... | |
void | gen_necklaces (std::vector< int > &necklace, int index, int n, int k, std::vector< std::vector< int >> &necklaces) |
Generate necklaces. More... | |
void | gen_lyndon_words (std::string prefix, int n, int max, std::vector< std::string > &lyndonWords) |
Generate Lyndon words. More... | |
std::vector< int > | divisors (int n) |
Calculate the divisors of a positive integer. More... | |
template<typename T > | |
std::vector< std::vector< T > > | permutations (const std::vector< T > &vec) |
Generate all permutations of a vector. More... | |
template<typename T > | |
std::vector< std::vector< T > > | combinations (const std::vector< T > &vec, int r) |
Generate all combinations of a vector. More... | |
A class providing various combinatorial functions and algorithms.
std::vector< int64_t > gpmp::Comb::bell_num | ( | int | n | ) |
Calculate the Bell numbers.
n | The number of Bell numbers to calculate |
Definition at line 99 of file comb.cpp.
Referenced by main().
int64_t gpmp::Comb::binom_coeff | ( | int | n, |
int | r | ||
) |
int64_t gpmp::Comb::combination | ( | int | n, |
int | r | ||
) |
std::vector<std::vector<T> > gpmp::Comb::combinations | ( | const std::vector< T > & | vec, |
int | r | ||
) |
Generate all combinations of a vector.
T | The type of elements in the vector |
vec | The vector |
r | The size of combinations |
Referenced by main().
std::vector< std::vector< int > > gpmp::Comb::compositions | ( | int | n | ) |
Generate all compositions of a positive integer n.
n | The integer to compose |
Definition at line 167 of file comb.cpp.
Referenced by main().
std::vector< int > gpmp::Comb::divisors | ( | int | n | ) |
Calculate the divisors of a positive integer.
n | The integer |
int64_t gpmp::Comb::dyck_words | ( | int | n | ) |
int64_t gpmp::Comb::factorial | ( | int | n | ) |
void gpmp::Comb::gen_compositions | ( | int | n, |
int | max, | ||
std::vector< int > & | composition, | ||
std::vector< std::vector< int >> & | result | ||
) |
void gpmp::Comb::gen_dyck_words | ( | std::string | prefix, |
int | open, | ||
int | close, | ||
std::vector< std::string > & | dyck_words | ||
) |
Generate Dyck words.
prefix | The current Dyck word prefix |
open | The number of open parentheses remaining |
close | The number of close parentheses remaining |
dyck_words | The resulting vector of Dyck words |
Definition at line 294 of file comb.cpp.
std::vector< std::string > gpmp::Comb::gen_dyck_words_until | ( | int | n | ) |
void gpmp::Comb::gen_lyndon_words | ( | std::string | prefix, |
int | n, | ||
int | max, | ||
std::vector< std::string > & | lyndonWords | ||
) |
Generate Lyndon words.
prefix | The current Lyndon word prefix |
n | The length of the Lyndon words |
max | The maximum value in each Lyndon word |
lyndonWords | The resulting vector of Lyndon words |
Definition at line 328 of file comb.cpp.
void gpmp::Comb::gen_necklaces | ( | std::vector< int > & | necklace, |
int | index, | ||
int | n, | ||
int | k, | ||
std::vector< std::vector< int >> & | necklaces | ||
) |
Generate necklaces.
necklace | The current necklace being generated |
index | The current index in the necklace |
n | The length of the necklace |
k | The number of colors |
necklaces | The resulting vector of necklaces |
Definition at line 311 of file comb.cpp.
void gpmp::Comb::gen_partition_distinct | ( | int | n, |
int | max, | ||
std::vector< int > & | partition, | ||
std::vector< std::vector< int >> & | result | ||
) |
Generate distinct partitions of a positive integer.
n | The integer to partition |
max | The maximum value in each partition |
partition | The current partition being generated |
result | The resulting vector of partitions |
Definition at line 257 of file comb.cpp.
std::vector< std::string > gpmp::Comb::generateLyndonWords | ( | int | n | ) |
std::vector< std::vector< int > > gpmp::Comb::generateNecklaces | ( | int | n, |
int | k | ||
) |
void gpmp::Comb::generatePartitions | ( | int | n, |
int | max, | ||
std::vector< int > & | partition, | ||
std::vector< std::vector< int >> & | result | ||
) |
Generate partitions of a positive integer.
n | The integer to partition |
max | The maximum value in each partition |
partition | The current partition being generated |
result | The resulting vector of partitions |
Definition at line 240 of file comb.cpp.
std::vector< int > gpmp::Comb::gray_code | ( | int | n | ) |
int64_t gpmp::Comb::lyndonWords | ( | int | n | ) |
Calculate the number of Lyndon words of length n.
n | The length of the Lyndon word |
Definition at line 224 of file comb.cpp.
Referenced by main().
int64_t gpmp::Comb::necklaces | ( | int | n, |
int | k | ||
) |
std::vector< std::vector< int > > gpmp::Comb::partition_distinct | ( | int | n | ) |
std::vector< std::vector< int > > gpmp::Comb::partitions | ( | int | n | ) |
int64_t gpmp::Comb::permutation | ( | int | n, |
int | r | ||
) |
std::vector<std::vector<T> > gpmp::Comb::permutations | ( | const std::vector< T > & | vec | ) |
Generate all permutations of a vector.
T | The type of elements in the vector |
vec | The vector |
Referenced by main().
int gpmp::Comb::phi | ( | int | n | ) |
Calculate Euler's totient function (φ)
n | The integer |
int64_t gpmp::Comb::stirling_num | ( | int | n, |
int | k | ||
) |
Calculate the Stirling number of the second kind.
n | The total number of items |
k | The number of non-empty subsets |
Definition at line 111 of file comb.cpp.
Referenced by main().
int64_t gpmp::Comb::subfactorial | ( | int | n | ) |
Calculate the subfactorial (!n) or derangement of n.
n | The number |
Definition at line 131 of file comb.cpp.
Referenced by main().
int64_t gpmp::Comb::subsequences | ( | int | n, |
int | k | ||
) |