vector-tools v0.1.0
vector-tools
A set of pure functions that perform common calculations on JavaScript TypedArrays.
Warning: This library is not production-ready.
This is not a particularly complete or optimised library, I just made it for my personal use as I was tired of writing the same for loops over and over. But it's super lightweight and ES6 ready so someone may enjoy it.
Installation
node
npm install vector-toolsweb
Include dist/web/vector-tools.js in a <script> tag in your HTML head.
Usage
- in node, you can import specific functions from the library:
import { vscale } from 'vector-tools';- or import the entire library:
import * as VectorTools from 'vector-tools';- in the Web, you get the
VectorToolsobject on the window.
API
Functions starting with v always return a TypedArray vector. Return types of other functions and utilities are indicated below.
Number sum(a)
calculates the sum of components in vector a
Number dot(a, b)
calculates the dot product of a and b
Array<TypedArray> segment(a, seq_len)
segments the vector into ceil(a/seq_len) segments of length seq_len. The last segment may be smaller than seq_len. Returns a list of segments.
vsum(a, b)
returns component-wise sum vector of a and b.
vadd(a, x)
returns a copy of a with components increased by x.
vscale(a, x)
returns a copy of a with components scaled (multiplied) by x.
vconcat(a, b)
returns a concatenated vector of a and b.
vnormalize(a[, scale])
returns a normalized copy of a, optionally scaled by scale.
utils
Array typed_to_array(a)– convertsTypedArraytoArrayTypedArray array_to_typed(a, type)– convertsArraytoTypedArrayof typetype.Number randint(range)- returns a random integer between 0 andrange.TypedArrayConstructor get_type(a)– returns the constructor (and therefore type) of the passedTypedArray. If argument is notTypedArray, throwsTypeError.
Development
cd vector-tools
npm installTests are written with Jasmine, run with npm test.
To build for node, run npm run node-compile. For web (webpack), npm run web-compile. For everything, npm run build.
9 years ago