Asiman Data School

Module 2, topic 13, week 7, Tuesday

Linear Algebra: vectors and vector operations

Why linear algebra matters in this course, then vectors from the beginning: adding and scaling, length and distance, the dot product, angles and projection. Worked by hand.

Introduction: the first lesson of Module 2

The first lesson of Module 2. This module is a mathematics course: the work is done with a pencil, on paper, and the goal is to understand the ideas and to calculate with confidence. This guide first explains why linear algebra matters in this course and where you will meet it again. Then it teaches vectors from the very beginning: what a vector is, how to add vectors and multiply them by a number, how to measure their length and the distance between them, and the most used operation of all, the dot product, with its meaning as an angle. Every idea comes as a definition, a picture and worked examples calculated by hand. Then it walks through 20 solved problems from the easiest to the hardest, each with the reasoning written out. At the end there are 34 practice problems with a hint and a full solution for each.

You need no mathematics beyond school arithmetic, simple equations, the theorem of Pythagoras and square roots. A calculator is enough for the square roots and the angles. Python appears only once, in a short part near the end that shows how to check your results.

What you will learn

  1. 1.Why linear algebra is the language of data science and machine learning, and where it appears in this course
  2. 2.What a vector is: a list of numbers, an arrow, a point, a row of data
  3. 3.Adding and subtracting vectors, and what the result means
  4. 4.Multiplying a vector by a number, parallel vectors, midpoints and linear combinations
  5. 5.The length of a vector, unit vectors, the distance between two points and the triangle inequality
  6. 6.The dot product: how to calculate it, its rules and what it measures
  7. 7.The angle between two vectors, perpendicular vectors, the Cauchy-Schwarz inequality, cosine similarity and projection
  8. 8.Where these calculations are used: a prediction, a similarity search and a nearest neighbour
Notation used in this lesson
WrittenMeaning
a=(3,4)\va = (3, 4)a vector with the components 3 and 4; vectors are printed in bold
a1a_1, a2a_2the first and the second component of a\va
0\vzerothe zero vector
∣a∣\len{\va}the length of a\va
a⋅b\va \cdot \vbthe dot product of a\va and b\vb
θ\thetaan angle (the Greek letter theta)
AB→\arrow{AB}the vector from the point AA to the point BB
2×32 \times 3ordinary multiplication of two numbers; the dot is kept for the dot product

1Why linear algebra?

1.1Data is made of vectors

Look at one row of a table:

One row of a table of employees
nameageyearssalaryscore
Konul29417103.8

Take away the name, and the employee is a list of four numbers: (29,4,1710,3.8)(29, 4, 1710, 3.8). Such an ordered list of numbers is called a vector. The whole table is a stack of vectors, one for every row.

This is not special to tables. Almost everything a computer learns from is turned into vectors first:

Things and the vectors they become
The thingAs a vector
A customer(age, orders, money spent, ...)
A photo of 28 by 28 pixels784 numbers, the brightness of every pixel
A sentencethe count of every word, or a list of a few hundred numbers called an embedding
A song(tempo, loudness, energy, ...)
A point on a map(latitude, longitude)

Linear algebra is the mathematics of vectors: how to add them, scale them, measure them and compare them. A model that learns from data does these operations millions of times. So linear algebra is not one more topic next to machine learning. It is the language machine learning is written in.

1.2Where you will meet it again in this course

Later topics and the vector idea behind each
Later in the courseThe vector idea behind it
Linear and logistic regressiona prediction is a dot product: weights times features, plus a number
k-Nearest Neighbors, k-means clustering"similar" means a small distance between two vectors
Feature scalingchanging the size of the features so that no feature dominates the distance
Support Vector Machinesthe side of a line a point lies on is the sign of a dot product
Principal Component Analysis (PCA)projecting data onto the most useful directions
Images and convolutional networksan image is a vector of pixels; a filter is a dot product moved over the image
Word embeddingswords as vectors; similar words point in similar directions (cosine similarity)
Neural networksevery neuron calculates a dot product of its inputs and its weights
RAG and vector databasesfinding the texts whose vectors are closest to the vector of the question

Three operations carry almost all of this: addition, multiplication by a number and the dot product. They are the subject of this lesson. The next two lessons build on them: matrices (many vectors at once), then eigenvectors and norms.

1.3Why by hand?

A library can calculate a dot product in one line, and you already know the line. But a library cannot tell you what the number means, whether it is plausible, or which operation a problem needs. That understanding comes from doing the calculations yourself with small numbers. In this module every example is worked out by hand. Read with a pencil: cover the solution, calculate, then compare.

2What is a vector?

2.1Scalars and vectors

A scalar is one single number: 55, −2.5-2.5, 3.143.14. A temperature, a price, an age.

A vector is an ordered list of numbers:

a=(3,4)b=(2,−1,5)c=(29,4,1710,3.8)\begin{aligned} \va &= (3, 4) \\ \vb &= (2, -1, 5) \\ \vc &= (29, 4, 1710, 3.8) \end{aligned}
  • The numbers are the components of the vector. The first component of a\va is a1=3a_1 = 3, the second is a2=4a_2 = 4.
  • The number of components is the dimension of the vector. a\va is a 2-dimensional vector, b\vb is 3-dimensional, c\vc is 4-dimensional.
  • The order matters: (3,4)(3, 4) and (4,3)(4, 3) are different vectors.
  • Two vectors are equal only when they have the same dimension and every component is equal.

In this lesson a vector is printed in bold, a\va, as in most books. On paper nobody can write bold, so by hand a vector gets an arrow above the letter, a⃗\vec{a}. A vector can be written in a row, (3,4)(3, 4), or in a column. For this lesson the two mean the same; the difference matters in the next lesson, with matrices.

Example. For which xx and yy is (x+1,6)=(4,2y)(x + 1, 6) = (4, 2y)?

first components:x+1=4⇒x=3second components:6=2y⇒y=3\begin{aligned} \text{first components:} \quad & x + 1 = 4 & \Rightarrow \quad & x = 3 \\ \text{second components:} \quad & 6 = 2y & \Rightarrow \quad & y = 3 \end{aligned}

One vector equation is as many ordinary equations as the vector has components.

2.2A vector as an arrow

A 2-dimensional vector can be drawn. The vector a=(3,4)\va = (3, 4) is the arrow that goes 3 steps to the right and 4 steps up.

-112345-112345a = (3, 4)3 to the right4 up
The vector (3, 4) as an arrow from the origin: 3 steps to the right, 4 steps up.

An arrow has two properties: a direction and a length. That is the physical idea of a vector: a quantity with a size and a direction, such as a velocity (60 km/h to the north) or a force. A scalar has only a size.

A negative component goes the other way: (−2,1)(-2, 1) goes 2 steps to the left and 1 up.

2.3Where the arrow starts does not matter

A vector says how far to go and in which direction, not where to start. All three arrows below are the same vector (2,1)(2, 1): each goes 2 to the right and 1 up.

-1123456-112345(2, 1)(2, 1)(2, 1)
Three arrows, one vector: each goes 2 to the right and 1 up. A vector has no fixed place.

Usually we draw a vector from the origin (0,0)(0, 0). Then its tip is at the point with the same coordinates, so a vector can also be seen as a point. Both pictures are used: arrows when we add and scale, points when the vectors are data (a cloud of customers, each a point).

2.4The vector from one point to another

The vector that leads from the point AA to the point BB is written AB→\arrow{AB}. Its components are the coordinates of the end minus the coordinates of the start:

AB→=B−A\arrow{AB} = B - A

For A=(1,2)A = (1, 2) and B=(4,6)B = (4, 6):

AB→=(4−1,  6−2)=(3,4)\arrow{AB} = (4 - 1,\; 6 - 2) = (3, 4)
-1123456-11234567AB = (3, 4)A = (1, 2)B = (4, 6)4 - 1 = 36 - 2 = 4
The vector from A to B: the coordinates of the end minus the coordinates of the start.

"End minus start" is worth learning by heart. The opposite vector BA→=A−B=(−3,−4)\arrow{BA} = A - B = (-3, -4) leads back.

2.5Vectors in more dimensions

A vector with 3 components is an arrow in space. A vector with 4, 100 or 784 components cannot be drawn, but nothing else changes: every rule of this lesson works in the same way for any number of components. The pictures in 2 dimensions build the intuition; the formulas do the work in 784 dimensions.

2.6Special vectors

  • The zero vector has only zeros: (0,0)(0, 0), (0,0,0)(0, 0, 0). It is written 0\vzero. It has no length and no direction.
  • The standard unit vectors have one 1 and zeros elsewhere. In 2 dimensions: e1=(1,0)\ve_1 = (1, 0) (one step to the right) and e2=(0,1)\ve_2 = (0, 1) (one step up). In 3 dimensions there are three: (1,0,0)(1, 0, 0), (0,1,0)(0, 1, 0), (0,0,1)(0, 0, 1).

Try it yourself

For the points A=(2,5)A = (2, 5) and B=(7,1)B = (7, 1) find the vectors AB→\arrow{AB} and BA→\arrow{BA}.

Show a hint
End minus start. For AB→\arrow{AB} the end is BB.
Show the solution

Answer: AB→=(5,−4)\arrow{AB} = (5, -4) and BA→=(−5,4)\arrow{BA} = (-5, 4).

AB→=B−A=(7−2,  1−5)=(5,−4)BA→=A−B=(2−7,  5−1)=(−5,4)\begin{aligned} \arrow{AB} &= B - A = (7 - 2,\; 1 - 5) = (5, -4) \\ \arrow{BA} &= A - B = (2 - 7,\; 5 - 1) = (-5, 4) \end{aligned}

The two vectors are opposite: the same arrow, walked in the other direction.

3Adding and subtracting vectors

3.1Addition

Two vectors of the same dimension are added component by component:

a+b=(a1+b1,  a2+b2,  …,  an+bn)\va + \vb = (a_1 + b_1,\; a_2 + b_2,\; \dots,\; a_n + b_n)

Example 1.

(2,1)+(1,3)=(2+1,  1+3)=(3,4)(2, 1) + (1, 3) = (2 + 1,\; 1 + 3) = (3, 4)

Example 2. With three components and negative numbers:

(4,−2,7)+(−1,5,−3)=(4−1,  −2+5,  7−3)=(3,3,4)\begin{aligned} (4, -2, 7) + (-1, 5, -3) &= (4 - 1,\; -2 + 5,\; 7 - 3) \\ &= (3, 3, 4) \end{aligned}

Example 3. Three vectors are added in the same way, all first components together, all second components together:

(1,2)+(3,−1)+(−2,4)=(1+3−2,  2−1+4)=(2,5)\begin{aligned} (1, 2) + (3, -1) + (-2, 4) &= (1 + 3 - 2,\; 2 - 1 + 4) \\ &= (2, 5) \end{aligned}

3.2What addition means

Walk along a\va, then walk along b\vb. The sum a+b\va + \vb is the arrow from where you started to where you ended. This is the tip-to-tail rule: put the tail of b\vb at the tip of a\va.

-112345-112345a = (2, 1)b = (1, 3)a + b = (3, 4)
Tip to tail: walk along a, then along b. The sum a + b leads from the start to the end.

The order does not matter. Walking b\vb first and then a\va ends at the same point: the two paths are the sides of a parallelogram, and the sum is its diagonal. This is the parallelogram rule.

In data terms: if a\va is what a shop sold of three products on Monday and b\vb what it sold on Tuesday, then a+b\va + \vb is what it sold on both days.

3.3Subtraction

a−b=(a1−b1,  a2−b2,  …,  an−bn)\va - \vb = (a_1 - b_1,\; a_2 - b_2,\; \dots,\; a_n - b_n)

Example.

(3,4)−(1,3)=(3−1,  4−3)=(2,1)(1,3)−(3,4)=(−2,−1)\begin{aligned} (3, 4) - (1, 3) &= (3 - 1,\; 4 - 3) = (2, 1) \\ (1, 3) - (3, 4) &= (-2, -1) \end{aligned}

a−b\va - \vb is the vector you must add to b\vb to arrive at a\va. Drawn from the origin, a\va and b\vb are two points, and a−b\va - \vb is the arrow from the tip of b\vb to the tip of a\va. It is the rule "end minus start" again.

-3-2-112345-112345a = (3, 4)b = (1, 3)a - b = (2, 1)
The difference a - b is the arrow from the tip of b to the tip of a.

This picture is worth remembering: the difference of two vectors is the arrow between them. Its length is the distance between the two points (Part 5), and that is how a computer decides that two customers are similar.

b−a\vb - \va is the same arrow in the opposite direction.

Move the sliders: the blue arrow is the result.

-6-5-4-3-2-1123456-6-5-4-3-2-1123456ab

a + b = (2 + 1, 1 + 3) = (3, 4)

b starts where a ends. The blue arrow goes from the start of a to the end of b.

3.4The rules of addition

For all vectors of the same dimension:

The rules of vector addition
RuleIn symbolsName
The order does not mattera+b=b+a\va + \vb = \vb + \vacommutative
Grouping does not matter(a+b)+c=a+(b+c)(\va + \vb) + \vc = \va + (\vb + \vc)associative
Adding the zero vector changes nothinga+0=a\va + \vzero = \vazero vector
Every vector has an oppositea+(−a)=0\va + (-\va) = \vzeroopposite vector

