Skip to content

Commit 470962c

Browse files
Fix UnitQuaternion constructor with v keyword. (#118)
1 parent ad98b0e commit 470962c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

spatialmath/quaternion.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def __init__(self, s: Any = None, v=None, check: Optional[bool] = True):
7777
"""
7878
super().__init__()
7979

80+
if s is None and smb.isvector(v, 4):
81+
v,s = (s,v)
82+
8083
if v is None:
8184
# single argument
8285
if super().arghandler(s, check=False):
@@ -979,6 +982,11 @@ def __init__(
979982
"""
980983
super().__init__()
981984

985+
# handle: UnitQuaternion(v)`` constructs a unit quaternion with specified elements
986+
# from ``v`` which is a 4-vector given as a list, tuple, or ndarray(4)
987+
if s is None and smb.isvector(v, 4):
988+
v,s = (s,v)
989+
982990
if v is None:
983991
# single argument
984992
if super().arghandler(s, check=check):

tests/test_quaternion.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def test_constructor(self):
7070
qcompare(UnitQuaternion(2, [0, 0, 0]), np.r_[1, 0, 0, 0])
7171
qcompare(UnitQuaternion(-2, [0, 0, 0]), np.r_[1, 0, 0, 0])
7272

73+
qcompare(UnitQuaternion([1, 2, 3, 4]), UnitQuaternion(v = [1, 2, 3, 4]))
74+
qcompare(UnitQuaternion(s = 1, v = [2, 3, 4]), UnitQuaternion(v = [1, 2, 3, 4]))
75+
7376
# from R
7477

7578
qcompare(UnitQuaternion(np.eye(3)), [1, 0, 0, 0])
@@ -753,6 +756,9 @@ def test_constructor(self):
753756
nt.assert_array_almost_equal(Quaternion(2, [0, 0, 0]).vec, [2, 0, 0, 0])
754757
nt.assert_array_almost_equal(Quaternion(-2, [0, 0, 0]).vec, [-2, 0, 0, 0])
755758

759+
qcompare(Quaternion([1, 2, 3, 4]), Quaternion(v = [1, 2, 3, 4]))
760+
qcompare(Quaternion(s = 1, v = [2, 3, 4]), Quaternion(v = [1, 2, 3, 4]))
761+
756762
# pure
757763
v = [5, 6, 7]
758764
nt.assert_array_almost_equal(

0 commit comments

Comments
 (0)