I have rather simple question but still couldn´t make it work.
I want a block diagonal n^2*n^2 matrix. The blocks are sparse n*n matrices with just the diagonal, first off diagonals and forth off diag. For the simple case of n=4
this can easily be done
datanew = ones((5,n1))
datanew[2] = -2*datanew[2]
diagsn = [-4,-1,0,1,4]
DD2 = sparse.spdiags(datanew,diagsn,n,n)
new = sparse.block_diag([DD2,DD2,DD2,DD2])
Since this only useful for small n's, is there a way better way to use block_diag? Thinking of n -> 1000
A simple way of constructing a long list of
DD2
matrices, is with a list comprehension:At least in my version,
block_diag
wants a list of arrays, not*args
:This probably isn't the fastest way to construct such a block diagonal array, but it's a start.
================
Looking at the code for
sparse.block_mat
I deduce that it does:In other words,
rows
is a 'matrix' ofNone
withDD2
along the diagonals. It then passes these tosparse.bmat
.bmat
in turn collects thedata,rows,cols
from thecoo
format of all the input matricies, joins them into master arrays, and builds a newcoo
matrix from them.So an alternative is to construct those 3 arrays directly.