Giter Club home page Giter Club logo

vector's Introduction

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.

vector's People

Contributors

quartercastle avatar jacksonfrankland avatar solarlune avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.