block_diag#
- scipy.sparse.block_diag(mats, format=None, dtype=None)[source]#
Build a block diagonal sparse matrix or array from provided matrices.
Warning
block_diagis switching to the sparse array interface.For the case where no input arrays are sparse, this function is switching to returning a sparse array instead of sparse matrix. Control the sparse return class by making at least one input sparse, e.g.,
block_diag([coo_matrix(A), B]), orblock_diag([coo_array(A), B]). That removes any deprecation warnings as well. For more general information about sparrays, see Migration from spmatrix to sparray. Handling of this no sparse input case will change no earlier than v1.20.- Parameters:
- matssequence of matrices or arrays
Input matrices or arrays.
- formatstr, optional
The sparse format of the result (e.g., “csr”). If not given, the result is returned in “coo” format.
- dtypedtype specifier, optional
The data-type of the output. If not given, the dtype is determined from that of blocks.
- Returns:
- ressparse matrix or array
If at least one input is a sparse array, the output is a sparse array. Otherwise the output is a sparse matrix.
See also
Notes
Added in version 0.11.0.
Examples
>>> from scipy.sparse import coo_array, block_diag >>> A = coo_array([[1, 2], [3, 4]]) >>> B = coo_array([[5], [6]]) >>> C = coo_array([[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]])