fn

block_diag

Tensor
block_diag(tensors: Tensor = ())
source

Construct a block-diagonal matrix from a sequence of blocks.

Each input is placed on the diagonal of a larger matrix, with zeros everywhere off-block. Scalars (0-D) become 1×11 \times 1 blocks and 1-D inputs become 1×n1 \times n blocks before stacking.

Parameters

*tensorsTensor
One or more 0-, 1-, or 2-D tensors to place on the diagonal, in order from top-left to bottom-right.

Returns

Tensor

2-D block-diagonal matrix of shape (sum(h_i), sum(w_i)) where h_i, w_i are the (promoted) block heights and widths.

Notes

For blocks A1,A2,,AkA_1, A_2, \dots, A_k, the result is

blockdiag(A1,A2,,Ak)=(A1000A2000Ak).\operatorname{blockdiag}(A_1, A_2, \dots, A_k) = \begin{pmatrix} A_1 & 0 & \cdots & 0 \\ 0 & A_2 & \cdots & 0 \\ \vdots & & \ddots & \vdots \\ 0 & 0 & \cdots & A_k \end{pmatrix}.

Called with zero arguments, returns an empty (0, 0) matrix.

Examples

>>> import lucid
>>> A = lucid.tensor([[1., 2.], [3., 4.]])
>>> B = lucid.tensor([[5.]])
>>> lucid.block_diag(A, B)
Tensor([[1., 2., 0.],
        [3., 4., 0.],
        [0., 0., 5.]])