Post · 2 min read· pp. 130–131

Quaternion.product

Quaternion.product(q: ndarray) → ndarray : Product of two quaternions.

code
Given two unit quaternions p=(pw,pv)\mathbf{p}=(p_w, \mathbf{p}_v) and
q=(qw,qv)\mathbf{q} = (q_w, \mathbf{q}_v), their product is defined
[[Sol17](/u/fab8b0a9fec670cf#id65)] [[Dan02](/u/fab8b0a9fec670cf#id12)] as:

\begin{split}\begin{eqnarray} \mathbf{pq} & = & \big( (q_w p_w - \mathbf{q}_v \cdot \mathbf{p}_v) \; , \; \mathbf{p}_v \times \mathbf{q}_v + q_w \mathbf{p}_v + p_w \mathbf{q}_v \big) \\ & = & \begin{bmatrix}q_w p_w - \mathbf{q}_v \cdot \mathbf{p}_v \\ \mathbf{p}_v \times \mathbf{q}_v + q_w \mathbf{p}_v + p_w \mathbf{q}_v \end{bmatrix} \\ & = & \begin{bmatrix} p_w & -\mathbf{p}_v^T \\ \mathbf{p}_v & p_w \mathbf{I}_3 + \lfloor \mathbf{p}_v \rfloor_\times \end{bmatrix} \begin{bmatrix} q_w \\ \mathbf{q}_v \end{bmatrix} \\ & = & \begin{bmatrix} p_w & -p_x & -p_y & -p_z \\ p_x & p_w & -p_z & p_y \\ p_y & p_z & p_w & -p_x \\ p_z & -p_y & p_x & p_w \end{bmatrix} \begin{bmatrix} q_w \\ q_x \\ q_y \\ q_z \end{bmatrix} \\ & = & \begin{bmatrix} p_w q_w - p_x q_x - p_y q_y - p_z q_z \\ p_w q_x + p_x q_w + p_y q_z - p_z q_y \\ p_w q_y - p_x q_z + p_y q_w + p_z q_x \\ p_w q_z + p_x q_y - p_y q_x + p_z q_w \end{bmatrix} \end{eqnarray}\end{split}
where ⌊a⌋×\lfloor \mathbf{a} \rfloor_\times represents the [skew-symmetric matrix](https://en.wikipedia.org/wiki/Skew-symmetric_matrix) of a\mathbf{a}. Parameters: : **q** (*numpy.ndarray, Quaternion*) – Quaternion to multiply with. Returns: : **pq** – Product of quaternions. Return type: : numpy.ndarray Examples ```default

p = Quaternion(random=True) p.view() Quaternion([ 0.22091606, 0.94554179, -0.23723731, 0.02941561])

q = Quaternion(random=True) Quaternion([-0.12430979, 0.83988925, -0.39229689, 0.35388736])

p.product(q) array([-0.92508969, -0.0044107 , -0.3670832 , -0.09715728])

code

    It holds with the result after the cross and dot product definition

    ```default
>>> rw = p.w*q.w - np.dot(p.v, q.v)
>>> rv = p.w*q.v + q.w*p.v + np.cross(p.v, q.v)
>>> rw, rv
(-0.9250896906548645, array([-0.0044107 , -0.3670832 , -0.09715728]))
code
Can multiply with a normalized quaternion in an array …

```default

p.product([-0.12430979, 0.83988925, -0.39229689, 0.35388736]) array([-0.92508969, -0.0044107 , -0.3670832 , -0.09715728])

code

    but it ALSO MULTIPLIES with non-unitary quaternions (assumed to be
    simple vectors with 4 elements in them.)

    ```default
>>> r = np.random.random(4) * 10
>>> r.view()
array([8.48031045, 2.49690044, 5.78466679, 6.30034199])
>>> p.product(r)
array([ 0.69952346,  6.90525759, -6.61770901,  7.70330242])
code
The result of this latter operation does NOT yield a unitary
quaternion. Thus, the user must normalize the array, if a unitary
quaternion is desired.