import torch


def xcal(x: torch.Tensor, thr: torch.Tensor) -> torch.Tensor:
    """Computes the XCAL learning function on a tensor (vectorized)

    See the Emergent docs for more info.

    Args:
      x: A 1D tensor of inputs to the function.
      thr: A 1D tensor of threshold values.

    Returns:
      A tensor with XCAL computed for each value.

    """
    d_thr = 0.0001
    d_rev = 0.1
    result = torch.zeros_like(x)

    mask = x > (thr * d_rev)
    result[mask] = x[mask] - thr[mask]
    result[~mask] = -x[~mask] * ((1 - d_rev) / d_rev)

    result[x < d_thr] = 0
    return result