Physics Library
 An open source physics library
Encyclopedia | Forums | Docs | Random  

[parent] example of rotating vectors with quaternions

(Example)

Rotating Vectors with Quaternions: Examples, Exercises, and Solutions

This entry is the self study companion to the PhysicsLibrary article rotating vectors with quaternions. All exercises are stated first. Complete worked solutions follow afterward.

The central convention is passive. A unit quaternion Bq A maps coordinates from frame A into frame B:

Bv = BqA  Av(BqA )∗.
(1)

If frame B is obtained from frame A by a positive frame rotation through angle 𝜃 about unit axis u, then

Bq  =  cos 𝜃-− ^u sin 𝜃-.
  A       2        2
(2)

The conjugate

 B   ∗       𝜃        𝜃
( qA)  = cos 2-+ ^u sin 2-
(3)

is both the reverse passive frame map and the familiar Hamilton active rotor for the corresponding positive physical vector rotation.

1 Convention summary

PhysicsLibrary uses Hamilton multiplication,

ij = k,    jk =  i,     ki = j,

with reversed products changing sign.

A vector is embedded as a pure quaternion:

                             ⌊ 0 ⌋
                             |   |
v =  vxi + vyj + vzk  ← →    |vx |.
                             ⌈ vy⌉
                               vz
(4)

The passive Rodrigues formula associated with equation (2) is

Bv = Av  cos𝜃 − (^u × Av )sin 𝜃 + ^u(^u ⋅ Av )(1 − cos𝜃).
(5)

The corresponding active formula uses the opposite sign on the cross product term.

2 Exercises

  1. Pure quaternion representation.

    Write the vector

    v = 2 ^x − 3^y + 4^z

    as a scalar first pure quaternion.

  2. Positive   ∘
90 frame rotation about +z  .

    Frame B is obtained from frame A by a positive 90∘ rotation about +z. A fixed physical vector has

    Av = x^.

    Construct Bq A and use direct quaternion multiplication to compute Bv. Show every multiplication step.

  3. Positive   ∘
90 frame rotation about +x  .

    Frame B is obtained from frame A by a positive 90∘ rotation about +x. A fixed vector has

    A
 v = y^.

    Determine Bv using the quaternion sandwich.

  4. Vector parallel to the axis.

    Let

    B            ∘         ∘
  qA = cos35  − k sin35 .

    What positive frame rotation does this quaternion represent? Apply it to

    Av = 5k

    and explain the result geometrically.

  5. Frame rotation through     ∘
180 .

    A fixed physical vector has

    A
 v = 2i − 3j + 4k.

    Frame B is obtained from frame A by a positive 180∘ rotation about +z. Determine Bv.

  6. Symmetric    ∘
120 frame rotation.

    Let

    ^u = i-+√j +-k-,   𝜃 =  120∘.
         3

    Construct the passive quaternion Bq A and show that the coordinate map sends

    i − → k.

    Then state what the corresponding positive active rotation does to i.

  7. Passive Rodrigues formula.

    Using

    ^u =  k,    𝜃 =  60∘,    Av =  2i + j + 3k,

    evaluate Bv using the passive Rodrigues formula.

  8. Quaternion sign ambiguity.

    Prove directly that q and −q produce the same vector transformation under

    v′ = qvq∗.
  9. Inverse coordinate transformation.

    If

    Bv = BqA  Av(BqA )∗,

    prove that

    A     B    ∗B  B
 v = (  qA)  v  qA.
  10. norm preservation.

    Use quaternion norm multiplicativity to prove that a unit frame quaternion preserves Euclidean vector magnitude.

  11. Deriving passive Rodrigues’ formula.

    Start with

                     ∗
q = c − s^u,     q =  c + s^u,

    where

           𝜃-             𝜃-
c = cos2 ,    s = sin 2.

    Derive

    vB = v cos 𝜃 − (^u × v)sin𝜃 + ^u (u^⋅ v)(1 − cos𝜃).
  12. Vector only implementation.

    Show that for any unit Hamilton quaternion

    q = (qw,q ),

    the sandwich product

     ′      ∗
v =  qvq

    can be evaluated as

    v′ = v + 2qw(q × v ) + 2q × (q ×  v).

    Then define

    t = 2 (q ×  v)

    and rewrite the result using t.

    Explain why the formula works unchanged for a passive frame quaternion whose vector part already carries the passive minus sign.

  13. Active versus passive.

    A fixed physical vector has

    Av = x^.

    Frame B is obtained from frame A by a positive 90∘ rotation about +z.

    1. Construct the passive quaternion Bq A and determine Bv.
    2. Construct the corresponding positive active rotor and apply it to x in a fixed frame.
    3. Explain why the two resulting vectors point in opposite coordinate directions without contradiction.
  14. Scalar first and scalar last software arrays.

    Write the passive quaternion for a positive 90∘ frame rotation about +z in PhysicsLibrary scalar first order.

    Then write the same quaternion in scalar last array storage.

    Does changing storage order change Hamilton multiplication?

  15. Debugging the wrong sandwich order.

    A program intends to compute the passive coordinate transformation for a positive 90∘ frame rotation about +z. It correctly constructs

           √ --   √ --
BqA  = --2-−  --2k,
        2      2

    but accidentally evaluates

     B   ∗ B
( qA) i qA.

    Predict the output and explain the error.

  16. Nonunit quaternion pitfall.

    Let

    r = 2q,

    where q is unit. Show that

    rvr∗ = 4qvq ∗.

    Explain why a nonunit quaternion should not be used with the conjugate sandwich when a pure orthogonal transformation is intended. State the general similarity transformation that removes this scale problem.

3 Solutions

Solution 1: pure quaternion representation

A vector has zero scalar part. Therefore

       ⌊   ⌋
         0
v ← →  || 2 || .
       ⌈− 3⌉
         4
(6)

Equivalently,

v =  2i − 3j + 4k.

Solution 2: positive   ∘
90 frame rotation about +z

The half angle is 45∘. Therefore

       √ --   √ --
B      --2-   --2-
  qA =  2  −   2 k.
(7)

Let

     √ --
     --2-
a =   2 .

Then

Bq  =  a(1 − k)
   A

and

B    ∗
( qA)  = a(1 + k).

The input vector is the pure quaternion

A
  v = i.

First multiply on the left:

Bq  i = a(1 − k)i
   A
      = a(i − ki)
      = a(i − j).

Now multiply by the conjugate:

Bq  i(Bq  )∗ = a2 (i − j)(1 + k )
  A     A
            =  1(i − j + ik − jk) .
               2

Using

ik = − j

and

jk =  i,

we obtain

      1
Bv  = --(i − j − j − i)
      2
    = − j.

Therefore

A ^x −→  B(− ^y).
(8)

The physical vector did not rotate. The coordinate axes rotated positively, so the new coordinates move in the inverse sense.

Solution 3: positive 90∘ frame rotation about +x

The passive quaternion is

       √2--  √2--
BqA =  ---−  ---i.
        2     2

Let again

     √ --
     --2-
a =   2 .

Then

q =  a(1 − i),     q∗ = a(1 + i).

Apply the sandwich to j:

qj = a(j − ij)

  =  a(j − k ).

Then

qjq∗ = 1-(j − k)(1 + i)
       2
       1
     = 2-(j − k + ji − ki ).

Using

ji = − k

and

ki = j,

gives

qjq∗ = − k.

Hence

B
 v =  − ^z.
(9)

Solution 4: vector parallel to the axis

The quaternion has phase 35∘. Therefore the frame rotation angle is

𝜃 = 70∘.

Because the vector part is negative k, the positive frame rotation axis is

+ ^z.

The vector

5k

lies along the rotation axis. A rotation about an axis does not change the component parallel to that axis, so

Bq  (5k)(Bq  )∗ = 5k.
   A        A
(10)

Geometrically, the z axis is common to both frames.

Solution 5: frame rotation through 180∘

For a 180∘ frame rotation about +z, the x and y coordinate axes reverse relative to the original frame while the z axis is unchanged.

Therefore

B
 v = − 2i + 3j + 4k.
(11)

The result is the same numerical transformation as the active 180∘ rotation because a rotation by +π and its inverse differ only by an equivalent axis sign.

Solution 6: symmetric 120 ∘ frame rotation

The half angle is

60∘.

Since

      ∘   1
cos60  =  --
          2

and

         √ --
           3
sin 60∘ = ----,
          2

the passive quaternion is

       1   i + j + k √3-
BqA =  --− ---√---------
       2        3    2
       1-
    =  2(1 − i − j − k).

Thus

Bq  =  1(1 − i − j − k ).
  A    2
(12)

The passive map is the inverse of the familiar positive active 120∘ rotation about (1, 1, 1).

That active rotation cyclically sends

i − → j − → k − → i.

The inverse passive coordinate map therefore cycles in the opposite direction:

i − → k −→  j − → i.

Hence

i − → k
(13)

under Bq A.

The corresponding positive active rotor is

 B   ∗   1
( qA)  = 2-(1 + i + j + k),

and it sends

i − → j.
(14)

Solution 7: passive Rodrigues formula

Here

^u = k

and

A
 v =  2i + j + 3k.

First,

^u × Av =  k × (2i + j + 3k) = 2j − i.

Also,

^u ⋅ Av = 3.

For 𝜃 = 60∘,

                          √ --
      ∘   1           ∘     3                ∘   1
cos 60  = -,     sin 60  = ----,    1 − cos60  =  -.
          2                2                     2

Substitute into the passive Rodrigues formula:

B     1
 v =  2(2i + j + 3k )
         √--
      −  -3-(2j − i)
          2
         3-
      +  2k.

Collecting components gives

      (     √ -)     (        )
B             3        1   √ --
  v =   1 + ----  i +  --−   3  j + 3k.
             2         2
(15)

The z component remains unchanged because the rotation axis is z.

Solution 8: quaternion sign ambiguity

Because

(− q)∗ = − q ∗,

we have

(− q )v (− q)∗ = (− q)v(− q∗)
                 ∗
            = qvq .

Therefore

q  and    − q
(16)

produce the same vector transformation.

This is why a unit quaternion representation of orientation is two to one.

Solution 9: inverse coordinate transformation

Start from

B    B    A  B    ∗
 v =   qA  v(  qA) .

Multiply on the left by (Bq A)∗ and on the right by Bq A:

(BqA)∗Bv BqA  = (BqA )∗(BqA Av (BqA )∗)BqA.

Associativity gives

(Bq  )∗Bv Bq  =  [(Bq )∗Bq  ]Av [(Bq )∗Bq  ].
    A       A        A    A         A     A

Since the frame quaternion is unit,

(Bq  )∗Bq  = 1.
   A     A

Therefore

A     B    ∗B  B
 v = (  qA)  v  qA.
(17)

Equivalently, with

A      B    ∗
 qB = (  qA) ,

the reverse map has the same canonical sandwich form.

Solution 10: norm preservation

Quaternion norm multiplicativity gives

∥Bv∥ =  ∥∥Bq  Av (Bq )∗∥∥
           A       A
     =  ∥BqA∥ ∥Av∥∥(BqA )∗∥.

For a unit frame quaternion,

∥BqA ∥ = ∥ (BqA )∗∥ = 1.

Hence

∥Bv ∥ = ∥Av∥.
(18)

The coordinate transformation preserves Euclidean vector length.

Solution 11: deriving passive Rodrigues’ formula

Let

q = c − s^u.

In scalar vector form,

q = (c,− s^u )

and

v = (0,v).

The first product is

qv = (s^u ⋅ v, cv − s^u × v).
(19)

Now multiply by

q∗ = (c,s^u).

The scalar part cancels. After collecting the vector terms,

vB = (c2 − s2)v

      − 2cs(^u × v )
      + 2s2^u (^u ⋅ v).
(20)

Use

c2 − s2 = cos𝜃,

2cs = sin𝜃,

and

  2
2s  = 1 − cos𝜃.

Then

vB = v cos 𝜃 − (^u × v)sin𝜃 + ^u (u^⋅ v)(1 − cos𝜃).
(21)

The negative sine term is the signature of the passive coordinate transformation for a positive frame rotation.

Solution 12: vector only implementation

Let

q = (qw, q)

and

v = (0,v).

Expanding qvq∗ and collecting vector terms gives

 ′
v =  v + 2qw(q × v ) + 2q × (q ×  v).
(22)

Define

t = 2(q × v ).
(23)

Then

  ′
v  = v + qwt + q × t.
(24)

This formula is not specifically active or passive. It evaluates the Hamilton sandwich qvq∗ for whichever quaternion is supplied.

For the PhysicsLibrary passive frame quaternion,

q = − ^u sin 𝜃-.
           2

The passive sign is already contained in q, so the implementation formula itself does not change.

Solution 13: active versus passive

For the positive 90∘ frame rotation about +z,

       √ --   √ --
         2      2
BqA  = ----−  ---k.
        2      2
(25)

Applying the passive sandwich to

Av = i

gives

Bv =  − j.
(26)

The corresponding positive active rotor is the conjugate:

                  √ --  √ --
         B    ∗   --2-  --2-
qactive = ( qA ) =   2  +  2 k.
(27)

In a fixed frame,

q    iq∗    = j.
 active  active

Thus

i −→  − j
(28)

for the passive coordinate change, while

i −→ +j
(29)

for the corresponding positive active physical rotation.

There is no contradiction. Rotating a vector positively and rotating the coordinate axes positively are inverse operations on the numerical components.

