EcGen 1.1.8
Loading...
Searching...
No Matches
ecgen Namespace Reference

Functions

auto emk_comb_gen (int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
 Generate all k-combinations of n elements (revolving door algorithm).
template<typename Container>
auto emk (int n, int k, Container lst) -> py::Generator< Container & >
 Generate all k-combinations in reverse lexicographic order (revolving door).
template<int N, int K>
constexpr auto Combination ()
 Calculate binomial coefficient C(N, K) at compile time.
auto emk_gen (int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
 Generate all k-combinations from n elements (recursive, forward order).
auto emk_neg (int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
 Generate all k-combinations from n elements (recursive, reverse order).
auto brgc_gen (int n) -> py::RecursiveGenerator< int >
 Generate recursive Binary Reflected Gray Code sequence.
template<typename Container>
auto brgc (int n) -> py::Generator< Container & >
 Generate Binary Reflected Gray Code (BRGC) sequence using recursion.
void init_logger ()
 Initialize the logger with default filter (info).
void init_logger_with_filter (const std::string &filter)
 Initialize the logger with a specific filter string.
bool try_init_logger ()
 Try to initialize the logger with default filter (info).
bool try_init_logger_with_filter (const std::string &filter)
 Try to initialize the logger with a specific filter string.
bool is_logger_initialized ()
 Check if the logger has been initialized.
auto sjt_gen (int n) -> py::Generator< int >
 Generate permutation indices using Steinhaus-Johnson-Trotter algorithm.
auto ehr_gen (int n) -> py::Generator< int >
 Generate permutation indices using Eades-Hickey-Read (EHR) algorithm.
template<int N>
constexpr auto Factorial ()
 Compute factorial N! at compile time.
template<typename Container>
auto sjt (Container &perm) -> py::Generator< Container & >
 Generate all permutations via adjacent transpositions (SJT algorithm).
auto sjt2 (int n) -> py::Generator< std::vector< int > >
 Generate all permutations as lists using SJT algorithm.
template<int N>
constexpr auto Stirling2nd2 ()
 Compute Stirling numbers of the second kind S(N,2).
auto set_bipart_gen (int n) -> py::RecursiveGenerator< int >
template<int N, int K>
constexpr auto Stirling2nd ()
 Compute Stirling numbers of the second kind S(N, K).
auto set_partition_gen (int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
auto set_partition_gen_old (int n, int k) -> py::RecursiveGenerator< std::pair< int, int > >
 Generates all set partitions of {1,...,n} into k blocks (legacy).

Function Documentation

◆ brgc()

template<typename Container>
auto ecgen::brgc ( int n) ->py::Generator< Container & >

Generate Binary Reflected Gray Code (BRGC) sequence using recursion.

The Binary Reflexed Gray Code is an ordering of all bitstrings of length n, such that adjacent strings differ by only one bit flip.

Example visualization for n=3:

*    Step 0: [0,0,0] (000)
*    Step 1: [0,0,1] (001) - flip bit 0
*    Step 2: [0,1,1] (011) - flip bit 1
*    Step 3: [0,1,0] (010) - flip bit 0
*    Step 4: [1,1,0] (110) - flip bit 2
*    Step 5: [1,1,1] (111) - flip bit 0
*    Step 6: [1,0,1] (101) - flip bit 1
*    Step 7: [1,0,0] (100) - flip bit 0
* 

This implementation uses a for loop to iterate through the recursive Gray Code generator, flipping bits at each step to generate the sequence.

Template Parameters
Container- The type of container to store the bitstrings.
Parameters
[in]n- The length of the Gray code sequence to generate.
Returns
A generator that yields each value in the Gray code sequence.

◆ brgc_gen()

auto ecgen::brgc_gen ( int n) ->py::RecursiveGenerator< int >
extern

Generate recursive Binary Reflected Gray Code sequence.

The recursive Gray code sequence is an ordering of all 2^n bitstrings of length n, such that adjacent strings differ by only one bit flip.

Example visualization for n=3:

*    000 -> 001 -> 011 -> 010 -> 110 -> 111 -> 101 -> 100
*     |     |     |     |     |     |     |     |
*     0     1     3     2     6     7     5     4
*     (each adjacent pair differs by exactly one bit)
* 

This generates the sequence efficiently using recursion and bit manipulation. The encoding formula for the Binary Reflected Gray Code is: \( G(n) = n \oplus \lfloor n/2 \rfloor \)

Parameters
[in]n- The length of the Gray code sequence to generate.
Returns
A recursive generator that yields each value in the Gray code sequence.

◆ Combination()

template<int N, int K>
auto ecgen::Combination ( )
constexpr

Calculate binomial coefficient C(N, K) at compile time.

This is a constexpr function that computes the result at compile-time using Pascal's rule:

\[ \binom{N}{K} = \binom{N-1}{K-1} + \binom{N-1}{K} \]

Example calculation for C(4,2) = 6:

*    C(4,2) = 4! / (2! * (4-2)!) = 24 / (2 * 2) = 6
* 
Template Parameters
N- The size of the set
K- The number of elements to select
Returns
The number of combinations, computed at compile-time.

◆ ehr_gen()

auto ecgen::ehr_gen ( int n) ->py::Generator< int >
extern

Generate permutation indices using Eades-Hickey-Read (EHR) algorithm.

Example visualization for n=3:

*    Start: 0 1 2
*    Step 1: 1 0 2 (swap 0,1)
*    Step 2: 2 0 1 (swap 1,2)
*    Step 3: 0 2 1 (swap 0,1)
*    Step 4: 1 2 0 (swap 0,1)
*    Step 5: 2 1 0 (swap 1,2)
* 
Parameters
[in]nThe permutation length
Returns
A py::Generator that yields the permutation indices

◆ emk()

template<typename Container>
auto ecgen::emk ( int n,
int k,
Container lst )->py::Generator< Container & >

Generate all k-combinations in reverse lexicographic order (revolving door).

Example visualization of combinations (4 choose 2):

*    0 1 2 3
*    x x . .  -> indices 0,1
*    x . x .  -> indices 0,2
*    x . . x  -> indices 0,3
*    . x x .  -> indices 1,2
*    . x . x  -> indices 1,3
*    . . x x  -> indices 2,3
* 
Parameters
[in]n- The number of elements in the set.
[in]k- The size of the combinations to generate.
[in]lst- The container holding the set elements.
Returns
A generator that yields each k-combination of lst in reverse lexicographic order.

◆ emk_comb_gen()

auto ecgen::emk_comb_gen ( int n,
int k )->py::RecursiveGenerator< std::pair< int, int > >
extern

Generate all k-combinations of n elements (revolving door algorithm).

This is implemented as a recursive generator function that yields index pairs representing the combinations using the revolving door algorithm.

Example visualization of combinations (4 choose 2):

*    0 1 2 3
*    x x . .  -> indices 0,1
*    x . x .  -> indices 0,2
*    x . . x  -> indices 0,3
*    . x x .  -> indices 1,2
*    . x . x  -> indices 1,3
*    . . x x  -> indices 2,3
* 
Parameters
[in]n- The number of elements in the full set.
[in]k- The number of elements to select in each combination.
Returns
A recursive generator yielding index pairs for the k-combinations of n elements.

◆ emk_gen()

auto ecgen::emk_gen ( int n,
int k )->py::RecursiveGenerator< std::pair< int, int > >
extern

Generate all k-combinations from n elements (recursive, forward order).

This is a recursive generator function that yields all possible k-combinations of integer indices ranging from 0 to n-1.

Parameters
[in]n- The size of the set to generate combinations from.
[in]k- The size of each generated combination.
Returns
A recursive generator yielding all k-combinations as pairs of indices.

◆ emk_neg()

auto ecgen::emk_neg ( int n,
int k )->py::RecursiveGenerator< std::pair< int, int > >
extern

Generate all k-combinations from n elements (recursive, reverse order).

This is the reverse variant of emk_gen, generating all possible k-combinations of integer indices ranging from 0 to n-1 in reverse lexicographic order.

Parameters
[in]n- The size of the set to generate combinations from.
[in]k- The size of each generated combination.
Returns
A recursive generator yielding all k-combinations as pairs of indices (in reverse order).

◆ Factorial()

template<int N>
auto ecgen::Factorial ( )
constexpr

Compute factorial N! at compile time.

This is used to calculate the total number of permutations of length N, which is N!. It is implemented as a constexpr function template.

\[ N! = N \times (N-1) \times \cdots \times 2 \times 1 \]

Example calculation for N=4:

*    4! = 4 * 3 * 2 * 1 = 24
*    So there are 24 permutations of 4 elements
* 
Template Parameters
N- The number to calculate the factorial of.
Returns
constexpr auto - The factorial of N as a std::integral_constant.

◆ init_logger()

void ecgen::init_logger ( )

Initialize the logger with default filter (info).

Initializes spdlog with a stdout color sink at the info level. Call once at program start.

◆ init_logger_with_filter()

void ecgen::init_logger_with_filter ( const std::string & filter)

Initialize the logger with a specific filter string.

Parameters
filterLog level string (e.g. "info", "debug", "warn", "error")

◆ is_logger_initialized()

bool ecgen::is_logger_initialized ( )

Check if the logger has been initialized.

Returns
true if the logger has been initialized

◆ set_bipart_gen()

auto ecgen::set_bipart_gen ( int n) ->py::RecursiveGenerator< int >
extern

Generates all bipartitions of a set of n elements into 2 blocks.

The generator yields element indices that should be moved from one block to the other, following a Gray code ordering where each step moves a single element.

Example for n=4 (bipartitions of {1,2,3,4} into 2 nonempty blocks):

*    {1,2,3}{4} -> {1,2}{3,4} -> {1,3}{2,4} -> {1}{2,3,4} -> ...
* 
Parameters
[in]n- The number of elements to bipartition (n >= 3).
Returns
A recursive generator that yields element indices to move.

◆ set_partition_gen()

auto ecgen::set_partition_gen ( int n,
int k )->py::RecursiveGenerator< std::pair< int, int > >
extern

A recursive generator that produces all set partitions of {1, ..., n} into k nonempty blocks.

Example visualization for n=4, k=2 (partitions into 2 blocks):

*    {1,2,3}{4}  {1,2,4}{3}  {1,3,4}{2}  {1}{2,3,4}  {1,2}{3,4}  {1,3}{2,4}  {1,4}{2,3}
* 
Parameters
[in]n- The size of the set to partition.
[in]k- The number of blocks in the partition.
Returns
A recursive generator that yields std::pair representing each partition.

◆ set_partition_gen_old()

auto ecgen::set_partition_gen_old ( int n,
int k )->py::RecursiveGenerator< std::pair< int, int > >
extern

Generates all set partitions of {1,...,n} into k blocks (legacy).

This is the old implementation of the set partition generator. Unlike set_partition_gen, it does not check for boundary conditions (k=1, k=n, k>n) and yields directly into the recursive helpers.

Parameters
[in]n- The size of the set to partition.
[in]k- The number of blocks in the partition.
Returns
py::RecursiveGenerator<std::pair<int, int>>

◆ sjt()

template<typename Container>
auto ecgen::sjt ( Container & perm) ->py::Generator< Container & >
inline

Generate all permutations via adjacent transpositions (SJT algorithm).

Example visualization for adjacent transpositions:

*    [a, b, c] -> [b, a, c] (swap positions 0,1)
*    [b, a, c] -> [b, c, a] (swap positions 1,2)
*    [b, c, a] -> [c, b, a] (swap positions 0,1)
* 
Template Parameters
Container
Parameters
[in]perm
Returns
py::Generator<Container&>

◆ sjt2()

auto ecgen::sjt2 ( int n) ->py::Generator< std::vector< int > >

Generate all permutations as lists using SJT algorithm.

Unlike sjt_gen which yields swap indices, sjt2 yields the actual permutation vectors directly. The algorithm recursively generates permutations by inserting the largest element at every possible position, alternating between upward and downward directions.

Example for n=3:

*    [0, 1, 2]  [0, 2, 1]  [2, 0, 1]  [2, 1, 0]  [1, 2, 0]  [1, 0, 2]
* 
Parameters
[in]nThe number of elements to permute
Returns
A generator that yields each permutation as a vector of ints

◆ sjt_gen()

auto ecgen::sjt_gen ( int n) ->py::Generator< int >
extern

Generate permutation indices using Steinhaus-Johnson-Trotter algorithm.

Example visualization for n=3 (generating all permutations):

*    Start: 0 1 2
*    Step 1: 0 2 1 (swap 1,2)
*    Step 2: 2 0 1 (swap 0,2)
*    Step 3: 2 1 0 (swap 0,1)
*    Step 4: 1 2 0 (swap 1,2)
*    Step 5: 1 0 2 (swap 0,2)
* 
Parameters
[in]nThe permutation length
Returns
A py::Generator that yields the permutation indices

◆ Stirling2nd()

template<int N, int K>
auto ecgen::Stirling2nd ( )
constexpr

Compute Stirling numbers of the second kind S(N, K).

Counts the number of ways to partition a set of N elements into K nonempty subsets. Implemented recursively using the recurrence relation for Stirling numbers.

\[ S(N,K) = S(N-1,K-1) + K \cdot S(N-1,K) \]

Example: S(4,2) = 7 partitions of 4 elements into 2 blocks:

*    {1,2,3}{4}  {1,2,4}{3}  {1,3,4}{2}  {1}{2,3,4}  {1,2}{3,4}  {1,3}{2,4}  {1,4}{2,3}
* 
Template Parameters
N
K
Returns
constexpr auto

◆ Stirling2nd2()

template<int N>
auto ecgen::Stirling2nd2 ( )
constexpr

Compute Stirling numbers of the second kind S(N,2).

Counts the number of ways to partition a set of N elements into 2 nonempty subsets.

This is a specialized version for k=2, computed with the recurrence:

\[ S(N,2) = 1 + 2 \cdot S(N-1,2) \]

or equivalently \(S(N,2) = 2^{N-1} - 1\).

Example:

*    S(5,2) = 15  (2^4 - 1 = 15)
*    S(4,2) = 7   (2^3 - 1 = 7)
* 
Template Parameters
N- The size of the set.
Returns
constexpr auto - The Stirling number S(N,2) as a std::integral_constant.

◆ try_init_logger()

bool ecgen::try_init_logger ( )

Try to initialize the logger with default filter (info).

Returns
true if logger was successfully initialized, false if already initialized

◆ try_init_logger_with_filter()

bool ecgen::try_init_logger_with_filter ( const std::string & filter)

Try to initialize the logger with a specific filter string.

Parameters
filterLog level string (e.g. "info", "debug", "warn", "error")
Returns
true if logger was successfully initialized, false if already initialized