import torch


def listperm2matperm(listperm):
    """Converts permutation list to matrix form.

    Args:
    listperm: (..., n) - tensor of list permutations of the set [n].

    Return:
    matperm: (..., n, n) - permutation matrices,
    matperm[t, i, listperm[t,i]] = 1
    """
    n = listperm.size(-1)
    P = torch.eye(n, dtype=torch.long, device=listperm.device)[listperm]
    return P