Python Forum

Full Version: extract lower/upper antitriangular matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In numpy you can use np.tril(x) to extract the lower triangular matrix of an array x. However, I am interested in extracting the lower (upper) antitriangular matrix, that is, with zeros above (below) the antidiagonal. Is there a way to do this in python?
Look at the numpy and scipy linear algebra libraries.
You could perhaps compose np.tril() and np.triu() with np.flip()
>>> import numpy as np
>>> a = np.array(range(1, 17)).reshape((4,4))
>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
>>> np.flip(a, 0)
array([[13, 14, 15, 16],
       [ 9, 10, 11, 12],
       [ 5,  6,  7,  8],
       [ 1,  2,  3,  4]])
>>> np.flip(np.tril(np.flip(a, 0)), 0)
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  0],
       [ 9, 10,  0,  0],
       [13,  0,  0,  0]])
>>> np.flip(np.triu(np.flip(a, 0)), 0)
array([[ 0,  0,  0,  4],
       [ 0,  0,  7,  8],
       [ 0, 10, 11, 12],
       [13, 14, 15, 16]])
>>>