Why are they true? Because the work is done with ordinary numbers, component by component, and ordinary numbers obey these rules. For example a+b=(a1+b1, a2+b2)\va + \vb = (a_1 + b_1,\, a_2 + b_2) and b+a=(b1+a1, b2+a2)\vb + \va = (b_1 + a_1,\, b_2 + a_2), and a1+b1=b1+a1a_1 + b_1 = b_1 + a_1.

Because of these rules, vector equations are solved like ordinary ones.

Example. Find x\vx with x+(2,−1)=(5,3)\vx + (2, -1) = (5, 3).

x=(5,3)−(2,−1)=(5−2,  3−(−1))=(3,4)\vx = (5, 3) - (2, -1) = (5 - 2,\; 3 - (-1)) = (3, 4)

3.5Only the same dimension

(2,1)+(1,3,5)(2, 1) + (1, 3, 5) has no meaning: the second vector has a component without a partner. Before any operation with two vectors, check that they have the same number of components.

Try it yourself

For a=(3,−1,2)\va = (3, -1, 2) and b=(−4,2,5)\vb = (-4, 2, 5) find a+b\va + \vb and a−b\va - \vb.

Show a hint
Work component by component. Subtracting a negative number adds.
Show the solution

Answer: a+b=(−1,1,7)\va + \vb = (-1, 1, 7) and a−b=(7,−3,−3)\va - \vb = (7, -3, -3).

a+b=(3−4,  −1+2,  2+5)=(−1,1,7)a−b=(3−(−4),  −1−2,  2−5)=(7,−3,−3)\begin{aligned} \va + \vb &= (3 - 4,\; -1 + 2,\; 2 + 5) \\ &= (-1, 1, 7) \\ \va - \vb &= (3 - (-4),\; -1 - 2,\; 2 - 5) \\ &= (7, -3, -3) \end{aligned}

4Multiplying a vector by a number

4.1Scalar multiplication

A vector is multiplied by a number (a scalar) by multiplying every component:

c a=(c a1,  c a2,  …,  c an)c\,\va = (c\,a_1,\; c\,a_2,\; \dots,\; c\,a_n)

Examples.

2 (2,1)=(4,2)−1 (2,1)=(−2,−1)0.5 (2,1)=(1,0.5)−3 (1,0,−2)=(−3,0,6)\begin{aligned} 2\,(2, 1) &= (4, 2) \\ -1\,(2, 1) &= (-2, -1) \\ 0.5\,(2, 1) &= (1, 0.5) \\ -3\,(1, 0, -2) &= (-3, 0, 6) \end{aligned}

We write 2a2\va for "2 times a\va", and −a-\va for (−1) a(-1)\,\va.

4.2What it means

The number scales the arrow, which is where the word scalar comes from:

  • a number greater than 1 stretches the arrow,
  • a number between 0 and 1 shrinks it,
  • a negative number turns it around,
  • the line the arrow lies on never changes.
-3-2-112345-2-11232a = (4, 2)a = (2, 1)-a = (-2, -1)
Multiplying by 2 stretches the arrow, multiplying by -1 turns it around. All three lie on one line.

Choose the number c and watch the vector c a.

-5-4-3-2-1123456-3-2-11234a = (2, 1)

1.5 a = (1.5 × 2, 1.5 × 1) = (3, 1.5)

The direction stays the same, and the arrow is 1.5 times as long.

4.3Parallel vectors

Two vectors are parallel when one is a multiple of the other: b=c a\vb = c\,\va for some number cc. They lie on the same line through the origin.

To test two vectors, divide their components in pairs. The same number every time means parallel.

Testing whether two vectors are parallel
The vectorsThe ratiosResult
(2,1)(2, 1) and (6,3)(6, 3)6/2=36 / 2 = 3 and 3/1=33 / 1 = 3parallel: (6,3)=3 (2,1)(6, 3) = 3\,(2, 1)
(2,1)(2, 1) and (6,4)(6, 4)6/2=36 / 2 = 3 and 4/1=44 / 1 = 4not parallel
(1,−2,3)(1, -2, 3) and (−2,4,−6)(-2, 4, -6)every ratio is −2-2parallel, opposite directions

Three points on one line. The points AA, BB and CC lie on one straight line exactly when the vectors AB→\arrow{AB} and AC→\arrow{AC} are parallel.

4.4The rules of scalar multiplication

The rules of scalar multiplication
RuleIn symbols
A number goes into a sum of vectorsc (a+b)=c a+c bc\,(\va + \vb) = c\,\va + c\,\vb
A sum of numbers can be split(c+d) a=c a+d a(c + d)\,\va = c\,\va + d\,\va
Two numbers can be multiplied firstc (d a)=(c d) ac\,(d\,\va) = (c\,d)\,\va
Multiplying by 1 changes nothing1 a=a1\,\va = \va

Example. Simplify 3(a+2b)−2(a−b)3(\va + 2\vb) - 2(\va - \vb).

3(a+2b)−2(a−b)=3a+6b−2a+2b=a+8b\begin{aligned} 3(\va + 2\vb) - 2(\va - \vb) &= 3\va + 6\vb - 2\va + 2\vb \\ &= \va + 8\vb \end{aligned}

Vectors are handled like letters in ordinary algebra, as long as you only add them and multiply them by numbers.

4.5The midpoint and other points of a segment

The point halfway between two points a\va and b\vb is their average:

midpoint=a+b2\text{midpoint} = \frac{\va + \vb}{2}

For a=(2,−4)\va = (2, -4) and b=(6,8)\vb = (6, 8):

midpoint=(2+6,  −4+8)2=(8,4)2=(4,2)\text{midpoint} = \frac{(2 + 6,\; -4 + 8)}{2} = \frac{(8, 4)}{2} = (4, 2)

More generally, start at AA and go a part tt of the way to BB:

point=A+t AB→=A+t (B−A)\text{point} = A + t\,\arrow{AB} = A + t\,(B - A)

Example. One third of the way from A=(1,1)A = (1, 1) to B=(7,4)B = (7, 4):

AB→=(6,3)point=(1,1)+13 (6,3)=(1,1)+(2,1)=(3,2)\begin{aligned} \arrow{AB} &= (6, 3) \\ \text{point} &= (1, 1) + \tfrac{1}{3}\,(6, 3) = (1, 1) + (2, 1) = (3, 2) \end{aligned}

With t=0t = 0 you are at AA, with t=1t = 1 at BB, with t=0.5t = 0.5 at the midpoint.

4.6Linear combinations

Scaling and adding together give the most important construction of linear algebra. A linear combination of the vectors a\va and b\vb is

c1 a+c2 bc_1\,\va + c_2\,\vb

for any two numbers c1c_1 and c2c_2: scale each vector, then add.

Example. With a=(2,1)\va = (2, 1) and b=(1,3)\vb = (1, 3):

2a+b=(4,2)+(1,3)=(5,5)a−2b=(2,1)−(2,6)=(0,−5)\begin{aligned} 2\va + \vb &= (4, 2) + (1, 3) = (5, 5) \\ \va - 2\vb &= (2, 1) - (2, 6) = (0, -5) \end{aligned}
-1123456-1123456aab2a + b = (5, 5)
The linear combination 2a + b: two steps of a, then one step of b.

A linear combination is a recipe: "two steps of a\va and one step of b\vb". The word linear in linear algebra comes from here: only these two operations are allowed, scaling and adding.

Every vector is a linear combination of the standard unit vectors, with its own components as the numbers:

(3,4)=3 (1,0)+4 (0,1)=3 e1+4 e2(3, 4) = 3\,(1, 0) + 4\,(0, 1) = 3\,\ve_1 + 4\,\ve_2

The opposite question. Given a vector, find the recipe. Write (7,2)(7, 2) as a linear combination of a=(2,1)\va = (2, 1) and b=(1,−1)\vb = (1, -1).

x (2,1)+y (1,−1)=(2x+y,  x−y)=(7,2)x\,(2, 1) + y\,(1, -1) = (2x + y,\; x - y) = (7, 2)

Each component gives one equation:

2x+y=7x−y=2\begin{aligned} 2x + y &= 7 \\ x - y &= 2 \end{aligned}

Adding the two equations removes yy: 3x=93x = 9, so x=3x = 3, and then y=x−2=1y = x - 2 = 1.

(7,2)=3a+bcheck:(6,3)+(1,−1)=(7,2)\begin{aligned} (7, 2) &= 3\va + \vb \\ \text{check:} \quad (6, 3) + (1, -1) &= (7, 2) \end{aligned}

One vector equation in 2 dimensions became two ordinary equations with two unknowns. Solving such systems for many unknowns at once is what matrices are for, in the next lesson.

Try it yourself

For a=(1,−2)\va = (1, -2) and b=(3,1)\vb = (3, 1) find the linear combination 3a−2b3\va - 2\vb.

Show a hint
Write 3a3\va and 2b2\vb on their own lines first, then subtract.
Show the solution

Answer: 3a−2b=(−3,−8)3\va - 2\vb = (-3, -8).

3a=(3,−6)2b=(6,2)3a−2b=(3−6,  −6−2)=(−3,−8)\begin{aligned} 3\va &= (3, -6) \\ 2\vb &= (6, 2) \\ 3\va - 2\vb &= (3 - 6,\; -6 - 2) = (-3, -8) \end{aligned}

5Length and distance

5.1The length of a vector

The vector a=(3,4)\va = (3, 4) is the long side of a right-angled triangle with the sides 3 and 4. By the theorem of Pythagoras its length is

32+42=9+16=25=5\sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5
-112345-112345length 534
The vector (3, 4) is the long side of a right-angled triangle with the sides 3 and 4, so its length is 5.

The same works in any dimension. The length (also called the magnitude or the norm) of a vector is written ∣a∣\len{\va}; many books write ∥a∥\lVert \va \rVert.

∣a∣=a12+a22+⋯+an2\len{\va} = \sqrt{a_1^2 + a_2^2 + \dots + a_n^2}

Square every component, add, take the square root.

Examples.

∣(1,2,2)∣=1+4+4=9=3∣(−5,12)∣=25+144=169=13∣(1,1)∣=2≈1.414∣(2,−1,0,2)∣=4+1+0+4=9=3\begin{aligned} \len{(1, 2, 2)} &= \sqrt{1 + 4 + 4} = \sqrt{9} = 3 \\ \len{(-5, 12)} &= \sqrt{25 + 144} = \sqrt{169} = 13 \\ \len{(1, 1)} &= \sqrt{2} \approx 1.414 \\ \len{(2, -1, 0, 2)} &= \sqrt{4 + 1 + 0 + 4} = \sqrt{9} = 3 \end{aligned}

A negative component causes no trouble, because it is squared. When the number under the root is not a perfect square, leave the root (2\sqrt{2}) or give a decimal (1.4141.414).

5.2Facts about the length

  1. A length is never negative, and only the zero vector has the length 0.
  2. Scaling a vector scales its length by the size of the number: ∣c a∣=∣c∣ ∣a∣\len{c\,\va} = \len{c}\,\len{\va}. Here ∣c∣\len{c} is the absolute value of the number. Example: (6,8)=2 (3,4)(6, 8) = 2\,(3, 4) has the length 2×5=102 \times 5 = 10, and (−6,−8)(-6, -8) has the length 10 as well.
  3. The triangle inequality: ∣a+b∣≤∣a∣+∣b∣\len{\va + \vb} \le \len{\va} + \len{\vb}. The straight road is never longer than a detour.

Example for the triangle inequality. a=(3,0)\va = (3, 0) and b=(0,4)\vb = (0, 4):

∣a+b∣=∣(3,4)∣=5∣a∣+∣b∣=3+4=75≤7\begin{aligned} \len{\va + \vb} &= \len{(3, 4)} = 5 \\ \len{\va} + \len{\vb} &= 3 + 4 = 7 \\ 5 &\le 7 \end{aligned}

The two sides are equal only when a\va and b\vb point in the same direction. So remember: the length of a sum is not the sum of the lengths.

This length is the usual, straight-line one. There are other ways to measure the size of a vector (other norms); they come in Topic 15.

5.3Unit vectors

A unit vector has the length 1. It carries a direction and nothing else. Every vector except the zero vector can be turned into the unit vector of the same direction by dividing it by its length. This is called normalizing:

u=a∣a∣\vu = \frac{\va}{\len{\va}}

For a=(3,4)\va = (3, 4), with ∣a∣=5\len{\va} = 5:

u=(35, 45)=(0.6, 0.8)check:∣u∣=0.36+0.64=1=1\begin{aligned} \vu &= \left(\tfrac{3}{5},\, \tfrac{4}{5}\right) = (0.6,\, 0.8) \\ \text{check:} \quad \len{\vu} &= \sqrt{0.36 + 0.64} = \sqrt{1} = 1 \end{aligned}

Why does it always work? By fact 2 above, dividing by ∣a∣\len{\va} divides the length by ∣a∣\len{\va}, and a number divided by itself is 1.

A vector of a given length. To get a vector of the length 10 in the direction of a\va, first normalize, then multiply by 10:

