2. Geometry of weighted sums
In this article we will give a geometry to the array of numbers (aka a vector). We will also give a geometric meaning to the two basic operations involving a vector - scaling a vector and adding two vectors. In other words, we will visualize the following items:
a) A vector
b) Scaling a vector i.e. multiplying a vector with a scalar
c) Adding two vectors
2.1. A vector is a point in space
Note that we interpret an array of length n as a n-dimensional vector. We represent an array [3, 2] byThe array is shown in orange in the figure below. The teal arrows show the distances moved along the x-axis and y-axis respectively.
- moving a distance of 3 along the x-axis, and then
- moving a distance of 2 along the y-axis.
Press the arrow buttons in the figure below to change these values.
\begin{bmatrix}3 \\2 \\\end{bmatrix}
Distance along x-axis: 3
Distance along y-axis: 2
The same logic applies to an array [a,b,c] with three elements - in this case you have a 3-dimensional space with x, y and z axes.
- First you move a distance a along the x-axis.
- Then you move a distance b along the y-axis.
- Then you move a distance c along the z-axis.
2.2. Notions of length and direction of a vector
This visualization introduces two new properties which are intimately tied to vectors in a space:
1. How long the vector is
2. In what direction the vector points
I would like to emphasize that these properties become more obvious only after visualizing an array (or a vector). This is a benefit of ascribing a geometry to a vector.
2.2.1. length of a vector represented by the array[a,b,c,d]
A repeated application of the Pythagoras theorem tells us that the length of a vector [a,b,c,d,...] is \( \sqrt{ a^2 + b^2 + c^2 + d^2 + \text{...} } \). We will use the word norm to denote how long the vector is. A function to calculate the norm of a vector would look something like
def norm(array):
return sum(i**2 for i in array)**0.5
2.2.2.direction of a vector represented by the array[a,b,c,d]
Specifying the direction of a vector in 2-dimensional space may seem effortless. For a vector [x,y], we can use the angle of the vector from the x-axis, \( \theta = \text{tan}^{-1}(y/x) \) as a measure of direction. But it gets complicated immediately as we move to a 3-dimensional space. The idea of direction is not tied to a single value like angle from the x-axis anymore.
Unfortunately we do not have an elegant formula (or even a concept) for direction for a space with three or more dimensions. But the next best thing we can do is answer the question: how similar are the directions of two vectors represented by arrays [a,b,c,d,...] and [p,q,r,s,...]? For this question to make sense, the number of elements in these arrays should be the same. We will discuss this problem and the elegance of its solution later on.
2.3. Scaling and addition
Now let's see what it looks like to scale a vector and add two vectors.
2.3.1. Scaling a vector
Scaling a vector only changes its norm. It does not change its direction. Let's scale the vector [3, 2] by a scalar scale = 1.00. The figure below shows how the vector gets shorter or longer while pointing in the same direction as the scale is changed:
Scale: 1.00
\(
1.00 * \begin{bmatrix}
3 \\ 2
\end{bmatrix}
= \begin{bmatrix}
1.00 * 3 \\ 1.00 * 2
\end{bmatrix}
= \begin{bmatrix}
3.00 \\ 2.00
\end{bmatrix}
\)2.3.2. Adding two or more vectors
We saw how to add two or more vectors algebriacally (or programmatically). Note that you can only add vectors which have the same dimension. Adding two vectors can be seen geometrically as placing the second vector at the point where the first vector ends. Then the vector which joins the origin to the head of the second vector is the sum. This can be extended to more than two vectors also.
Play with the interactive visual below to understand how the geometry of vector addition looks like. You can add two or more of the vectors: [3,2], [-4,3], [-4,-4], [3,-4].
\(\begin{bmatrix}3.0\\2.0\end{bmatrix}+\begin{bmatrix}-4.0\\3.0\end{bmatrix}\)
Animate:
Add another vector:
Remove last vector:
2.3.3. The Commutative property translates into symmetry
Now let us look at a simple observation: the order of vectors does not matter when adding them. In other words, addition is commutative: sum(vector1, vector2) == sum(vector2, vector1).
We can visualize this commutativity by adding two vectors v1, v2 in different orders - v1 then v2 or v2 then v1. Note how the property of commutativity (in algebra) translates into the property of symmetry (in geometry):
Just like the previous section, you can add or remove vectors from the following collection: [3,2], [0,-4], [4,-2], [-3,4]. Can you see which two vectors are swapped each time you switch the two vectors?
\(\begin{bmatrix}3.0\\2.0\end{bmatrix}+\begin{bmatrix}0.0\\-4.0\end{bmatrix} = \begin{bmatrix}3.0\\-2.0\end{bmatrix}\)
Switch vectors:
Add another vector:
Remove last vector:
The sum of vectors does not depend on the order in which the vectors are added.
Also notice how changing the sequence of vectors in addition changes the "journey" but without changing the destination - in other words, the final result does not change by changing the sequence in which the vectors are added.
2.4. Combining scaling and addition
Now let us combine all the concepts we have covered so far - scaling and adding vectors. In the visual below, you can scale and add the following vectors: [3,2], [-2,4], [-3,-3].
You cannot change these vectors. But you can change the values these vectors are scaled by. We also show this scaled addition of vectors in the notation we (re)invented in the last section: the matrix notation.
Scale 1: 1.00
Scale 2: 1.00
Scale 3: 1.00
\(
\begin{bmatrix}
3 & -2 & -3 \\ 2 & 4 & -3
\end{bmatrix}
\begin{bmatrix} 1.0\\1.0\\1.0 \end{bmatrix}
\)
2.4.1. Matrix-vector multiplication is just scaling and adding vectors
By now, this heading should be obvious: a matrix vector multiplication is just a sum of scaled vectors. These vectors are the columns of the matrix and the scales are the elements of the vector.
At this point, when you see a matrix vector addition like the one shown below, you should easily be able to visualize it and associate a geometrical interpretation with it:
2.4.2. A different point of view of matrix-vector multiplication
In the next chapter, we will learn another way to look at matrix-vector multiplication.