Skip to main content

MATH 345: Linear Algebra and Optimization

Section B.5 Dot Products, Norms, Distances, and Cosine Similarity

This section helps you read short NumPy expressions for dot products, lengths, distances, and direction-based comparisons.
u = np.array([3.0, 4.0])
v = np.array([4.0, 3.0])

dot = u @ v
norm_u = np.linalg.norm(u)
distance = np.linalg.norm(u - v)
cosine = (u @ v) / (np.linalg.norm(u) * np.linalg.norm(v))

Dot product.

u @ v
Read as. The dot product \(u\cdot v\text{.}\)
Shape/return. A scalar.
Used for. Alignment or score.

Norm.

np.linalg.norm(u)
Read as. The norm \(\|u\|\text{.}\)
Shape/return. A scalar.
Used for. Length.

Row-wise norms.

np.linalg.norm(X, axis=1)
Read as. One norm for each row of X.
Shape/return. A one-dimensional array with one entry per row.
Used for. Row-wise cosine similarities, such as document-ranking scores.

Euclidean distance.

np.linalg.norm(u - v)
Read as. The Euclidean distance between \(u\) and \(v\text{.}\)
Shape/return. A scalar.
Used for. Distance between vectors.

Cosine similarity.

(u @ v) / (...)
Read as. The cosine similarity \(\frac{u\cdot v}{\|u\|\|v\|}\text{.}\)
Shape/return. A scalar.
Used for. Direction-based comparison.

Warning B.5.1. Cosine similarity is not distance.

Cosine similarity compares direction after normalization. It is not the same as Euclidean distance.