
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
A lightweight and powerful TypeScript/JavaScript vector mathematics library for 2D, 3D, and n-dimensional vector operations.
npm install vijk
// Named import
import { Vector } from "vijk";
// Default import
import Vector from "vijk";
const { Vector } = require("vijk");
import { Vector } from "vijk";
// Create 2D vectors
const v1 = new Vector([3, 4]);
const v2 = new Vector([1, 2]);
// Basic operations
console.log(v1.magnitude()); // 5
console.log(v1.add(v2).toArray()); // [4, 6]
console.log(v1.dot(v2)); // 11
// 3D vectors
const v3d1 = new Vector([1, 0, 0]);
const v3d2 = new Vector([0, 1, 0]);
console.log(v3d1.cross(v3d2).toArray()); // [0, 0, 1]
// Vector analysis
console.log(v1.angleTo(v2)); // angle in radians
console.log(v1.normalize().toArray()); // [0.6, 0.8]
// Static factory methods
const zero = Vector.zero(3); // [0, 0, 0]
const unitX = Vector.unitVector(3, 0); // [1, 0, 0]
A comprehensive class for mathematical vector operations in any dimension.
new Vector(components: (number | string)[])
Creates a new vector with the specified components.
Parameters:
components: Array of numbers or numeric strings representing vector componentsExample:
const v1 = new Vector([3, 4]);
const v2 = new Vector([1, 2, 3]);
const v3 = new Vector(["1.23", "4.56"]); // can use strings
dimension: numberReturns the dimension of the vector.
const v = new Vector([1, 2, 3]);
console.log(v.dimension); // 3
components: number[]Returns a copy of the vector components as an array.
const v = new Vector([3, 4]);
console.log(v.components); // [3, 4]
get(index: number): numberGets a specific component by index (0-based).
const v = new Vector([3, 4, 5]);
console.log(v.get(0)); // 3
console.log(v.get(2)); // 5
set(index: number, value: number | string): VectorReturns a new vector with the specified component updated.
const v1 = new Vector([3, 4]);
const v2 = v1.set(1, 5);
console.log(v2.toArray()); // [3, 5]
magnitude(): numberCalculates the magnitude (length) of the vector.
const v = new Vector([3, 4]);
console.log(v.magnitude()); // 5
normalize(): VectorReturns a unit vector in the same direction.
const v = new Vector([3, 4]);
const unit = v.normalize();
console.log(unit.toArray()); // [0.6, 0.8]
console.log(unit.magnitude()); // 1
add(other: Vector): VectorAdds another vector to this vector.
const v1 = new Vector([1, 2]);
const v2 = new Vector([3, 4]);
console.log(v1.add(v2).toArray()); // [4, 6]
subtract(other: Vector): VectorSubtracts another vector from this vector.
const v1 = new Vector([5, 7]);
const v2 = new Vector([2, 3]);
console.log(v1.subtract(v2).toArray()); // [3, 4]
scale(scalar: number | string): VectorMultiplies the vector by a scalar value.
const v = new Vector([1, 2, 3]);
console.log(v.scale(2).toArray()); // [2, 4, 6]
console.log(v.scale(0.5).toArray()); // [0.5, 1, 1.5]
dot(other: Vector): numberCalculates the dot product with another vector.
const v1 = new Vector([1, 2, 3]);
const v2 = new Vector([4, 5, 6]);
console.log(v1.dot(v2)); // 32 (1*4 + 2*5 + 3*6)
cross(other: Vector): VectorCalculates the cross product with another 3D vector (3D only).
const v1 = new Vector([1, 0, 0]);
const v2 = new Vector([0, 1, 0]);
console.log(v1.cross(v2).toArray()); // [0, 0, 1]
angleTo(other: Vector): numberCalculates the angle between this vector and another (in radians).
const v1 = new Vector([1, 0]);
const v2 = new Vector([0, 1]);
console.log(v1.angleTo(v2)); // 1.5708 (π/2 radians, 90 degrees)
projectOnto(other: Vector): VectorProjects this vector onto another vector.
const v1 = new Vector([3, 4]);
const v2 = new Vector([1, 0]);
console.log(v1.projectOnto(v2).toArray()); // [3, 0]
isParallel(other: Vector, tolerance?: number): booleanChecks if this vector is parallel to another vector.
const v1 = new Vector([2, 4]);
const v2 = new Vector([1, 2]);
console.log(v1.isParallel(v2)); // true
isPerpendicular(other: Vector, tolerance?: number): booleanChecks if this vector is perpendicular to another vector.
const v1 = new Vector([1, 0]);
const v2 = new Vector([0, 1]);
console.log(v1.isPerpendicular(v2)); // true
isOrthogonal(other: Vector, tolerance?: number): booleanChecks if this vector is orthogonal to another vector (alias for isPerpendicular).
const v1 = new Vector([1, 0]);
const v2 = new Vector([0, 1]);
console.log(v1.isOrthogonal(v2)); // true
equals(other: Vector, tolerance?: number): booleanChecks if this vector equals another vector.
const v1 = new Vector([1, 2, 3]);
const v2 = new Vector([1, 2, 3]);
console.log(v1.equals(v2)); // true
clone(): VectorCreates a copy of this vector.
const v1 = new Vector([1, 2, 3]);
const v2 = v1.clone();
console.log(v2.toArray()); // [1, 2, 3]
toArray(): number[]Converts the vector to an array.
const v = new Vector([1, 2, 3]);
console.log(v.toArray()); // [1, 2, 3]
toString(): stringConverts the vector to a string representation.
const v = new Vector([1, 2, 3]);
console.log(v.toString()); // "[1, 2, 3]"
Vector.zero(dimension: number): VectorCreates a zero vector of specified dimension.
const zero2D = Vector.zero(2);
console.log(zero2D.toArray()); // [0, 0]
const zero3D = Vector.zero(3);
console.log(zero3D.toArray()); // [0, 0, 0]
Vector.unitVector(dimension: number, axis: number): VectorCreates a unit vector along a specific axis.
const unitX = Vector.unitVector(3, 0);
console.log(unitX.toArray()); // [1, 0, 0]
const unitY = Vector.unitVector(3, 1);
console.log(unitY.toArray()); // [0, 1, 0]
const unitZ = Vector.unitVector(3, 2);
console.log(unitZ.toArray()); // [0, 0, 1]
Vector.fromPoints(from: Vector, to: Vector): VectorCreates a vector from one point to another.
const p1 = new Vector([1, 2]);
const p2 = new Vector([4, 6]);
const direction = Vector.fromPoints(p1, p2);
console.log(direction.toArray()); // [3, 4]
All vector operations return new vectors, allowing for method chaining:
const v1 = new Vector([1, 2, 3]);
const v2 = new Vector([4, 5, 6]);
const result = v1.add(v2).scale(2).normalize();
console.log(result.toArray());
// 2D vectors (common for graphics, physics)
const position2D = new Vector([100, 200]);
const velocity2D = new Vector([5, -3]);
// 3D vectors (common for 3D graphics, physics)
const position3D = new Vector([1, 2, 3]);
const force3D = new Vector([0, -9.8, 0]);
// Higher dimensions (machine learning, data science)
const feature4D = new Vector([0.5, 0.3, 0.8, 0.2]);
const weights = new Vector([1.2, 0.8, 1.5, 0.9]);
// Calculate resultant force
const force1 = new Vector([10, 0]); // 10N to the right
const force2 = new Vector([0, 15]); // 15N upward
const resultant = force1.add(force2);
console.log(resultant.magnitude()); // 18.03N
// Calculate surface normal
const v1 = new Vector([1, 0, 0]);
const v2 = new Vector([0, 1, 0]);
const normal = v1.cross(v2).normalize();
console.log(normal.toArray()); // [0, 0, 1]
// Calculate similarity between feature vectors
const features1 = new Vector([0.8, 0.6, 0.3]);
const features2 = new Vector([0.7, 0.5, 0.4]);
const similarity = features1.dot(features2);
For comprehensive examples demonstrating real-world usage, check out the examples directory:
Each example is available in both TypeScript and JavaScript. See the examples README for more details.
MIT © SkorpionG
FAQs
A TypeScript/JavaScript vector class
We found that vijk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.