

# Understanding rotation matrices

Rotation matrices are a fundamental concept in linear algebra, geometry, and computer graphics. They are used to represent rotations in two-dimensional and three-dimensional spaces. A rotation matrix is a square matrix that, when multiplied by a vector, rotates the vector by a certain angle.

In this section, we will explore the properties of rotation matrices, their geometric interpretation, and how to apply them in Python.

A rotation matrix is an orthogonal matrix, meaning that its transpose is equal to its inverse. Orthogonal matrices have the property that they preserve the lengths of vectors. This is because the dot product of a vector with its transpose is equal to the square of its length.

In two dimensions, a rotation matrix is a 2x2 matrix with the form:

$$
R(\theta) = \begin{bmatrix}
\cos(\theta) & -\sin(\theta) \\
\sin(\theta) & \cos(\theta)
\end{bmatrix}
$$

In three dimensions, a rotation matrix is a 3x3 matrix with the form:

$$
R(\theta, \mathbf{n}) = I + \sin(\theta) \mathbf{n} \times + (1 - \cos(\theta)) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n}$ is the rotation axis, $\times$ is the cross product operator, and $I$ is the identity matrix.

Example:

Consider a 2D rotation matrix:

$$
R(\theta) = \begin{bmatrix}
\cos(\theta) & -\sin(\theta) \\
\sin(\theta) & \cos(\theta)
\end{bmatrix}
$$

To rotate a vector $\mathbf{v} = [x, y]^T$ by an angle $\theta$, we can multiply the rotation matrix by the vector:

$$
R(\theta) \mathbf{v} = \begin{bmatrix}
\cos(\theta) & -\sin(\theta) \\
\sin(\theta) & \cos(\theta)
\end{bmatrix}
\begin{bmatrix}
x \\
y
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [x', y']^T$ that has been rotated by an angle $\theta$.

## Exercise
Exercise:

Calculate the rotation matrix for a 2D rotation by an angle $\theta$. Then, rotate a vector $\mathbf{v} = [1, 0]^T$ by an angle $\frac{\pi}{2}$.

### Solution
The rotation matrix for a 2D rotation by an angle $\theta$ is:

$$
R(\theta) = \begin{bmatrix}
\cos(\theta) & -\sin(\theta) \\
\sin(\theta) & \cos(\theta)
\end{bmatrix}
$$

To rotate the vector $\mathbf{v} = [1, 0]^T$ by an angle $\frac{\pi}{2}$, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{2}) \mathbf{v} = \begin{bmatrix}
0 & -1 \\
1 & 0
\end{bmatrix}
\begin{bmatrix}
1 \\
0
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [0, 1]^T$.

# Python implementation of rotation matrices

To implement rotation matrices in Python, we can use the NumPy library, which provides a convenient and efficient way to work with arrays and matrices.

First, we need to install NumPy:

```
pip install numpy
```

Then, we can import NumPy and define a function to create a rotation matrix:

```python
import numpy as np

def rotation_matrix_2d(theta):
    return np.array([[np.cos(theta), -np.sin(theta)],
                     [np.sin(theta), np.cos(theta)]])
```

We can also define a function to rotate a vector using a rotation matrix:

```python
def rotate_vector(vector, rotation_matrix):
    return np.dot(rotation_matrix, vector)
```

Example:

To rotate a vector $\mathbf{v} = [1, 0]^T$ by an angle $\frac{\pi}{2}$ using the rotation matrix defined above, we can use the following code:

```python
theta = np.pi / 2
rotation_matrix = rotation_matrix_2d(theta)
vector = np.array([1, 0])
rotated_vector = rotate_vector(vector, rotation_matrix)
```

The result is a new vector $\mathbf{v}' = [0, 1]^T$.

# Example problems and their solutions



## Exercise
Exercise:

Calculate the rotation matrix for a 2D rotation by an angle $\frac{3\pi}{4}$. Then, rotate a vector $\mathbf{v} = [1, 1]^T$ by an angle $\frac{\pi}{2}$.

### Solution
The rotation matrix for a 2D rotation by an angle $\frac{3\pi}{4}$ is:

$$
R(\frac{3\pi}{4}) = \begin{bmatrix}
\frac{\sqrt{2}}{2} & \frac{-\sqrt{2}}{2} \\
\frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2}
\end{bmatrix}
$$

To rotate the vector $\mathbf{v} = [1, 1]^T$ by an angle $\frac{\pi}{2}$, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{2}) \mathbf{v} = \begin{bmatrix}
\frac{\sqrt{2}}{2} & \frac{-\sqrt{2}}{2} \\
\frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [\frac{-\sqrt{2}}{2}, \frac{\sqrt{2}}{2}]^T$.

# Applying rotation matrices to 2D and 3D rotations



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{\pi}{4}$ around the z-axis. Then, rotate a vector $\mathbf{v} = [1, 1, 0]^T$ by an angle $\frac{\pi}{4}$ around the z-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{\pi}{4}$ around the z-axis is:

$$
R(\frac{\pi}{4}, \mathbf{n}) = I + \sin(\frac{\pi}{4}) \mathbf{n} \times + (1 - \cos(\frac{\pi}{4})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [0, 0, 1]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 1, 0]^T]$ by an angle $\frac{\pi}{4}$ around the z-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{4}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
1 & 0 & 0 \\
0 & \frac{\sqrt{2}}{2} & \frac{-\sqrt{2}}{2} \\
0 & \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
1 \\
0
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [1, \frac{\sqrt{2}}{2}, \frac{\sqrt{2}}{2}]^T$.

