Skip to content

quartercastle/vector

Repository files navigation

vector

Version GoDoc Go Report Card

The motivation behind this package is to find a better way to write vector math in Golang, there has to be a more expressive way without it getting to verbose.

Install

go get github.com/quartercastle/vector

Usage

package main

import "github.com/quartercastle/vector"

type vec = vector.Vector

func main() {
  a, b := vec{1, 2}, vec{3, 4}
  c := a.Add(b)
}

Tackling verbosity

Another goal of this experiment is to minimize the verbosity around using the package, this can be achieved by using type aliasing. In this way you can omit the package identifier and give the Vector a shorter name like vec or something else, it is up to you.

// Minimize the verbosity by using type aliasing
type vec = vector.Vector

// addition of two vectors
result := vec{1, 2}.Add(vec{2, 4})

A nice side effect of representing a vector as a list of float64 values is that a slice of float64 values can easily be turned into a vector by using type casting. This elimitates the need for any constructor functions for the vector type.

// Turn a list of floats into a vector
v := vec([]float64{1, 2, 3})

Mutability vs Immutability

All arithmetic operations are immutable by default. But if needed a Vector can be turned into a MutableVector with the vector.In function, see example below. A mutable vector performs arithemetic operations much faster without taking up any memory.

// create vectors
v1, v2 := vec{1, 2}, vec{2, 4}

// Immutable addition, will return a new vector containing the result.
result := v1.Add(v2)

// Mutable addition, will do the calculation in place in the v1 vector
vector.In(v1).Add(v2)

Slicing a vector

Another benefit of using a list of float64 to represent a vector is that you can slice vectors as you normally would slice lists in go.

v1 := vec{1, 2, 3}
v2 := v1[1:] // returns a new vec{2, 3}

Documentation

The full documentation of the package can be found on godoc.

Contributions

Contributions with common vector operations that are not included in this package are welcome.

Credits

Thanks to gonum for inspiration and the following functions axpyUnitaryTo, scalUnitaryTo that enhances the performance of arithmetic operations in this package.

License

This project is licensed under the MIT License and includes gonum code that is licensed under 3-Clause BSD license.