10 (0.6, 0.8)=(6,8)10\,(0.6,\, 0.8) = (6, 8)

5.4The distance between two points

See two vectors as two points. The distance between them is the length of the arrow from one to the other, and that arrow is their difference:

distance(a,b)=∣a−b∣=(a1−b1)2+⋯+(an−bn)2\begin{aligned} \text{distance}(\va, \vb) &= \len{\va - \vb} \\ &= \sqrt{(a_1 - b_1)^2 + \dots + (a_n - b_n)^2} \end{aligned}

Examples.

a=(5,3), b=(1,0)a−b=(4,3)distance=16+9=5\begin{aligned} \va = (5, 3),\ \vb &= (1, 0) \\ \va - \vb &= (4, 3) \\ \text{distance} &= \sqrt{16 + 9} = 5 \end{aligned}
p=(1,2,3), q=(4,6,3)q−p=(3,4,0)distance=9+16+0=5\begin{aligned} \vp = (1, 2, 3),\ \vq &= (4, 6, 3) \\ \vq - \vp &= (3, 4, 0) \\ \text{distance} &= \sqrt{9 + 16 + 0} = 5 \end{aligned}

It is called the Euclidean distance. Three facts:

  • The distance from a\va to b\vb equals the distance from b\vb to a\va, because the squares remove the signs.
  • The distance is 0 only when the two points are the same.
  • Going through a third point is never shorter: distance(a,c)≤distance(a,b)+distance(b,c)\text{distance}(\va, \vc) \le \text{distance}(\va, \vb) + \text{distance}(\vb, \vc). This is the triangle inequality again.

This one formula is behind every method that works with "similar" data points: two customers are similar when the distance between their vectors is small.

Try it yourself

For a=(−8,6)\va = (-8, 6) find the length ∣a∣\len{\va} and the unit vector in the direction of a\va.

Show a hint
Square, add, take the root. Then divide every component by the length.
Show the solution

Answer: ∣a∣=10\len{\va} = 10 and the unit vector is (−0.8, 0.6)(-0.8,\, 0.6).

∣a∣=64+36=100=10u=110 (−8,6)=(−0.8, 0.6)\begin{aligned} \len{\va} &= \sqrt{64 + 36} = \sqrt{100} = 10 \\ \vu &= \tfrac{1}{10}\,(-8, 6) = (-0.8,\, 0.6) \end{aligned}

Check: 0.64+0.36=1\sqrt{0.64 + 0.36} = 1. The unit vector keeps the signs of a\va, so it points in the same direction.

6The dot product

6.1The definition

There is a way to multiply two vectors that gives one number. Multiply the components in pairs and add the products:

a⋅b=a1b1+a2b2+⋯+anbn\va \cdot \vb = a_1 b_1 + a_2 b_2 + \dots + a_n b_n

It is called the dot product (also the scalar product or the inner product). Two things to notice: both vectors must have the same dimension, and the result is a scalar, not a vector.

Examples.

(2,1)⋅(1,3)=2×1+1×3=5(1,2,3)⋅(4,−5,6)=4−10+18=12(2,−1)⋅(3,6)=6−6=0(1,0,−2,3)⋅(2,5,1,1)=2+0−2+3=3\begin{aligned} (2, 1) \cdot (1, 3) &= 2 \times 1 + 1 \times 3 = 5 \\ (1, 2, 3) \cdot (4, -5, 6) &= 4 - 10 + 18 = 12 \\ (2, -1) \cdot (3, 6) &= 6 - 6 = 0 \\ (1, 0, -2, 3) \cdot (2, 5, 1, 1) &= 2 + 0 - 2 + 3 = 3 \end{aligned}

A good habit: write every product first, then add. Most mistakes are lost minus signs.

6.2The dot product is a weighted sum

Most uses of the dot product read like this: one vector holds amounts, the other holds what each amount is worth.

A shopping bill. You buy 4 notebooks, 10 pens and 2 books. They cost 2.5, 1.2 and 4.0.

(4,10,2)⋅(2.5, 1.2, 4.0)=4×2.5+10×1.2+2×4.0=10+12+8=30\begin{aligned} & (4, 10, 2) \cdot (2.5,\, 1.2,\, 4.0) \\ &= 4 \times 2.5 + 10 \times 1.2 + 2 \times 4.0 \\ &= 10 + 12 + 8 = 30 \end{aligned}

A final grade. Homework counts 20%, the midterm 30% and the final exam 50%. A student got 80, 70 and 90.

(0.2, 0.3, 0.5)⋅(80,70,90)=0.2×80+0.3×70+0.5×90=16+21+45=82\begin{aligned} & (0.2,\, 0.3,\, 0.5) \cdot (80, 70, 90) \\ &= 0.2 \times 80 + 0.3 \times 70 + 0.5 \times 90 \\ &= 16 + 21 + 45 = 82 \end{aligned}

Work in physics. A force F=(3,4)\vF = (3, 4) moves an object along d=(5,0)\vd = (5, 0). The work done is F⋅d=15+0=15\vF \cdot \vd = 15 + 0 = 15: only the part of the force along the movement counts.

Every time you "multiply pairs and add them up", you are calculating a dot product.

6.3The rules of the dot product

The rules of the dot product
RuleIn symbols
The order does not mattera⋅b=b⋅a\va \cdot \vb = \vb \cdot \va
It goes into a suma⋅(b+c)=a⋅b+a⋅c\va \cdot (\vb + \vc) = \va \cdot \vb + \va \cdot \vc
A number can be taken out(c a)⋅b=c (a⋅b)(c\,\va) \cdot \vb = c\,(\va \cdot \vb)
A vector with itself gives its length squareda⋅a=∣a∣2\va \cdot \va = \len{\va}^2
With the zero vector it is 0a⋅0=0\va \cdot \vzero = 0

Each rule follows from the definition. The fourth one: a⋅a=a1a1+a2a2=a12+a22\va \cdot \va = a_1 a_1 + a_2 a_2 = a_1^2 + a_2^2, and that is exactly what stands under the root in the length. So

∣a∣=a⋅a\len{\va} = \sqrt{\va \cdot \va}

Example: (3,4)⋅(3,4)=9+16=25=52(3, 4) \cdot (3, 4) = 9 + 16 = 25 = 5^2.

Checking a rule with numbers. a=(1,2)\va = (1, 2), b=(3,0)\vb = (3, 0), c=(−1,4)\vc = (-1, 4):

a⋅(b+c)=(1,2)⋅(2,4)=2+8=10a⋅b+a⋅c=(3+0)+(−1+8)=3+7=10\begin{aligned} \va \cdot (\vb + \vc) &= (1, 2) \cdot (2, 4) = 2 + 8 = 10 \\ \va \cdot \vb + \va \cdot \vc &= (3 + 0) + (-1 + 8) = 3 + 7 = 10 \end{aligned}

What does not exist. There is no dot product of three vectors: a⋅b\va \cdot \vb is a number, and a number cannot be "dotted" with c\vc. And you cannot divide by a vector.

6.4The square of a sum

With these rules the dot product behaves like ordinary multiplication:

∣a+b∣2=(a+b)⋅(a+b)=a⋅a+2 (a⋅b)+b⋅b=∣a∣2+2 (a⋅b)+∣b∣2\begin{aligned} \len{\va + \vb}^2 &= (\va + \vb) \cdot (\va + \vb) \\ &= \va \cdot \va + 2\,(\va \cdot \vb) + \vb \cdot \vb \\ &= \len{\va}^2 + 2\,(\va \cdot \vb) + \len{\vb}^2 \end{aligned}

It is the formula (x+y)2=x2+2xy+y2(x + y)^2 = x^2 + 2xy + y^2 for vectors, and it will be used in the next part.

Try it yourself

Find (2,−3,1)⋅(4,1,−2)(2, -3, 1) \cdot (4, 1, -2) and (5,2)⋅(−2,5)(5, 2) \cdot (-2, 5).

Show a hint
Write every product first, with its sign, then add.
Show the solution

Answer: 3 and 0.

(2,−3,1)⋅(4,1,−2)=8−3−2=3(5,2)⋅(−2,5)=−10+10=0\begin{aligned} (2, -3, 1) \cdot (4, 1, -2) &= 8 - 3 - 2 = 3 \\ (5, 2) \cdot (-2, 5) &= -10 + 10 = 0 \end{aligned}

The second dot product is 0. The next part shows what that means: the two vectors are perpendicular.

7What the dot product measures: angles

7.1The geometric formula

The dot product has a second formula, and it is the reason the dot product is so useful:

a⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta

where θ\theta is the angle between the two vectors (between 0∘0^\circ and 180∘180^\circ). So the dot product knows three things at once: the two lengths and the angle.

Where it comes from. The vectors a\va, b\vb and a−b\va - \vb form a triangle. The law of cosines from school says

∣a−b∣2=∣a∣2+∣b∣2−2 ∣a∣ ∣b∣ cos⁡θ\len{\va - \vb}^2 = \len{\va}^2 + \len{\vb}^2 - 2\,\len{\va}\,\len{\vb}\,\cos\theta

The square-of-a-sum formula of Part 6, with a minus, says

∣a−b∣2=∣a∣2−2 (a⋅b)+∣b∣2\len{\va - \vb}^2 = \len{\va}^2 - 2\,(\va \cdot \vb) + \len{\vb}^2

Compare the two lines: a⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta.

7.2Calculating an angle

Turn the formula around:

cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \frac{\va \cdot \vb}{\len{\va}\,\len{\vb}}

Example 1. a=(2,1)\va = (2, 1) and b=(1,3)\vb = (1, 3).

a⋅b=2+3=5∣a∣=5,∣b∣=10∣a∣ ∣b∣=50≈7.071cos⁡θ=57.071≈0.7071θ=45∘\begin{aligned} \va \cdot \vb &= 2 + 3 = 5 \\ \len{\va} &= \sqrt{5}, \quad \len{\vb} = \sqrt{10} \\ \len{\va}\,\len{\vb} &= \sqrt{50} \approx 7.071 \\ \cos\theta &= \frac{5}{7.071} \approx 0.7071 \\ \theta &= 45^\circ \end{aligned}

Example 2. a=(1,2,2)\va = (1, 2, 2) and b=(2,0,0)\vb = (2, 0, 0).

a⋅b=2,∣a∣=3,∣b∣=2cos⁡θ=26≈0.3333θ≈70.53∘\begin{aligned} \va \cdot \vb &= 2, \quad \len{\va} = 3, \quad \len{\vb} = 2 \\ \cos\theta &= \frac{2}{6} \approx 0.3333 \\ \theta &\approx 70.53^\circ \end{aligned}

The last step uses the inverse cosine of a calculator (the key cos⁡−1\cos^{-1} or arccos). Some angles are worth knowing:

Cosines worth knowing
θ\theta0∘0^\circ45∘45^\circ60∘60^\circ90∘90^\circ120∘120^\circ180∘180^\circ
cos⁡θ\cos\theta110.70710.70710.50.500−0.5-0.5−1-1

7.3The sign of the dot product

Lengths are never negative, so the sign of the dot product is the sign of the cosine, and that says in which way the two vectors point:

What the sign of the dot product says
Dot productThe angleThe vectors
positiveless than 90∘90^\circ (acute)point in a similar direction
zeroexactly 90∘90^\circ (right)are perpendicular
negativemore than 90∘90^\circ (obtuse)point in opposite directions
-3-2-1123-11234ab

a · b = 5

less than 90°

-3-2-1123-11234ab

a · b = 0

exactly 90°

-3-2-1123-11234ab

a · b = -5

more than 90°

The same vector a = (2, 1) with three different vectors b. The sign of the dot product tells how they point to each other.

In all three pictures a=(2,1)\va = (2, 1). With b=(1,3)\vb = (1, 3) the dot product is 5, with b=(−1,2)\vb = (-1, 2) it is −2+2=0-2 + 2 = 0, and with b=(−3,1)\vb = (-3, 1) it is −6+1=−5-6 + 1 = -5. Often the sign is all you need, and it costs no square root.

Move the sliders and watch the dot product, the angle and the shadow of a on b.

-5-4-3-2-112345-5-4-3-2-112345ba

a · b = 2 × 4 + 3 × 2 = 14

|a| = 3.61, |b| = 4.47, cos θ = 14 / (3.61 × 4.47) = 0.87, θ = 29.7°

The projection of a onto b (blue): (2.8, 1.4)

The dot product is positive: the angle is less than 90°, and the vectors point in a similar direction.

7.4Perpendicular vectors

Two vectors are perpendicular (the word in linear algebra is orthogonal) when the angle between them is 90∘90^\circ. Because cos⁡90∘=0\cos 90^\circ = 0:

a and b are orthogonal⟺a⋅b=0\va \text{ and } \vb \text{ are orthogonal} \quad \Longleftrightarrow \quad \va \cdot \vb = 0

This is the fastest test there is. (2,1)⋅(−1,2)=−2+2=0(2, 1) \cdot (-1, 2) = -2 + 2 = 0, so the two are orthogonal. The standard unit vectors are orthogonal to each other: (1,0)⋅(0,1)=0(1, 0) \cdot (0, 1) = 0.

