Combinations

class Combinations(*args)

Combinations generator.

Parameters:
kint

The cardinal of the subsets

nint

The cardinal of the base set

Methods

generateNext()

Generate the next element of the combinatorial sequence.

getClassName()

Accessor to the object's name.

getDimension()

Get the dimension of the generated indices.

getK()

Accessor to the cardinal of the subsets.

getN()

Accessor to the cardinal of the base set.

getName()

Accessor to the object's name.

getSize()

Get the number of elements of the combinatorial sequence.

hasName()

Test if the object is named.

restart()

Restart the combinatorial sequence from its beginning.

setK(k)

Accessor to the cardinal of the subsets.

setN(n)

Accessor to the cardinal of the base set.

setName(name)

Accessor to the object's name.

generate

Notes

In the first usage, the generator is built using the default values k = 1, n = 1.

In the second usage, the generator produces all the subsets with k elements of a base set with n elements. The subsets are produced as a collection of Indices in lexical order, the elements of each subset being sorted in increasing order.

The number of indices generated is:

\frac{n!}{k! (n - k)!}

The combinations generator generates a collection of Indices where:

  • the Indices are sorted in lexical order,

  • the components are sorted within a given Indices.

Examples

>>> import openturns as ot
>>> tuples = ot.Combinations(2, 5)
>>> print(tuples.getSize())
10
>>> for indices in tuples:
...     print(indices)
[0,1]
[0,2]
[0,3]
[0,4]
[1,2]
[1,3]
[1,4]
[2,3]
[2,4]
[3,4]
__init__(*args)
generateNext()

Generate the next element of the combinatorial sequence.

Returns:
indicesIndices

The next element of the sequence. The order in which the elements are generated depends on the concrete generator.

Notes

The generator holds an internal state, as SplitterImplementation does. restart starts the sequence over from its beginning. Once all the getSize elements have been generated, calling this method raises an exception. In Python, the generator can also be iterated over directly:

for indices in ot.Combinations(2, 5):
    print(indices)
getClassName()

Accessor to the object’s name.

Returns:
class_namestr

The object class name (object.__class__.__name__).

getDimension()

Get the dimension of the generated indices.

Returns:
dimensionint

The size of each Indices generated by generateNext.

getK()

Accessor to the cardinal of the subsets.

Returns:
kint

The cardinal of the subsets.

getN()

Accessor to the cardinal of the base set.

Returns:
nint

The cardinal of the base set.

getName()

Accessor to the object’s name.

Returns:
namestr

The name of the object.

getSize()

Get the number of elements of the combinatorial sequence.

Returns:
sizeint

The number of total elements generated, without building the whole collection.

hasName()

Test if the object is named.

Returns:
hasNamebool

True if the name is not empty.

restart()

Restart the combinatorial sequence from its beginning.

Notes

The next call to generateNext (or to the Python iterator) generates the first element of the sequence again.

setK(k)

Accessor to the cardinal of the subsets.

Parameters:
kint

The cardinal of the subsets.

setN(n)

Accessor to the cardinal of the base set.

Parameters:
nint

The cardinal of the base set.

setName(name)

Accessor to the object’s name.

Parameters:
namestr

The name of the object.

Examples using the class

Combinatorial generators

Combinatorial generators