mathnet-numerics
mathnet-numerics copied to clipboard
Add combinatorial generation algorithms
This PR is intended to satisfy some of the requests made in #485.
What's there:
- Methods for:
- Generating all combinations for a given set and subset size, without replacement.
- Generating all permutations for a given set, without replacement.
- Inverting a permutation.
- Some tests.
Missing:
- Variations
- Repetitions
I'm happy to work on the missing pieces, but would appreciate any feedback first!
I'm specifically (and very) interested in any feedback related to some of the comments in the code about lazy evaluation.
For example:
var permutations = Combinatorics.GenerateAllPermutations(n).ToList();
is just going to end disastrously for anybody who tries it.
I've experienced the same problem with IEnumerable's that shouldn't be enumerated from end to end. One way of solving it would be to return a concrete type with an implementation of ToList()
which throws an exception for larger numbers of permutations. That would at least prevent the most causal mistakes. That also gives you the opportunity to use Debugger Display Attributes to prevent the enumeration to crash Visual Studio when the watch window tries to enumerate the permutations.