Open
Description
Motivation
Functions or subroutines that take an array (or a string) and return an array (of higher rank, or of strings) with combinatorial compositions of the input values. Common facilities (e.g. Python's itertools) with example results given here in a non-Fortran nested format:
products([1, 2, 3], r=2)
returns[[1, 1], [2, 1], [3, 1], [1, 2], [2, 2], [3, 2], [1, 3], [2, 3], [3, 3]]
permutations([1, 2, 3], r=2)
returns[[2, 1], [3, 1], [1, 2], [3, 2], [1, 3], [2, 3]]
combinations([1, 2, 3], r=2)
returns[[2, 1], [3, 1], [3, 2]]
combinations_with_replacement([1, 2, 3], r=2)
returns[[1, 1], [2, 1], [3, 1], [2, 2], [3, 2], [3, 3]]
These examples are in column-major order, which makes sense for indices but less so for other elements. If this behavior is consistent, for strings the result would not be lexically ordered:
products('ABC', r=2)
returns['AA', 'BA', 'CA', 'AB', 'BB', 'CB', 'AC', 'BC', 'CC']
permutations('ABC', r=2)
returns['BA', 'CA', 'AB', 'CB', 'AC', 'BC']
combinations('ABC', r=2)
returns['BA', 'CA', 'CB']
combinations_with_replacement('ABC', r=2)
returns['AA', 'BA', 'CA', 'BB', 'CB', 'CC']
The procedures can be extended to lists.
Questions
- What names/features should be used (e.g.
combinations_with_replacement
is quite long)? - Should the results be in column-major order?
- Should some be merged into a single procedure?
- Should be there alternative generator-like routines to get the next iteration from a given or saved state?
Prior Art
Additional Information
- What should be part of stdlib? #1 mentions permutations on the side
- sample #404 mentions a single permutation step as a special case of a
sample
procedure