https://github.com/spikeburton/vector-js
⚙️ An open source implementation of mathematical vectors in JavaScript
https://github.com/spikeburton/vector-js
javascript mathematical-vectors vector vector-space
Last synced: about 2 months ago
JSON representation
⚙️ An open source implementation of mathematical vectors in JavaScript
- Host: GitHub
- URL: https://github.com/spikeburton/vector-js
- Owner: spikeburton
- License: mit
- Created: 2019-01-24T02:20:46.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T08:46:42.000Z (over 2 years ago)
- Last Synced: 2025-04-07T12:05:19.430Z (about 2 months ago)
- Topics: javascript, mathematical-vectors, vector, vector-space
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@glazier/vector-js
- Size: 877 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# VectorJS
[](https://travis-ci.org/spikeburton/vector-js)
[](https://www.npmjs.com/package/@glazier/vector-js)
[](https://github.com/spikeburton/vector-js/blob/master/LICENSE.md)
[](https://github.com/spikeburton/vector-js/issues)
[](https://github.com/spikeburton/vector-js/issues?q=is%3Aissue+is%3Aclosed)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
```sh
npm i @glazier/vector-js
```## Usage
```js
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