---
title: Rotations, reflections and coordinate frames
heading: Orthogonal matrices at work, from a phone's accelerometer to a radio's constellation
eyebrow: A7 · Lesson 49 · Rotations, reflections and coordinate frames
xp: 40
---

::: hook
A phone's accelerometer measures along the phone's own axes. To know which way is down in the room, the reading has to be turned into the room's coordinates. A robot arm chains a rotation and a shift for every joint, and a radio receiver turns its whole constellation back by the carrier's phase error. All of these are **orthogonal matrices** (lesson 24): matrices whose columns are perpendicular unit vectors, so they move vectors without stretching them. In three dimensions they also have a habit that 2D rotations don't: the order you apply them in matters.
:::

::: play
- The first arrow is a vector $\mathbf{x}$; the second is $\mathbf{x}$ rotated by the angle $; the third is $\mathbf{x}$ reflected in the mirror line at angle $.
- All three keep the same length. Turning the rotation through a full circle brings it back; moving the mirror swings the reflection twice as fast, because a reflection doubles the angle between the vector and the mirror.
:::

```sim plot
view: complex
range: 2
slider: t = 0..360 step 5 = 40 °
slider: m = 0..180 step 5 = 60 °
arrow: 1.2 + 0.5*j | label: x
arrow: (1.2 + 0.5*j)*exp(j*t°) | color: ch2 | label: rotated
arrow: (1.2 - 0.5*j)*exp(2*j*m°) | color: ch3 | label: reflected
arrow: 1.9*exp(j*m°) | color: ch4 | label: mirror
readout: length of every arrow but the mirror = sqrt(1.2^2 + 0.5^2) | digits: 3
```

::: predict
## Rotate the vector $(1, 0, 0)$ by 90° about the $x$ axis, then 90° about the $z$ axis. Now do it in the other order. Do you land in the same place?

- [ ] Yes, rotations commute
- [x] No: one order gives $(0, 1, 0)$ and the other $(0, 0, 1)$
- [ ] Only if the angles are equal
- [ ] No: one of them gives $(0, 0, 0)$

::: reveal
No. About $x$ first, $(1, 0, 0)$ lies on the axis and doesn't move; then 90° about $z$ takes it to $(0, 1, 0)$. About $z$ first takes it to $(0, 1, 0)$; then 90° about $x$ turns that to $(0, 0, 1)$. In matrix terms $R_zR_x \neq R_xR_z$: 3D rotations don't commute, which is why orientation is specified with an agreed order of axes.
:::
:::

::: nameit
## Orthogonal matrices

$Q$ is **orthogonal** when $Q^TQ = I$. Then:
- $Q^{-1} = Q^T$, so undoing it costs nothing;
- $|Q\mathbf{x}| = |\mathbf{x}|$ and angles are kept;
- $\det Q = \pm 1$: $+1$ for a **rotation**, $-1$ for a **reflection**.

## Rotations

In the plane, $R(\theta) = \begin{bmatrix}\cos\theta & -\sin\theta\\\sin\theta & \cos\theta\end{bmatrix}$ turns vectors counterclockwise by $\theta$. In space, the rotations about the coordinate axes are

$$R_x(\theta) = \begin{bmatrix}1 & 0 & 0\\0 & c & -s\\0 & s & c\end{bmatrix} \quad R_y(\theta) = \begin{bmatrix}c & 0 & s\\0 & 1 & 0\\-s & 0 & c\end{bmatrix} \quad R_z(\theta) = \begin{bmatrix}c & -s & 0\\s & c & 0\\0 & 0 & 1\end{bmatrix}$$

with $c = \cos\theta$, $s = \sin\theta$. A product $R_zR_yR_x$ applies $R_x$ first. Every 3D rotation turns about some axis by some angle $\theta$, and its trace gives the angle: $\text{tr}\,R = 1 + 2\cos\theta$.

## Reflections

The **Householder reflection** across the plane perpendicular to a unit vector $\mathbf{u}$ is

$$H = I - 2\mathbf{u}\mathbf{u}^T$$

It sends $\mathbf{u}$ to $-\mathbf{u}$ and leaves every vector perpendicular to $\mathbf{u}$ alone. $H$ is symmetric and orthogonal, so $H^2 = I$. Choosing $\mathbf{u}$ along $\mathbf{x} - |\mathbf{x}|\mathbf{e}_1$ reflects $\mathbf{x}$ onto the first axis, the step that QR factorization (lesson 28) repeats column by column in numerical libraries.

