Python | Trace of a Matrix

Trace of a Matrix in Python: Here, we are going to learn about the Trace of a Matrix and how to find it using Python code?
Submitted by Anuj Singh, on July 17, 2020

The sum of diagonal elements of a matrix is commonly known as the trace of the matrix. It is mainly used in eigenvalues and other matrix applications. In this article, we are going to find the trace of a matrix using inbuilt function numpy.trace(a).

Python code to find trace of a matrix

# Linear Algebra Learning Sequence
# Trace of matrix

import numpy as np

print('Trace of an 3x3 identity matrix : ', np.trace(np.eye(3)))

a = np.arange(9).reshape((3,3))

print(' Matrix a :\n', a)
print('Trace of Matrix a : ', np.trace(a))

Output:

Trace of an 3x3 identity matrix :  3.0
 Matrix a :
 [[0 1 2]
 [3 4 5]
 [6 7 8]]
Trace of Matrix a :  12


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.