Block tridiagonal matrix python

后端 未结 8 1489
甜味超标
甜味超标 2020-11-28 14:52

I would like to create a block tridiagonal matrix starting from three numpy.ndarray. Is there any (direct) way to do that in python?

Thank you in advance!

Ch

8条回答
  •  粉色の甜心
    2020-11-28 15:33

    @TheCorwoodRep's answer can actually be done in a single line. No need for a seperate function.

    np.eye(3,3,k=-1) + np.eye(3,3)*2 + np.eye(3,3,k=1)*3
    

    This produces:

    array([[ 2.,  3.,  0.],
           [ 1.,  2.,  3.],
           [ 0.,  1.,  2.]])
    

提交回复
热议问题