A module for basic quarternion operations and 3D spatial rotations using quaternion representation Quaternions are a 4-component analogue to complex numbers
where the three imaginary components obey \(\mathbf{ijk} = \mathbf{i}^2 = \mathbf{j}^2 = \mathbf{k}^2 = -1\). This leads to a structure similar to that of complex numbers, except that the quaternion product defined according to the above rules is non-commutative \(\mathbf{q}_1\mathbf{q}_2 \ne \mathbf{q}_2\mathbf{q}_1\).
It turns out that unit quaternions \(||\mathbf{q}|| = \sqrt{w^2+x^2+y^2+z^2} = 1\) represent the space of 3D rotations so that the rotation by angle \(\alpha\) around unit axis \(\mathbf{u} = [x,y,z]\) is represented by the quaternion
and joining rotations is represented as a a quaternion product (rotating first by \(\mathbf{q}_1\), then by \(\mathbf{q}_2\) yields the combined rotation of \(\mathbf{q}_{12} = \mathbf{q}_2 \mathbf{q}_1\)).
- norm_tolerance¶
double precision scalar parameter
initial value = 1.0d-8
the threshold value for the norm for treating vectors as zero vectors
- qtrn¶
The quarternion type. It only contains four real components, but the main advantage for defining it as a custom type is the possibility to write routines and operators for quaternion algebra.
Contained data:
- y: double precision scalar
- an “imaginary” component of the quaternion
- x: double precision scalar
- an “imaginary” component of the quaternion
- z: double precision scalar
- an “imaginary” component of the quaternion
- w: double precision scalar
- the “real” component of the quaternion
- norm_quaternion(qq)¶
norms the given quaternion
Parameters:
- qq: TYPE(qtrn) intent() scalar
- quaternion to be normed to unity
- cross(v, u)¶
Normal cross product of vectors \(\mathbf{v} \times \mathbf{u}\) (Note: for 3-vectors only!)
Parameters:
- v: double precision intent() size(3)
- vector
- u: double precision intent() size(3)
- vector
- dot(v, u)¶
Normal dot product of vectors \(\mathbf{v}\cdot\mathbf{u}\) (Note: for 3-vectors only!)
Parameters:
- v: double precision intent() size(3)
- vector
- u: double precision intent() size(3)
- vector
- q2angle(q)¶
Returns the angle of rotation described by the UNIT quarternion \(\mathbf{q}\). Note that the unity of \(\mathbf{q}\) is not checked (as it would be time consuming to calculate the norm all the time if we know the quaternions used have unit length).
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion representation of rotation
- q2axis(q)¶
Returns the axis of rotation described by the UNIT quarternion \(\mathbf{q}\). Note that the unity of \(\mathbf{q}\) is not checked (as it would be time consuming to calculate the norm all the time if we know the quaternions used have unit length).
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion representation of rotation
- q2matrix(q)¶
Returns the rotation matrix described by the UNIT quarternion \(\mathbf{q}\). Note that the unity of \(\mathbf{q}\) is not checked (as it would be time consuming to calculate the norm all the time if we know the quaternions used have unit length).
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion representation of rotation
- qconj(q)¶
Returns the quarternion conjugate of \(\mathbf{q}\): \(\mathbf{q}^* = w+x\mathbf{i}+y\mathbf{j}+z\mathbf{k} \to w-x\mathbf{i}-y\mathbf{j}-z\mathbf{k}\)
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- qdiv(q, r)¶
Returns the quarternion \(\mathbf{q}\) divided by scalar \(r\) component-wise
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- r: double precision intent() scalar
- a real scalar
- qinv(q)¶
Returns the quarternion inverse of \(\mathbf{q}\): \(\mathbf{q}^*/||\mathbf{q}||\)
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- qminus(q, r)¶
Returns the quarternion \(\mathbf{q}\) subtracted by scalar \(r\) component-wise
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- r: double precision intent() scalar
- a real scalar
- qnorm(q)¶
Returns the quarternion norm of \(\mathbf{q}\): \(||\mathbf{q}|| = \sqrt{w^2+x^2+y^2+z^2}\)
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- qplus(q, r)¶
Returns the quarternion \(\mathbf{q}\) added by scalar \(r\) component-wise
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- r: double precision intent() scalar
- a real scalar
- qprod(q1, q2)¶
Returns the quarternion product \(\mathbf{q}_1\mathbf{q}_2\) Note that the product is non-commutative: \(\mathbf{q}_1\mathbf{q}_2 \ne \mathbf{q}_2\mathbf{q}_1\)
Parameters:
- q1: TYPE(qtrn) intent() scalar
- a quaternion
- q2: TYPE(qtrn) intent() scalar
- a quaternion
- qtimes(r, q)¶
Returns the quarternion \(\mathbf{q}\) multiplied by scalar \(r\) component-wise
Parameters:
- r: double precision intent() scalar
- a real scalar
- q: TYPE(qtrn) intent() scalar
- a quaternion
- qtimesB(q, r)¶
Returns the quarternion \(\mathbf{q}\) multiplied by scalar \(r\) component-wise
Parameters:
- q: TYPE(qtrn) intent() scalar
- a quaternion
- r: double precision intent() scalar
- a real scalar
- rot2q(a, u)¶
Returns the quarternion representing a rotation around axis \(\mathbf{u}\) by angle \(\alpha\)
Parameters:
- a: double precision intent() scalar
- angle in radians
- u: double precision intent() size(3)
- 3D vector, defining an axis of rotation
- rotate_a(vec, da)¶
Returns the vector rotated according to the vector \(\mathbf{d}\). The axis of rotation is given by the direction of \(\mathbf{d}\) and the angle by \(||\mathbf{d}||\).
Parameters:
- vec: double precision intent() size(3)
- vector to be rotated
- da: double precision intent() size(3)
- rotation vector (e.g., angular velocity x time \(\mathbf{\omega} t\))
- rotate_au(vec, a, u)¶
Returns the vector rotated according to the axis \(\mathbf{u}\) and angle \(\alpha\).
Parameters:
- vec: double precision intent() size(3)
- vector to be rotated
- a: double precision intent() scalar
- angle of rotation
- u: double precision intent() size(3)
- axis of rotation
- rotate_q(vec, q)¶
Returns the 3D vector rotated according to the UNIT quarternion \(\mathbf{q}\). Note that the unity of \(\mathbf{q}\) is not checked (as it would be time consuming to calculate the norm all the time if we know the quaternions used have unit length).
Parameters:
- vec: double precision intent() size(3)
- vector to be rotated
- q: TYPE(qtrn) intent() scalar
- a quaternion representation of rotation
- vec2q(v)¶
Returns the quarternion representing a rotation around axis \(\mathbf{v}\) by angle \(||\mathbf{v}||\). If \(\mathbf{v} = 0\), the quaternion \(\mathbf{q} = [1 0 0 0]\) will be returned.
Parameters:
- v: double precision intent() size(3)
- 3D vector, defining both the angle and axis of rotation
- vnorm(v)¶
Norm of a vector, \(||\mathbf{v}||\)
Parameters:
- v: double precision intent() size(3)
- vector