numpy.tri#

numpy.tri(N,M=None,k=0,dtype=<class'float'>,*,like=None)[source]#

An array with ones at and below the given diagonal and zeros elsewhere.

Parameters:
Nint

Number of rows in the array.

Mint, optional

Number of columns in the array.By default,M is taken equal toN.

kint, optional

The sub-diagonal at and below which the array is filled.k = 0 is the main diagonal, whilek < 0 is below it,andk > 0 is above. The default is 0.

dtypedtype, optional

Data type of the returned array. The default is float.

likearray_like, optional

Reference object to allow the creation of arrays which are notNumPy arrays. If an array-like passed in aslike supportsthe__array_function__ protocol, the result will be definedby it. In this case, it ensures the creation of an array objectcompatible with that passed in via this argument.

New in version 1.20.0.

Returns:
trindarray of shape (N, M)

Array with its lower triangle filled with ones and zero elsewhere;in other wordsT[i,j]==1 forj<=i+k, 0 otherwise.

Examples

>>>importnumpyasnp>>>np.tri(3,5,2,dtype=int)array([[1, 1, 1, 0, 0],       [1, 1, 1, 1, 0],       [1, 1, 1, 1, 1]])
>>>np.tri(3,5,-1)array([[0.,  0.,  0.,  0.,  0.],       [1.,  0.,  0.,  0.,  0.],       [1.,  1.,  0.,  0.,  0.]])
On this page