JavaScript Program to Find the Determinant of a Matrix Last Updated : 08 Mar, 2024 Comments Improve Suggest changes Like Article Like Report The determinant is a term that is used to represent the matrices in terms of a real number. It can be defined only for the matrices that have the same number of rows and columns. Determinants can be used to find out the inverse of a matrix. There are several ways to find the determinant of the matrix in JavaScript which are as follows: Table of Content Recursive ImplementationNon-Recursive ImplementationRecursive ImplementationIn this approach, we will implement a recursive function that calls itself again and again until it finds the determinant of the passed matrix. You can change the order of the matrix by changing the value of N, then pass a square matrix of the same order. Example: The below code example implements recursion in JavaScript to find determinant of a matrix. JavaScript // N is the Order of the matrix let N = 3; // Recursive function to find determinant function determinantOfMatrixRecursive(mat, n) { if (n === 1) { return mat[0][0]; } let det = 0; let sign = 1; for (let i = 0; i < n; i++) { let submatrix = createSubmatrix(mat, i, n); det += sign * mat[0][i] * determinantOfMatrixRecursive(submatrix, n - 1); sign = -sign; } return det; } // Function to find sub-matrices of different orders function createSubmatrix(mat, colToRemove, n) { let submatrix = []; for (let i = 1; i < n; i++) { let newRow = []; for (let j = 0; j < n; j++) { if (j !== colToRemove) { newRow.push(mat[i][j]); } } submatrix.push(newRow); } return submatrix; } let mat = [ [0, 4, -1], [0, 0, -2], [1, 2, -1] ]; console.log( "Determinant of the matrix is : ", determinantOfMatrixRecursive(mat, N) ); OutputDeterminant of the matrix is : -8 Time complexity: O(n!) Auxiliary Space: O(n^2), Space used for storing row. Non-Recursive ImplementationThis method implements the JavaScript loops to iterate over the matrix elements and creates sub matrices to find the determinant of the matrix. You can change the value of N to change order of the matrix and pass a square matrix of same order. Example: The below code example is a practical implementation of the iterative approach to find determinant of the matrix. JavaScript // N is the order of the matrix const N = 3; function determinantOfMatrixIterative(mat, n) { let det = 1; let total = 1; const temp = Array(n).fill(0); for (let i = 0; i < n; i++) { let index = i; while (index < n && mat[index][i] === 0) { index++; } if (index === n) { continue; } if (index !== i) { for (let j = 0; j < n; j++) { [mat[i][j], mat[index][j]] = [mat[index][j], mat[i][j]]; } det *= Math.pow(-1, index - i); } for (let j = 0; j < n; j++) { temp[j] = mat[i][j]; } for (let j = i + 1; j < n; j++) { const num1 = temp[i]; const num2 = mat[j][i]; for (let k = 0; k < n; k++) { mat[j][k] = num1 * mat[j][k] - num2 * temp[k]; } total *= num1; } } for (let i = 0; i < n; i++) { det *= mat[i][i]; } return det / total; } const mat = [ [8, 1, 2], [2, 1, 4], [1, 0, 5] ]; console.log( "Determinant of the matrix is:", determinantOfMatrixIterative(mat, N)); OutputDeterminant of the matrix is: 32 Time Complexity: O(n^2) Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article JavaScript Program to Find the Determinant of a Matrix A akshitsaxena2 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Find the Rank of a Matrix Given a matrix of size M x N, your task is to find the rank of a matrix using JavaScript.Example:Input: mat[][] = [[10, 20, 10], [20, 40, 20], [30, 50, 0]]Output: Rank is 2Explanation: Ist and 2nd rows are linearly dependent. But Ist and 3rd or 2nd and 3rd are independent. Input: mat[][] = [[10, 20, 5 min read Javascript Program to Interchange elements of first and last rows in matrix Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very 2 min read JavaScript Program to check if matrix is lower triangular Given a square matrix and the task is to check the matrix is in lower triangular form or not. A square matrix is called lower triangular if all the entries above the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 0, 0, 0}, {1, 4, 0, 0}, {4, 6, 2, 0}, {0, 4, 7, 6}};Output : Matrix is in l 2 min read Javascript Program to check if matrix is upper triangular Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero.Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}};Output : Matrix is in Up 2 min read JavaScript Program to Print all Palindromic Paths from Top Left to Bottom Right in a Matrix We are given a matrix containing only lower-case alphabetical characters. The task is to print all the palindromic paths present in the given matrix. A path is a sequence of cells starting from the top-left cell and ending at the bottom-right cell. We are allowed to move only to the right and down f 4 min read How to find the Determinant of a Matrix? Answer: The following steps are to be followed to find the Determinant of a Matrix Select any row or column. To find the determinant, we normally start with the first row.Determine the co-factors of each of the row/column items that we picked in Step 1.Multiply the row/column items from Step 1 by th 6 min read Javascript Program for Diagonally Dominant Matrix In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant 2 min read Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 3 min read Javascript Program to check Involutory Matrix Given a matrix, the task is to check matrix is an involutory matrix or not. Involutory Matrix: A matrix is said to be an involutory matrix if the matrix multiplied by itself returns the identity matrix. The involutory matrix is the matrix that is its inverse. The matrix A is said to be an involutory 2 min read Javascript Program to Print matrix in snake pattern Given n x n matrix In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5 2 min read Like