JavaScript Program to Find the Normal and Trace of a Matrix Last Updated : 15 Apr, 2025 Comments Improve Suggest changes Like Article Like Report JavaScript provides us with ways to find a square matrix's normal and trace. In linear algebra, the total number of entries on the principal diagonal is indicated by the trace, while the normal represents the size of the matrix overall. It is critical to understand these concepts in a variety of computational and mathematical applications. Understanding the ConceptsNormal: The normal of a matrix represents its overall "magnitude" or size. It is calculated as the square root of the sum of the squares of each element in the matrix.Trace: In a square matrix, the trace is the sum of the elements on the main diagonal. It provides insights into the matrix's "linearity."Table of Content Using LoopsUsing Array MethodsUsing LoopsThe sum of squares for the normal and the sum of diagonal elements for the trace are calculated iteratively using for loop via the matrix elements. Example: To demonstrate the use of the function to computer the normal and trace of a given matrix using JavaScript's nested loops. JavaScript function calculateNormalAndTrace(matrix) { let normalSum = 0; let traceSum = 0; for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { normalSum += Math.pow(matrix[i][j], 2); if (i === j) { traceSum += matrix[i][j]; } } } const normal = Math.sqrt(normalSum); return { normal, trace: traceSum }; } const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const { normal, trace } = calculateNormalAndTrace(matrix); console.log("Normal:", normal); console.log("Trace:", trace); OutputNormal: 16.881943016134134 Trace: 15 Using Array MethodsUsing JavaScript's built-in array operations, such as map and reduce, this method efficiently calculates a matrix's normal and trace. It gives a straightforward solution by utilizing the concepts of functional programming, improving maintainability and performance—especially for larger matrices. Example: This code snippet shows the use of array methods by showing how to effectively compute the normal and trace of a matrix in JavaScript using the `reduce` and `map` functions, improving readability and efficiency. JavaScript function calculateNormalAndTrace(matrix) { const normalSum = matrix.flat().reduce((acc, value) => { return acc + Math.pow(value, 2), 0 }); const traceSum = matrix.map((row, i) => row[i]) .reduce((acc, value) => acc + value, 0); const normal = Math.sqrt(normalSum); return { normal, trace: traceSum }; } const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const { normal, trace } = calculateNormalAndTrace(matrix); console.log("Normal:", normal); console.log("Trace:", trace); OutputNormal: 0 Trace: 15 Comment More infoAdvertise with us Next Article JavaScript Program to Find the Normal and Trace of a Matrix H heysaiyad Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 for Markov matrix Given a m x n 2D matrix, check if it is a Markov Matrix.Markov Matrix : The matrix in which the sum of each row is equal to 1.Example of Markov MatrixExamples: Input :1 0 00.5 0 0.50 0 1Output : yesExplanation :Sum of each row results to 1, therefore it is a Markov Matrix.Input :1 0 00 0 21 0 0Outpu 2 min read C Program To Find Normal and Trace of Matrix Here, we will see how to write a C program to find the normal and trace of a matrix. Below are the examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};Output: Normal = 16Trace = 15 Explanation: Normal = sqrt(1*1+ 2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7 + 8*8 + 9*9) = 16Trace = 1+5+9 = 15 Input: m 2 min read Javascript Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row.Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : NoA Simple solution is to do follo 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 Java Program to Check Whether a Matrix is Symmetric or Not A symmetric matrix is a square matrix in which the transpose of the square matrix is the same as the original square matrix. A square matrix is a matrix in which the number of columns is the same as the number of rows. If the matrix is a symmetric matrix then it is also a square matrix but vice vers 4 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 Java Program to Check Whether a Given Matrix is Lower Triangular Matrix or Not In the lower triangular matrix, the elements that are present above the diagonal should be 0. Given a matrix and the task is to check the matrix is in lower triangular form or not. A matrix must be a square matrix. A square matrix is called lower triangular if all the entries above the main diagonal 2 min read Java Program to Display Upper Triangular Matrix Upper Triangular Matrix is a matrix in which all the elements below the principal matrix are 0. A necessary condition is that matrix must be Square matrix in nature. If the matrix is not a square matrix, it can never be called the upper triangular matrix. ExamplesInput 1: mat[][] = { {2, 1, 4}, {1, 2 min read Java Program to Display Lower Triangular Matrix Lower Triangular Matrix is a square matrix in which all the elements above the principal diagonal are 0. If the matrix is not a square matrix, it can never be called the lower triangular matrix. Examples Input 1: mat[][] = { {2, 1, 4}, {1, 2, 3}, {3, 6, 2}} Output : mat[][]= {{2, 0, 0}, {1, 2, 0}, { 2 min read Like