Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spatialmath/quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_quaternion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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(
Expand Down