def test_errors_raise(self): """Test errors raise.""" with self.assertRaises(ValueError): GaussLaguerre(10, -1) with self.assertRaises(ValueError): TanhSinh(10, 1) with self.assertRaises(ValueError): Simpson(4) with self.assertRaises(ValueError): GaussChebyshevType2(-10) with self.assertRaises(ValueError): GaussChebyshevLobatto(-10) with self.assertRaises(ValueError): Trapezoidal(-10) with self.assertRaises(ValueError): RectangleRuleSineEndPoints(-10) with self.assertRaises(ValueError): RectangleRuleSine(-10) with self.assertRaises(ValueError): TanhSinh(-11, 1) with self.assertRaises(ValueError): Simpson(-11) with self.assertRaises(ValueError): MidPoint(-10) with self.assertRaises(ValueError): ClenshawCurtis(-10) with self.assertRaises(ValueError): FejerFirst(-10) with self.assertRaises(ValueError): FejerSecond(-10)
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_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_MidPoint(self): """Test for midpoint rule.""" grid = MidPoint(10) points = np.zeros(10) weights = np.ones(10) idx = np.arange(10) weights *= 2 / 10 points = -1 + (2 * idx + 1) / 10 assert_allclose(grid.points, points) assert_allclose(grid.weights, weights)
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_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)