import torch


def interp(x: torch.Tensor, xp: torch.Tensor, fp: torch.Tensor) -> torch.Tensor:
    """Interpolate ``x`` tensor according to ``xp`` and ``fp`` as in ``np.interp``.

    This implementation cannot reproduce numpy results identically, but reasonable.
    Code referred to `here <https://github.com/pytorch/pytorch/issues/1552#issuecomment-926972915>`_.

    Args:
        x: the input tensor that needs to be interpolated.
        xp: the x-coordinates of the referred data points.
        fp: the y-coordinates of the referred data points, same length as ``xp``.

    Returns:
        The interpolated values, same shape as ``x``.
    """
    if x.dim() != xp.dim() != fp.dim() != 1:
        raise ValueError(
            f"Required 1D vector across ``x``, ``xp``, ``fp``. Got {x.dim()}, {xp.dim()}, {fp.dim()}."
        )

    slopes = (fp[1:] - fp[:-1]) / (xp[1:] - xp[:-1])
    locs = torch.searchsorted(xp, x)
    locs = locs.clip(1, len(xp) - 1) - 1
    return slopes[locs] * (x - xp[locs]) + xp[locs]