Quaternions

Note that all these functions work with single quaternions and quaternion vectors, as well as with arrays containing these.

Functions for working with quaternions

Conversion routines

Details

Functions for working with quaternions. Note that all the functions also work on arrays, and can deal with full quaternions as well as with quaternion vectors.

quat.deg2quat(inDeg)[source]

Convert axis-angles or plain degree into the corresponding quaternion values. Can be used with a plain number or with an axis angle.

Parameters:

inDeg : float or (N,3)

quaternion magnitude or quaternion vectors.

Returns:

outQuat : float or array (N,3)

number or quaternion vector.

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> quat.deg2quat(array([[10,20,30], [20,30,40]]))
array([[ 0.08715574,  0.17364818,  0.25881905],
   [ 0.17364818,  0.25881905,  0.34202014]])
>>> quat.deg2quat(10)
0.087155742747658166
quat.quat2deg(inQuat)[source]

Calculate the axis-angle corresponding to a given quaternion.

Parameters:

inQuat: float, or array_like, shape ([3/4],) or (N,[3/4]) :

quaternion(s) or quaternion vector(s)

Returns:

axAng : corresponding axis angle(s)

float, or shape (3,) or (N,3)

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> quat.quat2deg(0.1)
array([ 11.47834095])
>>> quat.quat2deg([0.1, 0.1, 0])
array([ 11.47834095,  11.47834095,   0.        ])
>>> quat.quat2deg([cos(0.1), 0, sin(0.1), 0])
array([  0.       ,  11.4591559,   0.       ])
quat.quat2rotmat(inQuat)[source]

Calculate the rotation matrix corresponding to the quaternion. If “inQuat” contains more than one quaternion, the matrix is flattened (to facilitate the work with rows of quaternions), and can be restored to matrix form by “reshaping” the resulting rows into a (3,3) shape.

Parameters:

inQuat : array_like, shape ([3,4],) or (N,[3,4])

quaternions or quaternion vectors

Returns:

rotMat : corresponding rotation matrix/matrices (flattened)

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> r = quat.quat2rotmat([0, 0, 0.1])
>>> r.shape
(1, 9)
>>> r.reshape((3,3))
array([[ 0.98      , -0.19899749,  0.        ],
    [ 0.19899749,  0.98      ,  0.        ],
    [ 0.        ,  0.        ,  1.        ]])
quat.quat2vect(inQuat)[source]

Extract the quaternion vector from a full quaternion.

Parameters:

inQuat : array_like, shape ([3,4],) or (N,[3,4])

quaternions or quaternion vectors.

Returns:

vect : array, shape (3,) or (N,3)

corresponding quaternion vectors

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> quat.quat2vect([[cos(0.2), 0, 0, sin(0.2)],[cos(0.1), 0, sin(0.1), 0]])
array([[ 0.        ,  0.        ,  0.19866933],
       [ 0.        ,  0.09983342,  0.        ]])
quat.quatinv(q)[source]

Quaternion inversion

Parameters:

q: array_like, shape ([3,4],) or (N,[3/4]) :

quaternion or quaternion vectors

Returns:

qinv : inverse quaternion(s)

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>>  quat.quatinv([0,0,0.1])
array([[-0. , -0. , -0.1]])
>>> quat.quatinv([[cos(0.1),0,0,sin(0.1)],
...: [cos(0.2),0,sin(0.2),0]])
array([[ 0.99500417, -0.        , -0.        , -0.09983342],
       [ 0.98006658, -0.        , -0.19866933, -0.        ]])
quat.quatmult(p, q)[source]

Quaternion multiplication: Calculates the product of two quaternions r = p * q If one of both of the quaterions have only three columns, the scalar component is calculated such that the length of the quaternion is one. The lengths of the quaternions have to match, or one of the two quaternions has to have the length one. If both p and q only have 3 components, the returned quaternion also only has 3 components (i.e. the quaternion vector)

Parameters:

p,q : array_like, shape ([3,4],) or (N,[3,4])

quaternions or quaternion vectors

Returns:

r : quaternion or quaternion vector (if both

p and q are contain quaternion vectors).

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> p = [cos(0.2), 0, 0, sin(0.2)]
>>> q = [[0, 0, 0.1],
>>>    [0, 0.1, 0]]
>>> r = quat.quatmult(p,q)
quat.rotate_vector(vector, q)[source]

Rotates a vector, according to the given quaternions. Note that a single vector can be rotated into many orientations; or a row of vectors can all be rotated by a single quaternion.

Parameters:

vector : array, shape (3,) or (N,3)

vector(s) to be rotated.

q : array_like, shape ([3,4],) or (N,[3,4])

quaternions or quaternion vectors.

Returns:

rotated : array, shape (3,) or (N,3)

rotated vector(s)

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> mymat = eye(3)
>>> myVector = r_[1,0,0]
>>> quats = array([[0,0, sin(0.1)],[0, sin(0.2), 0]])
>>> quat.rotate_vector(myVector, quats)
array([[ 0.98006658,  0.19866933,  0.        ],
       [ 0.92106099,  0.        , -0.38941834]])
>>> quat.rotate_vector(mymat, [0, 0, sin(0.1)])
array([[ 0.98006658,  0.19866933,  0.        ],
       [-0.19866933,  0.98006658,  0.        ],
       [ 0.        ,  0.        ,  1.        ]])
quat.rotmat2quat(rMat)[source]

Assumes that R has the shape (3,3), or the matrix elements in columns

Parameters:

rMat : array, shape (3,3) or (N,9)

single rotation matrix, or matrix with rotation-matrix elements.

Returns:

outQuat : array, shape (4,) or (N,4)

corresponding quaternion vector(s)

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> rotMat = array([[cos(alpha), -sin(alpha), 0],
>>>    [sin(alpha), cos(alpha), 0],
>>>    [0, 0, 1]])
>>> quat.rotmat2quat(rotMat)
array([[ 0.99500417,  0.        ,  0.        ,  0.09983342]])
quat.vect2quat(inData)[source]

Utility function, which turns a quaternion vector into a unit quaternion.

Parameters:

inData : array_like, shape (3,) or (N,3)

quaternions or quaternion vectors

Returns:

quats : array, shape (4,) or (N,4)

corresponding unit quaternions.

Notes

More info under http://en.wikipedia.org/wiki/Quaternion

Examples

>>> quats = array([[0,0, sin(0.1)],[0, sin(0.2), 0]])
>>> quat.vect2quat(quats)
array([[ 0.99500417,  0.        ,  0.        ,  0.09983342],
       [ 0.98006658,  0.        ,  0.19866933,  0.        ]])
quat.vel2quat(vel, q0, rate, CStype)[source]

Take an angular velocity (in deg/s), and convert it into the corresponding orientation quaternion.

Parameters:

vel : array, shape (3,) or (N,3)

angular velocity.

q0 : array (3,)

vector-part of quaternion (!!)

rate : float

sampling rate (in [Hz])

CStype: string :

coordinate_system, space-fixed (“sf”) or body_fixed (“bf”)

Returns:

quats : array, shape (N,4)

unit quaternion vectors.

Notes

Take care that you choose a high enough sampling rate!

Examples

>>> v0 = [0., 0., 100.]
>>> vel = tile(v0, (1000,1))
>>> rate = 100
>>> out = quat.vel2quat(vel, [0., 0., 0.], rate, 'sf')
>>> out[-1:]
array([[-0.76040597,  0.        ,  0.        ,  0.64944805]])

Table Of Contents

Previous topic

thLib - Documentation

Next topic

Rotation Matrices

This Page