Click or drag to resize
Accord.NET (logo)

CombinatoricsCombinationsT Method (T, Boolean)

Enumerates all possible value combinations for a given array.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static IEnumerable<T[]> Combinations<T>(
	this T[] values,
	bool inPlace = false
)
Request Example View Source

Parameters

values
Type: T
The array whose combinations need to be generated.
inPlace (Optional)
Type: SystemBoolean
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.

Type Parameters

T

Return Value

Type: IEnumerableT

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Examples
// Let's say we would like to compute all the
// possible combinations of elements (1,2,3,4):
// 
int[] elements = new[] { 1, 2, 3, 4 };

// The number of possible subsets might be too large
// to fit on memory. For this reason, we can compute
// values on-the-fly using foreach:

foreach (int[] combination in Combinatorics.Combinations(elements))
{
    // ...
}

// Or we could try to compute them all and store in an array:
int[][] combinations = Combinatorics.Combinations(elements).ToArray();

// In either case, the result will be:

int[][] expected =
{
   new [] { 1 },
   new [] { 2 },
   new [] { 3 },
   new [] { 4 },
   new [] { 1, 2 },
   new [] { 1, 3 },
   new [] { 2, 3 },
   new [] { 1, 4 },
   new [] { 2, 4 },
   new [] { 3, 4 },
   new [] { 1, 2, 3 },
   new [] { 1, 2, 4 },
   new [] { 1, 3, 4 },
   new [] { 2, 3, 4 },
   new [] { 1, 2, 3, 4 }
};

// Note: although the empty set is technically a subset
// of all sets, it won't be included in the enumeration
See Also