Open In App

How to Work with Vectors in MathJS?

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Vectors are fundamental components in mathematics and physics, representing quantities that have both magnitude and direction. Using MathJS we can perform vector operations like addition, subtraction, dot products, and cross products.

These are the following ways to work with Vector in MathJS:

First, install mathjs library

npm install mathjs 

Creating Vectors in MathJS

Vectors in MathJS are represented as arrays. We can create a vector by simply defining an array of numbers.

Example: This script creates and displays two vectors, vector A and vector B, using arrays in JavaScript, and logs them to the console using MathJS.

JavaScript
const math = require('mathjs');

// Creating vectors using arrays
const vectorA = [1, 2, 3];
const vectorB = [4, 5, 6];

// Displaying the vectors
console.log('Vector A:', vectorA);
console.log('Vector B:', vectorB);

Output:

Vector A: [1, 2, 3]
Vector B: [4, 5, 6]

Basic Vector Operations

In this we use MathJS library to perform basic operations on vectors, such as addition, subtraction, and scalar multiplication.

Example: This script performs vector addition, subtraction, and scalar multiplication using the `MathJS` library in JavaScript.

JavaScript
const math = require('mathjs');

// Creating vectors using arrays
const vectorA = [1, 2, 3];
const vectorB = [4, 5, 6];

// Adding vectors
const sum = math.add(vectorA, vectorB);

// Subtracting vectors
const difference = math.subtract(vectorA, vectorB);

// Scalar multiplication
const scalarProduct = math.multiply(2, vectorA);

console.log('Sum of Vectors:', sum);
console.log('Difference of Vectors:', difference);
console.log('Scalar Product:', scalarProduct);

Output:

Sum of Vectors: [5, 7, 9]
Difference of Vectors: [-3, -3, -3]
Scalar Product: [2, 4, 6]

Advanced Vector Operations

In this we use MathJS library to perform advanced vector operations like dot products and cross products, which are essential in many applications, such as physics and engineering.

Example: This script calculates and displays the dot product and cross product of two vectors using the MathJS library in JavaScript.

JavaScript
const math = require('mathjs');

// Creating vectors using arrays
const vectorA = [1, 2, 3];
const vectorB = [4, 5, 6];

// Dot product of two vectors
const dotProduct = math.dot(vectorA, vectorB);

// Cross product of two vectors
const crossProduct = math.cross(vectorA, vectorB);

console.log('Dot Product:', dotProduct);
console.log('Cross Product:', crossProduct);

Output:

Dot Product: 32
Cross Product: [-3, 6, -3]

Working with Vector Magnitude and Direction

In this we calculate the magnitude (or length) of a vector using the math.norm function, while the direction can be represented by normalizing the vector.

Example: This script calculates the magnitude and normalizes a vector using the `MathJS` library in JavaScript.

JavaScript
const math = require('mathjs');

// Creating a vector using an array
const vectorA = [1, 2, 3];

// Magnitude of a vector
const magnitudeA = math.norm(vectorA);

// Normalizing a vector
const normalizedA = math.divide(vectorA, magnitudeA);

console.log('Magnitude of Vector A:', magnitudeA);
console.log('Normalized Vector A:', normalizedA);

Output:

Magnitude of Vector A: 3.7416573867739413
Normalized Vector A: [0.2672612419124244, 0.5345224838248488, 0.8017837257372732]

Next Article

Similar Reads