`import "gonum.org/v1/gonum/stat/combin"`

Package combin implements routines involving combinatorics (permutations, combinations, etc.).

- func Binomial(n, k int) int
- func Cartesian(dst *mat.Dense, data [][]float64) *mat.Dense
- func Combinations(n, k int) [][]int
- func GeneralizedBinomial(n, k float64) float64
- func IdxFor(sub, dims []int) int
- func LogGeneralizedBinomial(n, k float64) float64
- func SubFor(sub []int, idx int, dims []int) []int
- type CombinationGenerator

Binomial returns the binomial coefficient of (n,k), also commonly referred to as "n choose k".

The binomial coefficient, C(n,k), is the number of unordered combinations of k elements in a set that is n elements big, and is defined as

C(n,k) = n!/((n-k)!k!)

n and k must be non-negative with n >= k, otherwise Binomial will panic. No check is made for overflow.

Cartesian returns the cartesian product of the slices in data. The Cartesian product of two sets is the set of all combinations of the items. For example, given the input

[][]float64{{1,2},{3,4},{5,6}}

the returned matrix will be

[ 1 3 5 ] [ 1 3 6 ] [ 1 4 5 ] [ 1 4 6 ] [ 2 3 5 ] [ 2 3 6 ] [ 2 4 5 ] [ 2 4 6 ]

If dst is nil, a new matrix will be allocated and returned, otherwise the number of rows of dst must equal \prod_i len(data[i]), and the number of columns in dst must equal len(data). Cartesian also panics if len(data) = 0.

Combinations generates all of the combinations of k elements from a set of size n. The returned slice has length Binomial(n,k) and each inner slice has length k.

n and k must be non-negative with n >= k, otherwise Combinations will panic.

CombinationGenerator may alternatively be used to generate the combinations iteratively instead of collectively.

Code:

data := []string{"a", "b", "c", "d", "e"} cs := combin.Combinations(len(data), 2) for _, c := range cs { fmt.Printf("%s%s\n", data[c[0]], data[c[1]]) }

Output:

ab ac ad ae bc bd be cd ce de

GeneralizedBinomial returns the generalized binomial coefficient of (n, k), defined as

Γ(n+1) / (Γ(k+1) Γ(n-k+1))

where Γ is the Gamma function. GeneralizedBinomial is useful for continuous relaxations of the binomial coefficient, or when the binomial coefficient value may overflow int. In the latter case, one may use math/big for an exact computation.

n and k must be non-negative with n >= k, otherwise GeneralizedBinomial will panic.

IdxFor converts a multi-dimensional index into a linear index for a multi-dimensional space. sub specifies the index for each dimension, and dims specifies the size of each dimension. IdxFor is the inverse of SubFor. IdxFor panics if any of the entries of sub are negative, any of the entries of dim are non-positive, or if sub[i] >= dims[i] for any i.

LogGeneralizedBinomial returns the log of the generalized binomial coefficient. See GeneralizedBinomial for more information.

SubFor returns the multi-dimensional subscript for the input linear index to the multi-dimensional space. dims specifies the size of each dimension, and idx specifies the linear index. SubFor is the inverse of IdxFor.

If sub is non-nil the result is stored in-place into sub, and SubFor will panic if len(sub) != len(dims). If sub is nil a new slice of the appropriate length is allocated. SubFor panics if idx < 0 or if idx is greater than or equal to the product of the dimensions.

❖

```
type CombinationGenerator struct {
// contains filtered or unexported fields
}
```

CombinationGenerator generates combinations iteratively. Combinations may be called to generate all combinations collectively.

❖

func NewCombinationGenerator(n, k int) *CombinationGenerator

NewCombinationGenerator returns a CombinationGenerator for generating the combinations of k elements from a set of size n.

n and k must be non-negative with n >= k, otherwise NewCombinationGenerator will panic.

❖

func (c *CombinationGenerator) Combination(combination []int) []int

Combination generates the current combination. If next is non-nil, it must have length k and the result will be stored in-place into combination. If combination is nil a new slice will be allocated and returned. If all of the combinations have already been constructed (Next() returns false), Combination will panic.

Next must be called to initialize the first value before calling Combination or Combination will panic. The value returned by Combination is only changed during calls to Next.

❖

func (c *CombinationGenerator) Next() bool

Next advances the iterator if there are combinations remaining to be generated, and returns false if all combinations have been generated. Next must be called to initialize the first value before calling Combination or Combination will panic. The value returned by Combination is only changed during calls to Next.

Package combin imports 2 packages (graph) and is imported by 2 packages. Updated 2019-07-23. Refresh now. Tools for package owners.