rotation_matrix_from_quaternion

rotation_matrix_from_quaternion(Q)[source]

Create rotation matrices out of quaternions. Quaternions have the form

Q[…, 0]*i + Q[…, 1]*j + Q[…, 2]*k + Q[…, 3].

Examples

Given the quaternion for a 90-degree rotation along the z-axis, the corresponding rotation matrix is computed.

>>> Q90z = np.array([ 0, 0, 0.7071068, 0.7071068])
>>> ROT90z = rotation_matrix_from_quaternion(Q90z)

The rotation matrix is used to rotate the x-Vector forward and backward.

>>> x_vector = np.array([1, 0, 0])
>>> rotated_vector = do_rotation_vector(ROT90z, x_vector)
>>> np.round(rotated_vector, 6)
array([-0.,  1.,  0.])
>>> np.round(undo_rotation_vector(ROT90z, rotated_vector), 6)
array([1., 0., 0.])

A tensor is rotated using do_rotation_tensor() and undo_rotation_tensor().

>>> tensor = np.array([
...     [1.0, 0.2, 0.3],
...     [0.2, 2.0, 0.4],
...     [0.3, 0.4, 3.0],
... ])
>>> rotated_tensor = do_rotation_tensor(ROT90z, tensor)
>>> np.round(rotated_tensor, 6)
array([[ 2. , -0.2, -0.4],
       [-0.2,  1. ,  0.3],
       [-0.4,  0.3,  3. ]])
>>> np.round(undo_rotation_tensor(ROT90z, rotated_tensor), 6)
array([[1. , 0.2, 0.3],
       [0.2, 2. , 0.4],
       [0.3, 0.4, 3. ]])
Parameters:

Q – array of shape (…, 4) containing the quaternions

Returns:

array of shape (…, 3, 3) containing the rotation matrices.