Solution 14: scalar first and scalar last arrays

The passive quaternion for a positive 90∘ frame rotation about +z is

       √ --   √ --
B        2      2
  qA = ----−  ---k.
        2      2

PhysicsLibrary scalar first order is

⌊ √ --   ⌋
    2 ∕2
|    0   |
|⌈    0   |⌉ .
   √ --
  −  2∕2
(30)

Scalar last storage is

⌊        ⌋
     0
||  √ 0-  ||
⌈ −  2∕2 ⌉ .
  √2--∕2
(31)

Only the array indexing changes. Hamilton multiplication remains

ij = k.

Storage order does not define multiplication convention.

Solution 15: debugging the wrong sandwich order

The intended passive transformation is

B    B    A  B    ∗
 v =   qA  v(  qA) .

The program instead computes

(BqA)∗iBqA.

This is the inverse sandwich. Since

          √ --  √ --
 B    ∗   --2-  --2-
( qA ) =   2 +   2 k,

the expression actively rotates i by positive 90∘ about +z and produces

+j.
(32)

The intended passive result was

− j.

The program used the correct quaternion coefficients but reversed the sandwich order, thereby applying the inverse coordinate transformation.

Solution 16: nonunit quaternion pitfall

Let

r = 2q.

Then

 ∗     ∗
r  = 2q .

Therefore

rvr∗ = (2q)v(2q∗)
            ∗
     = 4qvq  .

Hence

∥rvr∗∥ = 4∥v ∥
(33)

when q is unit.

The conjugate sandwich represents an orthogonal transformation only when the quaternion is unit.

For a general nonzero quaternion r, the scale independent similarity transformation is

v′ = rvr−1.
(34)

Since

         ∗
r−1 = -r---,
      ∥r ∥2

the extra quaternion scale cancels.

4 Compact verification table

For the PhysicsLibrary convention, the following checks are useful when debugging a vector transformation implementation:



Case Expected result


identity quaternion v → v


Passive +90 ∘ frame rotation about +z  i →−j


Active +90 ∘ vector rotation about +z  i → +j


Vector parallel to axis unchanged


Replace q  by − q  unchanged transformation


Unit quaternion vector norm preserved


The second and third rows are especially effective at detecting an accidental active versus passive reversal.

5 Sources and exercise provenance

The quaternion sandwich transformation is classical. The problems and solutions in this companion are newly written or rewritten for PhysicsLibrary under the passive frame convention.

Joly and Hathaway provide public domain historical treatments of quaternion rotators. Sommer and coauthors provide a modern convention analysis that is useful for separating Hamilton multiplication from active and passive interpretation.

References

[1]   C. J. Joly, A Manual of Quaternions, Macmillan and Co., London, 1905. Public domain historical source. Internet Archive scan

[2]   A. S. Hathaway, A Primer of Quaternions, 1896. Public domain historical source. Project Gutenberg edition

[3]   H. Sommer, I. Gilitschenski, M. Bloesch, S. Weiss, R. Siegwart, and J. Nieto, “Why and How to Avoid the Flipped Quaternion Multiplication,” Aerospace, vol. 5, no. 3, article 72, 2018. Published under CC BY 4.0. Publisher article

License

Unless otherwise noted, this PhysicsLibrary entry is intended for release under the Creative Commons Attribution ShareAlike 4.0 International license.


"example of rotating vectors with quaternions" is owned by bloftin.
(view preamble)
View style:
See Also: quaternion series overview and article guide, Quaternions for Physics and Engineering: Orientation, Notation, and Conventions, quaternion definition and basic algebra, example of quaternion definition and basic algebra, quaternion product, example of quaternion product, quaternion conjugate, example of quaternion conjugate, quaternion norm, example of quaternion norm, quaternion inverse, example of quaternion inverse

Keywords:  quaternion, vector rotation, unit quaternion, Rodrigues formula, pure quaternion, active rotation, passive rotation, exercises, worked solutions

This object's parent.

Cross-references: domain, identity, operations, program, works, magnitude, quaternion norm, norm, quaternion multiplication, scalar, representation, cross product, formula, vector, quaternion, unit, rotating vectors with quaternions
There is 1 reference to this object.

This is version 2 of example of rotating vectors with quaternions, born on 2026-08-23, modified 2026-08-26.
Object id is 1098, canonical name is ExampleOfRotatingVectorsWithQuaternions.
Accessed 239 times total.

Classification:
Physics Classification: 02.40.Yy (Geometric mechanics )
 02.10.Hh (Rings and algebras)

Pending Errata and Addenda

None.

Discussion

No messages.

Interact