[SciPy-user] Easy way to make a block diagonal matrix?

Stéfan van der Walt stefan@sun.ac...
Wed May 20 17:11:17 CDT 2009


2009/5/20 Joseph Smidt <josephsmidt@gmail.com>:
> Actually,  I don't know if you could submit this routine for inclusion
> into scipy itself.  I'm sure there are lots of people who need to
> create block diagonal arrays like this.  Plus, your script looks
> really well written.

I'd be glad if others find it useful.  I'm not quite sure where in
SciPy it would go, though?

I see the scipy.sparse module has similar functionality, although it
is a bit more painful to use in this situation:

In [2]: import scipy.sparse as ss

In [3]: A = np.array([[1, 2], [3, 4]])

In [4]: B = np.array([[1, 2, 3, 4]])

In [5]: C = np.array([[4]])

In [10]: ss.bmat([[A, None, None], [None, B, None], [None, None, C]]).todense()
Out[10]:
matrix([[1, 2, 0, 0, 0, 0, 0],
        [3, 4, 0, 0, 0, 0, 0],
        [0, 0, 1, 2, 3, 4, 0],
        [0, 0, 0, 0, 0, 0, 4]])

More generally:

import scipy.sparse as ss
import numpy as np

def block_diag(*arrs):
    arrs = [np.asarray(a) for a in arrs]
    D = len(arrs)
    Dr = np.arange(D)
    diag_arr = np.empty((D, D), dtype=object)
    diag_arr[Dr, Dr] = arrs
    return ss.bmat(diag_arr).todense()

Cheers
Stéfan


More information about the SciPy-user mailing list