You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"typevec= vector.Vectorfuncmain() {
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 aliasingtypevec= vector.Vector// addition of two vectorsresult:=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 vectorv:=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 vectorsv1, 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 vectorvector.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.