mathmath librarymathmathnodejsmatameticsmathmathlib-nmathmathmathjsmathmathlib-nmathlibmathlibjsmathlibnodejsnodejsmatameticsmathlib-nnodejsmath librarymath librarymathlib-nnodejsmatameticsmathlib-nmathlibjsmathjsnodejs

Vector Constructor

Overview

The vector constructor is a JavaScript class that represents a mathematical vector in 2D or 3D space. It provides various functions for creating, manipulating, and inspecting vectors.

Usage

To create a new vector, you can call the constructor with either an array of numeric values or two or three numeric arguments, depending on whether you want to create a 2D or 3D vector. For example:

There are two ways to call the constructor. You can import the Vector class only from the mathlib-n module. This is the recommended way to use the constructor, if you are calculating only Vector operations.

 const { Vector } = require('mathlib-n');
// Create a 2D vector with x = 1, y = 2
const vec2 = new Vector(1, 2);
// Create a 3D vector with x = 1, y = 2, z = 3
const vec3 = new Vector(1, 2, 3);

Alternatively, you can import the entire mathlib-n module. This is the recommended way to use the constructor, if you are calculating all types of math operations. Here for example I used the 1st method.

 const math = require('mathlib-n');
const vec2 = new math.Vector(1, 2);
const vec3 = new math.Vector(1, 2, 3);

Let's see how to use the Vector class.

const vec2 = new Vector(1, 2);
const vec3 = new Vector(3,4);

Functions

value

Get the value of the vector.

const val = vector.value();

add

Add two vectors together and return the result.

 const result = vector1.add(vector2);

sub

Subtract one vector from another and return the result.

 const result = vector1.sub(vector2);

dot

Calculate the dot product of two vectors.

 const product = vector1.dot(vector2);

cross

Calculate the cross product of two vectors.

 const product = vector1.cross(vector2);

div

Divide the vector by a scalar value.

 const result = vector.div(scalar);

angle

Calculate the angle between two vectors in radians.

 const angle = vector1.angle(vector2);

equals

Check if two vectors are equal.

 const isEqual = vector1.equals(vector2);

toString

Convert the vector to a string representation.

 const str = vector.toString();

toArray

Convert the vector to an array.

 const arr = vector.toArray();

Example Usage

 // Create vectors
    const vector1 = new Vector(2, 3, 5);
    const vector2 = new Vector(4, 5);
    // Perform vector operations
    const sum = vector1.add(vector2);
    const difference = vector1.sub(vector2);
    const dotProduct = vector1.dot(vector2);
    const crossProduct = vector1.cross(vector2);
    const divided = vector1.div(2);
    const angle = vector1.angle(vector2);
    const areEqual = vector1.equals(vector2);
    const stringRep = vector1.toString();
    const arrayRep = vector1.toArray();
    // Print results
    console.log("Vector 1:", vector1);
    console.log("Vector 2:", vector2);
    console.log("Sum:", sum);
    console.log("Difference:", difference);
    console.log("Dot Product:", dotProduct);
    console.log("Cross Product:", crossProduct);
    console.log("Divided:", divided);
    console.log("Angle:", angle);
    console.log("Are Equal:", areEqual);
    console.log("String Representation:", stringRep);
    console.log("Array Representation:", arrayRep);

Output

    Vector 1: Vector { x: 2, y: 3, z: 5 }
    Vector 2: Vector { x: 4, y: 5, z: 0 }
    Sum: Vector { x: 6, y: 8, z: 5 }
    Difference: Vector { x: -2, y: -2, z: 5 }
    Dot Product: 23
    Cross Product: Vector { x: -25, y: 20, z: -2 }
    Divided: Vector { x: 1, y: 1.5, z: 2.5 }
    Angle: 0.582698780728861
    Are Equal: false
    String Representation: 2i+3j+5k
    Array Representation: [ 2, 3, 5 ]