def replace(arr, mask, val):
    """Replace values in mask with val; returns a copy of the array.

    Parameters
    ----------
    arr: ndarray
        array of values to replace
    mask: ndarray
        boolean mask of values to replace
    val: float
        value replacing the elements on the mask

    Returns
    -------
    res: ndarray
        a copy of the array with the values replaced
    """
    res = arr.copy()
    res[mask] = val
    return res