## Coordinate frames

A sensor mounted on a robot is rotated by $R$ and shifted by $\mathbf{t}$ from the robot's frame, so a point $\mathbf{p}_s$ it measures sits at $R\mathbf{p}_s + \mathbf{t}$. Stacking both into one $4 \times 4$ **homogeneous** matrix makes the shift a matrix product too:

$$T = \begin{bmatrix}R & \mathbf{t}\\\mathbf{0}^T & 1\end{bmatrix} \qquad T\begin{bmatrix}\mathbf{p}\\1\end{bmatrix} = \begin{bmatrix}R\mathbf{p} + \mathbf{t}\\1\end{bmatrix} \qquad T^{-1} = \begin{bmatrix}R^T & -R^T\mathbf{t}\\\mathbf{0}^T & 1\end{bmatrix}$$

A chain of joints is a product of such matrices, read right to left from the tool back to the base.
:::

::: example
## A phone lies rolled 30° about its own $x$ axis. Its accelerometer reads $(0, -4.905, -8.496)$ m/s² in the phone's axes. What is that in the room's axes, where the phone's frame is $R_x(30°)$ relative to the room?

::: solution
1. **Multiply by $R_x(30°)$.** The first entry stays 0, and the second is
   $$\begin{aligned} &0.866(-4.905) + 0.5(8.496) \\ &= 0 \end{aligned}$$
2. **Third entry.**
   $$\begin{aligned} &0.5(-4.905) + 0.866(-8.496) \\ &= -9.81 \end{aligned}$$
3. **Read the result.**
   $$\begin{bmatrix}0\\0\\-9.81\end{bmatrix}$$
   Straight down at 9.81 m/s²: gravity, as it should be for a phone at rest.
4. **Going the other way.** From the room to the phone uses $R_x(30°)^T$, the inverse for free. This is the first step of every orientation filter.

**Answer:** $(0, 0, -9.81)$ m/s² in the room's axes: gravity, straight down.
:::
:::

::: example
## Find the Householder reflection that sends $\mathbf{x} = (3, 4)$ onto the first axis.

::: solution
1. **Aim for the axis.** $|\mathbf{x}| = 5$, so aim for $(5, 0)$:
   $$\mathbf{u} \propto \mathbf{x} - (5, 0) = (-2, 4)$$
   which normalizes to $(-0.447, 0.894)$.
2. **Build $H$.**
   $$\begin{aligned} H &= I - 2\mathbf{u}\mathbf{u}^T \\ &= \begin{bmatrix}1 - 0.4 & 0.8\\0.8 & 1 - 1.6\end{bmatrix} \\ &= \begin{bmatrix}0.6 & 0.8\\0.8 & -0.6\end{bmatrix} \end{aligned}$$
3. **Check.**
   $$\begin{aligned} H(3, 4) &= (1.8 + 3.2,\ 2.4 - 2.4) \\ &= (5, 0) \end{aligned}$$
   $$\begin{aligned} \det H &= -0.36 - 0.64 \\ &= -1 \end{aligned}$$
   a reflection.
4. **The mirror.** It's the line through the origin halfway between $\mathbf{x}$ and $(5, 0)$, so $\mathbf{x}$ lands on the axis with its length unchanged.

**Answer:** $H = \begin{bmatrix}0.6 & 0.8\\0.8 & -0.6\end{bmatrix}$.
:::
:::

::: example
## A rotation is $R = R_z(40°)\,R_x(25°)$. By what single angle does it turn space, about its own axis?

::: solution
1. **Diagonal of $R$.** Multiplying out, the diagonal is $\cos 40°$, $\cos 40°\cos 25°$ and $\cos 25°$: 0.766, 0.694 and 0.906, so
   $$\text{tr}\,R = 2.367$$
2. **Angle from the trace.**
   $$1 + 2\cos\theta = 2.367$$
   gives $\cos\theta = 0.683$ and $\theta = 46.9°$.
3. **Compare.** Two turns of 40° and 25° about different axes add up to one turn of 46.9°, less than their sum because the axes aren't the same.

**Answer:** it turns space by 46.9° about its own axis.
:::
:::

::: check
given: x = 1 | 2 | 3; y = 1 | -2 | 4; th = 30 | 45 | 120 | 200 °
answer: x*cos(th°) - y*sin(th°)
tolerance: 0.5%

## Rotate the vector $(\given{x}, \given{y})$ counterclockwise by {th}. What is the new $x$ component?

::: right
Right. The first row of $R(\theta)$ gives $x\cos\theta - y\sin\theta$ = {answer}.
:::

::: misses
- [x*cos(th°) + y*sin(th°)] That's a clockwise rotation, by $-\theta$. Counterclockwise has $-\sin\theta$ in the first row.
- [x*sin(th°) + y*cos(th°)] That's the new $y$ component.
- [any] Not yet. Multiply by the first row of $R(\theta)$: $(\cos\theta, -\sin\theta)$.
:::
:::

::: check
given: tr = 2.8 | 2.5 | 2 | 1 | 0
answer: acos((tr - 1)/2)*180/pi
unit: °
tolerance: 0.5%

## A 3D rotation matrix has trace {tr}. Through what angle does it rotate?

::: right
Right. $\text{tr}\,R = 1 + 2\cos\theta$, so $\theta = \arccos((\given{tr} - 1)/2)$ = {answer}.
:::

::: misses
- [acos(tr/3)*180/pi] The trace of a rotation by $\theta$ is $1 + 2\cos\theta$: the axis contributes 1 and the plane of turning $2\cos\theta$.
- [acos((tr - 1)/2)] That's the angle in radians. Give it in degrees.
- [any] Not yet. Solve $1 + 2\cos\theta =$ {tr}.
:::
:::

::: check
kind: python
given: a = 10 | 35 | 70; b = 20 | 45; c = 15 | 60

## Write `Rx(t)`, `Ry(t)` and `Rz(t)` returning the 3D rotation matrices for an angle in degrees. Set `R = Rz(a) @ Ry(b) @ Rx(c)` with `a` = {a}, `b` = {b}, `c` = {c}; `ortho` to the largest entry of $|R^TR - I|$; `det` to its determinant; `v` to `R @ [1, 2, 3]`; and `gap` to the distance between `v` and `Rx(c) @ Ry(b) @ Rz(a) @ [1, 2, 3]`.

```python starter
import numpy as np
def Rx(t): return np.eye(3)
def Ry(t): return np.eye(3)
def Rz(t): return np.eye(3)
R = Rz(a) @ Ry(b) @ Rx(c)
ortho = det = gap = 0
v = R @ np.array([1, 2, 3])
print(v, gap)
```

```python solution
import numpy as np
def Rx(t):
    c, s = np.cos(np.radians(t)), np.sin(np.radians(t))
    return np.array([[1, 0, 0], [0, c, -s], [0, s, c]])
def Ry(t):
    c, s = np.cos(np.radians(t)), np.sin(np.radians(t))
    return np.array([[c, 0, s], [0, 1, 0], [-s, 0, c]])
def Rz(t):
    c, s = np.cos(np.radians(t)), np.sin(np.radians(t))
    return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
R = Rz(a) @ Ry(b) @ Rx(c)
ortho = np.max(np.abs(R.T @ R - np.eye(3)))
det = np.linalg.det(R)
p = np.array([1, 2, 3])
v = R @ p
gap = np.linalg.norm(v - Rx(c) @ Ry(b) @ Rz(a) @ p)
print(v, gap)
```

```python tests
import numpy as np
def rx(t): c, s = np.cos(np.radians(t)), np.sin(np.radians(t)); return np.array([[1, 0, 0], [0, c, -s], [0, s, c]])
def ry(t): c, s = np.cos(np.radians(t)), np.sin(np.radians(t)); return np.array([[c, 0, s], [0, 1, 0], [-s, 0, c]])
def rz(t): c, s = np.cos(np.radians(t)), np.sin(np.radians(t)); return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
assert np.allclose(Rx(30), rx(30)) and np.allclose(Ry(30), ry(30)) and np.allclose(Rz(30), rz(30)), "Check the signs of the sines: R_y has +s in its top right corner."
p = np.array([1, 2, 3]); W = rz(a) @ ry(b) @ rx(c)
assert np.allclose(v, W @ p) and abs(np.linalg.norm(v) - np.linalg.norm(p)) < 1e-12, "v is R @ [1, 2, 3], and its length should equal √14."
assert ortho < 1e-12 and abs(det - 1) < 1e-12, "A rotation has RᵀR = I and determinant 1."
assert abs(gap - np.linalg.norm(W @ p - rx(c) @ ry(b) @ rz(a) @ p)) < 1e-12 and gap > 0.01, "The two orders give different vectors."
```

