import torch


def phi(A: torch.Tensor) -> torch.Tensor:
    """Phi operator.

    See here for more details https://homepages.inf.ed.ac.uk/imurray2/pub/16choldiff/choldiff.pdf

    Parameters
    ----------
    A: torch.Tensor, shape=(B, X, X)
        A batched matrix.

    Returns
    -------
    B: torch.Tensor, shape=(B, X, X)
        Phi(A)
    """
    B = torch.tril(A)
    B.diagonal(dim1=-2, dim2=-1)[:] /= 2  # type: ignore
    return B