Class Quaternion

class ahrs.common.quaternion.Quaternion

Representation of a quaternion. It can be built with 3- or 4-dimensional vectors. The quaternion object is always normalized to represent rotations in 3D space, also known as a versor.

Parameters:
  • q (array-like, default: None) – Vector to build the quaternion with. It can be either 3- or 4-dimensional.
  • versor (bool, default: True) – Treat the quaternion as versor. It will normalize it immediately.
  • dcm (array-like) – Create quaternion object from a 3-by-3 rotation matrix. It is built only if no array was given to build.
  • rpy (array-like) – Create quaternion object from roll-pitch-yaw angles. It is built only if no array was given to build.
Variables:
  • A (numpy.ndarray) – Array with the 4 elements of quaternion of the form [w, x, y, z]
  • w (float) – Scalar part of the quaternion.
  • x (float) – First element of the vector part of the quaternion.
  • y (float) – Second element of the vector part of the quaternion.
  • z (float) – Third element of the vector part of the quaternion.
  • v (numpy.ndarray) – Vector part of the quaternion.
Raises:

ValueError – When length of input array is not equal to either 3 or 4.

Examples

>>> from ahrs import Quaternion
>>> q = Quaternion([1., 2., 3., 4.])
>>> q
Quaternion([0.18257419, 0.36514837, 0.54772256, 0.73029674])
>>> x = [1., 2., 3.]
>>> q.rot(x)
[1.8 2.  2.6]
>>> R = q.to_DCM()
>>> R@x
[1.8 2.  2.6]

A call to method product() will return an array of a multiplied vector.

>>> q1 = Quaternion([1., 2., 3., 4.])
>>> q2 = Quaternion([5., 4., 3., 2.])
>>> q1.product(q2)
array([-0.49690399,  0.1987616 ,  0.74535599,  0.3975232 ])

Multiplication operators are overriden to perform the expected hamilton product.

>>> q1*q2
array([-0.49690399,  0.1987616 ,  0.74535599,  0.3975232 ])
>>> q1@q2
array([-0.49690399,  0.1987616 ,  0.74535599,  0.3975232 ])

Basic operators are also overriden.

>>> q1+q2
Quaternion([0.46189977, 0.48678352, 0.51166727, 0.53655102])
>>> q1-q2
Quaternion([-0.69760203, -0.25108126,  0.19543951,  0.64196028])

Pure quaternions are built from arrays with three elements.

>>> q = Quaternion([1., 2., 3.])
>>> q
Quaternion([0.        , 0.26726124, 0.53452248, 0.80178373])
>>> q.is_pure()
True

Conversions between representations are also possible.

>>> q.to_axang()
(array([0.26726124, 0.53452248, 0.80178373]), 3.141592653589793)
>>> q.to_angles()
array([ 1.24904577, -0.44291104,  2.8198421 ])

And a nice representation as a string is also implemented to conform to Hamilton’s notation.

>>> str(q)
'(0.0000 +0.2673i +0.5345j +0.8018k)'

Attributes

Methods