Vectors

Definition

The mathmatical definition of a vector is a list of numbers or ‘array’. Vectors can be any dimension (except 1 since it would just be a scalar). For example a 3D vector would look like this:

\[\boldsymbol{v} = \begin{bmatrix} 7 \\ 5 \\ 2 \end{bmatrix}\]

We can refer to a element in a vector in 2 ways. We can use letters to refer to a number in a vector but this is limited to 4D vectors. When refering to a number as a letter the first element is x the second y followed by z and finally w for the 4th element. We can also just use integers to refer to elements in a vector. For example v2 would refer to the second element.

Now we know the mathmatical definition of vectors lets get to the intresting stuff: geometry. The geometric interpretation of vectors is a directed line segment that has magnitude and direction.

  • The magnitude of a vector is the length of the vector. A vector may have any nonnegative length.
  • The direction of a vector describes which way the vector is facing.

Lets start by visualizing the vector.

\[\begin{tikzpicture}[framed] \node (A) at (1,2) {Tail}; \node (B) at (3,4) {Head}; \draw [->, blue, thick] (A) -- (B); \end{tikzpicture}\]

You might wonder what the coordinates are of this vector. This question is however wrong because vectors only have magnitude and direction. We can think of it like movement. Lets say I tell you to take 4 steps forward. Forward is the direction and 4 is the magnitude of the displacement vector I told you to move about. The same principal works for velocity vectors (Move at a speed of 20 km/h backwards).

Points

You may have noticed that some programmers use vectors as a manner of specifying positions which is fine to do as long as its understandable whatever you are using it for positioning or displacement. Some people say that vectors only should be used to specify displacement and points only relative position. This is because you can add two vectors, you can add a vector to a point but you cannot add two points together. We programmers however experience that programmes who follow these ethics are always longer and never faster. Whatever it makes code easier to read is subjective.

Scalar Multiplication

We cannot add a vector to a scalar but we can multiply a vector by a scalar. The result is a vector that is parallel to the original vector, with different magnitude and possibly oposite direction. Vector times scalar multiplication is straightforward. We can just multiply each component of the vector by the scalar. For example lets multiply a 3D vector by a scalar (s):

\[\boldsymbol{r} \begin{bmatrix} x \\ y \\ z \end{bmatrix} \times s = \begin{bmatrix} x \times s \\ y \times s \\ z \times s \end{bmatrix}\]

With real world values it would look like this:

\[\boldsymbol{r} \begin{bmatrix} 3 \\ 6 \\ 4 \end{bmatrix} \times 3 = \begin{bmatrix} 3 \times 3 \\ 6 \times 3 \\ 4 \times 3 \end{bmatrix} = \begin{bmatrix} 9 \\ 18 \\ 12 \end{bmatrix}\]

Now you know the formula it can ofton help to understand multiplication of vectors by visualizing it. Here are some examples:

\begin{tikzpicture} \draw[step=1cm,gray,very thin] (0,0) grid (4,4);

\node label=below:$\boldsymbol{v}$ at (0,0) {}; \node label=below:$\boldsymbol{v} 2$ at (1,0) {}; \node label=below:$\boldsymbol{v} 1.5$ at (2,0) {}; \node label=below:$-\boldsymbol{v}$ at (3,0) {};

\draw [->, blue, thick] (0,0) – (1,2); \draw [->, blue, thick] (1,0) – (2,4); \draw [->, blue, thick] (2,0) – (3.5, 3); \draw [->, blue, thick] (4,2) – (3, 0); \end{tikzpicture}

Addition and Subtraction

Again this is really straightforward as long as they have the same dimensions. Unlike multiplication you can’t add a scalar to a vector. Here is the example of vector addition:

\[\boldsymbol{a} \begin{bmatrix} x \\ y \\ z \end{bmatrix} + \boldsymbol{b} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} \boldsymbol{a}x + \boldsymbol{b}x \\ \boldsymbol{a}y + \boldsymbol{b}y \\ \boldsymbol{a}z + \boldsymbol{b}z \end{bmatrix}\]

In the following example we have a red vector ($\boldsymbol{a}$) with the values $\begin{bmatrix} 4 \ 1 \end{bmatrix}$ and a green vector ($\boldsymbol{b}$) with the values $\begin{bmatrix} -2 \ 3 \end{bmatrix}$.

\begin{tikzpicture} \draw[step=1cm,gray,very thin] (0,0) grid (4,4);

\draw [->, red, thick] (0,0) – (4,1); \draw [->, green, thick] (4,1) – (2, 4); \draw [->, blue, thick] (0,0) – (2, 4); \end{tikzpicture}

If we position the vectors so that the head of $\boldsymbol{a}$ toutches the tail of $\boldsymbol{b}$ and then draw a vector (the blue vector in the previous example) from the tail of $\boldsymbol{a}$ to the head of $\boldsymbol{b}$ we get the resulting vector of $\boldsymbol{a} + \boldsymbol{b}$. This is known as the triangle rule. If you would do draw the addition of more than 2 vectors it would still work.

Displacement Vector From One Point To Another

It is often that you will need to find the displacement vector between to points. This can be done by using the triangle rule and vector subtraction. To find the displacement vector from point b to a can be computed by substracting a from b.

To compute the displacement vector we interpret the points as vectors from the origin, and then use the triangle rule.

Magnitude

The magnitude of a vector is also known as the “length” or “norm”. The magnitude is not stored directly in the vector (Meaning vx nor vy is the magnitude in a 2D vector). We will need to calculate the magnitude ourselfs.

The magnitude of a vector is the square root of the sum of the squares of the components of the vector. This sounds complicated but is infact very simple. I’ll demonstrate it with a 2D and a 3D vector:

\[m = \sqrt{\boldsymbol{v}x^2 + \boldsymbol{v}y^2} \label{2D Vector}\] \[m = \sqrt{\boldsymbol{v}x^2 + \boldsymbol{v}y^2 + \boldsymbol{v}z^2} \label{3D Vector}\]

Unit Vectors

Sometimes you don’t want to bother with the magnitude of a vector and just want to use the direction. A vector with a magnitude of 1 is called a Unit Vector. This means multiplying a unit (directional) vector by a displacement vector won’t affect the speed of movement only the direction.

We can compute a unit vector from a non-unit vector. To normalize a vector we divide the vector by its magnitude. Here is a example of normalizing a 3D vector called v split into 2 steps for readability.

\[m = \sqrt{\boldsymbol{v}x^2 + \boldsymbol{v}y^2 + \boldsymbol{v}z^2} \label{Calculate magnitude}\] \[\boldsymbol{r} = \begin{bmatrix} \boldsymbol{v}x / m \\ \boldsymbol{v}y / m \\ \boldsymbol{v}z / m \end{bmatrix} \label{Normalize vector}\]

Dot Product

I talked about multiplying a vector by a scalar previously in this paper but I never mentioned multiplying a vector by a vector. This is because you can multiply vectors with 2 methods: cross and dot. The cross product can for example be used to calculate the angle between 2 vectors.

The dot product is defined by a . in maths. To calculate the product of a vector is equal to the product of every corresponding component of the vectors added together. So here we go with another example:

\[d = \boldsymbol{a} . \boldsymbol{b} = \boldsymbol{a}1 \times \boldsymbol{b}1 + \boldsymbol{a}1 \times \boldsymbol{b}2 + ... + \boldsymbol{a}n \times \boldsymbol{b}n\]

Cross Product

This is the other vector product. The cross product can only be applied in 3D sinec instead of the formula yielding a scalar like in the dot product the cross product will yield a 3D vector.

For the dot product we used a . to specify a dot product operation. For the cross product we will use X. The equation for the cross product is:

\[\begin{bmatrix} \boldsymbol{a}x / m \\ \boldsymbol{a}y / m \\ \boldsymbol{a}z / m \end{bmatrix} \times \begin{bmatrix} \boldsymbol{b}x / m \\ \boldsymbol{b}y / m \\ \boldsymbol{b}z / m \end{bmatrix} = \begin{bmatrix} \boldsymbol{a}y \times \boldsymbol{b}z - \boldsymbol{a}z \times \boldsymbol{b}y \\ \boldsymbol{a}z \times \boldsymbol{b}x - \boldsymbol{a}x \times \boldsymbol{b}z \\ \boldsymbol{a}x \times \boldsymbol{b}y - \boldsymbol{a}y \times \boldsymbol{b}x \\ \end{bmatrix}\]

Geometrically the cross product yields a vector that is perpendicular to the original two vectors. The length of $\boldsymbol{a} \times \boldsymbol{b}$ is equal to the product of the magnitudes of a and b and the sine of the angle between a and b.

If A and B are parallel or if a or b is the zero vector, then \(\boldsymbol{a} \times \boldsymbol{b} = 0\). So the cross product interprets the zero vector as being parallel to every other vector. See how this is different from the dot product which interprets the zero vector as being perpendicular to every other vector.

Matrices

Dimensions and Notation

We define the size of a matrix by counting the amount of collumns and rows. We say that a matrix with r rows and c collumns is an $r \times c$ (Pronounced as r by c) matrix. So a $2 \times 3$ matrix has 2 collumns and 3 rows.

\[M = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}\]

This is a $3 \times 2$ matrix demonstration one of the standard notiations for writing matrices. I will keep using this notation going forward and will represenent a matrix by a bold uppercase letter. for example: M. And I will reference positions of a matrix with the bold uppercase letter follewed by the row and the collumn in itallic. Like this: M12 (Read as M one two not twelve).

\[M = \begin{bmatrix} 9 & 7 & 4 \\ 19 & 3 & 8 \end{bmatrix}\]

