vec

package
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2016 License: MIT Imports: 2 Imported by: 0

README

vec

-- import "github.com/NDari/gocrunch/vec"

Package vec implements a large set of functions which act on one and one dimensional slices of float64. For functions that act on [][]float64, look at the gocrunch/mat package.

Many of the functions in this library expect either a float64 or a []float64, and do "the right thing" based on what is passed. For example, consider the function:

vec.Mul(m, n)

In this function, m is a []float64, where as n could be a float64, or a []float64. This allows the same function to be called for wide range of situations. This trades compile time safety for runtime errors. We believe that Go's fast compile time, along with the verbose errors in this package make up for that, however.

All errors encountered in this package, such as attempting to access an element out of bounds are treated as critical error, and thus, the code immediately panics. In such cases, the function in which the error was encountered is printed to the screen along with the reason for the panic, in addition to the full stack trace, in order to help fix any issues rapidly.

As mentioned, all the functions in this library act on Go primitive types, which allows the code to be easily modified to serve in different situations.

Usage

Create a []float64, then set all elements to 11.0:

v := make([]float64, 10)
vec.Set(v, 11.0)

Check if all elements of v are set to 11.0:

isEleven := func(i float64) float64 {
    if i == 11.0 {
        return true
    }
    return false
}

if !vec.All(m, isEleven) {
    log.Fatal("Non-11 values found!")
}

make a copy and check if they are equal:

v2 := make([]float64, 10)
copy(v1, v) // builtin copy function
if vec.Equal(v2, v) {
    fmt.Println("yay")
}

Add 2.0 to all elements of v:

vec.Add(v, 2.0)

subtract v1 from v, which will make all the elements of v to be equal to 2.0:

vec.Sub(v, v1)

Documentation

Full documentation is at godoc.org GoDoc

Badges

Documentation

Overview

Package vec implements a large set of functions which act on one dimensional slices of float64.

Many of the functions in this library expect either a float64 or a []float64, and do "the right thing" based on what is passed. For example,consider the function

vec.Mul(m, n)

In this function, m is a []float64, where as n could be a float64 or a []float64. This allows the same function to be called for wide range of situations. This trades compile time safety for runtime errors. We believe that Go's fast compile time, along with the verbose errors in this package make up for that, however.

All errors encountered in this package, such as attempting to access an element out of bounds are treated as critical error, and thus, the code immediately panics. In such cases, the function in which the error was encountered is printed to the screen along with the reason for the panic, in addition to the full stack trace, in order to help fix any issues rapidly.

As mentioned, all the functions in this library act on Go primitive types, which allows the code to be easily modified to serve in different situations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(v []float64, val interface{}) []float64

Add takes a []float64, and a second argument, which can be a float64 or a []float64, and applies the addition operation on each element, storing the result in the first []float64. for clarification, consider the case where the second argument is a float64:

val := 10
v := []float64{1.0, 2.0, 3.0}
v := vec.Add(v, val) // v is {11.0, 12.0, 13.0}

The second argument can also we a []float64, as below:

v := []float64{1.0, 2.0, 3.0}
w := []float64{3.0, 2.0, 4.0}
q := vec.Add(v, w) // q is {4.0, 4.0, 7.0}

The original arguments are not modified in this function. In the case where the second argument is a []float64, the length of both arguments must be equal.

func All

func All(v []float64, f func(float64) bool) bool

All checks to see if all elements of a []float64 return true for a passed function. If not, All() returns false. Consider:

negative := func(i float64) bool {
	if i < 0.0 {
		return true
	}
	return false
}
v := make([]float64, 10)
v = vec.Set(v, -12.0)
allNegatives := vec.All(v, negative) // true

To check if any element of a []float64 pass a certain function, look at vec.Any().

func Any

func Any(v []float64, f func(float64) bool) bool

Any checks to see if any element of a []float64 returns true for a passed function. If no elements return true, then Any() will return false. Consider:

negative := func(i float64) bool {
	if i < 0.0 {
		return true
	}
	return false
}
v := make([]float64, 10)
v = vec.Set(v, 12.0)
anyNegatives := vec.Any(v, negative) // false

To check if all elements of a []float64 pass a certain function, look at vec.All().

func Avg

func Avg(v []float64) float64

Avg returns the average value of a []float64. Consider

v := []float64{ 1.0, 2.0, 3.0 }
s := vec.Avg(v) // 2.0

This function does not alter the original []float64.

func Clone

func Clone(v []float64) []float64

Clone replicated the passed []slice. The returned slice is a copy of original, both in terms of the length and the value of the elements at each index. The returned copy is "deep", and manupilating it does not effect the original slice.

func Cut

func Cut(v []float64, args ...int) []float64

Cut removes a range of entries from a []float64. This function can be used in one of two ways. First is providing a single int, such as:

vec.Cut(v, x)

which means that all elements of v whose index is x or larger will be dropped. The second method of using this function is as:

vec.Cut(v, 2, 4)

which means that the second and 3rd elements of v are dropped. The passed slice is mutated in this function.

func Div

func Div(v []float64, val interface{}) []float64

Div takes a []float64, and a second argument, which can be a float64 or a []float64, and applies the division operation on each element, storing the result in the first []float64. for clarification, consider the case where the second argument is a float64:

val := 1.0
v := []float64{1.0, 2.0, 3.0}
v = vec.Div(v, val) // v is now {1.0, 2.0, 3.0}

