Python Program to Find the Normal and Trace


Normal and Trace of a Matrix

The normal of a matrix is defined as the square root of the sum of squares of all the elements that are present in the entire matrix. This is one of the most used applications related to the matrix concepts.

Consider a two dimensional array.

arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]].

The matrix representation of the given array will be as follows −

1     2     3     4
5     6     7     8
9     10    11    12
13    14    15    16

Now, let us find the trace and normal of the given matrix by following the algorithms discussed above.

  • We have already considered the array.

  • The diagonal elements of the matrix which is converted from the given array are 1, 6, 11, and 16.

  • So, in order to find the trace of the matrix, we need to add all the elements. The trace of the matrix is 1 + 6 + 11 + 16 which is equal to 34.

Algorithm

Now, let us discuss an algorithm that helps us to find the “ Normal of a Matrix ”.

  • Step 1 − Initially, consider a two dimensional array which is a square matrix. The square matrix is a matrix which has an order of ( n x n ).

  • Step 2 − As soon as the array is taken, calculate the squares of all elements of the matrix.

  • Step 3 − Add all the squares of the elements of the matrix taken.

  • Step 4 − After adding the squares, find the square root of the resulting sum to get the Normal of the matrix. Allow the value to be printed later.

Example

Following is an example to find the normal of a matrix −

import math
def function_normal(matrix, size):    
   sum = 0
   for i in range(size):
      for j in range(size):
         sum += matrix[i][j] * matrix[i][j]  
   return math.floor(math.sqrt(sum)) 

matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] 
print("The normal of the matrix is equal to: ", function_normal(matrix, 4))

Trace of a Matrix

The trace of a matrix is defined as the sum of all diagonal elements of a matrix. The diagonal elements of matrix are the elements that are part of the principle diagonal of a square matrix. They start from the first element and end with the last element of the square matrix diagonally.

If we consider the previous 2D array −

1     2     3     4
5     6     7     8
9     10    11    12
13    14    15    16
  • To find the normal of the matrix, we need to find the squares of the elements present in the matrix, add them up and finally find the square root.

  • 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 + 121 + 144 + 169 + 196 + 225 + 256 which is equal to 1496. The square root of 1496 is 38.67. If the floor value is considered, 38 will be the required normal of the respective matrix.

Algorithm

Now, let us discuss an algorithm that helps us to find the “ Trace of a Matrix ”.

  • Step 1 − Initially, consider a two dimensional array which is a square matrix. The square matrix is a matrix which has an order of ( n x n ).

  • Step 2 − As soon as the array is taken, find out the diagonal elements in order to find the trace of the matrix.

  • Step 3 − Now, add all the diagonal elements which finally results the trace of that particular matrix. Print the value of the trace.

Now, let us consider an Input output scenario and then calculate the values of both normal and trace of that particular matrix accordingly.

Example

Following is an example to find the trace of a matrix −

import math
def function_trace(matrix, size):
   sum = 0
   for i in range(size):
      sum = sum + matrix[i][i]
   return sum

matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]] 
print("The trace of the matrix is equal to: ", function_trace(matrix, 4))

Output

The trace of the matrix is equal to:  34

Updated on: 05-May-2023

55 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements