diff --git a/spatialmath/spatialvector.py b/spatialmath/spatialvector.py index 52a2641c..12b96229 100644 --- a/spatialmath/spatialvector.py +++ b/spatialmath/spatialvector.py @@ -96,9 +96,9 @@ def __init__(self, value): super().__init__() if base.isvector(value, 6): - self.data = [np.array(value)] + self.data = [base.getvector(value, 6)] elif base.isvector(value, 3): - self.data = [np.r_[value, 0, 0, 0]] + self.data = [np.r_[base.getvector(value, 3), 0, 0, 0]] elif isinstance(value, SpatialVector): self.data = [value.A] elif base.ismatrix(value, (6, None)): diff --git a/tests/test_spatialvector_shaped_input.py b/tests/test_spatialvector_shaped_input.py new file mode 100644 index 00000000..9f41c9b1 --- /dev/null +++ b/tests/test_spatialvector_shaped_input.py @@ -0,0 +1,36 @@ +"""Row and column vectors must use the same flat internal representation.""" + +import numpy as np +import numpy.testing as nt +import pytest +from spatialmath import ( + SpatialVelocity, + SpatialAcceleration, + SpatialForce, + SpatialMomentum, +) + + +@pytest.mark.parametrize( + "cls", [SpatialVelocity, SpatialAcceleration, SpatialForce, SpatialMomentum] +) +@pytest.mark.parametrize("size", [3, 6]) +@pytest.mark.parametrize("shape", ["flat", "row", "column"]) +def test_shaped_vector_constructor(cls, size, shape): + values = np.arange(1, size + 1, dtype=float) + supplied = ( + values + if shape == "flat" + else values.reshape((1, size) if shape == "row" else (size, 1)) + ) + original = supplied.copy() + vector = cls(supplied) + expected = values if size == 6 else np.r_[values, 0, 0, 0] + assert len(vector) == 1 + assert vector.A.shape == (6,) + assert vector.isvalid(vector.A, check=False) + nt.assert_array_equal(vector.A, expected) + nt.assert_array_equal(vector[0].A, expected) + assert str(vector).startswith(cls.__name__ + "[") + nt.assert_array_equal((-vector).A, -expected) + nt.assert_array_equal(supplied, original)