``````

In [1]:

import numpy as np

``````
``````

In [2]:

a = np.arange(16).reshape(4, 4)
print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]

``````
``````

In [3]:

print(np.tril(a))

``````
``````

[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]
[12 13 14 15]]

``````
``````

In [4]:

print(np.tril(a, k=2))

``````
``````

[[ 0  1  2  0]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]

``````
``````

In [5]:

print(np.tril(a, k=-1))

``````
``````

[[ 0  0  0  0]
[ 4  0  0  0]
[ 8  9  0  0]
[12 13 14  0]]

``````
``````

In [6]:

a = np.arange(12).reshape(3, 4)
print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

``````
``````

In [7]:

print(np.tril(a))

``````
``````

[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]]

``````
``````

In [8]:

print(np.tril(a, k=-1))

``````
``````

[[0 0 0 0]
[4 0 0 0]
[8 9 0 0]]

``````
``````

In [9]:

print(np.tril(np.arange(32).reshape(2, 4, 4)))

``````
``````

[[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]
[12 13 14 15]]

[[16  0  0  0]
[20 21  0  0]
[24 25 26  0]
[28 29 30 31]]]

``````
``````

In [10]:

print(np.tril(np.arange(16).reshape(1, 1, 4, 4)))

``````
``````

[[[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]
[12 13 14 15]]]]

``````
``````

In [11]:

a_tril = np.tril(np.arange(16).reshape(4, 4))
print(a_tril)

``````
``````

[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]
[12 13 14 15]]

``````
``````

In [12]:

print(a_tril.T)

``````
``````

[[ 0  4  8 12]
[ 0  5  9 13]
[ 0  0 10 14]
[ 0  0  0 15]]

``````
``````

In [13]:

print(a_tril.T.T)

``````
``````

[[ 0  0  0  0]
[ 4  5  0  0]
[ 8  9 10  0]
[12 13 14 15]]

``````