A useful trick in 2 dimensions. A vector perpendicular to (a1,a2)(a_1, a_2) is (−a2,a1)(-a_2, a_1): swap the components and change one sign. Check: (a1,a2)⋅(−a2,a1)=−a1a2+a2a1=0(a_1, a_2) \cdot (-a_2, a_1) = -a_1 a_2 + a_2 a_1 = 0.

Pythagoras for vectors. When a\va and b\vb are orthogonal, the middle term of the square of a sum disappears: ∣a+b∣2=∣a∣2+∣b∣2\len{\va + \vb}^2 = \len{\va}^2 + \len{\vb}^2. The theorem of Pythagoras is a special case of the dot product.

In data, orthogonal vectors have "nothing in common": two texts that share no word have count vectors with the dot product 0.

7.5The Cauchy-Schwarz inequality

A cosine is always between −1-1 and 11. Put this into a⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta:

∣a⋅b∣≤∣a∣ ∣b∣\len{\va \cdot \vb} \le \len{\va}\,\len{\vb}

The dot product of two vectors is never larger in size than the product of their lengths. This is the Cauchy-Schwarz inequality, one of the most used facts of mathematics. The two sides are equal exactly when the vectors are parallel.

Example. a=(1,2)\va = (1, 2), b=(3,1)\vb = (3, 1): a⋅b=5\va \cdot \vb = 5, and ∣a∣ ∣b∣=5×10≈7.07\len{\va}\,\len{\vb} = \sqrt{5} \times \sqrt{10} \approx 7.07. Indeed 5≤7.075 \le 7.07.

It is also a check for your own work: if you ever calculate a cosine larger than 1, there is a mistake in the dot product or in a length.

7.6Cosine similarity

The cosine of the angle is used so often to compare two vectors that it has its own name, the cosine similarity:

cosine similarity(a,b)=a⋅b∣a∣ ∣b∣\text{cosine similarity}(\va, \vb) = \frac{\va \cdot \vb}{\len{\va}\,\len{\vb}}

By Cauchy-Schwarz it is always between −1-1 and 11:

  • 11: the same direction,
  • 00: perpendicular, nothing in common,
  • −1-1: exactly opposite directions.

It looks only at the direction and ignores the lengths. Multiplying a vector by a positive number does not change its cosine similarity with any other vector. A short text and a long text about the same subject have count vectors of very different length but nearly the same direction, so their cosine similarity is close to 1. That is why search engines, recommendation systems, word embeddings and RAG all use it.

Example. Three users rated four films from 0 to 5: Aylin (5,4,0,1)(5, 4, 0, 1), Murad (4,5,1,0)(4, 5, 1, 0), Leyla (1,0,5,4)(1, 0, 5, 4). Call their vectors a\va, b\vb and c\vc.

a⋅b=20+20+0+0=40∣a∣=25+16+0+1=42∣b∣=16+25+1+0=42similarity=4042×42=4042≈0.952\begin{aligned} \va \cdot \vb &= 20 + 20 + 0 + 0 = 40 \\ \len{\va} &= \sqrt{25 + 16 + 0 + 1} = \sqrt{42} \\ \len{\vb} &= \sqrt{16 + 25 + 1 + 0} = \sqrt{42} \\ \text{similarity} &= \frac{40}{\sqrt{42} \times \sqrt{42}} = \frac{40}{42} \approx 0.952 \end{aligned}

That was Aylin and Murad. Now Aylin and Leyla:

a⋅c=5+0+0+4=9∣c∣=1+0+25+16=42similarity=942≈0.214\begin{aligned} \va \cdot \vc &= 5 + 0 + 0 + 4 = 9 \\ \len{\vc} &= \sqrt{1 + 0 + 25 + 16} = \sqrt{42} \\ \text{similarity} &= \frac{9}{42} \approx 0.214 \end{aligned}

Aylin and Murad like the same films. Aylin and Leyla have a very different taste.

7.7Projection: how much of one vector lies along another

The last idea of this part answers the question: how far does a\va go in the direction of b\vb? Imagine a light shining straight down onto the line of b\vb. The shadow of a\va on that line is the projection of a\va onto b\vb.

-112345-11234b = (4, 2)a = (2, 3)projection (2.8, 1.4)
The projection of a onto b is the shadow of a on the line of b. The dotted line from the tip of a meets b at a right angle.

Two formulas. The scalar projection is the length of the shadow:

scalar projection=a⋅b∣b∣\text{scalar projection} = \frac{\va \cdot \vb}{\len{\vb}}

The vector projection is the shadow as a vector:

vector projection=a⋅bb⋅b b\text{vector projection} = \frac{\va \cdot \vb}{\vb \cdot \vb}\,\vb

Where they come from. In the right-angled triangle of the picture, the shadow has the length ∣a∣cos⁡θ\len{\va}\cos\theta. Since a⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta, this is (a⋅b)/∣b∣(\va \cdot \vb) / \len{\vb}. To make a vector of this length along b\vb, multiply by the unit vector b/∣b∣\vb / \len{\vb}; the two lengths in the denominator give ∣b∣2=b⋅b\len{\vb}^2 = \vb \cdot \vb.

Example. a=(2,3)\va = (2, 3) and b=(4,2)\vb = (4, 2):

a⋅b=8+6=14b⋅b=16+4=20vector projection=1420 (4,2)=(2.8, 1.4)scalar projection=1420≈3.13the rest=(2−2.8,  3−1.4)=(−0.8, 1.6)\begin{aligned} \va \cdot \vb &= 8 + 6 = 14 \\ \vb \cdot \vb &= 16 + 4 = 20 \\ \text{vector projection} &= \tfrac{14}{20}\,(4, 2) = (2.8,\, 1.4) \\ \text{scalar projection} &= \frac{14}{\sqrt{20}} \approx 3.13 \\ \text{the rest} &= (2 - 2.8,\; 3 - 1.4) = (-0.8,\, 1.6) \end{aligned}

Check the rest against b\vb: (−0.8, 1.6)⋅(4,2)=−3.2+3.2=0(-0.8,\, 1.6) \cdot (4, 2) = -3.2 + 3.2 = 0. The check shows the picture in numbers: what is left of a\va after taking away the shadow is perpendicular to b\vb. So every vector can be split into a part along b\vb and a part perpendicular to b\vb:

a=(2.8, 1.4)+(−0.8, 1.6)\va = (2.8,\, 1.4) + (-0.8,\, 1.6)

When b\vb is a unit vector, the scalar projection is simply a⋅b\va \cdot \vb: the dot product with a unit vector measures how far a vector goes in that direction. PCA, later in the course, is built on this sentence.

A negative scalar projection means that the shadow falls on the other side: a\va goes against the direction of b\vb.

Try it yourself

Find the angle between a=(1,1)\va = (1, 1) and b=(0,2)\vb = (0, 2), in degrees.

Show a hint
cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}}. The lengths are 2\sqrt{2} and 22.
Show the solution

Answer: 45∘45^\circ.

a⋅b=0+2=2∣a∣=2,∣b∣=2cos⁡θ=222=12≈0.7071θ=45∘\begin{aligned} \va \cdot \vb &= 0 + 2 = 2 \\ \len{\va} &= \sqrt{2}, \quad \len{\vb} = 2 \\ \cos\theta &= \frac{2}{2\sqrt{2}} = \frac{1}{\sqrt{2}} \approx 0.7071 \\ \theta &= 45^\circ \end{aligned}

The picture agrees: (0,2)(0, 2) points straight up, and (1,1)(1, 1) is the diagonal, halfway between up and right.

8Where these calculations are used

Three small examples of methods you will learn later. Each is one operation of this lesson, calculated by hand.

8.1A prediction is a dot product

A model that predicts the price of a flat from three features has learned one weight for every feature and one extra number, the bias:

prediction=w⋅x+bias\text{prediction} = \vw \cdot \vx + \text{bias}
The features of a flat and the weights of the model
FeatureValue (x\vx)Weight (w\vw)
area in m²801200
rooms315000
age of the building in years10−800-800

With the bias 20000:

w⋅x+bias=1200×80+15000×3+(−800)×10+20000=96000+45000−8000+20000=153000\begin{aligned} \vw \cdot \vx + \text{bias} &= 1200 \times 80 + 15000 \times 3 \\ &\quad + (-800) \times 10 + 20000 \\ &= 96000 + 45000 - 8000 + 20000 \\ &= 153000 \end{aligned}

Every square metre adds 1200, every room 15000, and every year of age takes away 800. This is linear regression (Topic 22): learning means finding the weights. A neuron of a neural network does the same calculation and then passes the result through one more function.

8.2Search by similarity

Three short texts are turned into vectors by counting three words: "python", "data" and "football". The question is q=(1,1,0)\vq = (1, 1, 0), the texts are A=(2,2,0)A = (2, 2, 0), B=(1,0,1)B = (1, 0, 1) and C=(0,0,3)C = (0, 0, 3). The length of the question is ∣q∣=2\len{\vq} = \sqrt{2}.

text A:q⋅A=4,∣A∣=842×8=44=1text B:q⋅B=1,∣B∣=212×2=12=0.5text C:q⋅C=0,so the similarity is 0\begin{aligned} \text{text A:} \quad & \vq \cdot A = 4, \quad \len{A} = \sqrt{8} \\ & \frac{4}{\sqrt{2} \times \sqrt{8}} = \frac{4}{4} = 1 \\ \text{text B:} \quad & \vq \cdot B = 1, \quad \len{B} = \sqrt{2} \\ & \frac{1}{\sqrt{2} \times \sqrt{2}} = \frac{1}{2} = 0.5 \\ \text{text C:} \quad & \vq \cdot C = 0, \quad \text{so the similarity is } 0 \end{aligned}

Text A points in exactly the same direction as the question, although it is twice as long. Text B shares one word, text C none. Sorting by this number is the heart of a search engine, and of the retrieval step in RAG at the end of the course. There the vectors have hundreds of components, but the formula is the same.

8.3The nearest neighbour

A shop knows two numbers about each customer: visits per month and average bill. Anar (8,30)(8, 30) and Sona (7,25)(7, 25) are regular customers, Emil (1,60)(1, 60) is a rare one. A new customer is (6,32)(6, 32).

to Anar:(6−8,  32−30)=(−2,2)4+4≈2.83to Sona:(6−7,  32−25)=(−1,7)1+49≈7.07to Emil:(6−1,  32−60)=(5,−28)25+784≈28.44\begin{aligned} \text{to Anar:} \quad & (6 - 8,\; 32 - 30) = (-2, 2) \\ & \sqrt{4 + 4} \approx 2.83 \\ \text{to Sona:} \quad & (6 - 7,\; 32 - 25) = (-1, 7) \\ & \sqrt{1 + 49} \approx 7.07 \\ \text{to Emil:} \quad & (6 - 1,\; 32 - 60) = (5, -28) \\ & \sqrt{25 + 784} \approx 28.44 \end{aligned}

The new customer is closest to Anar, so the best guess is "regular". This is k-Nearest Neighbors (Topics 32 and 33) with k=1k = 1: the distance of Part 5 and nothing else.

One warning that later lessons take seriously: the bill has much larger numbers than the visits, so it dominates the distance. That is why features are scaled before distances are used (Topic 24).

Try it yourself

A model predicts the number of visitors of a cafe with the weights w=(2, 0.5)\vw = (2,\, 0.5), the bias 1010 and the features x=(4,20)\vx = (4, 20) (hours of sunshine, temperature). Find the prediction.

Show a hint
First the dot product w⋅x\vw \cdot \vx, then add the bias.
Show the solution

Answer: 28.

w⋅x+bias=2×4+0.5×20+10=8+10+10=28\begin{aligned} \vw \cdot \vx + \text{bias} &= 2 \times 4 + 0.5 \times 20 + 10 \\ &= 8 + 10 + 10 = 28 \end{aligned}

9Common mistakes

Adding vectors of different dimensions. (2,1)+(1,3,5)(2, 1) + (1, 3, 5) is not defined. Check the dimensions first.

Writing a dot product as a vector. (1,2)⋅(3,4)=3+8=11(1, 2) \cdot (3, 4) = 3 + 8 = 11, one number. Writing (3,8)(3, 8) and stopping there is the most common mistake of the topic.

Losing a minus sign. In (2,−3)⋅(1,4)(2, -3) \cdot (1, 4) the second product is −12-12, so the result is 2−12=−102 - 12 = -10. Write every product before adding.

Forgetting the square root in the length. ∣(3,4)∣\len{(3, 4)} is 5, not 25. a⋅a\va \cdot \va is the length squared.

Taking the root of each part. ∣(3,4)∣\len{(3, 4)} is 9+16\sqrt{9 + 16}, not 3+43 + 4. The root of a sum is not the sum of the roots.

Adding the lengths. ∣a+b∣\len{\va + \vb} is not ∣a∣+∣b∣\len{\va} + \len{\vb}. For a=(3,0)\va = (3, 0) and b=(0,4)\vb = (0, 4): ∣a+b∣=5\len{\va + \vb} = 5, but ∣a∣+∣b∣=7\len{\va} + \len{\vb} = 7.

Subtracting in the wrong order. AB→=B−A\arrow{AB} = B - A: end minus start. For a distance the order does not matter; for a direction it does.

Taking the angle from the dot product alone. The dot product must be divided by both lengths before the inverse cosine is taken.

Reading a zero dot product as a zero vector. a⋅b=0\va \cdot \vb = 0 means that the vectors are perpendicular. Neither of them has to be zero.

Cancelling a vector. From a⋅b=a⋅c\va \cdot \vb = \va \cdot \vc it does not follow that b=c\vb = \vc. Example: a=(1,0)\va = (1, 0), b=(0,1)\vb = (0, 1), c=(0,5)\vc = (0, 5): both dot products are 0.

Normalizing the zero vector. Its length is 0, and dividing by 0 is not possible.

Degrees and radians. Set the calculator to degrees before using the inverse cosine, or convert: 180∘=π180^\circ = \pi radians.

Common mistakes and what is right
MistakeWhat is right
(2,1)+(1,3,5)(2, 1) + (1, 3, 5)only vectors of the same dimension can be added
(1,2)⋅(3,4)=(3,8)(1, 2) \cdot (3, 4) = (3, 8)(1,2)⋅(3,4)=11(1, 2) \cdot (3, 4) = 11
∣a∣=a12+a22\len{\va} = a_1^2 + a_2^2∣a∣=a12+a22\len{\va} = \sqrt{a_1^2 + a_2^2}
∣a+b∣=∣a∣+∣b∣\len{\va + \vb} = \len{\va} + \len{\vb}∣a+b∣≤∣a∣+∣b∣\len{\va + \vb} \le \len{\va} + \len{\vb}
AB→=A−B\arrow{AB} = A - BAB→=B−A\arrow{AB} = B - A
θ=cos⁡−1(a⋅b)\theta = \cos^{-1}(\va \cdot \vb)θ=cos⁡−1 ⁣(a⋅b∣a∣ ∣b∣)\theta = \cos^{-1}\!\left(\dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}}\right)
"dot product 0 means a zero vector"it means perpendicular vectors
a cosine larger than 1impossible: recalculate (Cauchy-Schwarz)

10Solved problems (from the easiest to the hardest)

Each problem has the question, the idea behind it with the part of the lesson it uses, and the calculation step by step. Cover the solution and try first.

Level 1: The operations

Problem 1: Add and subtract

For a=(4,−2,7)\va = (4, -2, 7) and b=(1,5,−3)\vb = (1, 5, -3) find a+b\va + \vb and a−b\va - \vb.

Idea. Addition and subtraction work component by component (Part 3). Keep the signs carefully: subtracting a negative number adds.

a+b=(4+1,  −2+5,  7+(−3))=(5,3,4)a−b=(4−1,  −2−5,  7−(−3))=(3,−7,10)\begin{aligned} \va + \vb &= (4 + 1,\; -2 + 5,\; 7 + (-3)) = (5, 3, 4) \\ \va - \vb &= (4 - 1,\; -2 - 5,\; 7 - (-3)) = (3, -7, 10) \end{aligned}

Why: the third component of a−b\va - \vb is 7−(−3)=107 - (-3) = 10. A quick check for any subtraction: b+(a−b)\vb + (\va - \vb) must give a\va again, and (1,5,−3)+(3,−7,10)=(4,−2,7)(1, 5, -3) + (3, -7, 10) = (4, -2, 7).

Problem 2: A linear combination

For a=(1,2)\va = (1, 2) and b=(3,−1)\vb = (3, -1) find 2a−3b2\va - 3\vb.

Idea. Scale first, then add (Part 4). Do it in two clear steps.

2a=(2,4)3b=(9,−3)2a−3b=(2−9,  4−(−3))=(−7,7)\begin{aligned} 2\va &= (2, 4) \\ 3\vb &= (9, -3) \\ 2\va - 3\vb &= (2 - 9,\; 4 - (-3)) = (-7, 7) \end{aligned}

Why: writing the scaled vectors on their own lines avoids the most common slip, a lost minus sign in the second component.

Problem 3: Two lengths

Find the length of (6,8)(6, 8) and of (1,2,2)(1, 2, 2).

Idea. Square, add, take the root (Part 5).

∣(6,8)∣=36+64=100=10∣(1,2,2)∣=1+4+4=9=3\begin{aligned} \len{(6, 8)} &= \sqrt{36 + 64} = \sqrt{100} = 10 \\ \len{(1, 2, 2)} &= \sqrt{1 + 4 + 4} = \sqrt{9} = 3 \end{aligned}

Why: (6,8)(6, 8) is 2 (3,4)2\,(3, 4), so its length is 2×5=102 \times 5 = 10: scaling a vector scales its length. Recognising a multiple of a known vector saves the calculation.

Problem 4: Two dot products

Find (1,2,3)⋅(4,−5,6)(1, 2, 3) \cdot (4, -5, 6) and (2,−1)⋅(3,6)(2, -1) \cdot (3, 6).

Idea. Multiply in pairs and add (Part 6).

(1,2,3)⋅(4,−5,6)=4−10+18=12(2,−1)⋅(3,6)=6−6=0\begin{aligned} (1, 2, 3) \cdot (4, -5, 6) &= 4 - 10 + 18 = 12 \\ (2, -1) \cdot (3, 6) &= 6 - 6 = 0 \end{aligned}

Why: the second result is 0, so (2,−1)(2, -1) and (3,6)(3, 6) are perpendicular. A dot product is never only a number to calculate; always read its sign.

Problem 5: A unit vector

Find the unit vector in the direction of a=(3,−4)\va = (3, -4).

Idea. Divide the vector by its length (Part 5).

∣a∣=9+16=5u=a∣a∣=(35, −45)=(0.6, −0.8)check:∣u∣=0.36+0.64=1\begin{aligned} \len{\va} &= \sqrt{9 + 16} = 5 \\ \vu &= \frac{\va}{\len{\va}} = \left(\tfrac{3}{5},\, -\tfrac{4}{5}\right) = (0.6,\, -0.8) \\ \text{check:} \quad \len{\vu} &= \sqrt{0.36 + 0.64} = 1 \end{aligned}

Why: the sign of a component does not change the length, because it is squared. The unit vector keeps the signs: it points in the same direction as a\va.

Problem 6: The bill

A cafe sold 12 teas, 5 coffees and 8 cakes. A tea costs 1.5, a coffee 3.0 and a cake 4.5. Write the two vectors and find the money earned.

Idea. Amounts and what each is worth: a dot product (Part 6).

sold=(12,5,8)prices=(1.5, 3.0, 4.5)sold⋅prices=12×1.5+5×3.0+8×4.5=18+15+36=69\begin{aligned} \text{sold} &= (12, 5, 8) \\ \text{prices} &= (1.5,\, 3.0,\, 4.5) \\ \text{sold} \cdot \text{prices} &= 12 \times 1.5 + 5 \times 3.0 + 8 \times 4.5 \\ &= 18 + 15 + 36 = 69 \end{aligned}

Why: the order of the components must be the same in both vectors: tea, coffee, cake. A vector is an ordered list.

Problem 7: From one point to another

For the points A=(2,−1)A = (2, -1) and B=(8,7)B = (8, 7) find the vector AB→\arrow{AB}, the distance between the points and the midpoint of the segment.

Idea. The vector between two points is end minus start (Part 2), the distance is its length (Part 5), and the midpoint is the average of the two points (Part 4).

AB→=B−A=(8−2,  7−(−1))=(6,8)distance=∣AB→∣=36+64=10midpoint=A+B2=(10,6)2=(5,3)\begin{aligned} \arrow{AB} &= B - A = (8 - 2,\; 7 - (-1)) = (6, 8) \\ \text{distance} &= \len{\arrow{AB}} = \sqrt{36 + 64} = 10 \\ \text{midpoint} &= \frac{A + B}{2} = \frac{(10, 6)}{2} = (5, 3) \end{aligned}

Why: the midpoint can be checked with the vector: go half of AB→\arrow{AB} from AA, (2,−1)+(3,4)=(5,3)(2, -1) + (3, 4) = (5, 3). Two ways to the same point is the best check there is.

Level 2: Length, distance and angle

Problem 8: The angle between two vectors

Find the angle between a=(1,0)\va = (1, 0) and b=(1,1)\vb = (1, 1).

Idea. cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}} (Part 7).

a⋅b=1+0=1∣a∣=1,∣b∣=2cos⁡θ=12≈0.7071θ=45∘\begin{aligned} \va \cdot \vb &= 1 + 0 = 1 \\ \len{\va} &= 1, \quad \len{\vb} = \sqrt{2} \\ \cos\theta &= \frac{1}{\sqrt{2}} \approx 0.7071 \\ \theta &= 45^\circ \end{aligned}

Why: the picture agrees. (1,0)(1, 0) lies along the x axis, and (1,1)(1, 1) is the diagonal of a square, exactly halfway to the y axis.

Problem 9: Make them perpendicular

For which number kk are a=(2,k)\va = (2, k) and b=(3,−6)\vb = (3, -6) perpendicular?

Idea. Perpendicular means the dot product is 0 (Part 7). Write the dot product with the unknown and solve.

a⋅b=2×3+k×(−6)=6−6k6−6k=0k=1check:(2,1)⋅(3,−6)=6−6=0\begin{aligned} \va \cdot \vb &= 2 \times 3 + k \times (-6) \\ &= 6 - 6k \\ 6 - 6k &= 0 \\ k &= 1 \\ \text{check:} \quad (2, 1) \cdot (3, -6) &= 6 - 6 = 0 \end{aligned}

Why: the condition "perpendicular" turned into one small equation. Many problems of linear algebra work like this: translate the geometric word into a dot product.

Problem 10: A distance in three dimensions

Find the distance between the points p=(1,2,3)\vp = (1, 2, 3) and q=(4,6,3)\vq = (4, 6, 3).

Idea. The distance is the length of the difference (Part 5).

q−p=(3,4,0)∣q−p∣=9+16+0=5\begin{aligned} \vq - \vp &= (3, 4, 0) \\ \len{\vq - \vp} &= \sqrt{9 + 16 + 0} = 5 \end{aligned}

Why: the third components are equal, so the two points are at the same height and the problem is really the 3-4-5 triangle in a plane. Looking at the difference vector before calculating often shows such a shortcut.

Problem 11: Two walks

A hiker walks 3 km to the east and 4 km to the north. After a break she walks 1 km to the west and 2 km to the north. How far is she from the start in a straight line, and how far did she walk?

Idea. Each walk is a vector (east, north). The total displacement is the sum; the straight-line distance is its length (Parts 3 and 5).

first=(3,4)second=(−1,2)total=(3−1,  4+2)=(2,6)straight line=4+36=40≈6.32 kmwalked=∣(3,4)∣+∣(−1,2)∣=5+5≈7.24 km\begin{aligned} \text{first} &= (3, 4) \\ \text{second} &= (-1, 2) \\ \text{total} &= (3 - 1,\; 4 + 2) = (2, 6) \\ \text{straight line} &= \sqrt{4 + 36} = \sqrt{40} \approx 6.32 \text{ km} \\ \text{walked} &= \len{(3, 4)} + \len{(-1, 2)} \\ &= 5 + \sqrt{5} \approx 7.24 \text{ km} \end{aligned}

Why: she walked 7.24 km but is only 6.32 km from the start. ∣a+b∣≤∣a∣+∣b∣\len{\va + \vb} \le \len{\va} + \len{\vb}: this is the triangle inequality of Part 5 in real life.

Problem 12: Find the recipe

Write (5,3)(5, 3) as a linear combination of a=(1,1)\va = (1, 1) and b=(1,−1)\vb = (1, -1).

Idea. We look for two numbers xx and yy with x a+y b=(5,3)x\,\va + y\,\vb = (5, 3) (Part 4). Each component gives one equation.

x (1,1)+y (1,−1)=(x+y,  x−y)=(5,3)x+y=5x−y=3\begin{aligned} x\,(1, 1) + y\,(1, -1) &= (x + y,\; x - y) = (5, 3) \\ x + y &= 5 \\ x - y &= 3 \end{aligned}

Adding the two equations gives 2x=82x = 8, so x=4x = 4 and y=1y = 1.

(5,3)=4a+bcheck:(4,4)+(1,−1)=(5,3)\begin{aligned} (5, 3) &= 4\va + \vb \\ \text{check:} \quad (4, 4) + (1, -1) &= (5, 3) \end{aligned}

Why: one vector equation in 2 dimensions is two ordinary equations. Always put the numbers back in at the end: the check takes ten seconds.

Problem 13: Three points on one line?

Do the points A=(1,2)A = (1, 2), B=(3,6)B = (3, 6) and C=(4,8)C = (4, 8) lie on one straight line?

Idea. They do exactly when AB→\arrow{AB} and AC→\arrow{AC} are parallel, that is, when one is a multiple of the other (Part 4).

AB→=B−A=(2,4)AC→=C−A=(3,6)ratios:32=1.5and64=1.5AC→=1.5 AB→\begin{aligned} \arrow{AB} &= B - A = (2, 4) \\ \arrow{AC} &= C - A = (3, 6) \\ \text{ratios:} \quad \tfrac{3}{2} &= 1.5 \quad \text{and} \quad \tfrac{6}{4} = 1.5 \\ \arrow{AC} &= 1.5\,\arrow{AB} \end{aligned}

