def linear_rescale()

in bayesmark/np_util.py [0:0]


def linear_rescale(X, lb0, ub0, lb1, ub1, enforce_bounds=True):
    """Linearly transform all elements of `X`, bounded between `lb0` and `ub0`, to be between `lb1` and `ub1`.

    Shapes of all input variables must be broadcast compatible.

    Parameters
    ----------
    X : :class:`numpy:numpy.ndarray`
        Array containing elements to rescale.
    lb0 : :class:`numpy:numpy.ndarray`
        Current lower bound of `X`.
    ub0 : :class:`numpy:numpy.ndarray`
        Current upper bound of `X`.
    lb1 : :class:`numpy:numpy.ndarray`
        Desired lower bound of `X`.
    ub1 : :class:`numpy:numpy.ndarray`
        Desired upper bound of `X`.
    enforce_bounds : bool
        If True, perform input bounds check (and clipping if slight violation) on the input `X` and again on the
        output. This argument is not meant to be vectorized like the other input variables.

    Returns
    -------
    X : :class:`numpy:numpy.ndarray`
        Elements of input `X` after linear rescaling.
    """
    assert np.all(np.isfinite(lb0))
    assert np.all(np.isfinite(lb1))
    assert np.all(np.isfinite(ub0))
    assert np.all(np.isfinite(ub1))
    assert np.all(lb0 < ub0)
    assert np.all(lb1 <= ub1)

    m = np.true_divide(ub1 - lb1, ub0 - lb0)
    assert np.all(m >= 0)

    if enforce_bounds:
        X = clip_chk(X, lb0, ub0)  # This will flag any non-finite X input.
        X = clip_chk(m * (X - lb0) + lb1, lb1, ub1)
    else:
        X = m * (X - lb0) + lb1
    return X