Unit 2 · Length, angle and the dot product

Lesson 10 · Available to read

Dot product: the recipe

The dot product multiplies corresponding components and adds the results, turning two vectors into one number.

The dot product (also called the scalar product or inner product) combines two vectors of the same dimension into a single number. The rule is to multiply corresponding components and add the products:

a · b = a₁b₁ + a₂b₂ + … + aₙbₙ.

For a = [1, 2, 3] and b = [4, 5, 6], a · b = 1·4 + 2·5 + 3·6 = 4 + 10 + 18 = 32. The inputs are vectors, but the output is a scalar.

Several properties follow directly from the definition. The dot product is commutative, a · b = b · a, and it distributes over addition, a · (b + c) = a · b + a · c. Most importantly, the dot product of a vector with itself is the sum of its squared components, which is the square of its norm: v · v = ‖v‖². Length can therefore be expressed entirely through dot products.

The entries of a = [1, 2, 3] and b = [4, 5, 6] multiplied in pairs to give 4, 10 and 18, which sum to 32

The dot product appears constantly in practice. If q lists the quantities of items purchased and p their unit prices, then q · p is the total cost. Two more examples:

  • Weighted average. If homework counts for 20% of a course grade, the midterm for 30% and the final for 50%, the grade is w · s, where w = [0.2, 0.3, 0.5] holds the weights and s the scores. With s = [90, 70, 80], the grade is 0.2·90 + 0.3·70 + 0.5·80 = 18 + 21 + 40 = 79. Because the weights sum to 1, each score counts in proportion to its weight.
A course grade as a dot product: weights 0.2, 0.3 and 0.5 times scores 90, 70 and 80 give 18, 21 and 40, which sum to 79; a bar shows the weights as 20%, 30% and 50%
  • Linear prediction score. A simple model estimates a house price from its features, x = [size in hundreds of m², bedrooms, age in decades], by multiplying each feature by a learned weight and adding: w · x. With w = [150, 20, −10] (in thousands of dollars) and x = [1.2, 3, 2], the prediction is 180 + 60 − 20 = 220, or about $220,000. Positive weights push the score up and negative weights pull it down. The same pattern underlies linear regression and each neuron in a neural network.
A house price prediction as a dot product: features 1.2, 3 and 2 times weights 150, 20 and −10 give +180, +60 and −20, shown as bars, for a total of 220, about $220,000

As later lessons show, each entry of a matrix product is also a dot product.

The takeaway

a · b = a₁b₁ + … + aₙbₙ; in particular, v · v = ‖v‖².

Practice

0 of 5 attempted

  1. 1Calculate

    Compute [2, 3] · [4, −1].

    Saved in this browser

  2. 2Calculate

    Compute [1, 2, 3] · [4, 5, 6].

    Saved in this browser

  3. 3Multiple choice

    What kind of object is the dot product of two vectors?

  4. 4Recall

    How is the norm of v related to the dot product?

    Saved in this browser

  5. 5Apply

    You buy 3 notebooks at $2, 1 calculator at $15 and 4 pens at $1. Express the total cost as a dot product and evaluate it.

    Saved in this browser