Horje
How to Perform Matrix Operations using Math.js?

Matrix operations can be computed in JavaScript using the math.js library, which provides a range of functions for handling matrices. With math.js, you can perform basic operations such as addition, subtraction, and multiplication, as well as more advanced operations like transposition, inversion, and computing various matrix norms. Here, we will learn to perform matrix operations using Math.js.

Run the below command before running the code in your local system:

npm i mathjs

These are the following Types of Matrix Operations using math.js:

Basic Matrix Operations (Addition, Subtraction, Multiplication)

In this approach, we will use math.js to perform basic matrix operations such as addition, subtraction, and multiplication. We will define two matrices and apply the respective operations using math. add, math. subtract, and math. multiply math functions, displaying the results in the console.

Example: The below example performs Basic Matrix Operations (Addition, Subtraction, Multiplication) with Math.js.

JavaScript
// script.js

const math = require('mathjs');

// Defining two matrices
const mat1 = math.matrix([[1, 2], [3, 4]]);
const mat2 = math.matrix([[5, 6], [7, 8]]);

// Performing matrix addition
const addRes = math.add(mat1, mat2);
console.log('Matrix Addition:\n', addRes.toString());

// Performing matrix subtraction
const subRes = math.subtract(mat1, mat2);
console.log('Matrix Subtraction:\n', subRes.toString());

// Perfoming matrix multiplication
const mulRes = math.multiply(mat1, mat2);
console.log('Matrix Multiplication:\n', mulRes.toString());

Output:

Matrix Addition:
[[6, 8], [10, 12]]
Matrix Subtraction:
[[-4, -4], [-4, -4]]
Matrix Multiplication:
[[19, 22], [43, 50]]

Matrix Transposition and Inversion

In this approach, we will use math.js to perform matrix transposition and inversion. We will start by defining a matrix and then compute its transpose using math.transpose. For inversion, we will define another matrix and compute its inverse using math.inv, displaying both results in the console.

Example: The below example performs Matrix Transposition and Inversion with Math.js.

JavaScript
// script.js
const math = require('mathjs');

// Defining a matrix
const mat = math.matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]);

// Performing matrix transposition
const transRes = math.transpose(mat);
console.log('Matrix Transposition:\n', transRes.toString());

// Performing matrix inversion
const matrixToInvert = math.matrix([[1, 2], [3, 4]]);
const invertRes = math.inv(matrixToInvert);
console.log('Matrix Inversion:\n', invertRes.toString());

Output:

Matrix Transposition:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Matrix Inversion:
[[-2, 1], [1.5, -0.5]]

Matrix Norms

In this approach, we will use math.js to compute different matrix norms. We will define a matrix and calculate the Frobenius norm using math.norm with the ‘fro‘ parameter, the 1-norm with 1, and the infinity norm with ‘inf‘. Each norm represents a different way of measuring the size or length of the matrix, and the results are displayed in the console.

Example: The below example performs Matrix Norms with Math.js.

JavaScript
// script.js

const math = require('mathjs');

// Defining a matrix
const mat = math.matrix([[1, 2, 3], [4, 5, 6]]);

// Computing the Frobenius norm (matrix norm)
const res1 = math.norm(mat, 'fro');
console.log('Frobenius Norm of the matrix:', res1);

// Computing the 1-norm (maximum absolute column sum)
const res2 = math.norm(mat, 1);
console.log('1-Norm of the matrix:', res2);

// Computing the infinity norm (maximum absolute row sum)
const res3 = math.norm(mat, 'inf');
console.log('Infinity Norm of the matrix:', res3);

Output:

Frobenius Norm of the matrix: 9.539392014169456
1-Norm of the matrix: 9
Infinity Norm of the matrix: 15



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Solve Equations with Math.js? How to Solve Equations with Math.js?
How to Handle Units and Conversions in math.js? How to Handle Units and Conversions in math.js?
How to Differentiate Functions Using Math.js? How to Differentiate Functions Using Math.js?
How to Hide API Key in JavaScript? How to Hide API Key in JavaScript?
How to Add event listener to Button in JavaScript ? How to Add event listener to Button in JavaScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
25