gonum.org/v1/gonum@v0.14.0/stat/combin/combinations_example_test.go (about) 1 // Copyright ©2019 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package combin_test 6 7 import ( 8 "fmt" 9 10 "gonum.org/v1/gonum/stat/combin" 11 ) 12 13 func ExampleCartesian() { 14 fmt.Println("Generate Cartesian products for given lengths:") 15 lens := []int{1, 2, 3} 16 list := combin.Cartesian(lens) 17 for i, v := range list { 18 fmt.Println(i, v) 19 } 20 // This is easy, but the number of combinations can be very large, 21 // and generating all at once can use a lot of memory. 22 // For big data sets, consider using CartesianGenerator instead. 23 24 // Output: 25 // Generate Cartesian products for given lengths: 26 // 0 [0 0 0] 27 // 1 [0 0 1] 28 // 2 [0 0 2] 29 // 3 [0 1 0] 30 // 4 [0 1 1] 31 // 5 [0 1 2] 32 } 33 34 func ExampleCartesianGenerator() { 35 fmt.Println("Generate products for given lengths:") 36 lens := []int{1, 2, 3} 37 gen := combin.NewCartesianGenerator(lens) 38 39 // Now loop over all products. 40 var i int 41 for gen.Next() { 42 fmt.Println(i, gen.Product(nil)) 43 i++ 44 } 45 46 // Output: 47 // Generate products for given lengths: 48 // 0 [0 0 0] 49 // 1 [0 0 1] 50 // 2 [0 0 2] 51 // 3 [0 1 0] 52 // 4 [0 1 1] 53 // 5 [0 1 2] 54 } 55 56 func ExampleCombinations() { 57 // combin provides several ways to work with the combinations of 58 // different objects. Combinations generates them directly. 59 fmt.Println("Generate list:") 60 n := 5 61 k := 3 62 list := combin.Combinations(n, k) 63 for i, v := range list { 64 fmt.Println(i, v) 65 } 66 // This is easy, but the number of combinations can be very large, 67 // and generating all at once can use a lot of memory. 68 69 // Output: 70 // Generate list: 71 // 0 [0 1 2] 72 // 1 [0 1 3] 73 // 2 [0 1 4] 74 // 3 [0 2 3] 75 // 4 [0 2 4] 76 // 5 [0 3 4] 77 // 6 [1 2 3] 78 // 7 [1 2 4] 79 // 8 [1 3 4] 80 // 9 [2 3 4] 81 } 82 83 func ExampleCombinations_index() { 84 // The integer slices returned from Combinations can be used to index 85 // into a data structure. 86 data := []string{"a", "b", "c", "d", "e"} 87 cs := combin.Combinations(len(data), 2) 88 for _, c := range cs { 89 fmt.Printf("%s%s\n", data[c[0]], data[c[1]]) 90 } 91 92 // Output: 93 // ab 94 // ac 95 // ad 96 // ae 97 // bc 98 // bd 99 // be 100 // cd 101 // ce 102 // de 103 } 104 105 func ExampleCombinationGenerator() { 106 // combin provides several ways to work with the combinations of 107 // different objects. CombinationGenerator constructs an iterator 108 // for the combinations. 109 n := 5 110 k := 3 111 gen := combin.NewCombinationGenerator(n, k) 112 idx := 0 113 for gen.Next() { 114 fmt.Println(idx, gen.Combination(nil)) // can also store in-place. 115 idx++ 116 } 117 // Output: 118 // 0 [0 1 2] 119 // 1 [0 1 3] 120 // 2 [0 1 4] 121 // 3 [0 2 3] 122 // 4 [0 2 4] 123 // 5 [0 3 4] 124 // 6 [1 2 3] 125 // 7 [1 2 4] 126 // 8 [1 3 4] 127 // 9 [2 3 4] 128 } 129 130 func ExampleIndexToCombination() { 131 // combin provides several ways to work with the combinations of 132 // different objects. IndexToCombination allows random access into 133 // the combination order. Combined with CombinationIndex this 134 // provides a correspondence between integers and combinations. 135 n := 5 136 k := 3 137 comb := make([]int, k) 138 for i := 0; i < combin.Binomial(n, k); i++ { 139 combin.IndexToCombination(comb, i, n, k) // can also use nil. 140 idx := combin.CombinationIndex(comb, n, k) 141 fmt.Println(i, comb, idx) 142 } 143 144 // Output: 145 // 0 [0 1 2] 0 146 // 1 [0 1 3] 1 147 // 2 [0 1 4] 2 148 // 3 [0 2 3] 3 149 // 4 [0 2 4] 4 150 // 5 [0 3 4] 5 151 // 6 [1 2 3] 6 152 // 7 [1 2 4] 7 153 // 8 [1 3 4] 8 154 // 9 [2 3 4] 9 155 } 156 157 func ExamplePermutations() { 158 // combin provides several ways to work with the permutations of 159 // different objects. Permutations generates them directly. 160 fmt.Println("Generate list:") 161 n := 4 162 k := 3 163 list := combin.Permutations(n, k) 164 for i, v := range list { 165 fmt.Println(i, v) 166 } 167 // This is easy, but the number of permutations can be very large, 168 // and generating all at once can use a lot of memory. 169 170 // Output: 171 // Generate list: 172 // 0 [0 1 2] 173 // 1 [0 2 1] 174 // 2 [1 0 2] 175 // 3 [1 2 0] 176 // 4 [2 0 1] 177 // 5 [2 1 0] 178 // 6 [0 1 3] 179 // 7 [0 3 1] 180 // 8 [1 0 3] 181 // 9 [1 3 0] 182 // 10 [3 0 1] 183 // 11 [3 1 0] 184 // 12 [0 2 3] 185 // 13 [0 3 2] 186 // 14 [2 0 3] 187 // 15 [2 3 0] 188 // 16 [3 0 2] 189 // 17 [3 2 0] 190 // 18 [1 2 3] 191 // 19 [1 3 2] 192 // 20 [2 1 3] 193 // 21 [2 3 1] 194 // 22 [3 1 2] 195 // 23 [3 2 1] 196 } 197 198 func ExamplePermutations_index() { 199 // The integer slices returned from Permutations can be used to index 200 // into a data structure. 201 data := []string{"a", "b", "c", "d"} 202 cs := combin.Permutations(len(data), 2) 203 for _, c := range cs { 204 fmt.Printf("%s%s\n", data[c[0]], data[c[1]]) 205 } 206 207 // Output: 208 // ab 209 // ba 210 // ac 211 // ca 212 // ad 213 // da 214 // bc 215 // cb 216 // bd 217 // db 218 // cd 219 // dc 220 } 221 222 func ExamplePermutationGenerator() { 223 // combin provides several ways to work with the permutations of 224 // different objects. PermutationGenerator constructs an iterator 225 // for the permutations. 226 n := 4 227 k := 3 228 gen := combin.NewPermutationGenerator(n, k) 229 idx := 0 230 for gen.Next() { 231 fmt.Println(idx, gen.Permutation(nil)) // can also store in-place. 232 idx++ 233 } 234 235 // Output: 236 // 0 [0 1 2] 237 // 1 [0 2 1] 238 // 2 [1 0 2] 239 // 3 [1 2 0] 240 // 4 [2 0 1] 241 // 5 [2 1 0] 242 // 6 [0 1 3] 243 // 7 [0 3 1] 244 // 8 [1 0 3] 245 // 9 [1 3 0] 246 // 10 [3 0 1] 247 // 11 [3 1 0] 248 // 12 [0 2 3] 249 // 13 [0 3 2] 250 // 14 [2 0 3] 251 // 15 [2 3 0] 252 // 16 [3 0 2] 253 // 17 [3 2 0] 254 // 18 [1 2 3] 255 // 19 [1 3 2] 256 // 20 [2 1 3] 257 // 21 [2 3 1] 258 // 22 [3 1 2] 259 // 23 [3 2 1] 260 } 261 262 func ExampleIndexToPermutation() { 263 // combin provides several ways to work with the permutations of 264 // different objects. IndexToPermutation allows random access into 265 // the permutation order. Combined with PermutationIndex this 266 // provides a correspondence between integers and permutations. 267 n := 4 268 k := 3 269 comb := make([]int, k) 270 for i := 0; i < combin.NumPermutations(n, k); i++ { 271 combin.IndexToPermutation(comb, i, n, k) // can also use nil. 272 idx := combin.PermutationIndex(comb, n, k) 273 fmt.Println(i, comb, idx) 274 } 275 276 // Output: 277 // 0 [0 1 2] 0 278 // 1 [0 2 1] 1 279 // 2 [1 0 2] 2 280 // 3 [1 2 0] 3 281 // 4 [2 0 1] 4 282 // 5 [2 1 0] 5 283 // 6 [0 1 3] 6 284 // 7 [0 3 1] 7 285 // 8 [1 0 3] 8 286 // 9 [1 3 0] 9 287 // 10 [3 0 1] 10 288 // 11 [3 1 0] 11 289 // 12 [0 2 3] 12 290 // 13 [0 3 2] 13 291 // 14 [2 0 3] 14 292 // 15 [2 3 0] 15 293 // 16 [3 0 2] 16 294 // 17 [3 2 0] 17 295 // 18 [1 2 3] 18 296 // 19 [1 3 2] 19 297 // 20 [2 1 3] 20 298 // 21 [2 3 1] 21 299 // 22 [3 1 2] 22 300 // 23 [3 2 1] 23 301 }