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
Use the function scipy.sparse.diags.
Example:
from scipy.sparse import diags
import numpy as np
#
n = 10
k = np.array([np.ones(n-1),-2*np.ones(n),np.ones(n-1)])
offset = [-1,0,1]
A = diags(k,offset).toarray()
This returns:
array([[-2., 1., 0., 0., 0.],
[ 1., -2., 1., 0., 0.],
[ 0., 1., -2., 1., 0.],
[ 0., 0., 1., -2., 1.],
[ 0., 0., 0., 1., -2.]])