::: right
Right. Each matrix keeps lengths, so their product does too, and its determinant is the product of three 1s. Reversing the order moves the vector somewhere else entirely, the non-commuting that the predict showed.
:::
:::

::: check
kind: python
given: k = 1..5 int

## Write `householder(x)` returning the reflection $H = I - 2\mathbf{u}\mathbf{u}^T$ with $\mathbf{u}$ the unit vector along $\mathbf{x} - |\mathbf{x}|\mathbf{e}_1$, so that $H\mathbf{x} = |\mathbf{x}|\mathbf{e}_1$. Set `H = householder(x)` for the starter's random 4-vector, `Hx = H @ x`, and `detH` to its determinant.

```python starter
import numpy as np
x = np.random.default_rng(k).normal(0, 1, 4)
def householder(x):
    return np.eye(len(x))
H = householder(x)
Hx = H @ x
detH = 1
print(Hx)
```

```python solution
import numpy as np
x = np.random.default_rng(k).normal(0, 1, 4)
def householder(x):
    e = np.zeros(len(x)); e[0] = np.linalg.norm(x)
    u = x - e
    u = u / np.linalg.norm(u)
    return np.eye(len(x)) - 2 * np.outer(u, u)
H = householder(x)
Hx = H @ x
detH = np.linalg.det(H)
print(Hx)
```

```python tests
import numpy as np
xx = np.random.default_rng(k).normal(0, 1, 4)
assert np.allclose(Hx, [np.linalg.norm(xx), 0, 0, 0], atol=1e-12), f"H x should be ({np.linalg.norm(xx):.4f}, 0, 0, 0)."
assert np.allclose(H @ H, np.eye(4)) and np.allclose(H, H.T), "A Householder reflection is symmetric and is its own inverse."
assert abs(detH + 1) < 1e-12, "A reflection has determinant −1."
```

::: right
Right. One reflection zeroes every entry of $\mathbf{x}$ below the first while keeping its length. QR factorization does exactly this to each column in turn, which is why it's so stable: every step is orthogonal and can't magnify errors.
:::
:::

::: check
kind: python
given: tx = 0.1 | 0.2; ty = 0 | -0.05; tz = 0.1 | 0.3; yaw = 30 | 90 | 150

## A lidar is mounted on a robot at `t` = ({tx}, {ty}, {tz}) m, turned by `yaw` = {yaw}° about the robot's $z$ axis. Build its $4 \times 4$ homogeneous transform `T` from lidar to robot, set `pw` to the robot-frame position of the lidar point (2, 0, 0), `Tinv` to the inverse built from $R^T$ and $-R^T\mathbf{t}$, and `back` to `Tinv` applied to `pw`.

```python starter
import numpy as np
T = np.eye(4)
pw = np.array([2.0, 0, 0])
Tinv = np.linalg.inv(T)
back = pw
print(pw)
```

```python solution
import numpy as np
c, s = np.cos(np.radians(yaw)), np.sin(np.radians(yaw))
R = np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
t = np.array([tx, ty, tz])
T = np.eye(4); T[:3, :3] = R; T[:3, 3] = t
pw = (T @ np.array([2.0, 0, 0, 1]))[:3]
Tinv = np.eye(4); Tinv[:3, :3] = R.T; Tinv[:3, 3] = -R.T @ t
back = (Tinv @ np.append(pw, 1))[:3]
print(pw, back)
```

```python tests
import numpy as np
c, s = np.cos(np.radians(yaw)), np.sin(np.radians(yaw))
R = np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]]); t = np.array([tx, ty, tz])
assert np.allclose(T[:3, :3], R) and np.allclose(T[:3, 3], t) and np.allclose(T[3], [0, 0, 0, 1]), "T has R in its top left, t in its last column and [0, 0, 0, 1] as its bottom row."
assert np.allclose(pw, R @ [2, 0, 0] + t), f"The point lands at {np.round(R @ [2, 0, 0] + t, 4)} in the robot's frame."
assert np.allclose(Tinv @ T, np.eye(4)) and np.allclose(back, [2, 0, 0]), "Tinv uses Rᵀ and -Rᵀt, and takes pw back to (2, 0, 0)."
```

::: right
Right. The homogeneous form turns "rotate, then shift" into one multiplication, and the inverse needs no general matrix inversion: transpose the rotation and undo the shift.
:::
:::

::: check
kind: parts
given: A = 1 | 2; d = 20 | 45 | 70 | 135; phi = 10 | 30 | -25

## A receiver expects a symbol at amplitude {A} and angle {d}°, but a carrier phase error of {phi}° has turned the whole constellation. It received $(I, Q) = \left(\given{A}\cos(\given{d}° + \given{phi}°),\ \given{A}\sin(\given{d}° + \given{phi}°)\right)$. Rotate it back by $-\given{phi}°$.

::: part
answer: A*cos(d°)
tolerance: 0.5%

### What is the corrected $I$?

::: misses
- [A*cos((d + 2*phi)°)] That turns it further by {phi}°. Undoing a rotation uses the opposite angle.
- [any] Not yet. Multiply $(I, Q)$ by $R(-\given{phi}°)$.
:::
:::

::: part
answer: A*sin(d°)
tolerance: 0.5%

### What is the corrected $Q$?

::: misses
- [A*sin((d + 2*phi)°)] That's a rotation the wrong way.
- [any] Not yet. Multiply $(I, Q)$ by $R(-\given{phi}°)$.
:::
:::

::: right
Right. A phase error rotates every point the same way, so one rotation by $-\varphi$, the transpose of the error's matrix, restores the whole constellation.
:::
:::

::: check
kind: choice

## $Q$ is a real orthogonal $3 \times 3$ matrix with $\det Q = -1$. What does it do?

- [x] It reflects: it reverses orientation while keeping lengths
- [ ] It rotates by 180°
- [ ] It shrinks every vector to zero
- [ ] Nothing: every orthogonal matrix has determinant 1

::: right
Right. Orthogonal matrices keep lengths and angles; the determinant's sign says whether they keep or reverse handedness. A right hand reflected in a mirror is a left hand, and no rotation can do that.
:::

::: misses
- [2] A 180° rotation is still a rotation, with determinant +1.
- [3] Orthogonal matrices keep every length; none of them shrinks anything.
- [4] Orthogonal matrices have determinant +1 or −1. The −1 ones are reflections, or a rotation combined with one.
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3

## Show that turning by $b$ and then by $a$ in the plane is a single turn by $a + b$.

::: step
answer: cos(a)*cos(b) - sin(a)*sin(b)

### What is the top-left entry of $R(a)R(b)$?
:::

::: step
answer: cos(a + b)

### Write it as a single cosine.
:::

::: step
answer: sin(a + b)

### Doing the same for the bottom-left entry, $\sin a\cos b + \cos a\sin b$, gives what?
:::

::: right
Right. $R(a)R(b) = R(a + b)$, so in the plane rotations commute and their angles add. Multiplying the matrices proves the angle-sum formulas, too.
:::
:::

::: explain
## Why is the inverse of a rotation matrix just its transpose? What does that save in practice?

::: model
A rotation's columns are the rotated coordinate axes, which are still perpendicular unit vectors. So in $R^TR$, each entry is the dot product of two of those columns: 1 for a column with itself and 0 for two different ones, which makes $R^TR = I$ and $R^{-1} = R^T$. In practice, converting between frames in both directions costs only a transpose, no elimination and no rounding error from inverting, which matters when an orientation filter does it a thousand times a second.
:::
:::

::: practice

::: check
given: th = 30 | 60 | 135
answer: sin(th°)
tolerance: 0.5%

## What is the bottom-left entry of the 2D rotation matrix $R(\given{th})$?

::: misses
- [-sin(th°)] That's the top-right entry.
- [any] $R(\theta) = \begin{bmatrix}\cos\theta & -\sin\theta\\\sin\theta & \cos\theta\end{bmatrix}$.
:::
:::

::: check
given: th = 20 | 50 | 90 | 170
answer: 1 + 2*cos(th°)
tolerance: 0.5%

## A 3D rotation turns by {th} about some axis. What is its trace?

::: misses
- [2*cos(th°)] The axis direction is unchanged, which adds 1 to the trace.
- [any] The trace is $1 + 2\cos\theta$.
:::
:::
:::