# The effect of rotation matrices on coordinates



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{\pi}{3}$ around the y-axis. Then, rotate a vector $\mathbf{v} = [1, 0, 1]^T$ by an angle $\frac{\pi}{3}$ around the y-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{\pi}{3}$ around the y-axis is:

$$
R(\frac{\pi}{3}, \mathbf{n}) = I + \sin(\frac{\pi}{3}) \mathbf{n} \times + (1 - \cos(\frac{\pi}{3})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [0, 1, 0]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 0, 1]^T$ by an angle $\frac{\pi}{3}$ around the y-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{3}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
\frac{\sqrt{3}}{2} & 0 & \frac{-\sqrt{3}}{2} \\
0 & 1 & 0 \\
\frac{\sqrt{3}}{2} & 0 & \frac{\sqrt{3}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
0 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [\frac{\sqrt{3}}{2}, 0, \frac{\sqrt{3}}{2}]^T$.

# Visualizing rotation transformations



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{2\pi}{3}$ around the x-axis. Then, rotate a vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{2\pi}{3}$ around the x-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{2\pi}{3}$ around the x-axis is:

$$
R(\frac{2\pi}{3}, \mathbf{n}) = I + \sin(\frac{2\pi}{3}) \mathbf{n} \times + (1 - \cos(\frac{2\pi}{3})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [1, 0, 0]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{2\pi}{3}$ around the x-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{2\pi}{3}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
1 & 0 & 0 \\
0 & \frac{\sqrt{3}}{2} & \frac{-\sqrt{3}}{2} \\
0 & \frac{\sqrt{3}}{2} & \frac{\sqrt{3}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
1 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [1, \frac{\sqrt{3}}{2}, \frac{\sqrt{3}}{2}]^T$.

# Using rotation matrices in computer graphics and physics simulations



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{\pi}{6}$ around the z-axis. Then, rotate a vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{6}$ around the z-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{\pi}{6}$ around the z-axis is:

$$
R(\frac{\pi}{6}, \mathbf{n}) = I + \sin(\frac{\pi}{6}) \mathbf{n} \times + (1 - \cos(\frac{\pi}{6})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [0, 0, 1]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{6}$ around the z-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{6}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
1 & 0 & 0 \\
0 & \frac{\sqrt{3}}{2} & \frac{-\frac{1}{2}}{2} \\
0 & \frac{\frac{1}{2}}{2} & \frac{\sqrt{3}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
1 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [1, \frac{\sqrt{3}}{2}, \frac{\sqrt{3}}{2}]^T$.

# Advanced topics: shear transformations and projective transformations



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{\pi}{2}$ around the x-axis. Then, rotate a vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{2}$ around the x-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{\pi}{2}$ around the x-axis is:

$$
R(\frac{\pi}{2}, \mathbf{n}) = I + \sin(\frac{\pi}{2}) \mathbf{n} \times + (1 - \cos(\frac{\pi}{2})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [1, 0, 0]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{2}$ around the x-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{2}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
1 & 0 & 0 \\
0 & 0 & -1 \\
0 & 1 & 0
\end{bmatrix}
\begin{bmatrix}
1 \\
1 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [1, 0, -1]^T$.

# Exploring applications in computer vision and robotics



## Exercise
Exercise:

Calculate the rotation matrix for a 3D rotation by an angle $\frac{\pi}{4}$ around the y-axis. Then, rotate a vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{4}$ around the y-axis.

### Solution
The rotation matrix for a 3D rotation by an angle $\frac{\pi}{4}$ around the y-axis is:

$$
R(\frac{\pi}{4}, \mathbf{n}) = I + \sin(\frac{\pi}{4}) \mathbf{n} \times + (1 - \cos(\frac{\pi}{4})) \mathbf{n} \mathbf{n}
$$

where $\mathbf{n} = [0, 1, 0]^T$ is the rotation axis.

To rotate the vector $\mathbf{v} = [1, 1, 1]^T$ by an angle $\frac{\pi}{4}$ around the y-axis, we can multiply the rotation matrix by the vector:

$$
R(\frac{\pi}{4}, \mathbf{n}) \mathbf{v} = \begin{bmatrix}
\frac{\sqrt{2}}{2} & 0 & \frac{-\sqrt{2}}{2} \\
0 & 1 & 0 \\
\frac{\sqrt{2}}{2} & 0 & \frac{\sqrt{2}}{2}
\end{bmatrix}
\begin{bmatrix}
1 \\
1 \\
1
\end{bmatrix}
$$

The result is a new vector $\mathbf{v}' = [\frac{\sqrt{2}}{2}, 1, \frac{\sqrt{2}}{2}]^T$.

# Conclusion and further reading

In this textbook, we have explored the concepts of rotation matrices, their geometric interpretation, and their applications in computer graphics, physics simulations, computer vision, and robotics.

Further reading on the topic includes:

- Linear algebra and its applications by Gilbert Strang
- Computer graphics: principles and practice by Peter Shirley, Michael Ashikhmin, and Steve Marschner
- An introduction to robotics by Peter Corke
- Computer vision: algorithms and applications by Richard Szeliski

These resources provide a more in-depth understanding of the topics covered in this textbook and can serve as a foundation for further study.