Class Combinatorics
Static class for combinatorics functions.
Inheritance
System.Object
Combinatorics
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Assembly: Mars.Numerics.dll
Syntax
public static class Combinatorics
Methods
|
Improve this Doc
View Source
Combinations<T>(T[], Boolean)
Enumerates all possible value combinations for a given array.
Declaration
public static IEnumerable<T[]> Combinations<T>(this T[] values, bool inPlace = false)
Parameters
Type |
Name |
Description |
T[] |
values |
The array whose combinations need to be generated. |
System.Boolean |
inPlace |
If set to true, the different generated combinations 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.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T[]> |
|
Type Parameters
|
Improve this Doc
View Source
Combinations<T>(T[], Int32, Boolean)
Enumerates all possible value combinations for a given array.
Declaration
public static IEnumerable<T[]> Combinations<T>(this T[] values, int k, bool inPlace = false)
Parameters
Type |
Name |
Description |
T[] |
values |
The array whose combinations need to be generated. |
System.Int32 |
k |
The length of the combinations to be generated. |
System.Boolean |
inPlace |
If set to true, the different generated combinations 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.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T[]> |
|
Type Parameters
|
Improve this Doc
View Source
Permutations<T>(T[], Boolean)
Enumerates all possible value permutations for a given array.
Declaration
public static IEnumerable<T[]> Permutations<T>(T[] values, bool inPlace = false)
Parameters
Type |
Name |
Description |
T[] |
values |
The array whose permutations need to be generated |
System.Boolean |
inPlace |
If set to true, the different generated permutations 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.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T[]> |
|
Type Parameters
|
Improve this Doc
View Source
Sequences(Int32, Boolean)
Provides a way to enumerate all possible ordered permutations
with repetitions allowed (i.e. a truth table), without using
many memory allocations.
Declaration
public static IEnumerable<int[]> Sequences(int length, bool inPlace = false)
Parameters
Type |
Name |
Description |
System.Int32 |
length |
The length of the sequence to generate. |
System.Boolean |
inPlace |
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.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Int32[]> |
|
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 length = 3; // The number of variables; or number
// of columns in the generated table.
foreach (int[] row in Combinatorics.Sequences(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 },
}
|
Improve this Doc
View Source
Sequences(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.
Declaration
public static IEnumerable<int[]> Sequences(int symbols, int length, bool inPlace = false)
Parameters
Type |
Name |
Description |
System.Int32 |
symbols |
The number of symbols. |
System.Int32 |
length |
The length of the sequence to generate. |
System.Boolean |
inPlace |
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.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Int32[]> |
|
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 },
}
|
Improve this Doc
View Source
Sequences(Int32[], Boolean, Boolean)
Provides a way to enumerate all possible ordered permutations
with repetitions allowed (i.e. a truth table), without using
many memory allocations.
Declaration
public static IEnumerable<int[]> Sequences(this int[] symbols, bool inPlace = false, bool firstColumnChangesFaster = false)
Parameters
Type |
Name |
Description |
System.Int32[] |
symbols |
The number of symbols for each variable. |
System.Boolean |
inPlace |
If set to true, the different generated permutations 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.
|
System.Boolean |
firstColumnChangesFaster |
If set to true, the first elements in the sequences will change faster
than last ones. This changes the order in which the sequences are presented,
but no their content.
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Int32[]> |
|
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:
foreach (int[] row in Combinatorics.Sequences(new[] { 2, 2 }))
{
// 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 },
}
|
Improve this Doc
View Source
Subsets<T>(IEnumerable<T>, Boolean)
Generates all possibles subsets of the given set.
Declaration
public static IEnumerable<SortedSet<T>> Subsets<T>(this IEnumerable<T> set, bool inPlace = false)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
set |
|
System.Boolean |
inPlace |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Collections.Generic.SortedSet<T>> |
|
Type Parameters
|
Improve this Doc
View Source
Subsets<T>(IEnumerable<T>, Int32, Boolean)
Generates all possibles subsets of size k of the given set.
Declaration
public static IEnumerable<SortedSet<T>> Subsets<T>(this IEnumerable<T> set, int k, bool inPlace = false)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
set |
|
System.Int32 |
k |
|
System.Boolean |
inPlace |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Collections.Generic.SortedSet<T>> |
|
Type Parameters
|
Improve this Doc
View Source
TruthTable(Int32, Int32)
Generates all possible ordered permutations
with repetitions allowed (a truth table).
Declaration
public static int[][] TruthTable(int symbols, int length)
Parameters
Type |
Name |
Description |
System.Int32 |
symbols |
The number of symbols. |
System.Int32 |
length |
The length of the sequence to generate. |
Returns
Type |
Description |
System.Int32[][] |
|
Examples
Suppose we would like to generate a truth table for a binary
problem. In this case, we are only interested in two symbols:
0 and 1. Let's then generate the table for three binary values
int symbols = 2; // Binary variables: either 0 or 1
int length = 3; // The number of variables; or number
// of columns in the generated table.
// Generate the table using Combinatorics.TruthTable(2,3)
int[][] table = Combinatorics.TruthTable(symbols, length);
// The generated table will be:
{
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
|
Improve this Doc
View Source
TruthTable(Int32)
Generates all possible two symbol ordered
permutations with repetitions allowed (a truth table).
Declaration
public static int[][] TruthTable(int length)
Parameters
Type |
Name |
Description |
System.Int32 |
length |
The length of the sequence to generate. |
Returns
Type |
Description |
System.Int32[][] |
|
Examples
Suppose we would like to generate a truth table for a binary
problem. In this case, we are only interested in two symbols:
0 and 1. Let's then generate the table for three binary values
int length = 3; // The number of variables; or number
// of columns in the generated table.
// Generate the table using Combinatorics.TruthTable(3)
int[][] table = Combinatorics.TruthTable(length);
// The generated table will be:
{
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 },
};
|
Improve this Doc
View Source
TruthTable(Int32[])
Generates all possible ordered permutations
with repetitions allowed (a truth table).
Declaration
public static int[][] TruthTable(this int[] symbols)
Parameters
Type |
Name |
Description |
System.Int32[] |
symbols |
The number of symbols for each variable. |
Returns
Type |
Description |
System.Int32[][] |
|
Examples
Suppose we would like to generate a truth table (i.e. all possible
combinations of a set of discrete symbols) for variables that contain
different numbers symbols. Let's say, for example, that the first
variable may contain symbols 0 and 1, the second could contain either
0, 1, or 2, and the last one again could contain only 0 and 1. Thus
we can generate the truth table in the following way:
// Number of symbols for each variable
int[] symbols = { 2, 3, 2 };
// Generate the truth table for the given symbols
int[][] table = Combinatorics.TruthTable(symbols);
// The generated table will be:
{
new int[] { 0, 0, 0 },
new int[] { 0, 0, 1 },
new int[] { 0, 1, 0 },
new int[] { 0, 1, 1 },
new int[] { 0, 2, 0 },
new int[] { 0, 2, 1 },
new int[] { 1, 0, 0 },
new int[] { 1, 0, 1 },
new int[] { 1, 1, 0 },
new int[] { 1, 1, 1 },
new int[] { 1, 2, 0 },
new int[] { 1, 2, 1 },
};
See Also