mathx

package
v0.0.0-...-8d852e1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2022 License: MIT Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSimilarity

func CosineSimilarity(v1, v2 []float64) (float64, bool)

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

func EuclideanDistance(v1, v2 []float64) (float64, bool)

EuclideanDistance finds the Euclidean distance between two vectors. Returns false if:

len(v1) != len(v2)

func RoundF64

func RoundF64(f float64, decimals int) float64

RoundF64 rounds a float64 to the specified amount of decimals. Rounds to the closest num, so no ceil or floor.

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

func NewSafeVec(elements ...float64) *SafeVec

NewSafeVec is a constructor for SafeVec, which is initialized with the given elements.

func NewSafeVecRand

func NewSafeVecRand(dim int) (*SafeVec, bool)

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) Clone

func (v *SafeVec) Clone() *SafeVec

Clone returns a clone of the type.

func (*SafeVec) CosineSimilarity

func (v *SafeVec) CosineSimilarity(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.

func (*SafeVec) Dim

func (v *SafeVec) Dim() int

Dim exposes the dimension of the underlying vector.

func (*SafeVec) Eq

func (v *SafeVec) Eq(other *SafeVec) bool

Eq does an equality check with the other SafeVec.

func (*SafeVec) EuclideanDistance

func (v *SafeVec) EuclideanDistance(other Distancer) (float64, bool)

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

func (v *SafeVec) In(others []*SafeVec) bool

In checks if this SafeVec is contained in a given slice. Equality checks are done with SafeVec.Eq(...), so not particularly fast.

func (*SafeVec) Iter

func (v *SafeVec) Iter(f func(int, float64) bool)

Iter allows a safe read-only iteration of the underlying vector. Accepts a func which receives the index and value (i.e a range loop) of each element -- this func can return false to stop the itaration.

func (*SafeVec) Norm

func (v *SafeVec) Norm() float64

Norm is the norm of the internal vector.

func (*SafeVec) Peek

func (v *SafeVec) Peek(index int) (float64, bool)

Peek returns the element of the underlying []float64 at a given index. Will return false if the index is out-of-bounds.

Jump to

Keyboard shortcuts

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