import torch


def _trunc(x, minval=None, maxval=None):
    """Truncate vector values to have values on range [minval, maxval]"""
    x = torch.clone(x)
    if minval != None:
        x[x < minval] = minval
    if maxval != None:
        x[x > maxval] = maxval
    return x