Zero dependency JavaScript
library for performing fommon vector calculations. The library can handle 2D/3D vectors.
[!NOTE]
The library was originally written back in 2018, but has since been re-written to make use of an ES6 class instead of separate functions. The original functions from v1.0.1
have been included in the re-write to maintain backward compatibility, but they will be deprecated in a future release.
The package is very simple to use and ll vector operations are done using the Vector
class, which will be detailed here.
npm i vector-math
The packace provides the following vector properties and operations:
- Modulus (class property)
- Unit vector (class property)
- Value (vector in object form {i, j, k} as a class property)
- Addition (method)
- Subtraction (method)
- Dot product (method)
- Cross product (method)
import { Vector } from "vector-math";
// OR
// const { Vector } = require("vector-math")
// Create vectors
const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);
const w = new Vector(1);
The above code will create the following vectors:
Initializing a vector with no values will creat a null vector
const q = new Vector();
Will create a null vector
:
// VECTORS
// u = i + 2j -3k
// v = -4i + j
// w = i
// MODULUS
let u_modulus = u.modulus; // modulus of u = 3.741657386773941
let v_modulus = v.modulus; // modulus of v = 4.123105625617661
let w_modulus = w.modulus; // modulus of w = 1
// UNIT VECTOR
let u_unitVector = u.unitVector; // unit vector as obj {i, j, k}
let v_unitVector = v.unitVector; // unit vector as obj {i, j, k}
let w_unitVector = w.unitVector; // unit vector as obj {i, j, k}
// VALUE
let u_value = u.value; // vector as obj {i, j, k}
let v_value = v.value; // vector as obj {i, j, k}
let w_value = w.value; // vector as obj {i, j, k}
// COMPONENTS
let u_i = u.i; // i component of vector u
let v_j = v.j; // j component of vector v
let w_k = w.k; // k component of vector w
// VECTORS
// u = i + 2j -3k
// v = -4i + j
// w = i
// ADDITION
let sum_uv = u.Add(v); // u + v (result returned as Vector class)
let sum_uvw = u.Add(v, w); // u + v + w (result returned as Vector class)
// SUBTRACTION
let diff_uv = u.Subtract(v); // u - v (result returned as Vector class)
let diff_uvw = u.Subtract(v, w); // u - v - w (result returned as Vector class)
// DOT PRODUCT
let u_dot_v = u.Dot(v); // u (dot) v (result returned as number)
// CROSS PRODUCT
let u_cross_v = u.Cross(v); // u x v (result returned as Vector class)
Given vectors
const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);
const a = u.Cross(v);
// a = i + 12j + 9k
// a.value = { i: 3, j: 12, k: 9 }
The library supports chaining of vector operations, so complex calculations can easily be expressed.
const u = new Vector(1, 2, -3);
const v = new Vector(-4, 1);
const w = new Vector(1);
const b = v.Cross(u).Add(w.Cross(u)).Dot(v);
// b = 0
The following vector operations will be added in future releases
-
Modulus -
Unit vector -
Addition -
Subtraction -
Dot product -
Cross product -
Scalar Triple product - Angle between vectors
- Scalar projection
- Vector projection
- Scalar multiplication
- Basis
- Check linear independence