def norm(arr):
    """ Demean and normalize a given input to unit std. """
    if arr.ndim == 1:
        arr -= arr.mean()
        arr /= arr.std()
    else:
        arr -= arr.mean(axis=-1, keepdims=True)
        arr = (arr.T / arr.std(axis=-1)).T
    return arr