Sometimes it is useful to build a matrix from matrices forming blocks of the former one.
As I understand, neither numpy nor scipy implement this functionality. Scipy has the scipy.linalg.special_matrices.block_diag function, but it only admits blocks along the diagonal. The following function builds a matrix from the elements of the input list a, as long as their dimension is compatible.
def blockmat(a):
n=size(a,0)
N=size(a[0],1)
sqn=sqrt(n)
ind=0
rows=[]
cols=[]
for i in range(0,n):
A=a[i]
M=size(A,0)
#if (M != N):
# return -1
if ((i>0) & (i%int(sqn) == 0)):
rows.append(numpy.concatenate(cols,axis=0))
cols=[]
cols.append(A)
rows.append(numpy.concatenate(cols,axis=0))
return numpy.concatenate(rows,axis=1)
No comments:
Post a Comment