combin

package
v0.0.0-...-9cb6ac2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2018 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Binomial

func Binomial(n, k int) int

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.

func Cartesian

func Cartesian(dst *mat.Dense, data [][]float64) *mat.Dense

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.

func Combinations

func Combinations(n, k int) [][]int

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.

func GeneralizedBinomial

func GeneralizedBinomial(n, k float64) float64

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.

func IdxFor

func IdxFor(sub, dims []int) int

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.

func LogGeneralizedBinomial

func LogGeneralizedBinomial(n, k float64) float64

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

func SubFor

func SubFor(sub []int, idx int, dims []int) []int

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.

Types

type CombinationGenerator

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

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

func NewCombinationGenerator

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 (*CombinationGenerator) Combination

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

Combination generates the next 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 (*CombinationGenerator) 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL