From 0d1d2c325050fc872b1b8bf86c7a6d4e3f6e1e54 Mon Sep 17 00:00:00 2001 From: Syed Ahnaf Raza Date: Tue, 10 Nov 2026 02:14:07 +0500 Subject: [PATCH 1/2] Compute moments on grids with points of shape (N,) Grid.moments read self.points.shape[1], so a grid whose points have shape (N,), such as every OneDGrid, raised IndexError. Treat those points as N points in one dimension, like shape (N, 1). --- src/grid/basegrid.py | 14 ++++++++------ src/grid/tests/test_grid.py | 9 +++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/grid/basegrid.py b/src/grid/basegrid.py index 3579dfea..e0e45883 100644 --- a/src/grid/basegrid.py +++ b/src/grid/basegrid.py @@ -249,19 +249,21 @@ def moments( each row/order is, e.g. for Cartesian, [(0, 0, 0), (1, 0, 0) ,...]. """ + # Points of shape (N,), as in OneDGrid, are N points in one dimension + points = self.points.reshape(len(self.points), -1) if func_vals.ndim > 1: raise ValueError(f"`func_vals` {func_vals.ndim} should have dimension one.") if centers.ndim != 2: raise ValueError(f"`centers` {centers.ndim} should have dimension one or two.") - if self.points.shape[1] != centers.shape[1]: + if points.shape[1] != centers.shape[1]: raise ValueError( - f"The dimension of the grid {self.points.shape[1]} should" + f"The dimension of the grid {points.shape[1]} should" f"match the dimension of the centers {centers.shape[1]}." ) - if len(func_vals) != self.points.shape[0]: + if len(func_vals) != points.shape[0]: raise ValueError( f"The length of function values {len(func_vals)} should match " - f"the number of points in the grid {self.points.shape[0]}." + f"the number of points in the grid {points.shape[0]}." ) if type_mom == "pure-radial" and orders == 0: raise ValueError( @@ -274,7 +276,7 @@ def moments( orders = range(0, orders + 1) if type_mom != "pure-radial" else range(1, orders + 1) else: raise TypeError(f"Orders {type(orders)} should be either integer, list or numpy array.") - dim = self.points.shape[1] + dim = points.shape[1] all_orders = generate_orders_horton_order(orders[0], type_mom, dim) for l_ord in orders[1:]: all_orders = np.vstack((all_orders, generate_orders_horton_order(l_ord, type_mom, dim))) @@ -282,7 +284,7 @@ def moments( integrals = [] for center in centers: # Calculate centered pts: [(X-c), (Y-c), (Z-c)] - centered_pts = self.points - center + centered_pts = points - center if type_mom == "cartesian": # Take the powers to get [(X-c)^mx, (Y-c)^my, (Z-c)^mz] diff --git a/src/grid/tests/test_grid.py b/src/grid/tests/test_grid.py index 6dee4ac7..b3fc3b76 100644 --- a/src/grid/tests/test_grid.py +++ b/src/grid/tests/test_grid.py @@ -172,6 +172,15 @@ def test_cartesian_moments(self): expected = [np.sum((points - 0.5) ** n * func_vals * self._ref_weights) for n in range(4)] assert_allclose(integrals[:, 0], expected) + def test_cartesian_moments_flat_points(self): + """Test that points of shape (N,), as in OneDGrid, give the same moments as (N, 1).""" + points = self._ref_points[:, 0] + func_vals = np.exp(-(points**2)) + center = np.array([[0.5]]) + expected = self.grid.moments(3, center, func_vals, type_mom="cartesian") + for grid in [Grid(points, self._ref_weights), OneDGrid(points, self._ref_weights)]: + assert_allclose(grid.moments(3, center, func_vals, type_mom="cartesian"), expected) + class TestGrid2D(TestGrid): """Grid testcase class for 2D point arrays.""" From 96d54eb6ce6b3050919d459c9acc4598692d3288 Mon Sep 17 00:00:00 2001 From: Syed Ahnaf Raza Date: Thu, 19 Nov 2026 12:47:38 +0500 Subject: [PATCH 2/2] Keep 2D points as they are when computing moments Reshaping every points array with reshape(len(points), -1) fails for an empty local grid of shape (0, 3), whose moments used to be zero. Only add the singleton axis for points of shape (N,). --- src/grid/basegrid.py | 2 +- src/grid/tests/test_grid.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/grid/basegrid.py b/src/grid/basegrid.py index e0e45883..b1b4dc94 100644 --- a/src/grid/basegrid.py +++ b/src/grid/basegrid.py @@ -250,7 +250,7 @@ def moments( """ # Points of shape (N,), as in OneDGrid, are N points in one dimension - points = self.points.reshape(len(self.points), -1) + points = self.points if self.points.ndim == 2 else self.points.reshape(-1, 1) if func_vals.ndim > 1: raise ValueError(f"`func_vals` {func_vals.ndim} should have dimension one.") if centers.ndim != 2: diff --git a/src/grid/tests/test_grid.py b/src/grid/tests/test_grid.py index b3fc3b76..f9b474fb 100644 --- a/src/grid/tests/test_grid.py +++ b/src/grid/tests/test_grid.py @@ -109,6 +109,14 @@ def test_get_localgrid_empty(self): assert localgrid.weights.shape == (0,) assert_allclose(localgrid.center, center) + def test_moments_empty_localgrid(self): + """Moments over a local grid without points are zero.""" + center = self.grid.points.max(axis=0) + 10.0 + localgrid = self.grid.get_localgrid(center, 0.5) + centers = np.atleast_1d(center).reshape(1, -1) + moments = localgrid.moments(1, centers, np.zeros(0), type_mom="cartesian") + assert_allclose(moments, 0.0) + def test_get_localgrid_radius_inf(self): """Test the creation of the local grid with an infinite radius.""" localgrid = self.grid.get_localgrid(self.grid.points[3], np.inf)