Composition - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Iterator

  

Composition

  

generate compositions of an integer

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

Calling Sequence

Composition(n, opts)

Parameters

n

-

posint; integer to compose, must be greater than one

opts

-

(optional) equation(s) of the form option = value; specify options for the Composition command

Options

• 

compile = truefalse

  

True means compile the iterator. The default is true.

• 

maxparts = nonnegint

  

Specifies the maximum number of integers in each composition. The default is n.

• 

parts = nonnegint

  

Specifies the number of integers in each composition. The default, 0, is ignored.

Description

• 

The Composition command returns an iterator that generates all compositions of the integer n.

• 

A composition of integer n is a sequence of positive integers that sum to n.

• 

The n parameter is the integer to decompose.

• 

The output of the iterator is an array of fixed length n. The composition is in the indices 1 to lengthP, where P is the assigned iterator.

Methods

In addition to the common iterator methods, this iterator object has the following methods. The self parameter is the iterator object.

• 

Number(self): return the number of iterations required to step through the iterator, assuming it started at rank one.

Examples

withIterator:

Iterate through the compositions of 5.

n5:

PCompositionn:

PrintP,'showrank':

 1: 5
 2: 4 1
 3: 3 1 1
 4: 3 2
 5: 2 1 2
 6: 2 1 1 1
 7: 2 2 1
 8: 2 3
 9: 1 1 3
10: 1 1 2 1
11: 1 1 1 1 1
12: 1 1 1 2
13: 1 2 2
14: 1 2 1 1
15: 1 3 1
16: 1 4

Compute the number of iterations.

NumberP

16

(1)

Add the parts of each composition to verify they sum to n.

seqaddpk,k=1..lengthP,p=P

5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5

(2)

References

  

Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, sec. 7.2.1.1, generating all n-tuples, algorithm A, p. 77, answer 12.

Compatibility

• 

The Iterator[Composition] command was introduced in Maple 2021.

• 

For more information on Maple 2021 changes, see Updates in Maple 2021.

• 

The Iterator[Composition] command was updated in Maple 2022.

• 

The n parameter was updated in Maple 2022.

• 

The maxparts and parts options were updated in Maple 2022.

See Also

combinat[composition]

Iterator