vectors

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package vectors provides basic vector operations.

Example 1

Summing vectors and calculating distances:

a, b, c, d := {some vectors}

Giving a nil result vector to Sum() allocates a new result, and thus does not change a, b, c or d.

dist := L2(Sum(nil, a, b), Sum(nil, c, d))

Giving a non-nil result vector to Sum() changes it. The following call will give the same distance but will modify a and c.

dist := L2(Sum(a, b), Sum(c, d))

Example 2

Creating a vector of -1's of length 10:

v := Mul(Ones(10), -1)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a []float64, b ...[]float64) []float64

Add adds b to a and returns a. b is unchanged. If a is nil, creates a new vector.

func Copy

func Copy(a []float64) []float64

Copy returns a copy of the given vector.

func Dot

func Dot(a, b []float64) float64

Dot returns the dot product of the input vectors.

func L1

func L1(a, b []float64) float64

L1 (Manhattan) distance. Equivalent to Lp(1).

func L2

func L2(a, b []float64) float64

L2 (Euclidean) distance. Equivalent to Lp(2).

func Lp

func Lp(p int) func([]float64, []float64) float64

Lp returns an Lp distance function. Lp is calculated as follows:

Lp(v) = the p'th root of sum_i(v[i]^p)

For convenience, L1 (Manhattan)and L2 (Euclidean) are prepared package variables.

func Mul

func Mul(a []float64, m float64) []float64

Mul multiplies the values of a by m and returns a.

func Norm

func Norm(a []float64) float64

Norm returns the norm of the vector, in L2.

func Ones

func Ones(n int) []float64

Ones returns a slice of ones of length n. Panics if n is negative.

func Sub

func Sub(a []float64, b ...[]float64) []float64

Sub subtracts b from a and returns a. b is unchanged. If a is nil, creates a new vector.

Types

This section is empty.

Jump to

Keyboard shortcuts

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