Documentation ¶
Overview ¶
mathx is an addition to the std math pkg. It contains some helper methods but primarily has distance funcs and a type representing a numeric vec which wraps a []float64 in a read-only way such that it is thread-safe.
Index ¶
- func CosineSimilarity(v1, v2 []float64) (float64, bool)
- func EuclideanDistance(v1, v2 []float64) (float64, bool)
- func RoundF64(f float64, decimals int) float64
- type Distancer
- type SafeVec
- func (v *SafeVec) Clone() *SafeVec
- func (v *SafeVec) CosineSimilarity(other Distancer) (float64, bool)
- func (v *SafeVec) Dim() int
- func (v *SafeVec) Eq(other *SafeVec) bool
- func (v *SafeVec) EuclideanDistance(other Distancer) (float64, bool)
- func (v *SafeVec) In(others []*SafeVec) bool
- func (v *SafeVec) Iter(f func(int, float64) bool)
- func (v *SafeVec) Norm() float64
- func (v *SafeVec) Peek(index int) (float64, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CosineSimilarity ¶
CosineSimilarity finds the cosine similarity of two vectors. Returns false on two conditions, if:
(A): len(v1) != len(v2) (B): One of the vectors is a zero vector.
func EuclideanDistance ¶
EuclideanDistance finds the Euclidean distance between two vectors. Returns false if:
len(v1) != len(v2)
Types ¶
type Distancer ¶
type Distancer interface { // EuclideanDistance computes the Euclidean distance to another vec that // implements the Distancer interface (this pkg). // False condition if: // neq dimension for the two vecs. EuclideanDistance(other Distancer) (float64, bool) // CosineSimilarity finds the cosine similarity between this vector and the // other. Returns false on two conditions, if; // (A): neq dimensions. // (B): one of the vectors is a zero vector. CosineSimilarity(other Distancer) (float64, bool) // Peek attempts to return an element of an underlying vector at // the given index. False return signals out-of-bounds. Peek(index int) (float64, bool) // Dim is intended to return the dimension of an underlying vector. Dim() int // Norm is the norm of the internal vector. Norm() float64 }
Distancer is an interface for types that can caluclate some select distance functions such as Euclidean or Cosine. This is meant to be used with some underlying vector such as []float64 or variants.
type SafeVec ¶
type SafeVec struct {
// contains filtered or unexported fields
}
SafeVec is a read-only wrapper around a []float64; the intent is for it to be safe to pass around in a highly concurrent context. Note 1; it implements the 'Distancer' interface in this pkg. Note 2; no locking as it is read-only.
func NewSafeVec ¶
NewSafeVec is a constructor for SafeVec, which is initialized with the given elements.
func NewSafeVecRand ¶
NewSafeVecRand is a constructor for SafeVec, which is initialized with a specified dimention and elements in rand range [0,1]. Returns nil and false if dim < 0.
func (*SafeVec) CosineSimilarity ¶
CosineSimilarity finds the cosine similarity between this vector and the other. Returns false on two conditions, if;
(A): neq dimensions. (B): one of the vectors is a zero vector.
func (*SafeVec) EuclideanDistance ¶
EuclideanDistance computes the Euclidean distance to another vec that implements the Distancer interface (this pkg). False condition if:
neq dimension for the two vecs.
func (*SafeVec) In ¶
In checks if this SafeVec is contained in a given slice. Equality checks are done with SafeVec.Eq(...), so not particularly fast.