The two vectors are parallel, so the three points are on one line.

Why: both vectors start at the same point AA. Two parallel arrows from the same point lie on the same line, so their tips BB and CC are on that line too.

Problem 14: A weighted average

A course has three grades with the weights 2, 3 and 5. A student got 90, 60 and 80. Find the weighted average.

Idea. Weights times grades is a dot product; divide by the sum of the weights (Part 6).

(2,3,5)⋅(90,60,80)=2×90+3×60+5×80=180+180+400=760sum of the weights=2+3+5=10average=76010=76\begin{aligned} (2, 3, 5) \cdot (90, 60, 80) &= 2 \times 90 + 3 \times 60 + 5 \times 80 \\ &= 180 + 180 + 400 = 760 \\ \text{sum of the weights} &= 2 + 3 + 5 = 10 \\ \text{average} &= \frac{760}{10} = 76 \end{aligned}

Why: the plain average of 90, 60 and 80 is 76.67. The weighted one is lower because the weakest grade has a larger weight than the strongest.

Problem 15: How similar are two texts?

Two texts are described by how often the words "data", "model" and "price" appear: d1=(2,1,0)\vd_1 = (2, 1, 0) and d2=(1,1,1)\vd_2 = (1, 1, 1). Find their cosine similarity.

Idea. a⋅b∣a∣ ∣b∣\dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}} (Part 7).

d1⋅d2=2+1+0=3∣d1∣=4+1+0=5∣d2∣=1+1+1=3similarity=35×3=315≈0.775\begin{aligned} \vd_1 \cdot \vd_2 &= 2 + 1 + 0 = 3 \\ \len{\vd_1} &= \sqrt{4 + 1 + 0} = \sqrt{5} \\ \len{\vd_2} &= \sqrt{1 + 1 + 1} = \sqrt{3} \\ \text{similarity} &= \frac{3}{\sqrt{5} \times \sqrt{3}} = \frac{3}{\sqrt{15}} \approx 0.775 \end{aligned}

Why: all counts are zero or positive, so the similarity of two count vectors is always between 0 and 1. A value of 0.775 says: mostly the same subject.

Level 3: Putting ideas together

Problem 16: A projection

Project a=(5,1)\va = (5, 1) onto b=(2,2)\vb = (2, 2). Give the vector projection and the part of a\va that is perpendicular to b\vb.

Idea. The projection is a⋅bb⋅b b\dfrac{\va \cdot \vb}{\vb \cdot \vb}\,\vb (Part 7). What is left, a\va minus the projection, must be perpendicular to b\vb.

a⋅b=10+2=12b⋅b=4+4=8projection=128 (2,2)=1.5 (2,2)=(3,3)rest=(5−3,  1−3)=(2,−2)\begin{aligned} \va \cdot \vb &= 10 + 2 = 12 \\ \vb \cdot \vb &= 4 + 4 = 8 \\ \text{projection} &= \tfrac{12}{8}\,(2, 2) = 1.5\,(2, 2) = (3, 3) \\ \text{rest} &= (5 - 3,\; 1 - 3) = (2, -2) \end{aligned}

Check: (2,−2)⋅(2,2)=4−4=0(2, -2) \cdot (2, 2) = 4 - 4 = 0.

Why: a=(3,3)+(2,−2)\va = (3, 3) + (2, -2): one part along the diagonal b\vb and one part across it. The check with the dot product catches almost every calculation mistake in a projection.

Problem 17: A right-angled triangle

A triangle has the corners A=(1,1)A = (1, 1), B=(4,1)B = (4, 1) and C=(1,5)C = (1, 5). Show that it has a right angle, and find the lengths of its three sides.

Idea. The sides are vectors between points (Part 2). A right angle at a corner means that the two sides leaving it have the dot product 0 (Part 7).

AB→=B−A=(3,0)AC→=C−A=(0,4)BC→=C−B=(−3,4)AB→⋅AC→=0+0=0\begin{aligned} \arrow{AB} &= B - A = (3, 0) \\ \arrow{AC} &= C - A = (0, 4) \\ \arrow{BC} &= C - B = (-3, 4) \\ \arrow{AB} \cdot \arrow{AC} &= 0 + 0 = 0 \end{aligned}

The dot product is 0, so the angle at AA is 90∘90^\circ. The lengths of the sides:

∣AB→∣=3∣AC→∣=4∣BC→∣=9+16=5\begin{aligned} \len{\arrow{AB}} &= 3 \\ \len{\arrow{AC}} &= 4 \\ \len{\arrow{BC}} &= \sqrt{9 + 16} = 5 \end{aligned}

Why: a point is not a side. The corners are given as points; the sides are the arrows between them, and every question about angles and lengths is asked about those arrows. For the angle at a corner, both arrows must start at that corner.

Problem 18: Classify by the nearest neighbour

Known flowers, described by (petal length, petal width) in cm: P=(1.4, 0.2)P = (1.4,\, 0.2) is of type A, Q=(4.7, 1.4)Q = (4.7,\, 1.4) is of type B, R=(5.9, 2.1)R = (5.9,\, 2.1) is of type C. A new flower measures (4.4, 1.2)(4.4,\, 1.2). Which type is the best guess?

Idea. Calculate the distance to every known flower and take the smallest (Parts 5 and 8).

to P:(4.4−1.4,  1.2−0.2)=(3.0, 1.0)9+1≈3.16to Q:(4.4−4.7,  1.2−1.4)=(−0.3, −0.2)0.09+0.04≈0.36to R:(4.4−5.9,  1.2−2.1)=(−1.5, −0.9)2.25+0.81≈1.75\begin{aligned} \text{to } P: \quad & (4.4 - 1.4,\; 1.2 - 0.2) = (3.0,\, 1.0) \\ & \sqrt{9 + 1} \approx 3.16 \\ \text{to } Q: \quad & (4.4 - 4.7,\; 1.2 - 1.4) = (-0.3,\, -0.2) \\ & \sqrt{0.09 + 0.04} \approx 0.36 \\ \text{to } R: \quad & (4.4 - 5.9,\; 1.2 - 2.1) = (-1.5,\, -0.9) \\ & \sqrt{2.25 + 0.81} \approx 1.75 \end{aligned}

The nearest flower is QQ, so the best guess is type B.

Why: to find only the nearest one, the square roots are not even needed: the smallest squared distance (0.13) belongs to the smallest distance. Programs use this to save time.

Problem 19: Lengths and an angle, no components

Two vectors have the lengths ∣a∣=2\len{\va} = 2 and ∣b∣=5\len{\vb} = 5, and the angle between them is 120∘120^\circ. Find a⋅b\va \cdot \vb, ∣a+b∣\len{\va + \vb} and ∣a−b∣\len{\va - \vb}.

Idea. The geometric formula gives the dot product from lengths and angle (Part 7). The square of a sum gives the length of a+b\va + \vb from the dot product (Part 6); for a−b\va - \vb the middle term changes its sign.

a⋅b=∣a∣ ∣b∣ cos⁡120∘=2×5×(−0.5)=−5∣a+b∣2=∣a∣2+2 (a⋅b)+∣b∣2=4−10+25=19∣a+b∣=19≈4.36∣a−b∣2=∣a∣2−2 (a⋅b)+∣b∣2=4+10+25=39∣a−b∣=39≈6.24\begin{aligned} \va \cdot \vb &= \len{\va}\,\len{\vb}\,\cos 120^\circ \\ &= 2 \times 5 \times (-0.5) = -5 \\ \len{\va + \vb}^2 &= \len{\va}^2 + 2\,(\va \cdot \vb) + \len{\vb}^2 \\ &= 4 - 10 + 25 = 19 \\ \len{\va + \vb} &= \sqrt{19} \approx 4.36 \\ \len{\va - \vb}^2 &= \len{\va}^2 - 2\,(\va \cdot \vb) + \len{\vb}^2 \\ &= 4 + 10 + 25 = 39 \\ \len{\va - \vb} &= \sqrt{39} \approx 6.24 \end{aligned}

Why: the components of the vectors were never needed. The angle is more than 90∘90^\circ, so the vectors point against each other: their sum is shorter than b\vb alone, and their difference is longer.

Problem 20: Which feature decides?

A model scores loan applications with the weights w=(0.5, −2.0, 0.1)\vw = (0.5,\, -2.0,\, 0.1) for the features (income in thousands, number of late payments, age) and the bias −3-3. An application is accepted when the score is positive. Applicant 1 is (12,1,30)(12, 1, 30), applicant 2 is (9,3,45)(9, 3, 45). Who is accepted, and which feature costs applicant 2 the most?

Idea. The score is w⋅x+bias\vw \cdot \vx + \text{bias} (Part 8). The single products show what every feature contributes.

applicant 1:0.5×12+(−2.0)×1+0.1×30−3=6−2+3−3=4(accepted)applicant 2:0.5×9+(−2.0)×3+0.1×45−3=4.5−6+4.5−3=0(not accepted)\begin{aligned} & \text{applicant 1:} \\ & 0.5 \times 12 + (-2.0) \times 1 + 0.1 \times 30 - 3 \\ & = 6 - 2 + 3 - 3 = 4 \quad \text{(accepted)} \\ & \text{applicant 2:} \\ & 0.5 \times 9 + (-2.0) \times 3 + 0.1 \times 45 - 3 \\ & = 4.5 - 6 + 4.5 - 3 = 0 \quad \text{(not accepted)} \end{aligned}

Why: the single products of applicant 2 are 4.54.5, −6-6 and 4.54.5. The three late payments take away 6 points, more than the income adds. The sign of w⋅x+bias\vw \cdot \vx + \text{bias} decides on which side of a border an applicant falls: this is the idea of logistic regression and of Support Vector Machines.

11Practice problems

Thirty-four problems in three levels. Solve each one on paper, type your result into the answer boxes and press Check. Under every problem there is a hint and a full solution: look at the hint only when you are stuck, and at the solution only after you have an answer of your own.

Level 1: The operations

  1. 1.For a=(4,−1)\va = (4, -1) and b=(2,5)\vb = (2, 5) find a+b\va + \vb and a−b\va - \vb.

    Show a hint
    Work component by component.
    Show one possible solution

    Answer: a+b=(6,4)\va + \vb = (6, 4) and a−b=(2,−6)\va - \vb = (2, -6).

    a+b=(4+2,  −1+5)=(6,4)a−b=(4−2,  −1−5)=(2,−6)\begin{aligned} \va + \vb &= (4 + 2,\; -1 + 5) = (6, 4) \\ \va - \vb &= (4 - 2,\; -1 - 5) = (2, -6) \end{aligned}
  2. 2.For a=(−2,0,5)\va = (-2, 0, 5) find 3a3\va and −2a-2\va.

    Show a hint
    Multiply every component by the number.
    Show one possible solution

    Answer: 3a=(−6,0,15)3\va = (-6, 0, 15) and −2a=(4,0,−10)-2\va = (4, 0, -10).

    3a=(3×(−2),  3×0,  3×5)=(−6,0,15)−2a=(−2×(−2),  −2×0,  −2×5)=(4,0,−10)\begin{aligned} 3\va &= (3 \times (-2),\; 3 \times 0,\; 3 \times 5) \\ &= (-6, 0, 15) \\ -2\va &= (-2 \times (-2),\; -2 \times 0,\; -2 \times 5) \\ &= (4, 0, -10) \end{aligned}

    The negative number turns the direction around.

  3. 3.For a=(1,2)\va = (1, 2) and b=(−1,1)\vb = (-1, 1) find 2a+3b2\va + 3\vb.

    Show a hint
    Scale each vector first, then add.
    Show one possible solution

    Answer: (−1,7)(-1, 7).

    2a=(2,4),3b=(−3,3)2a+3b=(2−3,  4+3)=(−1,7)\begin{aligned} 2\va &= (2, 4), \quad 3\vb = (-3, 3) \\ 2\va + 3\vb &= (2 - 3,\; 4 + 3) = (-1, 7) \end{aligned}
  4. 4.Find the length of (5,12)(5, 12).

    Show a hint
    Square, add, take the root.
    Show one possible solution

    Answer: 13.

    ∣(5,12)∣=25+144=169=13\len{(5, 12)} = \sqrt{25 + 144} = \sqrt{169} = 13
  5. 5.Find the length of (2,−2,1)(2, -2, 1).

    Show a hint
    A negative component becomes positive when it is squared.
    Show one possible solution

    Answer: 3.

    ∣(2,−2,1)∣=4+4+1=9=3\len{(2, -2, 1)} = \sqrt{4 + 4 + 1} = \sqrt{9} = 3
  6. 6.Find (3,−1)⋅(2,4)(3, -1) \cdot (2, 4).

    Show a hint
    Multiply the first components, multiply the second components, add.
    Show one possible solution

    Answer: 2.

    (3,−1)⋅(2,4)=3×2+(−1)×4=6−4=2(3, -1) \cdot (2, 4) = 3 \times 2 + (-1) \times 4 = 6 - 4 = 2
  7. 7.Find (1,0,−2,3)⋅(2,5,1,1)(1, 0, -2, 3) \cdot (2, 5, 1, 1).

    Show a hint
    Four pairs, four products.
    Show one possible solution

    Answer: 3.

    (1,0,−2,3)⋅(2,5,1,1)=2+0−2+3=3(1, 0, -2, 3) \cdot (2, 5, 1, 1) = 2 + 0 - 2 + 3 = 3
  8. 8.Find the unit vector in the direction of (8,6)(8, 6).

    Show a hint
    Divide the vector by its length.
    Show one possible solution

    Answer: (0.8, 0.6)(0.8,\, 0.6).

    ∣(8,6)∣=64+36=10110 (8,6)=(0.8, 0.6)\begin{aligned} \len{(8, 6)} &= \sqrt{64 + 36} = 10 \\ \tfrac{1}{10}\,(8, 6) &= (0.8,\, 0.6) \end{aligned}

    Check: 0.64+0.36=10.64 + 0.36 = 1.

  9. 9.Find the distance between the points (2,3)(2, 3) and (5,7)(5, 7).

    Show a hint
    The distance is the length of the difference.
    Show one possible solution

    Answer: 5.

    The difference is (5−2,  7−3)=(3,4)(5 - 2,\; 7 - 3) = (3, 4), and its length is 9+16=5\sqrt{9 + 16} = 5.

  10. 10.Find the midpoint of the segment from (−3,5)(-3, 5) to (7,1)(7, 1).

    Show a hint
    The midpoint is the average of the two points.
    Show one possible solution

    Answer: (2,3)(2, 3).

    (−3+72,  5+12)=(42,  62)=(2,3)\left(\frac{-3 + 7}{2},\; \frac{5 + 1}{2}\right) = \left(\frac{4}{2},\; \frac{6}{2}\right) = (2, 3)
  11. 11.Which of these vectors are parallel to a=(1,2,3)\va = (1, 2, 3): b=(2,4,6)\vb = (2, 4, 6), c=(2,4,5)\vc = (2, 4, 5), d=(−0.5, −1, −1.5)\vd = (-0.5,\, -1,\, -1.5)?

    Show a hint
    A parallel vector is a multiple of a\va: divide the components and look for the same number every time.
    Show one possible solution

    Answer: b\vb and d\vd are parallel to a\va; c\vc is not.

    b=2a\vb = 2\va, because 2/1=4/2=6/3=22/1 = 4/2 = 6/3 = 2. For c\vc the ratios are 22, 22 and 5/35/3: not the same, so it is not parallel. d=−0.5 a\vd = -0.5\,\va: parallel, pointing the opposite way.

