Code Monkey home page Code Monkey logo

vector's Introduction

Vector

A very simple math vector library that does 0 memory allocation, allowing you to reuse objects.

Simply specify an out *Vector and it will store the result there. I have found this to be espescially useful in iterative integration, where I can store the results of math operations in a variable declared outside of the for loop to make allocations O(1) instead of O(n)

With other libraries

O(n) allocations; slow.

counter := &Vector2f{.1, .1}
for i := 0; i < 200; i++ {
  counter = counter.Add(counter)
}

Ignore that this code can be simplified, this is just demonstrating that the Add operation returns a new object, hence an allocation.

With this library

O(1) allocations; fast.

counter := &Vector2f{.1, .1}
for i := 0; i < 200; i++ {
    // Add2f(v1, v2, out *Vector2f)
    vector.Add2f(counter, counter, counter)
}

This writes the result to the out vector directly, not creating any new object. The allocation is up to the user, which can reuse objects to make code faster, and possibly even cache friendly.

Structures and functions

Currently only Vector2f and Vector3f are implemented.

For brevity the 2f and 3f will be replaced with Xf

// out = v1 + v2
func AddXf(v1, v2, out *VectorXf)

// out = v1 - v2
func SubXf(v1, v2 out *VectorXf)

// out = v * k
func MulXf(v *VectorXf, k float64, out *VectorXf)

// out = v / k
func DivXf(v *VectorXf, k float64, out *VectorXf)

// return ||v||
func (v *VectorXf) Mag() float64

// return "<x, y[, z]>|mag|"
func (v *VectorXf) String() string

Possible improvements

Implementing SIMD is not something this library is aiming to do. This library is solely to allow the user to manage memory as allocations hog down calculations, espescially in tight loops.

  • Cross and dot multiplication
  • Unit tests
  • More types with more dimensions? This is up for discussion if it is necessary and at which point there is enough.

Pull requests and contributions welcome

vector's People

Contributors

matthewsanetra avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.