vector

package module
v0.0.0-...-a887ff7 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2017 License: Apache-2.0 Imports: 2 Imported by: 9

README

Vector

Build Status

Simple N-dimensional Vector math library. Uses standard []float64 type.

Quick Example
package main

import (
  "fmt"
  "github.com/atedja/go-vector"
)

func main() {
  v1 := vector.New(4)
  v2 := vector.NewWithValues([]float64{0.0, 1.0, 2.0, 3.0})

  result := vector.Add(v1, v2)
}
Full API Documentation
Basic Usage
Creating Vectors
var v vector.Vector
v = vector.New(3)
v = vector.NewWithValues([]float64{0.0, 1.0, 2.0})
Scale Vectors
v.Scale(2.0)
Dot and Cross Products
v1 := vector.NewWithValues([]float64{0.0, 1.0, 2.0})
v2 := vector.NewWithValues([]float64{2.0, -1.0, 4.0})
cross, _ := vector.Cross(v1, v2)
dot, _ := vector.Dot(v1, v2)

In general, all functions under vector package are designed to always return a new result without modifying the vectors in the arguments, while functions under the vector.Vector type will modify the vector.

And more!

Add, Subtract, Scale, Resize, Zero, Magnitude, Unit, Hadamard

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EPSILON = math.Nextafter(1, 2) - 1
View Source
var ErrVectorInvalidDimension = errors.New("Vectors' dimensions are not of the expected size.")
View Source
var ErrVectorNotSameSize = errors.New("Vectors are not the same size.")

Functions

func Dot

func Dot(v1, v2 Vector) (float64, error)

Dot products of two vectors.

Types

type Vector

type Vector []float64

func Add

func Add(v1, v2 Vector) Vector

Sums of two vectors, returns the resulting vector.

func Cross

func Cross(v1, v2 Vector) (Vector, error)

Cross products of two vectors. Vector dimensionality has to be 3.

func Hadamard

func Hadamard(v1, v2 Vector) (Vector, error)

func New

func New(size int) Vector

func NewWithValues

func NewWithValues(values []float64) Vector

func Subtract

func Subtract(v1, v2 Vector) Vector

Difference of two vectors, returns the resulting vector.

func Unit

func Unit(v Vector) Vector

func (Vector) Clone

func (self Vector) Clone() Vector

Clone this vector, returning a new Vector.

func (Vector) Do

func (self Vector) Do(applyFn func(float64) float64)

Iterates through the elements of this vector and for each element invokes the function.

func (Vector) DoWithIndex

func (self Vector) DoWithIndex(applyFn func(int, float64) float64)

Iterates through the elements of this vector and for each element invokes the function with index.

func (Vector) Magnitude

func (self Vector) Magnitude() float64

Returns the magnitude of this vector.

func (Vector) Scale

func (self Vector) Scale(value float64)

Scale this vector (performs scalar multiplication) by the specified value.

func (Vector) Set

func (self Vector) Set(values []float64)

Sets the values of this vector.

func (Vector) Zero

func (self Vector) Zero()

Zeroes this vector

Jump to

Keyboard shortcuts

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