def test_interpolation_of_gaussian_vertorized(self): r"""Test interpolation of a Gaussian function with vectorization.""" oned = MidPoint(50) cubic = Tensor1DGrids(oned, oned, oned) def gaussian(points): return np.exp(-3 * np.linalg.norm(points, axis=1)**2.0) gaussian_pts = gaussian(cubic.points) num_pts = 500 random_pts = np.random.uniform(-0.9, 0.9, (num_pts, 3)) interpolated = cubic.interpolate(random_pts, gaussian_pts, use_log=False) assert_allclose(interpolated, gaussian(random_pts), rtol=1e-5, atol=1e-6) interpolated = cubic.interpolate(random_pts, gaussian_pts, use_log=True) assert_allclose(interpolated, gaussian(random_pts), rtol=1e-5, atol=1e-6)
def test_origin_of_tensor1d_grids(self): r"""Test that the origin is the first point.""" oned = GaussLaguerre(10) oned2 = GaussLaguerre(20) grid = Tensor1DGrids(oned, oned2) assert_allclose(np.array([oned.points[0], oned2.points[0]]), grid.origin)
def test_interpolation_of_various_derivative_gaussian_using_logarithm( self): r"""Test interpolation of the thederivatives of a Gaussian function.""" oned = MidPoint(100) cubic = Tensor1DGrids(oned, oned, oned) def gaussian(points): return np.exp(-3 * np.linalg.norm(points, axis=1)**2.0) def derivative_wrt_one_var(point, i_var_deriv): return (np.exp(-3 * np.linalg.norm(point)**2.0) * point[i_var_deriv] * (-3 * 2.0)) def derivative_second_x(point): return np.exp(-3 * np.linalg.norm(point)**2.0) * point[0]**2.0 * ( -3 * 2.0)**2.0 + np.exp( -3 * np.linalg.norm(point)**2.0) * (-3 * 2.0) gaussian_pts = gaussian(cubic.points) pt = np.random.uniform(-0.5, 0.5, (3, )) # Test taking derivative in x-direction interpolated = cubic.interpolate(pt[np.newaxis, :], gaussian_pts, use_log=True, nu_x=1) assert_allclose(interpolated, derivative_wrt_one_var(pt, 0), rtol=1e-4) # Test taking derivative in y-direction interpolated = cubic.interpolate(pt[np.newaxis, :], gaussian_pts, use_log=True, nu_y=1) assert_allclose(interpolated, derivative_wrt_one_var(pt, 1), rtol=1e-4) # Test taking derivative in z-direction interpolated = cubic.interpolate(pt[np.newaxis, :], gaussian_pts, use_log=True, nu_z=1) assert_allclose(interpolated, derivative_wrt_one_var(pt, 2), rtol=1e-4) # Test taking second-derivative in x-direction interpolated = cubic.interpolate(pt[np.newaxis, :], gaussian_pts, use_log=True, nu_x=2, nu_y=0, nu_z=0) assert_allclose(interpolated, derivative_second_x(pt), rtol=1e-4) # Test raises error with self.assertRaises(NotImplementedError): cubic.interpolate(pt[np.newaxis, :], gaussian_pts, use_log=True, nu_x=2, nu_y=2)
def test_integration_of_gaussian(self): r"""Test integration of a rapidly-decreasing Gaussian.""" oned = MidPoint(250) cubic = Tensor1DGrids(oned, oned, oned) def gaussian(points): return np.exp(-6 * np.linalg.norm(points, axis=1)**2.0) gaussian_pts = gaussian(cubic.points) desired = np.sqrt(np.pi / 6)**3 actual = cubic.integrate(gaussian_pts) assert_allclose(desired, actual, atol=1e-3)
def test_raise_error_when_constructing_grid(self): r"""Test raising of error when constructing Tensor1DGrids.""" oned = GaussLaguerre(10) # Test raises error with self.assertRaises(TypeError) as err: Tensor1DGrids(5, oned, oned) self.assertEqual( "Argument oned_x should be an instance of `OneDGrid`, got <class 'int'>", str(err.exception), ) with self.assertRaises(TypeError) as err: Tensor1DGrids(oned, 5, oned) self.assertEqual( "Argument oned_y should be an instance of `OneDGrid`, got <class 'int'>", str(err.exception), ) with self.assertRaises(TypeError) as err: Tensor1DGrids(oned, oned, 5) self.assertEqual( "Argument oned_z should be an instance of `OneDGrid`, got <class 'int'>", str(err.exception), )
def test_interpolation_of_linear_function_using_scipy_linear_method(self): r"""Test interpolation of a linear function using scipy with linear method.""" oned = MidPoint(50) cubic = Tensor1DGrids(oned, oned, oned) def linear_func(points): return np.dot(np.array([1.0, 2.0, 3.0]), points.T) gaussian_pts = linear_func(cubic.points) num_pts = 50 random_pts = np.random.uniform(-0.9, 0.9, (num_pts, 3)) interpolated = cubic.interpolate(random_pts, gaussian_pts, use_log=False, method="linear") assert_allclose(interpolated, linear_func(random_pts))
def test_point_and_weights_are_correct(self): r"""Test that the points and weights are correctly computed.""" oned = GaussLaguerre(10) cubic = Tensor1DGrids(oned, oned, oned) index = 0 # Index for cubic points. for i in range(oned.size): for j in range(oned.size): for k in range(oned.size): actual_pt = np.array( [oned.points[i], oned.points[j], oned.points[k]]) assert_allclose(actual_pt, cubic.points[index, :]) actual_weight = oned.weights[i] * oned.weights[ j] * oned.weights[k] assert_allclose(actual_weight, cubic.weights[index]) index += 1
def test_moving_coordinates_to_index_and_back_two_dimensions(self): r"""Test moving from coordinates and index and back in two dimensions.""" oned = MidPoint(3) cubic = Tensor1DGrids(oned, oned) # Convert index to coordinate. index = 3 coord = (1, 0) assert_allclose(coord, cubic.index_to_coordinates(index)) # Convert coordinate to index coord = (1, 1) index = 4 assert_allclose(index, cubic.coordinates_to_index(coord)) # Convert back index = 1 assert_allclose( index, cubic.coordinates_to_index(cubic.index_to_coordinates(index)))
def test_interpolation_of_constant_function_using_scipy_nearest_method( self): r"""Test interpolation of a constant function using scipy with nearest method.""" oned = MidPoint(50) cubic = Tensor1DGrids(oned, oned, oned) def linear_func(points): return (np.array([1.0] * points.shape[0]) + np.random.random( (points.shape[0])) * 1.0e-6) gaussian_pts = linear_func(cubic.points) num_pts = 5 random_pts = np.random.uniform(-0.9, 0.9, (num_pts, 3)) for pt in random_pts: interpolated = cubic.interpolate(pt, gaussian_pts, use_log=False, method="nearest") assert_allclose(interpolated, linear_func(np.array([pt]))[0], rtol=1e-6)
def test_moving_coordinates_to_index_and_back_three_dimensions(self): r"""Test moving from coordinates and index and back in three_dimensions.""" oned = MidPoint(3) cubic = Tensor1DGrids(oned, oned, oned) # Convert index to coordinate. index = 3 coord = (0, 1, 0) assert_allclose(coord, cubic.index_to_coordinates(index)) # Convert coordinate to index coord = (1, 0, 1) index = 10 assert_allclose(index, cubic.coordinates_to_index(coord)) # Convert back index = 9 assert_allclose( index, cubic.coordinates_to_index(cubic.index_to_coordinates(index))) # Test raises error when index isn't positive with self.assertRaises(ValueError): cubic.index_to_coordinates(-1)