Home > Software design >  Reshaping a numpy array to a lower triangular matrix
Reshaping a numpy array to a lower triangular matrix

Time:09-22

I want to reshape an arbitrary 1-d Numpy array, call it a, into a specific lower triangular matrix, call it m. The following example illustrates the transformation.

Start with a 1-d array, a

array([ 3,  2,  9, 12])

and create the following lower triangular matrix, m

array([[ 3,  0,  0,  0],
       [ 2,  3,  0,  0],
       [ 9,  2,  3,  0],
       [12,  9,  2,  3]])

CodePudding user response:

If you have scipy available then there is scipy.linalg.toeplitz:

from scipy import linalg

linalg.toeplitz([3,2,9,12],[0,0,0,0])
# array([[ 3,  0,  0,  0],
#        [ 2,  3,  0,  0],
#        [ 9,  2,  3,  0],
#        [12,  9,  2,  3]])
  • Related