Given a 2D matrix, the task is to find Trace and Normal of matrix.
Normal of a matrix is defined as square root of sum of squares of matrix elements.
Trace of a n x n square matrix is sum of diagonal elements.
Examples :
Input : mat[][] = {{7, 8, 9},
{6, 1, 2},
{5, 4, 3}};
Output : Normal = 16
Trace = 11
Explanation :
Normal = sqrt(7*7+ 8*8 + 9*9 + 6*6 +
1*1 + 2*2 + 5*5 + 4*4 + 3*3)
= 16
Trace = 7+1+3 = 11
Input :mat[][] = {{1, 2, 3},
{6, 4, 5},
{2, 1, 3}};
Output : Normal = 10
Trace = 8
Explanation :
Normal = sqrt(1*1 +2*2 + 3*3 + 6*6 + 4*4 +
5*5 + 2*2 + 1*1 + 3*3)
Trace = 8(1+4+3)

Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
int findNormal( int mat[][MAX], int n)
{
int sum = 0;
for ( int i=0; i<n; i++)
for ( int j=0; j<n; j++)
sum += mat[i][j]*mat[i][j];
return sqrt (sum);
}
int findTrace( int mat[][MAX], int n)
{
int sum = 0;
for ( int i=0; i<n; i++)
sum += mat[i][i];
return sum;
}
int main()
{
int mat[][MAX] = {{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5},
};
cout << "Trace of Matrix = "
<< findTrace(mat, 5) << endl;
cout << "Normal of Matrix = "
<< findNormal(mat, 5) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static int findNormal( int mat[][], int n)
{
int sum = 0 ;
for ( int i= 0 ; i<n; i++)
for ( int j= 0 ; j<n; j++)
sum += mat[i][j]*mat[i][j];
return ( int )Math.sqrt(sum);
}
static int findTrace( int mat[][], int n)
{
int sum = 0 ;
for ( int i= 0 ; i<n; i++)
sum += mat[i][i];
return sum;
}
public static void main (String[] args) {
int mat[][] = {{ 1 , 1 , 1 , 1 , 1 },
{ 2 , 2 , 2 , 2 , 2 },
{ 3 , 3 , 3 , 3 , 3 },
{ 4 , 4 , 4 , 4 , 4 },
{ 5 , 5 , 5 , 5 , 5 },
};
System.out.println ( "Trace of Matrix = "
+ findTrace(mat, 5 ));
System.out.println ( "Normal of Matrix = "
+ findNormal(mat, 5 ));
}
}
|
Python3
import math
MAX = 100 ;
def findNormal(mat, n):
sum = 0 ;
for i in range (n):
for j in range (n):
sum + = mat[i][j] * mat[i][j];
return math.floor(math.sqrt( sum ));
def findTrace(mat, n):
sum = 0 ;
for i in range (n):
sum + = mat[i][i];
return sum ;
mat = [[ 1 , 1 , 1 , 1 , 1 ],
[ 2 , 2 , 2 , 2 , 2 ],
[ 3 , 3 , 3 , 3 , 3 ],
[ 4 , 4 , 4 , 4 , 4 ],
[ 5 , 5 , 5 , 5 , 5 ]];
print ( "Trace of Matrix =" , findTrace(mat, 5 ));
print ( "Normal of Matrix =" , findNormal(mat, 5 ));
|
C#
using System;
class GFG {
static int findNormal( int [,]mat, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
sum += mat[i,j] * mat[i,j];
return ( int )Math.Sqrt(sum);
}
static int findTrace( int [,]mat, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += mat[i,i];
return sum;
}
public static void Main ()
{
int [,]mat = { {1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{3, 3, 3, 3, 3},
{4, 4, 4, 4, 4},
{5, 5, 5, 5, 5},
};
Console.Write ( "Trace of Matrix = "
+ findTrace(mat, 5) + "\n" );
Console.Write( "Normal of Matrix = "
+ findNormal(mat, 5));
}
}
|
PHP
<?php
$MAX = 100;
function findNormal( $mat , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$sum += $mat [ $i ][ $j ] *
$mat [ $i ][ $j ];
return floor (sqrt( $sum ));
}
function findTrace( $mat , $n )
{
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$sum += $mat [ $i ][ $i ];
return $sum ;
}
$mat = array ( array (1, 1, 1, 1, 1),
array (2, 2, 2, 2, 2),
array (3, 3, 3, 3, 3),
array (4, 4, 4, 4, 4),
array (5, 5, 5, 5, 5));
echo "Trace of Matrix = " ,
findTrace( $mat , 5), "\n" ;
echo "Normal of Matrix = " ,
findNormal( $mat , 5) ;
?>
|
Javascript
<script>
var MAX = 100;
function findNormal(mat, n)
{
var sum = 0;
for ( var i = 0; i < n; i++)
for ( var j = 0; j < n; j++)
sum += mat[i][j] * mat[i][j];
return parseInt(Math.sqrt(sum));
}
function findTrace(mat, n)
{
var sum = 0;
for ( var i = 0; i < n; i++)
sum += mat[i][i];
return sum;
}
var mat = [ [ 1, 1, 1, 1, 1 ],
[ 2, 2, 2, 2, 2 ],
[ 3, 3, 3, 3, 3 ],
[ 4, 4, 4, 4, 4 ],
[ 5, 5, 5, 5, 5 ] ];
document.write( "Trace of Matrix = " +
findTrace(mat, 5) + "<br>" );
document.write( "Normal of Matrix = " +
findNormal(mat, 5));
</script>
|
Output
Trace of Matrix = 15
Normal of Matrix = 16
Time Complexity : O(n*n)
Space Complexity : O(1), since no extra space has been taken.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Aug, 2022
Like Article
Save Article