Lesson 3 of 18

Dot Product

The Dot Product

The dot product of two vectors multiplies corresponding elements and sums the results:

mathbfacdotmathbfb=a0b0+a1b1+a2b2+cdots=sumi=0n1aibimathbf{a} cdot mathbf{b} = a_0 b_0 + a_1 b_1 + a_2 b_2 + cdots = sum_{i=0}^{n-1} a_i b_i

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

result = sum(x * y for x, y in zip(a, b))
# 1·4 + 2·5 + 3·6 = 4 + 10 + 18 = 32
print(result)

Geometric Meaning

Vert cdot lVert mathbf{b} Vert cdot cos( heta)$$ Where $ heta$ is the angle between the vectors. - If $mathbf{a} cdot mathbf{b} = 0$ → vectors are **orthogonal** (perpendicular, 90°) - If $mathbf{a} cdot mathbf{b} > 0$ → vectors point in similar directions ($ heta < 90°$) - If $mathbf{a} cdot mathbf{b} < 0$ → vectors point in opposite directions ($ heta > 90°$) ```python # Orthogonal vectors — dot product is 0 x_axis = [1, 0] y_axis = [0, 1] print(sum(x*y for x, y in zip(x_axis, y_axis))) # 0 ``` ### Applications - **Projection** — how much of one vector lies along another - **Similarity** — used in cosine similarity for ML and recommendation systems - **Matrix multiplication** — each output entry is a dot product ### Your Task Implement `dot_product(a, b)` that returns the dot product of two vectors.
Pyodide loading...
Loading...
Click "Run" to execute your code.