{ } Raw JSON

bundles / scipy 1.17.1 / scipy / linalg / _special_matrices / block_diag

function

scipy.linalg._special_matrices:block_diag

source: /scipy/linalg/_special_matrices.py :361

Signature

def   block_diag ( * arrs )

Summary

Create a block diagonal array from provided arrays.

Extended Summary

For example, given 2-D inputs A, B and C, the output will have these arrays arranged on the diagonal

[[A, 0, 0],
 [0, B, 0],
 [0, 0, C]]

The documentation is written assuming array arguments are of specified "core" shapes. However, array argument(s) of this function may have additional "batch" dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see linalg_batch for details.

Parameters

A, B, C, ... : array_like

Input arrays. A 1-D array or array_like sequence of length n is treated as a 2-D array with shape (1, n).

Returns

D : ndarray

Array with A, B, C, ... on the diagonal of the last two dimensions. D has the same dtype as the result type of the inputs.

Notes

If all the input arrays are square, the output is known as a block diagonal matrix.

Empty sequences (i.e., array-likes of zero size) will not be ignored. Noteworthy, both [] and [[]] are treated as matrices with shape (1,0).

Array API Standard Support

block_diag has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ✅                   
JAX                   ⚠️ no JIT             ⚠️ no JIT           
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

import numpy as np
from scipy.linalg import block_diag
A = [[1, 0],
     [0, 1]]
B = [[3, 4, 5],
     [6, 7, 8]]
C = [[7]]
P = np.zeros((2, 0), dtype='int32')
block_diag(A, B, C)
block_diag(A, P, B, C)
block_diag(1.0, [2, 3], [[4, 5], [6, 7]])

Aliases

  • scipy.linalg.block_diag

Referenced by