[
https://issues.apache.org/jira/browse/NUMBERS-215?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18116985#comment-18116985
]
Alex Herbert commented on NUMBERS-215:
--------------------------------------
For reference the public API is:
{code:java}
public final class org.apache.commons.numbers.combinatorics.Stirling$S2 {
public static org.apache.commons.numbers.combinatorics.Stirling$S2 of(int,
int);
public long get();
public java.util.stream.Stream<int[][]> stream();
public <T> java.util.stream.Stream<java.util.List<java.util.List<T>>>
stream(java.util.List<T>);
public <T> java.util.stream.Stream<java.util.List<java.util.List<T>>>
stream(T...);
public java.lang.Iterable<int[][]> partitionGenerator();
}{code}
Example:
{code:java}
Stirling.S2.of(4, 2).stream().forEach(p ->
System.out.println(Arrays.deepToString(p)));
{code}
{noformat}
[[0, 1, 2], [3]]
[[0, 1, 3], [2]]
[[0, 1], [2, 3]]
[[0, 2, 3], [1]]
[[0, 2], [1, 3]]
[[0, 3], [1, 2]]
[[0], [1, 2, 3]]
{noformat}
This sort of output example could be added to the javadoc of the S2 class to
make it clear what the result is when using the class.
The actual implementation is neat and the tests are complete.
h2. API Change?
The two methods to accept an array of elements T or a List<T> are tied to the
instance. So if you want the k partitions of an object list:
{code:java}
List<X> list = ...
Stirling.S2.of(list.size(), k).stream(list);
{code}
I think it makes it more readable if the method is static allowing:
{code:java}
List<X> list = ...
Stirling.S2.stream(list, k);
{code}
Behind the scenes it will still raise the same exceptions and create the same
iterator via an S2 instance but the user only has to pass the list once.
> Stirling partitions generator
> -----------------------------
>
> Key: NUMBERS-215
> URL: https://issues.apache.org/jira/browse/NUMBERS-215
> Project: Commons Numbers
> Issue Type: New Feature
> Components: combinatorics
> Reporter: Gilles Sadowski
> Assignee: Gilles Sadowski
> Priority: Minor
> Fix For: 1.4
>
>
> Would it be useful to provide functionality to *iterate* over the partitions
> defined by "Stirling number of the second kind" (i.e. all partitions into K
> subsets, of a set of N elements)?
> Code would be in a nested static class of the existing
> [{{Stirling}}|https://commons.apache.org/proper/commons-numbers/commons-numbers-combinatorics/apidocs/src-html/org/apache/commons/numbers/combinatorics/Stirling.html]
> class:
> {code}
> public final class Stirling {
> // ...
> public static final class S2 implements Iterable<List<List<Integer>> {
> // ...
> public static S2 of(int n, int k) {
> // ...
> }
> }
> }
> {code}
> I have a potential use case (for very small "N").
--
This message was sent by Atlassian Jira
(v8.20.10#820010)