def test_1D(self): '''test a simple 1D linear interpolation''' smin = np.array([0.]) smax = np.array([2.]) orders = np.array([3]) # ie. grid = [0,1,2] grid = np.linspace(smin[0], smax[0], orders[0]) def f(x): 'function to interpolate' return x**2 values = f(grid) values = np.ascontiguousarray(np.atleast_2d(values)) eval_pts = np.linspace(smin[0], smax[0], 5) eval_pts = np.ascontiguousarray(np.atleast_2d(eval_pts)) print(eval_pts) interp_values = multilinear_interpolation(smin, smax, orders, values, eval_pts) print('interpolated values:') print(interp_values) interp_expected = np.array([0, 0.5, 1, 2.5, 4]) # linear interp. "by hand" print('expected interpolated values:') print(interp_expected) assert_true(np.all(np.abs(interp_values - interp_expected) < 1e-10))
def test_1D(self): """test a simple 1D linear interpolation""" smin = np.array([0.0]) smax = np.array([2.0]) orders = np.array([3]) # ie. grid = [0,1,2] grid = np.linspace(smin[0], smax[0], orders[0]) def f(x): "function to interpolate" return x ** 2 values = f(grid) values = np.ascontiguousarray(np.atleast_2d(values)) eval_pts = np.linspace(smin[0], smax[0], 5) eval_pts = np.ascontiguousarray(np.atleast_2d(eval_pts)) print(eval_pts) interp_values = multilinear_interpolation(smin, smax, orders, values, eval_pts) print("interpolated values:") print(interp_values) interp_expected = np.array([0, 0.5, 1, 2.5, 4]) # linear interp. "by hand" print("expected interpolated values:") print(interp_expected) assert_true(np.all(np.abs(interp_values - interp_expected) < 1e-10))
def __call__(self, *x_interp): '''evaluate the interpolated function at coordinates `x_interp` output shape is the shape of broadcasted coordinate inputs. ''' assert len(x_interp) == self.ndim # Prepare the interpolated coordinates array x_mesh = np.broadcast_arrays(*x_interp) shape = x_mesh[0].shape x_stack = np.row_stack([x.astype(float).ravel() for x in x_mesh]) # a = multilinear_interpolation(self._xmin, self._xmax, self._xshape, self.values, x_stack) a = a.reshape(shape) return a