def tensor_extend_new_dim(x, dim, n):
    """Extending the tensor along a new dimension with a replica of n.

    Args:
        x (Tensor): tensor to be extended
        dim (int): the value indicating the position of the newly
            inserted dimension
        n (int): the number of replica along dim
    Returns:
        Tensor: the extended tensor. Its shape is ``(*x.shape[0:dim], n, *x.shape[dim:])``
    """
    return x.unsqueeze(dim).expand(*x.shape[0:dim], n, *x.shape[dim:])