Level 2: Length, distance and angle

  1. 12.Find xx and yy so that (x,3)+(2,y)=(7,2)(x, 3) + (2, y) = (7, 2).

    Show a hint
    Two vectors are equal when every component is equal: each component gives one equation.
    Show one possible solution

    Answer: x=5x = 5 and y=−1y = -1.

    first components:x+2=7⇒x=5second components:3+y=2⇒y=−1\begin{aligned} \text{first components:} \quad & x + 2 = 7 & \Rightarrow \quad & x = 5 \\ \text{second components:} \quad & 3 + y = 2 & \Rightarrow \quad & y = -1 \end{aligned}
  2. 13.For a=(3,−2,1)\va = (3, -2, 1) and b=(1,1,1)\vb = (1, 1, 1) find the vector c\vc with a+c=b\va + \vc = \vb.

    Show a hint
    Solve the equation as with numbers: c=b−a\vc = \vb - \va.
    Show one possible solution

    Answer: c=(−2,3,0)\vc = (-2, 3, 0).

    c=b−a=(1−3,  1−(−2),  1−1)=(−2,3,0)\begin{aligned} \vc = \vb - \va &= (1 - 3,\; 1 - (-2),\; 1 - 1) \\ &= (-2, 3, 0) \end{aligned}

    Check: a+c=(3−2,  −2+3,  1+0)=(1,1,1)\va + \vc = (3 - 2,\; -2 + 3,\; 1 + 0) = (1, 1, 1).

  3. 14.Find the angle between (3,4)(3, 4) and (4,3)(4, 3), in degrees with 2 decimals.

    Show a hint
    cos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}}. Both lengths are 5.
    Show one possible solution

    Answer: 16.26∘16.26^\circ.

    (3,4)⋅(4,3)=12+12=24cos⁡θ=245×5=0.96θ=cos⁡−1(0.96)≈16.26∘\begin{aligned} (3, 4) \cdot (4, 3) &= 12 + 12 = 24 \\ \cos\theta &= \frac{24}{5 \times 5} = 0.96 \\ \theta &= \cos^{-1}(0.96) \approx 16.26^\circ \end{aligned}
  4. 15.For which kk are (k,2,−1)(k, 2, -1) and (2,1,4)(2, 1, 4) perpendicular?

    Show a hint
    Set the dot product equal to 0.
    Show one possible solution

    Answer: k=1k = 1.

    2k+2−4=0⇒2k=2⇒k=12k + 2 - 4 = 0 \quad \Rightarrow \quad 2k = 2 \quad \Rightarrow \quad k = 1

    Check: (1,2,−1)⋅(2,1,4)=2+2−4=0(1, 2, -1) \cdot (2, 1, 4) = 2 + 2 - 4 = 0.

  5. 16.Without calculating the angle: is the angle between a=(2,−3)\va = (2, -3) and b=(1,4)\vb = (1, 4) less than, equal to or more than 90∘90^\circ?

    Show a hint
    The sign of the dot product is enough.
    Show one possible solution

    Answer: More than 90∘90^\circ.

    a⋅b=2−12=−10\va \cdot \vb = 2 - 12 = -10. A negative dot product means an angle of more than 90∘90^\circ.

  6. 17.A shop sold 12, 5 and 8 pieces of three products. Their prices are 3.5, 10 and 2.25. Find the money earned with a dot product.

    Show a hint
    Amounts times prices, added up.
    Show one possible solution

    Answer: 110.

    (12,5,8)⋅(3.5, 10, 2.25)=12×3.5+5×10+8×2.25=42+50+18=110\begin{aligned} & (12, 5, 8) \cdot (3.5,\, 10,\, 2.25) \\ &= 12 \times 3.5 + 5 \times 10 + 8 \times 2.25 \\ &= 42 + 50 + 18 = 110 \end{aligned}
  7. 18.Three courses have 3, 4 and 2 credits. A student got 85, 70 and 95. Find the average weighted by the credits, with 2 decimals.

    Show a hint
    Dot product of credits and grades, divided by the sum of the credits.
    Show one possible solution

    Answer: 80.56.

    (3,4,2)⋅(85,70,95)=255+280+190=725sum of the credits=9average=7259≈80.56\begin{aligned} (3, 4, 2) \cdot (85, 70, 95) &= 255 + 280 + 190 = 725 \\ \text{sum of the credits} &= 9 \\ \text{average} &= \frac{725}{9} \approx 80.56 \end{aligned}
  8. 19.Normalize (1,2,2)(1, 2, 2) and check that the result has the length 1.

    Show a hint
    The length of the vector is a whole number.
    Show one possible solution

    Answer: (13, 23, 23)\left(\tfrac{1}{3},\, \tfrac{2}{3},\, \tfrac{2}{3}\right), about (0.333, 0.667, 0.667)(0.333,\, 0.667,\, 0.667).

    ∣(1,2,2)∣=1+4+4=3u=(13, 23, 23)check:∣u∣2=19+49+49=1\begin{aligned} \len{(1, 2, 2)} &= \sqrt{1 + 4 + 4} = 3 \\ \vu &= \left(\tfrac{1}{3},\, \tfrac{2}{3},\, \tfrac{2}{3}\right) \\ \text{check:} \quad \len{\vu}^2 &= \tfrac{1}{9} + \tfrac{4}{9} + \tfrac{4}{9} = 1 \end{aligned}

    You can type the fractions in the answer box: (1/3, 2/3, 2/3).

  9. 20.Find the vector of the length 15 that points in the direction of (3,4)(3, 4).

    Show a hint
    First the unit vector, then multiply by 15.
    Show one possible solution

    Answer: (9,12)(9, 12).

    ∣(3,4)∣=5,u=(0.6, 0.8)15 u=(9,12)check:81+144=225=15\begin{aligned} \len{(3, 4)} &= 5, \quad \vu = (0.6,\, 0.8) \\ 15\,\vu &= (9, 12) \\ \text{check:} \quad \sqrt{81 + 144} &= \sqrt{225} = 15 \end{aligned}
  10. 21.Find the cosine similarity of a=(1,1,0,1)\va = (1, 1, 0, 1) and b=(1,0,1,1)\vb = (1, 0, 1, 1), with 3 decimals.

    Show a hint
    Both vectors have three ones, so both lengths are 3\sqrt{3}.
    Show one possible solution

    Answer: 0.667.

    a⋅b=1+0+0+1=2∣a∣ ∣b∣=3×3=3similarity=23≈0.667\begin{aligned} \va \cdot \vb &= 1 + 0 + 0 + 1 = 2 \\ \len{\va}\,\len{\vb} &= \sqrt{3} \times \sqrt{3} = 3 \\ \text{similarity} &= \frac{2}{3} \approx 0.667 \end{aligned}
  11. 22.A delivery robot drives 5 km to the east, then 3 km to the north, then 2 km to the west. How far is it from the start in a straight line (2 decimals)?

    Show a hint
    Write each part as a vector (east, north) and add them.
    Show one possible solution

    Answer: 4.24 km.

    (5,0)+(0,3)+(−2,0)=(3,3)∣(3,3)∣=18≈4.24\begin{aligned} (5, 0) + (0, 3) + (-2, 0) &= (3, 3) \\ \len{(3, 3)} &= \sqrt{18} \approx 4.24 \end{aligned}
  12. 23.Do the points P=(0,1)P = (0, 1), Q=(2,5)Q = (2, 5) and R=(5,11)R = (5, 11) lie on one straight line?

    Show a hint
    Compare the vectors PQ→\arrow{PQ} and PR→\arrow{PR}: are they parallel?
    Show one possible solution

    Answer: Yes.

    PQ→=(2,4)\arrow{PQ} = (2, 4) and PR→=(5,10)\arrow{PR} = (5, 10). The ratios are 5/2=2.55/2 = 2.5 and 10/4=2.510/4 = 2.5, so PR→=2.5 PQ→\arrow{PR} = 2.5\,\arrow{PQ}. The vectors are parallel and start at the same point, so the three points are on one line.

  13. 24.A model predicts an exam score as w⋅x+b\vw \cdot \vx + b with w=(0.4, 0.6)\vw = (0.4,\, 0.6), b=5b = 5 and the features x=(50,80)\vx = (50, 80) (homework score, midterm score). Find the prediction.

    Show a hint
    First the dot product, then add the bias.
    Show one possible solution

    Answer: 73.

    w⋅x+b=0.4×50+0.6×80+5=20+48+5=73\begin{aligned} \vw \cdot \vx + b &= 0.4 \times 50 + 0.6 \times 80 + 5 \\ &= 20 + 48 + 5 = 73 \end{aligned}

