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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.