Dot Product

The dot product combines two vectors into one number.

It multiplies matching entries, then adds the results.

That makes it the simplest weighted-sum operation in this chapter. One vector can hold values. The other vector can hold weights. The dot product says how strongly the values match those weights.

For two vectors:

a = [2, 4]
b = [3, 5]

the dot product is:

ab=(2×3)+(4×5)=26a \cdot b = (2 \times 3) + (4 \times 5) = 26

Read it aloud:

multiply matching entries, then add
-6-4-2246-4-2246a = [2, 4]b = [3, 5]0
a · b = (2 × 3) + (4 × 5) = 26Drag either endpoint. Drag the plane to pan.

Formula

For two vectors with nn coordinates:

ab=a1b1+a2b2++anbna \cdot b = a_1b_1 + a_2b_2 + \cdots + a_nb_n

This is a sum of matching products.

The dot product needs matching shapes. If aa has three coordinates and bb has two, there is no complete set of matching pairs.

MATH-C02-T06-001Exercise: Compute by hand

Given a = [2, 4] and b = [3, 5], compute the dot product before writing code.

Compute it first, then check your number.

Check the numerical result first.
HintMultiply before adding

Pair the matching entries: 2 with 3, and 4 with 5.

SolutionTwo products, then one sum

The matching products are 2×3=62 \times 3 = 6 and 4×5=204 \times 5 = 20. Add them:

6+20=266 + 20 = 26

Why It Returns One Number

Vector addition returns a vector. The dot product returns a scalar.

That scalar often acts like a score.

For example, a simple model might compute:

score=wx\text{score} = w \cdot x

where ww stores weights and xx stores input features. A large positive score means the input lines up with the weights.

As a toy instance, suppose:

x = [hours_studied, hours_slept]
w = [2, 1]

Then:

w \cdot x = 2 \times \text{hours_studied} + 1 \times \text{hours_slept}

The first feature counts twice as much as the second feature in this toy score. This is not yet a trained model, but it is the same shape of computation used inside linear layers.

MATH-C02-T06-002Exercise: Dot product output type

Enter 1 if the dot product of two vectors returns one number rather than another vector.

Compute it first, then check your number.

HintLook at the operation

Multiplying matching entries creates products, and adding them collapses the result to one number.

SolutionScalar result

Enter 1. The dot product multiplies matching entries and then adds those products, producing one scalar.

MATH-C02-T06-005Exercise: Read weights in a dot product

A toy score uses w=[3,1]w = [3, -1] and x=[2,5]x = [2, 5]. What is wxw \cdot x?

Compute it first, then check your number.

HintKeep the negative sign

Compute 3×2+(1)×53 \times 2 + (-1) \times 5.

SolutionWeighted score
[3,1][2,5]=3×2+(1)×5=65=1[3,-1]\cdot[2,5] = 3\times2 + (-1)\times5 = 6 - 5 = 1

Read the first weight as adding three copies of the first feature and the second weight as subtracting one copy of the second feature. The positive part contributes 6; the negative part removes 5; the final scalar score is 1.

Sign and Alignment

The dot product also carries geometric meaning.

  • A positive dot product suggests the vectors point in broadly similar directions.
  • A zero dot product means the vectors are perpendicular in the geometric reading.
  • A negative dot product suggests the vectors point in opposing directions.

We will make this more precise when we study angles and cosine similarity. For now, keep the rough idea: dot products can behave like alignment scores.

MATH-C02-T06-003Exercise: Recognize perpendicular vectors

Compute the dot product of a=[1,0]a = [1, 0] and b=[0,5]b = [0, 5].

Compute it first, then check your number.

HintUse matching products

Compute 1×0+0×51 \times 0 + 0 \times 5.

SolutionZero dot product

Multiply matching coordinates:

[1,0][0,5]=1×0+0×5=0[1, 0] \cdot [0, 5] = 1 \times 0 + 0 \times 5 = 0

The first vector is purely horizontal. The second vector is purely vertical. Neither vector has any component in the other's direction, so the dot product is zero.

Code Mirror

In NumPy, the code mirrors the formula:

import numpy as np

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

score = np.dot(a, b)
print(score)

The output is:

26
MATH-C02-T06-004Exercise: Trace a score

Let w=[2,1]w = [2, -1] and x=[3,4]x = [3, 4]. What is wxw \cdot x?

Compute it first, then check your number.

HintWeighted sum

Compute 2×3+(1)×42 \times 3 + (-1) \times 4.

SolutionScore computation

Keep the sign attached to the second weight:

[2,1][3,4]=2×3+(1)×4=64=2[2, -1] \cdot [3, 4] = 2 \times 3 + (-1) \times 4 = 6 - 4 = 2

The first feature contributes 6. The second feature is penalized by the negative weight, so it subtracts 4. The dot product collapses those contributions into one scalar score.

The dot product is not only arithmetic. It is also the start of measuring how two vectors point together. Next, we use that idea to define length and distance.