Class Quaternion
class ahrs.common.quaternion.Quaternion(q: int | list | ndarray = None, versor: bool = True, **kwargs) : 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.
- **order** (*str, default: ‘H’*) – Specify the layout of the Quaternion, where the scalar part precedes
the vector part by default. If the order is ‘S’ the vector part
precedes the scalar part. The default is ‘H’ for a Hamiltonian notation
with the scalar part first.
Variables:
: **A** (*numpy.ndarray*) – Array with the 4 elements of quaternion of the form [w, x, y, z]
Raises:
: **ValueError** – When length of input array is not equal to either 3 or 4.
Examples
```defaultfrom ahrs import Quaternion Quaternion() Quaternion([1., 0., 0., 0.])
Quaternion(random=True) Quaternion([ 0.27216553, -0.40824829, 0.54433105, -0.68041382])
q = Quaternion([1., 2., 3., 4.]) q.view() Quaternion([0.18257419, 0.36514837, 0.54772256, 0.73029674])
x = [1., 2., 3.] R = q.to_DCM() # Get rotation matrix from quaternion R @ x [1.8 2. 2.6]
A call to method [`product()`](/u/626644f06dd07145#ahrs-common-quaternion-quaternion-product) will return an array of a multiplied
vector.
```default
>>> 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.
```defaultq1 * 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.
```default
>>> 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.
```defaultq = Quaternion([1., 2., 3.]) q Quaternion([0. , 0.26726124, 0.53452248, 0.80178373])
q.is_pure() True
Conversions between representations are also possible.
```default
>>> 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.
```defaultstr(q) '(0.0000 +0.2673i +0.5345j +0.8018k)'
If the parameter `order` is set to `'S'` the vector part will precede
the scalar part.
```default
>>> q = Quaternion([2., -3., 4., -5.], order='S')
>>> q.w
-0.6804138174397717
>>> q.v
array([ 0.27216553, -0.40824829, 0.54433105])
>>> print(q)
(0.2722i -0.4082j +0.5443k -0.6804)Attributes
Methods