The second argument can also we a []float64, as below:

v := []float64{1.0, 2.0, 3.0}
w := []float64{3.0, 2.0, 4.0}
q := vec.Div(v, w) // q is now {0.33, 1.0, 0.75}

The original arguments are not modified in this function. when the first argument is a float64, it cannot be 0.0. as division by zero is not allowed.

In the case where the second argument is a []float64, the length of both arguments must be equal. Additionally, the second argument must not contain any elements whose value is 0.0.

func Dot

func Dot(v1, v2 []float64) float64

Dot returns the sum of the element-wise multiplication of two []float64s passed to it. The passed slices are not altered in this function.

func Equal

func Equal(v, w []float64) bool

Equal checks if two []float64s are equal, by checking that they have the same length, and the same entries in each index.

func Foreach

func Foreach(v []float64, f func(float64) float64) []float64

Foreach applies a function to each element of a []float64, storing the result in a new []float64 which is returned. Consider:

double := func(i float64) float64 {
	return i * i
}
v := []float64{1.0, 2.0, 3.0}
c := vec.Foreach(v, double) // c is {1.0, 4.0, 9.0}

Thus the original []float64 is not modified in this function.

func Mul

func Mul(v []float64, val interface{}) []float64

Mul takes a []float64, and a second argument, which can be a float64 or a []float64, and applies the multiplication operation on each element, storing the result in the first []float64. for clarification, consider the case where the second argument is a float64:

val := 10
v := []float64{1.0, 2.0, 3.0}
v := vec.Mul(v, val) // v is now {10.0, 20.0, 30.0}

The second argument can also we a []float64, as below:

v := []float64{1.0, 2.0, 3.0}
w := []float64{3.0, 2.0, 4.0}
q := vec.Mul(v, w) // q is now {3.0, 4.0, 12.0}

The original arguments are not modified in this function. In the case where the second argument is a []float64, the length of both arguments must be equal.

func Pop

func Pop(v []float64) (float64, []float64)

Pop takes a []float64, and "pops" the last entry, returning it along with the modified []float64. The other elements of the []float64 remain intact. For example:

v := []float64{1.0, 2.0, 3.0}
x, v := vec.Pop(v) // x is 3.0, v is [1.0, 2.0]

This function mutates the passed []float64, and panics if the length of the slice is 0 since it cannot be "popped".

func Prod

func Prod(v []float64) float64

Prod multiplies all elements in a []float64. Consider

v := []float64{ 2.0, 2.0, 2.0 }
s := vec.Prod(v) // 8.0

This function does not alter the original []float64.

func Push

func Push(v []float64, x float64) []float64

Push appends a float64 to the end of a []float64, returning the modified []float64. The returned []float64 is one element longer, and the other elements remain intact. For example:

v := []float64{1.0, 2.0, 3.0}
v = vec.Push(v, 4.0) // v is [1.0, 2.0, 3.0, 4.0]

This function alters the passed []float64.

func Set

func Set(v []float64, val float64) []float64

Set returns a copy of the passed []float64 where all of the elements are set to the passed float64 in the second argument.

The original []float64 is not mutated in this function.

func Shift

func Shift(v []float64) (float64, []float64)

Shift removes the first element of a []float64, returning it along with the modified []float64. All the other elements in the []float64 remain intact, however their order is changed (the second element is now the first, etc). For example:

v := []float64{1.0, 2.0, 3.0}
x, v := vec.Shift(v) // x is 1.0, v is [2.0, 3.0]

This function alters the passed []float64, and panics if the length of the slice is 0 since it cannot be "shifted".

func Sub

func Sub(v []float64, val interface{}) []float64

Sub takes a []float64, and a second argument, which can be a float64 or a []float64, and applies the subtraction operation on each element, storing the result in the first []float64. for clarification, consider the case where the second argument is a float64:

val := 10
v := []float64{1.0, 2.0, 3.0}
v = vec.Sub(v, val) // v is now {-9.0, -8.0, -7.0}

The second argument can also we a []float64, as below:

v := []float64{1.0, 2.0, 5.0}
w := []float64{3.0, 2.0, 4.0}
q := vec.Sub(v, w) // q is now {-2.0, 0.0, 1.0}

The original arguments are not modified in this function. In the case where the second argument is a []float64, the length of both arguments must be equal.

func Sum

func Sum(v []float64) float64

Sum adds all elements in a []float64. Consider:

v := []float64{ 1.0, 2.0, 3.0 }
s := vec.Sum(v) // 6.0

This function does not alter the original []float64.

func To2D

func To2D(v []float64, stride int) [][]float64

To2D converts a []float64 to a [][]float64, using a passed stride. The values of the entries in the []float64 are put into the [][]float64 row by row. For example:

v := []float64{0.0, 1.0, 2.0, 3.0}
m := vec.To2D(v, 2) // m is [[0.0, 1.0], [2.0, 3.0]]

The original []float64 is not mutated in this function. The length of the []float64 must be exactly divisible by the passed stride, otherwise this function will panic.

func Unshift

func Unshift(v []float64, x float64) []float64

Unshift appends a float64 to the beginning of a []float64, returning the modified []float64. The elements in the original []float64 remain intact, however their order is now changed (the first element is now the second, etc.) For example:

v := []float64{1.0, 2.0, 3.0}
v = vec.Shift(v, 10.0) // v is [10.0, 1.0, 2.0, 3.0]

The passed slice is mutated in this function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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