0 votes
in Python by
explain numpy.tri() in Python

1 Answer

0 votes
by

numpy.tri() in Python

The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere.

Syntax

numpy.tri(N, M=None, k=0, dtype=<class 'float'>)

Parameter

R : It represents the number of rows

C : This parameter represents the number of columns. It is an optional parameter and, by default R = C

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.

dtype : It represents the data type of returned array. 

Return

This function returns an array with its lower triangle filled with ones and zero elsewhere(T[i,j] == 1 for i <= j + k, 0 otherwise).

Example 1

# Python Program explaining
# numpy.tri() function
import numpy as np
print("\ntri() with diagonal  value: 1 : \n",np.tri(4, 2, 1, dtype = float))
print("\ntri with main diagonal: 0 \n",np.tri(5, 3, 0))
print("\ntri with diagonal: -1 : \n",np.tri(5, 3, -1))

Output

tri() with diagonal  value: 1 :
[[ 1.  1.]
[ 1.  1.]
[ 1.  1.]
[ 1.  1.]]
tri with main diagonal: 0
[[ 1.  0.  0.]
[ 1.  1.  0.]
[ 1.  1.  1.]
[ 1.  1.  1.]
[ 1.  1.  1.]]
tri with diagonal: -1 :
[[ 0.  0.  0.]
[ 1.  0.  0.]
[ 1.  1.  0.]
[ 1.  1.  1.]
[ 1.  1.  1.]]
...