diff --git a/spatialmath/quaternion.py b/spatialmath/quaternion.py index be0ea2f0..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): @@ -979,6 +982,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..791e27bf 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]) @@ -753,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(