import numpy as np EPS = np.finfo(float).eps def get_arrays_tol(*arrays): """ Get a relative tolerance for a set of arrays. Parameters ---------- *arrays: tuple Set of `numpy.ndarray` to get the tolerance for. Returns ------- float Relative tolerance for the set of arrays. Raises ------ ValueError If no array is provided. """ if len(arrays) == 0: raise ValueError("At least one array must be provided.") size = max(array.size for array in arrays) weight = max( np.max(np.abs(array[np.isfinite(array)]), initial=1.0) for array in arrays ) return 10.0 * EPS * max(size, 1.0) * weight def exact_1d_array(x, message): """ Preprocess a 1-dimensional array. Parameters ---------- x : array_like Array to be preprocessed. message : str Error message if `x` cannot be interpreter as a 1-dimensional array. Returns ------- `numpy.ndarray` Preprocessed array. """ x = np.atleast_1d(np.squeeze(x)).astype(float) if x.ndim != 1: raise ValueError(message) return x def exact_2d_array(x, message): """ Preprocess a 2-dimensional array. Parameters ---------- x : array_like Array to be preprocessed. message : str Error message if `x` cannot be interpreter as a 2-dimensional array. Returns ------- `numpy.ndarray` Preprocessed array. """ x = np.atleast_2d(x).astype(float) if x.ndim != 2: raise ValueError(message) return x