import math
import torch


def wrap_phi_to_2pi_torch(x):
    """Shift input angle x to the range of [-pi, pi]
    """
    pi = math.pi
    x = torch.fmod(2 * pi + torch.fmod(x + pi, 2 * pi), 2 * pi) - pi
    return x