diff --git a/spatialmath/quaternion.py b/spatialmath/quaternion.py index 858522e8..a44f5ce6 100644 --- a/spatialmath/quaternion.py +++ b/spatialmath/quaternion.py @@ -185,7 +185,7 @@ def s(self) -> float: if len(self) == 1: return self._A[0] else: - return np.array([q.s for q in self]) + return np.array([q[0] for q in self.data]) @property def v(self) -> R3: @@ -212,7 +212,7 @@ def v(self) -> R3: if len(self) == 1: return self._A[1:4] else: - return np.array([q.v for q in self]) + return np.array([q[1:4] for q in self.data]) @property def vec(self) -> R4: @@ -241,7 +241,7 @@ def vec(self) -> R4: if len(self) == 1: return self._A else: - return np.array([q._A for q in self]) + return np.array(self.data) @property def vec_xyzs(self) -> R4: @@ -271,7 +271,7 @@ def vec_xyzs(self) -> R4: if len(self) == 1: return np.roll(self._A, -1) else: - return np.array([np.roll(q._A, -1) for q in self]) + return np.array([np.roll(q, -1) for q in self.data]) @property def matrix(self) -> R4x4: @@ -926,9 +926,9 @@ def __neg__(self) -> Quaternion: >>> -Quaternion([np.r_[1,2,3,4], np.r_[5,6,7,8]]) """ - return UnitQuaternion( - [-x for x in self.data] - ) # pylint: disable=invalid-unary-operand-type + result = self.__class__.Empty() + result.data = [-x for x in self.data] + return result def __repr__(self) -> str: """ diff --git a/tests/test_quaternion_value_preservation.py b/tests/test_quaternion_value_preservation.py new file mode 100644 index 00000000..b681914e --- /dev/null +++ b/tests/test_quaternion_value_preservation.py @@ -0,0 +1,70 @@ +import numpy as np +import pytest +from numpy.testing import assert_allclose, assert_array_equal +from spatialmath import Quaternion, UnitQuaternion + + +@pytest.mark.parametrize("cls", [Quaternion, UnitQuaternion]) +@pytest.mark.parametrize( + "rows", + [ + [[0.5, 0.5, 0.5, 0.5]], + [[-0.5, 0.5, 0.5, 0.5]], + [[-0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, 0.5]], + [[-0.5, 0.5, 0.5, 0.5]] * 6, + ], +) +def test_negation_preserves_values_and_type(cls, rows): + original = np.array(rows, dtype=float) + if cls is Quaternion: + original *= 3 + q = cls.Empty() + q.data = [row.copy() for row in original] + negative = -q + assert type(negative) is cls + assert len(negative) == len(q) + assert_array_equal(negative.data, -original) + assert_array_equal((-negative).data, original) + assert_allclose(negative.norm(), q.norm()) + assert_array_equal(q.data, original) + negative.data[0][0] += 0.1 + assert_array_equal(q.data, original) + + +@pytest.mark.parametrize("cls", [Quaternion, UnitQuaternion]) +def test_empty_negation(cls): + result = -cls.Empty() + assert type(result) is cls + assert len(result) == 0 + + +@pytest.mark.parametrize("name", ["s", "v", "vec", "vec_xyzs"]) +@pytest.mark.parametrize("cls", [Quaternion, UnitQuaternion]) +@pytest.mark.parametrize("count", [1, 2, 6]) +def test_exports_match_stored_coefficients(name, cls, count): + rows = np.tile(np.array([[-0.5, 0.5, -0.5, 0.5]]), (count, 1)) + if cls is Quaternion: + rows *= 3 + q = cls.Empty() + q.data = [row.copy() for row in rows] + expected = { + "s": rows[:, 0], + "v": rows[:, 1:], + "vec": rows, + "vec_xyzs": np.roll(rows, -1, axis=1), + }[name] + if count == 1: + expected = expected[0] + assert_array_equal(getattr(q, name), expected) + assert_array_equal(q.data, rows) + + +def test_general_zero_negation(): + assert_array_equal((-Quaternion()).vec, np.zeros(4)) + + +def test_signed_unit_export_from_supported_constructor(): + rows = np.array([[-0.5, 0.5, 0.5, 0.5], [0.5, -0.5, 0.5, 0.5]]) + q = UnitQuaternion(rows, norm=False) + assert_array_equal(q.vec, rows) + assert_array_equal((-q).vec, -rows)