Click or drag to resize
Accord.NET (logo)

CombinatoricsSequences Method (Int32, Int32, Boolean)

Provides a way to enumerate all possible ordered permutations with repetitions allowed (i.e. a truth table), without using many memory allocations.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static IEnumerable<int[]> Sequences(
	int symbols,
	int length,
	bool inPlace = false
)
Request Example View Source

Parameters

symbols
Type: SystemInt32
The number of symbols.
length
Type: SystemInt32
The length of the sequence to generate.
inPlace (Optional)
Type: SystemBoolean
If set to true, the different generated sequences will be stored in the same array, thus preserving memory. However, this may prevent the samples from being stored in other locations without having to clone them. If set to false, a new memory block will be allocated for each new object in the sequence.

Return Value

Type: IEnumerableInt32
Examples

Suppose we would like to generate the same sequences shown in the TruthTable(Int32, Int32)example, however, without explicitly storing all possible combinations in an array. In order to iterate over all possible combinations efficiently, we can use:

int symbols = 2; // Binary variables: either 0 or 1
int length = 3;  // The number of variables; or number 
                 // of columns in the generated table.

foreach (int[] row in Combinatorics.Sequences(symbols, length))
{
    // The following sequences will be generated in order:
    // 
    //   new int[] { 0, 0, 0 },
    //   new int[] { 0, 0, 1 },
    //   new int[] { 0, 1, 0 },
    //   new int[] { 0, 1, 1 },
    //   new int[] { 1, 0, 0 },
    //   new int[] { 1, 0, 1 },
    //   new int[] { 1, 1, 0 },
    //   new int[] { 1, 1, 1 },
}
See Also