import torch


def order_parameter_cos(x):
    """
    The order parameter calculated based on the cosine formula in the paper.
    :param x: the state of the oscillators
    :return: The r value
    """
    n = x.shape[-1]
    diff = torch.cos(x.unsqueeze(-1) - x.unsqueeze(-2))
    sum_diff = (diff).sum(-1).sum(-1)
    r = (1 / n) * (sum_diff ** (1 / 2))
    return r