From 517c43fef22cd4ce22507b1e8da88997e40157ca Mon Sep 17 00:00:00 2001 From: Mark Yeatman Date: Wed, 21 Feb 2024 13:15:06 -0500 Subject: [PATCH 1/2] Add code+test to make sure using 4x1 vector with v keyword argument works. --- spatialmath/quaternion.py | 5 +++++ tests/test_quaternion.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/spatialmath/quaternion.py b/spatialmath/quaternion.py index be0ea2f0..fde1ab9d 100644 --- a/spatialmath/quaternion.py +++ b/spatialmath/quaternion.py @@ -979,6 +979,11 @@ def __init__( """ super().__init__() + # handle: UnitQuaternion(v)`` constructs a unit quaternion with specified elements + # from ``v`` which is a 4-vector given as a list, tuple, or ndarray(4) + if s is None and smb.isvector(v, 4): + v,s = (s,v) + if v is None: # single argument if super().arghandler(s, check=check): diff --git a/tests/test_quaternion.py b/tests/test_quaternion.py index 0f2ac871..3c992512 100644 --- a/tests/test_quaternion.py +++ b/tests/test_quaternion.py @@ -70,6 +70,9 @@ def test_constructor(self): qcompare(UnitQuaternion(2, [0, 0, 0]), np.r_[1, 0, 0, 0]) qcompare(UnitQuaternion(-2, [0, 0, 0]), np.r_[1, 0, 0, 0]) + qcompare(UnitQuaternion([1, 2, 3, 4]), UnitQuaternion(v = [1, 2, 3, 4])) + qcompare(UnitQuaternion(s = 1, v = [2, 3, 4]), UnitQuaternion(v = [1, 2, 3, 4])) + # from R qcompare(UnitQuaternion(np.eye(3)), [1, 0, 0, 0]) From 11564a92193dd771e41108007de65d065821a51d Mon Sep 17 00:00:00 2001 From: Mark Yeatman Date: Wed, 21 Feb 2024 13:19:15 -0500 Subject: [PATCH 2/2] Propogate changes to "basic" quaternion as well --- spatialmath/quaternion.py | 3 +++ tests/test_quaternion.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/spatialmath/quaternion.py b/spatialmath/quaternion.py index fde1ab9d..26d8093a 100644 --- a/spatialmath/quaternion.py +++ b/spatialmath/quaternion.py @@ -77,6 +77,9 @@ def __init__(self, s: Any = None, v=None, check: Optional[bool] = True): """ super().__init__() + if s is None and smb.isvector(v, 4): + v,s = (s,v) + if v is None: # single argument if super().arghandler(s, check=False): diff --git a/tests/test_quaternion.py b/tests/test_quaternion.py index 3c992512..791e27bf 100644 --- a/tests/test_quaternion.py +++ b/tests/test_quaternion.py @@ -756,6 +756,9 @@ def test_constructor(self): nt.assert_array_almost_equal(Quaternion(2, [0, 0, 0]).vec, [2, 0, 0, 0]) nt.assert_array_almost_equal(Quaternion(-2, [0, 0, 0]).vec, [-2, 0, 0, 0]) + qcompare(Quaternion([1, 2, 3, 4]), Quaternion(v = [1, 2, 3, 4])) + qcompare(Quaternion(s = 1, v = [2, 3, 4]), Quaternion(v = [1, 2, 3, 4])) + # pure v = [5, 6, 7] nt.assert_array_almost_equal(