import torch


def hat_inv(h: torch.Tensor):
    """
    Compute the inverse Hat operator [1] of a batch of 3x3 matrices.

    Args:
        h: batch of skew-symmetric matrices of shape :math:`(\text{minibatch}, 3, 3)`

    Returns:
        batch of 3d vectors of shape :math:`(\text{minibatch}, 3)`

    Raises:
        ValueError if `h` is of incorrect shape
        ValueError if `h` not skew-symmetric

    [1] https://en.wikipedia.org/wiki/Hat_operator
    """

    N , dim1, dim2 = h.shape
    if dim1 != 3 or dim2 != 3:
        raise ValueError('Input has to be a batch of 3x3 Tensors.')

    ss_diff = (h + h.permute(0, 2, 1)).abs().max()
    if float(ss_diff) > 1e-5:
        raise ValueError('One of input matrices not skew-symmetric.')

    x = h[:, 2, 1]
    y = h[:, 0, 2]
    z = h[:, 1, 0]

    v = torch.stack((x, y, z), dim=1)

    return v