From 5b6028f97f9f24879e8f4e273a2f901401443868 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Wed, 18 Nov 2026 10:34:46 -0700 Subject: [PATCH 1/2] Handle quaternion exponential sequences elementwise Apply the existing scalar exponential to each stored quaternion, preserving tolerance and scalar return types. --- spatialmath/quaternion.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spatialmath/quaternion.py b/spatialmath/quaternion.py index 858522e8..abfdb709 100644 --- a/spatialmath/quaternion.py +++ b/spatialmath/quaternion.py @@ -501,6 +501,15 @@ def exp(self, tol: float = 20) -> Quaternion: :seealso: :meth:`Quaternion.log` :meth:`UnitQuaternion.log` :meth:`UnitQuaternion.AngVec` :meth:`UnitQuaternion.EulerVec` """ + if len(self) != 1: + values = [Quaternion(q).exp(tol=tol) for q in self.data] + if values and all(isinstance(q, UnitQuaternion) for q in values): + result = UnitQuaternion.Empty() + else: + result = Quaternion.Empty() + result.data = [q.data[0] for q in values] + return result + exp_s = math.exp(self.s) norm_v = smb.norm(self.v) s = exp_s * math.cos(norm_v) From 919cd3a97c22b376bd7d8a9a0d002498b8166b66 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Wed, 18 Nov 2026 10:35:08 -0700 Subject: [PATCH 2/2] Implement tests for Quaternion exp and log functions Cover pure, non-pure, mixed, empty, tolerance-sensitive and stored-sign cases against scalar and analytic controls. --- tests/test_quaternion_exp_sequence.py | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/test_quaternion_exp_sequence.py diff --git a/tests/test_quaternion_exp_sequence.py b/tests/test_quaternion_exp_sequence.py new file mode 100644 index 00000000..390d95f0 --- /dev/null +++ b/tests/test_quaternion_exp_sequence.py @@ -0,0 +1,77 @@ +import numpy as np +import pytest +from numpy.testing import assert_allclose +from spatialmath import Quaternion, UnitQuaternion + + +@pytest.mark.parametrize( + "rows", + [ + [[0, 0, 0, 0], [0, 0.1, -0.2, 0.3]], + [[1, 0, 0, 0], [2, 0.2, 0.4, -0.1]], + [[0, 0.1, 0.2, 0.3], [0.7, -0.2, 0, 0.1]], + [[0, 4, 0, 0], [0, 0, 4, 0]], + [[0, 0, 0, 0]] * 6, + ], +) +@pytest.mark.parametrize("tol", [1, 20, 100]) +def test_sequence_matches_independent_singletons(rows, tol): + sequence = Quaternion([np.array(row, dtype=float) for row in rows]) + expected = [Quaternion(row).exp(tol=tol) for row in rows] + actual = sequence.exp(tol=tol) + expected_type = ( + UnitQuaternion + if all(isinstance(q, UnitQuaternion) for q in expected) + else Quaternion + ) + assert type(actual) is expected_type + assert len(actual) == len(rows) + for value, scalar in zip(actual.data, expected): + assert value.shape == (4,) + assert_allclose(value, scalar.data[0], atol=1e-15) + assert_allclose(sequence.data, rows) + + +@pytest.mark.parametrize("count", [2, 3, 6]) +def test_log_exp_round_trip(count): + rows = [np.array([1 + k, 0.2, -0.3, 0.4]) for k in range(count)] + q = Quaternion(rows) + assert_allclose(q.log().exp().data, rows, atol=1e-14) + + +def test_empty_exponential(): + assert type(Quaternion.Empty().exp()) is Quaternion + assert len(Quaternion.Empty().exp()) == 0 + + +@pytest.mark.parametrize( + "row", [[0, 0, 0, 0], [0, 0.1, -0.2, 0.3], [1, 0, 0, 0], [0.7, 0.2, -0.1, 0.4]] +) +def test_singleton_analytic_formula(row): + row = np.array(row) + n = np.linalg.norm(row[1:]) + expected = np.exp(row[0]) * np.r_[np.cos(n), row[1:] * np.sinc(n / np.pi)] + assert_allclose(Quaternion(row).exp().data[0], expected, atol=1e-15) + + +@pytest.mark.parametrize("tol", [1, 20, 100]) +def test_sequence_forwards_scalar_tolerance(tol): + eps = np.finfo(float).eps + rows = [np.array([10 * eps, 0.2, -0.1, 0]), np.array([50 * eps, 0, 0, 0.3])] + expected = [Quaternion(row).exp(tol=tol) for row in rows] + actual = Quaternion(rows).exp(tol=tol) + expected_type = ( + UnitQuaternion + if all(isinstance(q, UnitQuaternion) for q in expected) + else Quaternion + ) + assert type(actual) is expected_type + assert_allclose(actual.data, [q.data[0] for q in expected]) + + +def test_unit_quaternion_sequence_uses_stored_values(): + rows = np.array([[0.5, 0.5, 0.5, 0.5], [-0.5, 0.5, 0.5, 0.5]]) + q = UnitQuaternion(rows, norm=False) + expected = [Quaternion(row).exp().data[0] for row in rows] + assert type(q.exp()) is Quaternion + assert_allclose(q.exp().data, expected)