AppDividend
Latest Code Tutorials

# np.tril: What is Numpy tril() Function in Python

The tril() function of the Python Numpy library returns a copy of an array with the elements above the k-th diagonal zeroed. k: This parameter represents the Diagonal we require. It is an optional integer parameter, and its default value is 0. If k>0, the diagonal is above the main diagonal or vice versa.

## np.tril

The np.tril() method returns a copy of the array matrix with the element of the lower part of the triangle with respect to k. The tril() method takes two parameters and returns the lower triangle of the array matrix.

The tril() method is defined under numpy, which can be imported as import numpy as np, and we can create multidimensional arrays and derive other mathematical statistics.

### Syntax

`numpy.tril(array, k=0)`

### Parameters

It takes two parameters, out of which one parameter is optional.

The first parameter is an array that takes the input array; the second parameter is k, optional. It is an integer value having value 0 as default.

K>0 represents the diagonal above the main diagonal, and the same goes in vice versa for K<0.

### Return Value

It returns the lower triangle of the array matrix, which has the same data type and shape as the main array.

### Example programs on tril() method in Python

#### Write a program to show the working of the tril() function in Python.

```import numpy as np

arr = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Main Diagonal elements (for k=0) : \n", np.tril(arr), "\n")
print("Diagonal above main Diagonal elements (For k=1): \n", np.tril(arr, 1),
"\n\n")
print("Main Diagonal elements (For k=-1) : \n", np.tril(arr, -1))```

#### Output

```Main Diagonal elements (for k=0) :
[[1 0 0]
[4 5 0]
[7 8 9]]

Diagonal above main Diagonal elements (For k=1):
[[1 2 0]
[4 5 6]
[7 8 9]]

Main Diagonal elements (For k=-1) :
[[0 0 0]
[4 0 0]
[7 8 0]]```

In this example, we can see bypassing different values of k. This is because we are getting different lower triangles according to the diagonal of the matrix.

#### Write a program to take a 4×4 matrix and then apply the tril() function.

```import numpy as np

arr = np.matrix([[1, 2, 3, 22], [4, 5, 6, 33], [7, 8, 9, 44], [10, 11, 12,
13]])
print("Main Diagonal elements (for k=0) : \n", np.tril(arr), "\n")
print("Diagonal above main Diagonal elements (For k=1): \n", np.tril(arr, 1),
"\n\n")
print("Main Diagonal elements (For k=-1) : \n", np.tril(arr, -1))```

#### Output

```Main Diagonal elements (for k=0) :
[[ 1  0  0  0]
[ 4  5  0  0]
[ 7  8  9  0]
[10 11 12 13]]

Diagonal above main Diagonal elements (For k=1):
[[ 1  2  0  0]
[ 4  5  6  0]
[ 7  8  9 44]
[10 11 12 13]]

Main Diagonal elements (For k=-1) :
[[ 0  0  0  0]
[ 4  0  0  0]
[ 7  8  0  0]
[10 11 12  0]]```

In this example, we can see that when we pass a 4×4 matrix, we get an output matrix accordingly for different values of k.

The first matrix is for k=0, The Second matrix is for k=1, and the third is for k=-1. It set values other than the lower part of the main diagonal as 0.

That is it for the numpy tril() method.