tf.linalg.band_part example

Example: what is tf.linalg.band_part?

# tf.linalg.band_part(input, num_lower, num_upper, name=None)
# num_lower is lower bound of number and num_upper is upper bound of number
# we want to keep number for a matrix in a range num_lower to num upper and 
# if the number of matrix not fall at this range then this api makes it 0
# Now this tensorflow api only do this work upper triangular region of matrix 
# and lower triangular region of this matrix
# If the num_lower is less than num_upper then it applies for lower triangular matrix
# and if num_lower is greater than num upper then it applies upper triangular of matrix


# this example the api effect lower triangle of matrix ---
input = np.array([[ 10,  1,  4, 3],
         [1,  20,  1, 2],
         [-2, 1,  30, 1],
         [3, 10, 1, 40]])

tf.linalg.band_part(
    input, num_lower = 1, num_upper = 2, name=None
)

>><tf.Tensor: shape=(4, 4), dtype=int64, numpy=
array([[10,  1, -4,  0],
       [ 1, 20,  1,  2],
       [ 0,  1, 30,  1],
       [ 0,  0,  1, 40]])>

# this example the api effect upper triangle of matrix  ---
input = np.array([[ 10,  1,  4, 3],
         [1,  20,  1, 6],
         [-2, 1,  30, 1],
         [3, 10, 1, 40]])

tf.linalg.band_part(
    input, num_lower = 2, num_upper = 1, name=None
)

>><tf.Tensor: shape=(4, 4), dtype=int64, numpy=
array([[10,  1,  0,  0],
       [ 1, 20,  1,  0],
       [-2,  1, 30,  1],
       [ 0, 10,  1, 40]])>