# Python Program To Find the Trace and Normal of a given Matrix

A matrix is defined as a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an m X n matrix and m and n are called its dimensions. A matrix is a two dimensional array, which is created by using lists or NumPy arrays in python.

## The Trace of a Matrix

The trace of a matrix is defined as the sum of its diagonal elements (i.e, elements from upper left to lower right ). Calculating the Trace of a matrix can be possible only for a square matrix (i.e., the matrix whose rows and columns have the same number of elements).

Assuming we have a 3X3 matrix like below −

[a, b, c]
[d, e, f]
[g, h, i]

And the Trace will be the sum of (a+e+i). Let’s take a 4X4 matrix

[1, 2, 3, 4]
[6, 4, 2, 0]
[5, 1, 6, 8]
[9, 3, 6, 0]

The resultant Trace of this matrix is

sum(1+4+6+0) = 11

Below, we will discuss the examples to find the Trace of a matrix.

## Using For Loop

We will iterate all matrix elements using a for loop to calculated the sum of all diagonal of matrix for finding the Trace.

### Example

Calculated the sum of the diagonals by iterating all the elements through the python “for loop”.

# Defining the matrix
matrix = [[10,2,3],
[4,5,2],
[2,2,1]]

# function for calculate the normal of a matrix
def findTrace(matrix):
diag_sum = 0
for i in range(len(matrix)):
diag_sum += matrix[i][i]
return diag_sum

print("Trace of Matrix =", findTrace(matrix))

### Output

Trace of Matrix = 16

The diagonals elements of the given matrix are (10, 5, 1) and the Trace is 16.

## Using NumPy.trace() Method

Also, we can use the trace() method from the Python Numpy module to calculate the trace of a matrix. The numpy.trace() method returns the sum of diagonals of a numpy array.

### Syntax

numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)

### Example

In this example we apply the numpy.trace() method to calculate the Trace of a matrix.

import numpy as np

matrix = np.array([[10,2,3],
[4,5,2],
[2,2,1]])

# find the trace
matrix_trace = np.trace(matrix)

print("Trace of Matrix =", matrix_trace)

### Output

Trace of Matrix = 16

The diagonals elements of the given matrix are (10, 5, 1) and the Trace is 16.

## Find the Normal of a Matrix

The normal of a matrix is defined as the square root of the sum of squares of all the elements of a matrix.

Assuming we have a matrix with 4 elements

[8, 4]
[5, 2]

Normal of this matrix is −

√(82 + 42 +52 +22) = 10

### Using For Loop

Iterate all matrix elements using a for loop to calculated the square root of the sum of squares of all the elements of a matrix.

### Example

To find the Normal of a matrix: initially, we will impost the math module to utilize the math.pow() and math.sqrt() properties.

import math
matrix = [[1,3,2],
[3,6,3],
[1,4,2]]

#function for calculate the normal of a matrix
def findNormal(matrix):
sum_of_elements = 0
for i in range(len(matrix)):
for j in range(len(matrix)):
sum_of_elements += math.pow(matrix[i][j], 2)
return math.floor(math.sqrt(sum_of_elements))

print("Normal of Matrix =", findNormal(matrix))

### Output

Normal of Matrix = 9

By using python for loop we have iterated each element of the matrix and calculated the squares by using math.pow() function then sum the squares and stored them in a variable. Finally calculated the square root by using math.sqrt() function.

### Using NumPy Module

With the help of numpy methods like power(), sqrt(), and sum() we can easily caluculate the normal of a matrix.

### Example

In this example, we will import the NumPy module to utilize the numpy.power(), numpy.sum() andnumpy.sqrt() properties.

import numpy as np

matrix = np.array([[1,3,2],
[3,6,3],
[1,4,2]])

# find the normal
sumOfsquares = np.power(matrix, 2).sum()
matrix_normal = round(np.sqrt(sumOfsquares))

print("Trace of Matrix =", matrix_normal)

### Output

Normal of Matrix = 9

By using the numpy.power() function we have calculated the square of each element in a given matrix, then calculated the sum of squares by using the numpy.sum() function. And finally calculate the square root by using the numpy.sqrt() function. To avoid float we then rounded the np.sqrt() value.

Updated on: 15-May-2023

411 Views

##### Kickstart Your Career

Get certified by completing the course