How to calculate the normal and trace of square matrix in Node.js

1 Answer

0 votes
function CalculateNormal(matrix) {
    let normal = 0;
 
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix.length; j++) {
            normal += matrix[i][j] * matrix[i][j];
        }
    }
 
    return Math.sqrt(normal);
}
 
function CalculateTrace(matrix) {
    let trace = 0;
 
    for (let i = 0; i < matrix.length; i++) {
        trace += matrix[i][i];
    }
 
    return trace;
}
 
const matrix = [[1, 1, 1, 1, 1],
                [2, 2, 2, 2, 2],
                [3, 3, 3, 3, 3],
                [4, 4, 4, 4, 4],
                [6, 6, 6, 6, 6]];
 
console.log(CalculateTrace(matrix));
 
console.log(CalculateNormal(matrix));

 
 
 
 
/*
run:
  
16
18.16590212458495
  
*/

 



answered Mar 1, 2023 by avibootz

Related questions

1 answer 175 views
1 answer 133 views
1 answer 138 views
1 answer 154 views
1 answer 130 views
...