Level 3: Putting ideas together

  1. 25.Project a=(5,1)\va = (5, 1) onto b=(1,2)\vb = (1, 2). Give the vector projection.

    Show a hint
    a⋅bb⋅b b\dfrac{\va \cdot \vb}{\vb \cdot \vb}\,\vb.
    Show one possible solution

    Answer: (1.4, 2.8)(1.4,\, 2.8).

    a⋅b=5+2=7,b⋅b=1+4=5projection=75 (1,2)=1.4 (1,2)=(1.4, 2.8)\begin{aligned} \va \cdot \vb &= 5 + 2 = 7, \quad \vb \cdot \vb = 1 + 4 = 5 \\ \text{projection} &= \tfrac{7}{5}\,(1, 2) = 1.4\,(1, 2) = (1.4,\, 2.8) \end{aligned}

    Check: the rest (5−1.4,  1−2.8)=(3.6, −1.8)(5 - 1.4,\; 1 - 2.8) = (3.6,\, -1.8) has the dot product 3.6−3.6=03.6 - 3.6 = 0 with b\vb.

  2. 26.Find the numbers xx and yy with x (2,1)+y (1,3)=(4,7)x\,(2, 1) + y\,(1, 3) = (4, 7).

    Show a hint
    Each component gives one equation. Multiply the second equation by 2 and subtract the first to remove xx.
    Show one possible solution

    Answer: x=1x = 1 and y=2y = 2.

    2x+y=4x+3y=7\begin{aligned} 2x + y &= 4 \\ x + 3y &= 7 \end{aligned}

    Twice the second equation is 2x+6y=142x + 6y = 14. Subtracting the first leaves 5y=105y = 10, so y=2y = 2, and then x=7−3×2=1x = 7 - 3 \times 2 = 1.

    Check: (2,1)+2 (1,3)=(2,1)+(2,6)=(4,7)(2, 1) + 2\,(1, 3) = (2, 1) + (2, 6) = (4, 7).

  3. 27.A triangle has the corners A=(0,0)A = (0, 0), B=(4,0)B = (4, 0) and C=(4,3)C = (4, 3). Find the angle at the corner AA, in degrees with 2 decimals.

    Show a hint
    The angle at AA is the angle between the vectors AB→\arrow{AB} and AC→\arrow{AC}.
    Show one possible solution

    Answer: 36.87∘36.87^\circ.

    AB→=(4,0),AC→=(4,3)AB→⋅AC→=16,∣AB→∣=4,∣AC→∣=5cos⁡θ=1620=0.8θ≈36.87∘\begin{aligned} \arrow{AB} &= (4, 0), \quad \arrow{AC} = (4, 3) \\ \arrow{AB} \cdot \arrow{AC} &= 16, \quad \len{\arrow{AB}} = 4, \quad \len{\arrow{AC}} = 5 \\ \cos\theta &= \frac{16}{20} = 0.8 \\ \theta &\approx 36.87^\circ \end{aligned}
  4. 28.Find a unit vector that is perpendicular to (3,4)(3, 4).

    Show a hint
    Swap the two components and change one sign to get a perpendicular vector. Then normalize.
    Show one possible solution

    Answer: (0.8, −0.6)(0.8,\, -0.6), or the opposite vector (−0.8, 0.6)(-0.8,\, 0.6).

    (4,−3)(4, -3) is perpendicular to (3,4)(3, 4), because 12−12=012 - 12 = 0. Its length is 5, so the unit vector is (0.8, −0.6)(0.8,\, -0.6). The opposite vector (−0.8, 0.6)(-0.8,\, 0.6) is a second right answer.

  5. 29.Find the point that lies one quarter of the way from A=(2,0)A = (2, 0) to B=(10,8)B = (10, 8).

    Show a hint
    Start at AA and add a quarter of the vector AB→\arrow{AB}.
    Show one possible solution

    Answer: (4,2)(4, 2).

    AB→=(8,8),14 AB→=(2,2)point=(2,0)+(2,2)=(4,2)\begin{aligned} \arrow{AB} &= (8, 8), \quad \tfrac{1}{4}\,\arrow{AB} = (2, 2) \\ \text{point} &= (2, 0) + (2, 2) = (4, 2) \end{aligned}
  6. 30.Find the angle between a=(2,−1,2)\va = (2, -1, 2) and b=(1,2,2)\vb = (1, 2, 2), in degrees with 2 decimals. Then check the Cauchy-Schwarz inequality for them: give ∣a⋅b∣\len{\va \cdot \vb} and ∣a∣ ∣b∣\len{\va}\,\len{\vb}.

    Show a hint
    Both lengths are whole numbers.
    Show one possible solution

    Answer: 63.61∘63.61^\circ, and ∣a⋅b∣=4≤9=∣a∣ ∣b∣\len{\va \cdot \vb} = 4 \le 9 = \len{\va}\,\len{\vb}.

    a⋅b=2−2+4=4∣a∣=4+1+4=3∣b∣=1+4+4=3∣a∣ ∣b∣=9,4≤9cos⁡θ=49≈0.4444θ≈63.61∘\begin{aligned} \va \cdot \vb &= 2 - 2 + 4 = 4 \\ \len{\va} &= \sqrt{4 + 1 + 4} = 3 \\ \len{\vb} &= \sqrt{1 + 4 + 4} = 3 \\ \len{\va}\,\len{\vb} &= 9, \quad 4 \le 9 \\ \cos\theta &= \frac{4}{9} \approx 0.4444 \\ \theta &\approx 63.61^\circ \end{aligned}
  7. 31.Known points: P=(1,1)P = (1, 1) is red, Q=(4,5)Q = (4, 5) is blue, R=(0,4)R = (0, 4) is blue. Which colour does the nearest neighbour give to the new point (2,2)(2, 2)?

    Show a hint
    Calculate the three distances and take the smallest.
    Show one possible solution

    Answer: Red.

    to P:(1,1),2≈1.41to Q:(−2,−3),13≈3.61to R:(2,−2),8≈2.83\begin{aligned} \text{to } P: \quad & (1, 1), \quad \sqrt{2} \approx 1.41 \\ \text{to } Q: \quad & (-2, -3), \quad \sqrt{13} \approx 3.61 \\ \text{to } R: \quad & (2, -2), \quad \sqrt{8} \approx 2.83 \end{aligned}

    PP is the nearest, and PP is red.

  8. 32.Three users rated three films: Aylin (5,1,4)(5, 1, 4), Murad (4,2,5)(4, 2, 5), Leyla (1,5,2)(1, 5, 2). Whose taste is closer to Aylin's by cosine similarity? Give both similarities with 3 decimals.

    Show a hint
    Calculate the similarity of Aylin with each of the two.
    Show one possible solution

    Answer: Murad: 0.966. Leyla: 0.507. Murad is closer.

    Aylin's vector has the length 25+1+16=42\sqrt{25 + 1 + 16} = \sqrt{42}.

    with Murad:dot product 20+2+20=42length 16+4+25=454242×45=4243.47≈0.966with Leyla:dot product 5+5+8=18length 1+25+4=301842×30=1835.50≈0.507\begin{aligned} \text{with Murad:} \quad & \text{dot product } 20 + 2 + 20 = 42 \\ & \text{length } \sqrt{16 + 4 + 25} = \sqrt{45} \\ & \frac{42}{\sqrt{42 \times 45}} = \frac{42}{43.47} \approx 0.966 \\ \text{with Leyla:} \quad & \text{dot product } 5 + 5 + 8 = 18 \\ & \text{length } \sqrt{1 + 25 + 4} = \sqrt{30} \\ & \frac{18}{\sqrt{42 \times 30}} = \frac{18}{35.50} \approx 0.507 \end{aligned}
  9. 33.Two vectors have the lengths ∣a∣=3\len{\va} = 3 and ∣b∣=4\len{\vb} = 4, and the angle between them is 60∘60^\circ. Find a⋅b\va \cdot \vb and ∣a+b∣\len{\va + \vb} (2 decimals).

    Show a hint
    Use a⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta with cos⁡60∘=0.5\cos 60^\circ = 0.5, then ∣a+b∣2=∣a∣2+2 (a⋅b)+∣b∣2\len{\va + \vb}^2 = \len{\va}^2 + 2\,(\va \cdot \vb) + \len{\vb}^2.
    Show one possible solution

    Answer: a⋅b=6\va \cdot \vb = 6 and ∣a+b∣≈6.08\len{\va + \vb} \approx 6.08.

    a⋅b=3×4×0.5=6∣a+b∣2=9+12+16=37∣a+b∣=37≈6.08\begin{aligned} \va \cdot \vb &= 3 \times 4 \times 0.5 = 6 \\ \len{\va + \vb}^2 &= 9 + 12 + 16 = 37 \\ \len{\va + \vb} &= \sqrt{37} \approx 6.08 \end{aligned}

    The components were never needed: the lengths and the angle were enough.

  10. 34.For a=(3,1)\va = (3, 1) and b=(1,2)\vb = (1, 2) check that (a+b)⋅(a−b)=∣a∣2−∣b∣2(\va + \vb) \cdot (\va - \vb) = \len{\va}^2 - \len{\vb}^2: give the value of both sides. When is (a+b)⋅(a−b)(\va + \vb) \cdot (\va - \vb) equal to 0?

    Show a hint
    Calculate both sides. For the second question look at the right side.
    Show one possible solution

    Answer: Both sides are 5. The product is 0 exactly when the two vectors have the same length.

    a+b=(4,3),a−b=(2,−1)(a+b)⋅(a−b)=8−3=5∣a∣2−∣b∣2=10−5=5\begin{aligned} \va + \vb &= (4, 3), \quad \va - \vb = (2, -1) \\ (\va + \vb) \cdot (\va - \vb) &= 8 - 3 = 5 \\ \len{\va}^2 - \len{\vb}^2 &= 10 - 5 = 5 \end{aligned}

    The right side is 0 when ∣a∣=∣b∣\len{\va} = \len{\vb}. In a picture: a+b\va + \vb and a−b\va - \vb are the two diagonals of the parallelogram of a\va and b\vb, and they are perpendicular when all four sides are equally long.

12Check yourself

Twelve quick questions. Your answers are saved in this browser, so you can leave the page and continue later. Each finished round is kept as an attempt below the questions; you can delete the attempts at any time.

Question 1

1.What is (2,−1)+(3,4)(2, -1) + (3, 4)?

Question 2

2.What is −2 (3,−1,0)-2\,(3, -1, 0)?

Question 3

3.What is the length of (−6,8)(-6, 8)?

Question 4

4.What is (1,2,3)⋅(3,2,1)(1, 2, 3) \cdot (3, 2, 1)?

Question 5

5.Which two vectors are perpendicular?

Question 6

6.For two vectors a⋅b=−4\va \cdot \vb = -4. What does this say about the angle between them?

Question 7

7.What is the vector AB→\arrow{AB} from A=(1,5)A = (1, 5) to B=(4,1)B = (4, 1)?

Question 8

8.Which of these is a unit vector?

Question 9

9.a\va is not the zero vector. What is the cosine similarity of a\va and 3a3\va?

Question 10

10.What is the distance between the points (1,1)(1, 1) and (4,5)(4, 5)?

Question 11

11.∣a∣=2\len{\va} = 2, ∣b∣=3\len{\vb} = 3, and the angle between the vectors is 60∘60^\circ. What is a⋅b\va \cdot \vb?

Question 12

12.What is the vector projection of (3,4)(3, 4) onto (1,0)(1, 0)?

0 of 12 answered.

13Checking your work with NumPy

This is the only code of the lesson. After you have solved a problem by hand, NumPy can confirm the result. A vector is a 1D array (Topics 8 and 9).

Python
import numpy as np

a = np.array([2, 1])
b = np.array([1, 3])

print(a + b, a - b, 2 * a - 3 * b)
print(np.linalg.norm(a))
print(np.linalg.norm(a - b))
print(a @ b)

cos = (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(round(cos, 4), round(np.degrees(np.arccos(cos)), 2))

print((a @ b) / (b @ b) * b)

Output

[3 4] [ 1 -2] [ 1 -7]
2.23606797749979
2.23606797749979
5
0.7071 45.0
[0.5 1.5]

Change it and run: put the vectors of a problem you solved by hand into a and b, and compare the numbers.

By hand and in NumPy
By handIn NumPy
a+b\va + \vb, a−b\va - \vb, c ac\,\vaa + b, a - b, c * a
∣a∣\len{\va}np.linalg.norm(a)
the distance ∣a−b∣\len{\va - \vb}np.linalg.norm(a - b)
a⋅b\va \cdot \vba @ b or np.dot(a, b)
the angle θ\thetanp.degrees(np.arccos(cos))

One trap: a * b in NumPy multiplies element by element and gives a vector. It is not the dot product. The dot product is a @ b.

14Quick summary

Every idea of the lesson with its definition or formula
IdeaDefinition or formula
Vectoran ordered list of numbers; its dimension is the number of components
Vector between two pointsAB→=B−A\arrow{AB} = B - A (end minus start)
Addition, subtractioncomponent by component; a−b\va - \vb is the arrow from b\vb to a\va
Scalar multiplicationc a=(c a1,…,c an)c\,\va = (c\,a_1, \dots, c\,a_n); stretches or turns the arrow
Parallel vectorsb=c a\vb = c\,\va for some number cc
Midpointa+b2\dfrac{\va + \vb}{2}
Linear combinationc1 a+c2 bc_1\,\va + c_2\,\vb
Length∣a∣=a12+⋯+an2\len{\va} = \sqrt{a_1^2 + \dots + a_n^2}
Unit vectora∣a∣\dfrac{\va}{\len{\va}}
Distance∣a−b∣\len{\va - \vb}
Triangle inequality∣a+b∣≤∣a∣+∣b∣\len{\va + \vb} \le \len{\va} + \len{\vb}
Dot producta⋅b=a1b1+⋯+anbn\va \cdot \vb = a_1 b_1 + \dots + a_n b_n, one number
Length from the dot producta⋅a=∣a∣2\va \cdot \va = \len{\va}^2
Square of a sum∣a+b∣2=∣a∣2+2 (a⋅b)+∣b∣2\len{\va + \vb}^2 = \len{\va}^2 + 2\,(\va \cdot \vb) + \len{\vb}^2
Geometric formulaa⋅b=∣a∣ ∣b∣ cos⁡θ\va \cdot \vb = \len{\va}\,\len{\vb}\,\cos\theta
Anglecos⁡θ=a⋅b∣a∣ ∣b∣\cos\theta = \dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}}
Perpendicular (orthogonal)a⋅b=0\va \cdot \vb = 0
Cauchy-Schwarz inequality∣a⋅b∣≤∣a∣ ∣b∣\len{\va \cdot \vb} \le \len{\va}\,\len{\vb}
Cosine similaritya⋅b∣a∣ ∣b∣\dfrac{\va \cdot \vb}{\len{\va}\,\len{\vb}}, from −1-1 to 11
Scalar projection of a\va onto b\vba⋅b∣b∣\dfrac{\va \cdot \vb}{\len{\vb}}
Vector projection of a\va onto b\vba⋅bb⋅b b\dfrac{\va \cdot \vb}{\vb \cdot \vb}\,\vb
Prediction of a linear modelw⋅x+bias\vw \cdot \vx + \text{bias}