fill_triangular_matrix#

bayesflow.utils.fill_triangular_matrix(x: Tensor, upper: bool = False, positive_diag: bool = False)[source]#

Reshapes a batch of matrix elements into a triangular matrix (either upper or lower).

Note: If final axis has length 1, this simply reshapes to (batch_size, 1, 1) and optionally applies softplus.

Parameters:
xTensor of shape (batch_size, m)

Batch of flattened nonzero matrix elements for triangular matrix.

upperbool

Return upper triangular matrix if True, else lower triangular matrix. Default is False.

positive_diagbool

Whether to apply a softplus operation to diagonal elements. Default is False.

Returns:
Tensor of shape (batch_size, n, n)

Batch of triangular matrices with m = n * (n + 1) / 2 unique nonzero elements.

Raises:
ValueError

If provided nonzero elements do not correspond to possible triangular matrix shape (n,n) with n = sqrt( 1/4 + 2 * m) - 1/2 due to m = n * (n + 1) / 2.