Documentation ¶
Overview ¶
Package alg stores useful algorithms and math functions
Index ¶
- Constants
- func ChooseX(weights []float64, n int) []int
- func CumulativeWeights(weights []float64) []float64
- func F64eq(f1, f2 float64) bool
- func F64eqEps(f1, f2, epsilon float64) bool
- func RemainingWeights(weights []float64) []float64
- func RoundF64(a float64) int
- func TriangulateConvex(face []int) [][3]int
- func UniqueChooseX(weights []float64, n int) []int
- func UniqueChooseXSeeded(weights []float64, n int, rng Float64Generator) []int
- func WeightedChooseOne(remainingWeights []float64) int
- func WeightedChooseOneSeeded(remainingWeights []float64, rng Float64Generator) int
- func WeightedMapChoice(weightMap map[int]float64) int
- func WeightedMapChoiceSeeded(weightMap map[int]float64, rng Float64Generator) int
- type Degree
- type Float64Generator
- type Radian
Constants ¶
const ( // DegToRad is the constant value something in // degrees should be multiplied by to obtain // something in radians. DegToRad = math.Pi / 180 // RadToDeg is the constant value something in // radians should be multiplied by to obtain // something in degrees. RadToDeg = 180 / math.Pi )
Variables ¶
This section is empty.
Functions ¶
func ChooseX ¶
ChooseX - also known as Roulette Search. This returns n indices from the input weights at a count relative to the weight of each index. It can return the same index multiple times.
func CumulativeWeights ¶
CumulativeWeights converts a slice of weights into a slice of cumulative weights, where each index is the sum of all weights up until that index in the original slice
func RemainingWeights ¶
RemainingWeights is equivalent to CumulativeWeights with the slice reversed, where the zeroth element will contain the total weight.
func TriangulateConvex ¶
TriangulateConvex takes a face, in the form of a slice of indices, and outputs those indicies split into triangles based on the assumption that the face is convex. This involves forming pairs of indices and drawing an edge back to the first index repeatedly.
If given less than 3 indices, returns an empty slice.
Example input: [0,1,2,3,4] Example output: [[0,1,2][0,2,3][0,3,4]]
Visual Example:
____0____ 4 1 \ / 3-----2 - - - ____0____ 4 / \ 1 \ / \ / 3-----2
This makes additional assumptions that the points represented by the indices are coplanar, and that there are no holes present in the face.
func UniqueChooseX ¶
UniqueChooseX returns n indices from the input weights at a count relative to the weight of each index. This will never return duplicate indices. if n > len(weights), it will return -1 after depleting the n elements from weights.
func UniqueChooseXSeeded ¶
func UniqueChooseXSeeded(weights []float64, n int, rng Float64Generator) []int
UniqueChooseXSeeded returns n indices from the input weights at a count relative to the weight of each index. This will never return duplicate indices. if n > len(weights), it will return -1 after depleting the n elements from weights. If you do not want to use your own rand use UniqueChooseX.
func WeightedChooseOne ¶
WeightedChooseOne returns a single index from the weights given at a rate relative to the magnitude of each weight. It expects the input to be in the form of RemainingWeights, cumulative with the total at index 0.
func WeightedChooseOneSeeded ¶
func WeightedChooseOneSeeded(remainingWeights []float64, rng Float64Generator) int
WeightedChooseOneSeeded returns a single index from the weights given at a rate relative to the magnitude of each weight. It expects the input to be in the form of RemainingWeights, cumulative with the total at index 0. If you do not want to use your own rand use WeightedChooseOne.
func WeightedMapChoice ¶
WeightedMapChoice converts the input map into a set where keys are indices and values are weights for WeightedChooseOne, then returns the key for WeightedChooseOne of the weights.
func WeightedMapChoiceSeeded ¶
func WeightedMapChoiceSeeded(weightMap map[int]float64, rng Float64Generator) int
WeightedMapChoiceSeeded converts the input map into a set where keys are indices and values are weights for WeightedChooseOne, then returns the key for WeightedChooseOne of the weight. If you do not want to use your own rand use WeightedMapChoice.
Types ¶
type Float64Generator ¶
type Float64Generator interface {
Float64() float64
}
A Float64Generator must be able to generate a float64. This is generally used to implement randomness See rand.Float64 for an example