From cc4a0959291531d29faee61035786426f7b915c4 Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Thu, 2 Jan 2025 21:33:00 +0100 Subject: [PATCH] Update continuous_space.py --- .../continuous_space/continuous_space.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mesa/experimental/continuous_space/continuous_space.py b/mesa/experimental/continuous_space/continuous_space.py index 6474cb1fc33..c3ee6e7c771 100644 --- a/mesa/experimental/continuous_space/continuous_space.py +++ b/mesa/experimental/continuous_space/continuous_space.py @@ -115,11 +115,15 @@ def calculate_difference_vector( if indices is not None else self.agent_positions ) - delta = point[np.newaxis, :] - positions + delta = positions - point[np.newaxis, :] if self.torus: - delta = np.abs(delta, out=delta) - delta = np.minimum(delta, self.size - delta, out=delta) + inverse_delta = delta - np.sign(delta) * self.size + logical = np.abs(delta) < np.abs(inverse_delta) + out = np.zeros(delta.shape) + out[logical] = delta[logical] + out[~logical] = inverse_delta[~logical] + delta = out return delta @@ -138,7 +142,13 @@ def calculate_distances(self, point, indices=None) -> tuple[np.ndarray, np.ndarr ) if self.torus: - delta = self.calculate_difference_vector(point) + delta = point[np.newaxis, :] - positions + + delta = np.abs(delta, out=delta) + delta = np.minimum( + delta, self.size - delta, out=delta + ) + dists = np.linalg.norm(delta, axis=1) else: dists = cdist(point[np.newaxis, :], positions)[:, 0]