You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Likely the most comprehensive vector library for TypeScript / JavaScript
currently available.
This package provides ~720 largely code generated functions and
supporting types to perform vector operations on fixed and
arbitrary-length vectors, both packed and strided (i.e. where individual
vector components are not successive array elements, for example in SOA
memory layouts).
Includes componentwise logic operations for boolean vectors,
componentwise comparisons for numeric vectors and componentwise binary
ops for signed & unsigned integer vectors.
Features
Small & fast: The vast majority of functions are code generated with
fixed-sized versions not using any loops. Minified + gzipped, the
entire package is ~10.1KB (though you'll hardly ever use all functions).
Unified API: Any ArrayLike type can be used as vector containers
(e.g. JS arrays, typed arrays, custom impls). Most functions are
implemented as multi-methods, dispatching to any potentially optimized
versions based on given vector arguments.
Highly modular: Each function is defined in its own submodule / file.
In addition to each generic multi-method base function, all
fixed-length optimized versions are exported too. E.g. If
add
performs vector addition on arbitrary-length vectors, add2, add3,
add4 are the optimized version for fixed-length vectors...
Extensible: Custom vector ops can be defined in a similar manner using
the provided code generation helpers (see
vop.ts
and
codegen.ts
for details).
Immutable by default: Each operation producing a vector result takes
an output vector as first argument. If null, the vector given as 2nd
argument will be used as output (i.e. for mutation).
Strided vector support is handled via the lightweight
Vec2/3/4
class wrappers and the
gvec()
proxy (for generic, arbitrary-length vectors). These types behave like
normal arrays (for read/write operations) and are also iterable. A
subset of functions (suffixed with S, e.g.
addS
vs. add) also support striding without the need for extra class
wrappers. This is handled via additional index and stride arguments
for each input/output vector. These functions are only available for
sizes 2 / 3 / 4, though.
Random vector functions support the IRandom interface defined by
@thi.ng/random
to work with custom (P)RNGs. If omitted, the built-in Math.random()
will be used.
Related packages
@thi.ng/color - vector based color operations / conversions
import*asvfrom"@thi.ng/vectors";// immutable vector addition (1st arg is result)v.add([],[1,2,3,4],[10,20,30,40]);// [11, 22, 33, 44]// mutable addition// (if first arg (output) is null writes result to 2nd arg)a=[1,2,3];v.add(null,a,a);// [2, 4, 6]// multiply-add (o = a * b + c)v.madd([],[10,20],[0.5,0.25],[1,2]);// [6, 7]// multiply-add w/ scalar (o = a * n + b)v.maddN([],[10,20],0.5,[1,2]);// [6, 12]// scalar addition w/ arbitrary length & strided vectorv.addN([],gvec([0,1,0,0,0,2,0,0,0,3,0,0,0],3,1,4),10);// [11, 12, 13]// or operate on raw arrays directly...// here the last 4 args define:// out index, src index, out stride, src stridev.addNS3(null,[0,1,0,0,0,2,0,0,0,3,0,0,0],10,1,1,4,4)// [0, 11, 0, 0, 0, 12, 0, 0, 0, 13, 0, 0, 0]v.dist([1,2],[100,200]);// 221.37072977247917v.distManhattan([1,2],[100,200]);// 297v.distChebyshev([1,2],[100,200]);// 198v.mixN([],[1,2],[10,20],0.5);// [5.5, 11]v.fromHomogeneous([],[100,200,0.5]);// [200, 400]v.swizzle4([],[1,2],1,1,0,0);// [ 2, 2, 1, 1 ]v.hash([1,2,3])// 2383338936
API
Breaking changes in v3.0.0
to avoid confusion, the arg order of madd and maddN functions have
been updated to be compatible with the OpenCL mad function and to
generally follow the expanded name, i.e. multiply-add:
madd([], a, b, c): before a + b * c, now: a * b + c
maddN([], a, b, n) => maddN([], a, n, b) (i.e. a * n + b)