scipy.sparse.block_diag#

scipy.sparse.block_diag(mats, format=None, dtype=None)[source]#

Build a block diagonal sparse matrix from provided matrices.

Parameters
matssequence of matrices

Input matrices.

formatstr, optional

The sparse format of the result (e.g., “csr”). If not given, the matrix is returned in “coo” format.

dtypedtype specifier, optional

The data-type of the output matrix. If not given, the dtype is determined from that of blocks.

Returns
ressparse matrix

See also

bmat, diags

Notes

New in version 0.11.0.

Examples

>>> from scipy.sparse import coo_matrix, block_diag
>>> A = coo_matrix([[1, 2], [3, 4]])
>>> B = coo_matrix([[5], [6]])
>>> C = coo_matrix([[7]])
>>> block_diag((A, B, C)).toarray()
array([[1, 2, 0, 0],
       [3, 4, 0, 0],
       [0, 0, 5, 0],
       [0, 0, 6, 0],
       [0, 0, 0, 7]])