Python | Lower Triangle of a Matrix

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

A matrix can be seen in different ways and one of them is the lower triangular matrix part. Some problems in linear algebra are concerned with the lower triangular part of the matrix.

For this purpose, we have a predefined function numpy.tril(a) in the NumPy library package which automatically stores the lower triangular elements in a separate matrix. In this article, we are going to print the lower triangular elements of a matrix using inbuilt function numpy.tril(a).

Python code to find lower triangle of a matrix

# Linear Algebra Learning Sequence
# Lower Triangle of matrix

import numpy as np

print('lower Triangle of an 3x3 identity matrix : ', np.tril(np.eye(3)))

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

print('\n\nMatrix a : ', a)
print('lower Triangle of Matrix a : ', np.tril(a))

b = np.tril(np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('\n\nMatrix b : ', np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]))
print('lower Triangle of Matrix b : ', b)

Output:

lower Triangle of an 3x3 identity matrix :  [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]


Matrix a :  [[0 1 2]
 [3 4 5]
 [6 7 8]]
lower Triangle of Matrix a :  [[0 0 0]
 [3 4 0]
 [6 7 8]]


Matrix b :  [[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
lower Triangle of Matrix b :  [[ 1  0  0]
 [ 4  5  0]
 [ 7  8  9]
 [10 11 12]]


Comments and Discussions!

Load comments ↻





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