From 2e6d80908d5cee095220187bf1627693a0aa1930 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Wed, 18 Nov 2026 10:35:47 -0700 Subject: [PATCH 1/2] Support slices of spatial inertia sequences Retain Python list slicing while keeping integer construction unchanged. --- spatialmath/spatialvector.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spatialmath/spatialvector.py b/spatialmath/spatialvector.py index 52a2641c..7c6f9ee9 100644 --- a/spatialmath/spatialvector.py +++ b/spatialmath/spatialvector.py @@ -610,6 +610,10 @@ def shape(self): return (6, 6) def __getitem__(self, i): + if isinstance(i, slice): + result = SpatialInertia.Empty() + result.data = self.data[i] + return result return SpatialInertia(self.data[i]) def __repr__(self): From 0fc759cac68a719ff88f8df4b55f4f5a5396d89c Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Wed, 18 Nov 2026 10:36:02 -0700 Subject: [PATCH 2/2] Test spatial inertia slicing and dynamics controls Cover empty, singleton and longer sequences, reversed and stepped slices, independent list storage, integer indexing and dynamics. --- tests/test_spatial_inertia_slice.py | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/test_spatial_inertia_slice.py diff --git a/tests/test_spatial_inertia_slice.py b/tests/test_spatial_inertia_slice.py new file mode 100644 index 00000000..6489bb6c --- /dev/null +++ b/tests/test_spatial_inertia_slice.py @@ -0,0 +1,57 @@ +import numpy as np +import pytest +from numpy.testing import assert_allclose +from spatialmath import SpatialInertia, SpatialVelocity, SpatialAcceleration + + +@pytest.mark.parametrize("count", [0, 1, 3, 6]) +@pytest.mark.parametrize( + "selection", + [ + slice(None), + slice(0, 1), + slice(0, 0), + slice(None, None, -1), + slice(1, None, 2), + slice(-2, None), + slice(99, 100), + ], +) +def test_slice_matches_python_list(count, selection): + inertia = SpatialInertia.Empty() + for k in range(count): + inertia.append(SpatialInertia(m=k + 1, r=[0.1 * k, 0.2, -0.3], I=np.eye(3))) + original = [a.copy() for a in inertia.data] + sliced = inertia[selection] + assert type(sliced) is SpatialInertia + assert sliced.data is not inertia.data + expected = inertia.data[selection] + assert len(sliced) == len(expected) + for actual, reference in zip(sliced.data, expected): + assert_allclose(actual, reference) + sliced.append(SpatialInertia()) + assert len(inertia) == count + for actual, reference in zip(inertia.data, original): + assert_allclose(actual, reference) + + +@pytest.mark.parametrize("index", [0, 1, -1]) +def test_integer_index_and_dynamics(index): + inertia = SpatialInertia.Empty() + for m in [1, 2, 3]: + inertia.append(SpatialInertia(m=m, r=[1, 2, 3], I=np.eye(3))) + assert_allclose(inertia[index].A, inertia.data[index]) + one = inertia[index : index + 1] if index >= 0 else inertia[-1:] + for vector_type in [SpatialVelocity, SpatialAcceleration]: + vector = vector_type([1, 2, 3, 4, 5, 6]) + assert_allclose((one * vector).A, inertia.data[index] @ vector.A) + + +def test_invalid_index_still_raises(): + inertia = SpatialInertia() + with pytest.raises(IndexError): + inertia[1] + with pytest.raises(TypeError): + inertia["bad"] + with pytest.raises(ValueError): + inertia[::0]