Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 is possible only for a square matrix (i.e., the matrix whose rows and columns have the same number of elements).
Assuming we have a 3×3 matrix like below ?
And the trace will be the sum of (a+e+i). Let's take a 4×4 matrix ?
matrix = [[1, 2, 3, 4],
[6, 4, 2, 0],
[5, 1, 6, 8],
[9, 3, 6, 0]]
# Print matrix
for row in matrix:
print(row)
[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
Finding Trace Using For Loop
We will iterate all matrix elements using a for loop to calculate the sum of all diagonal elements for finding the trace ?
# Defining the matrix
matrix = [[10, 2, 3],
[4, 5, 2],
[2, 2, 1]]
# Function to calculate the trace 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))
Trace of Matrix = 16
The diagonal elements of the given matrix are (10, 5, 1) and the trace is 16.
Using NumPy.trace() Method
We can use the trace() method from the 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
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)
Trace of Matrix = 16
The Normal of a Matrix
The normal of a matrix (also called Frobenius norm) 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 ?
Finding Normal Using For Loop
Iterate all matrix elements using a for loop to calculate the square root of the sum of squares of all elements ?
import math
matrix = [[1, 3, 2],
[3, 6, 3],
[1, 4, 2]]
# Function to calculate the normal of a matrix
def findNormal(matrix):
sum_of_squares = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
sum_of_squares += matrix[i][j] ** 2
return math.sqrt(sum_of_squares)
print("Normal of Matrix =", round(findNormal(matrix), 2))
Normal of Matrix = 9.75
Finding Normal Using NumPy Module
With the help of NumPy methods like power(), sqrt(), and sum() we can easily calculate the normal of a matrix ?
import numpy as np
matrix = np.array([[1, 3, 2],
[3, 6, 3],
[1, 4, 2]])
# Find the normal using NumPy
matrix_normal = np.linalg.norm(matrix)
print("Normal of Matrix =", round(matrix_normal, 2))
Normal of Matrix = 9.75
Alternative NumPy Approach
You can also calculate the normal manually using NumPy functions ?
import numpy as np
matrix = np.array([[1, 3, 2],
[3, 6, 3],
[1, 4, 2]])
# Calculate normal manually
sum_of_squares = np.power(matrix, 2).sum()
matrix_normal = np.sqrt(sum_of_squares)
print("Normal of Matrix =", round(matrix_normal, 2))
Normal of Matrix = 9.75
Comparison
| Method | Trace | Normal | Best For |
|---|---|---|---|
| For Loop | Manual diagonal sum | Manual calculation | Understanding the concept |
| NumPy Built-in | np.trace() |
np.linalg.norm() |
Production code |
| NumPy Manual | N/A | np.sqrt(np.power().sum()) |
Step-by-step control |
Conclusion
The trace is the sum of diagonal elements in a square matrix, while the normal (Frobenius norm) is the square root of the sum of squares of all elements. Use NumPy's built-in functions np.trace() and np.linalg.norm() for efficient calculations in production code.