In this arary M12 referes to 7. This manner of indexing is different from arrays in programming languages. This is why its common for classes that store fixed sized matrices to give each element its own variable instead of using a 2D array like this: m[3][4]. For example: float m34 which means the variable in row 3 collumn 4.

Square Matrices

When a matrix has the same amound of rows as it has collumns we call it a square matrix. These matrices are particularly important in 3D Maths. The diagonal elements in a matrix are the elements with the same row and collumn (Highlighted in blue in matrix M). In a $3 \times 3$ matrix are M11 M22 and M33 the diagonal elements not M13 and M31

\[D = \begin{bmatrix} 5 & 0 & 0 & 0 \\ 0 & 24 & 0 & 0 \\ 0 & 0 & 19 & 0 \\ 0 & 0 & 0 & 73 \end{bmatrix}\]

If all nondiagonal elements are zero as demonstrated in matrix D it means the matrix is a diagonal matrix. A special diagonal matrix is the identity matrix. A identity matrix is a square matrix with all diagonals elements defined as 1 and all nondiagonal elements as 0. For example a $3 \times 3$ matrix:

\[I_3 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}\]

The basic idea is that if you multiply a matrix by a identity matrix you get the original matrix.

Transposition

Transposition is the act of flipping a matrix diagonally. This means that the new matrix his collumns are formed from the rows of the original matrix. For example:

\[\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}\]

Multiplying with a scalar

When multiplieing a matrix with a scaler the result is a matrix with the same dimensions. The multiplication is quite simple. Just multiply each element of the matrix by the scalar. Adding, substracting and dividing is very similair. \(M = 3 \times \begin{bmatrix} 2 & 9 & 3 \\ 1 & 8 & 6 \end{bmatrix} = \begin{bmatrix} 3 \times 2 & 3 \times 9 & 3 \times 3 \\ 3 \times 1 & 3 \times 8 & 3 \times 6 \end{bmatrix} = \begin{bmatrix} 6 & 27 & 9 \\ 3 & 24 & 21 \end{bmatrix}\)

Multiplying with a matrix

Multiplying by a matrix is a bit more complicated than multiplying with a sclalar. Lets say we want to do the following calculation:

\[A \times B = \begin{bmatrix} 0 & 3 & 5 \\ 5 & 5 & 2 \end{bmatrix} \times \begin{bmatrix} 3 & 4 \\ 3 & -2 \\ 4 & -2 \end{bmatrix}\]

Matrix by matrix multiplication only works if the number of collumns of A is equal to B’s number of rows. Our result will be a square matrix with the dimension of said number. To find the value of R11 we need to calculate the dot product of A’s first row and B’s first collumn.

\[R = A \times B = \begin{bmatrix} 0 & 3 & 5 \\ 5 & 5 & 2 \end{bmatrix} \times \begin{bmatrix} 3 & 4 \\ 3 & -2 \\ 4 & -2 \end{bmatrix} = \begin{bmatrix} 0 \times 3 + 3 \times 3 + 5 \times 4 & ... \\ ... & ... \end{bmatrix}\]

R12 is the dot product of A’s first row and B’s second collumn.

\[R = A \times B = \begin{bmatrix} 0 & 3 & 5 \\ 5 & 5 & 2 \end{bmatrix} \times \begin{bmatrix} 3 & 4 \\ 3 & -2 \\ 4 & -2 \end{bmatrix} = \begin{bmatrix} 0 \times 3 + 3 \times 3 + 5 \times 4 & 0 \times 4 + 3 \times -2 + 5 \times -2 \\ ... & ... \end{bmatrix}\]

Now you can probably guess what we need to do to calculate R21. Get the cross product of the second collumn of A by the first collumn of B

\[R = A \times B = \begin{bmatrix} 0 & 3 & 5 \\ 5 & 5 & 2 \end{bmatrix} \times \begin{bmatrix} 3 & 4 \\ 3 & -2 \\ 4 & -2 \end{bmatrix} = \begin{bmatrix} 0 \times 3 + 3 \times 3 + 5 \times 4 & 0 \times 4 + 3 \times -2 + 5 \times -2 \\ 5 \times 3 + 5 \times 3 + 2 \times 4 & ... \end{bmatrix}\]

And finally the value of R22 is the dot product of A’s second collumn and B’s second row.

\[R = A \times B = \begin{bmatrix} 0 & 3 & 5 \\ 5 & 5 & 2 \end{bmatrix} \times \begin{bmatrix} 3 & 4 \\ 3 & -2 \\ 4 & -2 \end{bmatrix} = \begin{bmatrix} 0 \times 3 + 3 \times 3 + 5 \times 4 & 0 \times 4 + 3 \times -2 + 5 \times -2 \\ 5 \times 3 + 5 \times 3 + 2 \times 4 & 5 \times 4 + 5 \times -2 + 2 \times -2 \end{bmatrix} = \begin{bmatrix} 29 & -16 \\ 38 & 6 \end{bmatrix}\]

Now we have calculated R a observation can be made: Order of multiplication matters greatly.