numpy.tril() in Python
numpy.tril() in Python
The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed.
Syntax
numpy.tril(m, k=0)
Parameter
a: It represents the input array
k: This parameter represents the Diagonal we require. It is an optional integer parameter, and its default value is 0. If k>0, it means the diagonal is above the main diagonal or vice versa.
Return
This function returns the Lower triangle of the parameter ‘a’ while having the same shape and data-type as ‘a’.
Example 1
# Python Program explaining # numpy.tril() function import numpy as np # string input mat = np.matrix([[11, 29, 33], [93 ,44, 93], [55, 34, 56]]) print("The Diagonal elements: \n", np.tril(mat)) print("\nThe Diagonal above the main Diagonal elements: \n", np.tril(mat, 1)) print("\nThe Diagonal elements: \n", np.tril(mat, -1))
Output
The Diagonal elements: [[11 0 0] [93 44 0] [55 34 56]] The Diagonal above the main Diagonal elements: [[11 29 0] [93 44 93] [55 34 56]] The Diagonal elements: [[ 0 0 0] [93 0 0] [55 34 0]]