×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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