Git Product home page Git Product logo

vector-js's Introduction

VectorJS

Build Status npm (scoped) GitHub GitHub issues GitHub closed issues

An implementation of mathematical vectors in JavaScript. The vector space is n-dimensional, with support for cross product and tension vectors.

A vector is defined here as a set of ordered coordinates in the vector space of cardinality n which has both magnitude and direction. VectorJS implements functionality to represent a vector as a 1-dimensional array containing n elements as input for each method.

Install

npm i @glazier/vector-js

Usage

const { Vector, TensionVector } = require('@glazier/vector-js');

const v1 = new Vector(1, 2, 3);
const v2 = new Vector(2, 4, 6);

// Adding two vectors
// OUTPUT: (3, 6, 9)
console.log(v1.add(v2).toString());

// Using an array of coordinates
// OUTPUT: (3, 6, 9)
console.log(v1.add([2, 4, 6]).toString());

// Scalar multiplication
// OUTPUT: (4, 8, 12)
console.log(v1.mul(4).toString());

// Dot product
// OUTPUT: 28
console.log(v1.dot(v2).toString());

// Cross product
// OUTPUT: (0, 0, 0)
console.log(v1.cross(v2).toString());

// Unit vector
// NOTE: Normalization is non-destructive and returns a new Vector object
// OUTPUT: (0.2672612419124244, 0.1336306209562122, 0.0890870806374748)
console.log(v1.normalize().toString());

// Vector magnitude
// OUTPUT: 3.7416573867739413
console.log(v1.length)

// OUTPUT: [ 1, 2, 3 ]
console.log(v1.coords);

// Convert to an array
// OUTPUT: [ 1, 2, 3 ]
console.log(v1.toArray());

// OUTPUT: [ 1, 2, 13 ]
v1.setAxis(2, 13);
console.log(v1.coords);

// OUTPUT: 13
console.log(v1.getAxis(2));

Release Notes

  • 1.2.0:
    • New and improved algorithms for vector operations designed to be more efficient
    • Addition of combine and scale methods which are designed to be flexible by taking a callback to operate on a vector
    • Improved error handling for all operations
    • Improved checking to ensure scalars must be finite and non-empty
  • 1.1.1:
    • Improved documentation
    • Added Travis CI and Jest for testing
    • Refactored iterators in vector operations to not use reduce

🚧🚧🚧

TODO:

  • Add Babel for backwards compatibility
  • Add Rollup for build minification
  • Add ESLint integration
  • Improve documentation and add examples

vector-js's People

Contributors

spikeburton avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

vector-js's Issues

Subtraction and division are broken

const v1 = new Vector(1, 2, 3);console.log(`Subtraction: ${v1.sub([1, 2, 3]).toString()}`);
console.log(`Division: ${v1.div(2).toString()}`);

Output

Subtraction: (-2, -4, -6)
Division: (0.5, 0.25, 0.16666666666666666)

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.