stats/combin: add CartesianGenerator (#1111)

* stats/combin: add CartesianGenerator
This commit is contained in:
Nils Wogatzky
2019-10-13 11:54:04 +02:00
committed by Brendan Tracey
parent f714310635
commit c0d3984e72
3 changed files with 127 additions and 8 deletions

View File

@@ -10,6 +10,49 @@ import (
"gonum.org/v1/gonum/stat/combin"
)
func ExampleCartesian() {
fmt.Println("Generate cartesian products for given lengths:")
lens := []int{1, 2, 3}
list := combin.Cartesian(lens)
for i, v := range list {
fmt.Println(i, v)
}
// This is easy, but the number of combinations can be very large,
// and generating all at once can use a lot of memory.
// For big data sets, consider using CartesianGenerator instead.
// Output:
// Generate cartesian products for given lengths:
// 0 [0 0 0]
// 1 [0 0 1]
// 2 [0 0 2]
// 3 [0 1 0]
// 4 [0 1 1]
// 5 [0 1 2]
}
func ExampleCartesianGenerator() {
fmt.Println("Generate products for given lengths:")
lens := []int{1, 2, 3}
gen := combin.NewCartesianGenerator(lens)
// Now loop over all products.
var i int
for gen.Next() {
fmt.Println(i, gen.Product())
i++
}
// Output:
// Generate products for given lengths:
// 0 [0 0 0]
// 1 [0 0 1]
// 2 [0 0 2]
// 3 [0 1 0]
// 4 [0 1 1]
// 5 [0 1 2]
}
func ExampleCombinations() {
// combin provides several ways to work with the combinations of
// different objects. Combinations generates them directly.