Recipe 0.4. Generating t-uples.
Problem
You want to generate all possible t-uples.
Solution
MixedRadixNtupleFactory may generate all t-uples given the number of digits and a unique radix (same radix for each digit):
import import org.apache.commons.collections15.Transformer; // build the factory MixedRadixNtupleFactory factory = MixedRadixNtupleFactory.newInstance(); // generate all the triplets in radix 2 List<int[]> ntuples = factory.transform(3, 2); // Display n-tuples System.out.println(MixedRadixNtupleFactory.toString(ntuples)); // Output //---------------------------------------------------------- // [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], // [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], Assert.assertEquals((int) Math.pow(2, 3), ntuples.size());
MixedRadixNtupleFactory can also generate all t-uples given a mixed of radices and the number of digits (different radix for each digit):
// you have to define radix for all digits int[] radices = new int[] {3, 2, 2}; // generate the ntuples given the radices List<int[]> ntuples = factory.transform(radices); // Display n-tuples System.out.println(MixedRadixNtupleFactory.toString(ntuples)); // Output //---------------------------------------------------------- // [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], // [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1], // [2, 0, 0], [2, 0, 1], [2, 1, 0], [2, 1, 1]]
There are also many factory methods to convert t-uple to decimal and the reverse operation (see the API of MixedRadixNtupleFactory).
Discussion
See Also