Linear Algebra

Dot Product & Vector Norms: Similarity and Distance

Introduction

At the heart are two fundamental operations: Dot Product (alignment) and Norms (magnitude).

Every neural network prediction, recommender system suggestion, and regularization penalty uses these operations. Together, they form the foundation of how machines understand similarity, distance, and direction in high-dimensional space.

Why These Matter

Understanding dot products and norms is crucial for modern ML architectures. Matrix multiplication is just batched dot products. Transformer attention computes similarity via scaled dot products. Regularization techniques like Ridge and Lasso use different norms to constrain model complexity.

The Dot Product

The dot product (also called inner product or scalar product) takes two vectors of equal length and returns a single scalar. It is the most fundamental operation in linear algebra.

\mathbf{a} \cdot \mathbf{b} = \sum_{i = 1}^{n}a_{i}b_{i}

Multiply corresponding elements, then sum.

Algorithm

a = [1, 3] b = [4, -2] a · b = (1)(4) + (3)(-2) = -2

Matrix Notation

\mathbf{a}^{T}\mathbf{b}

Row vector × Column vector. This generalizes to specific rows and columns in larger matrices.

Geometric Interpretation

The dot product measures alignment between vectors. It bridges algebra and geometry.

\mathbf{a} \cdot \mathbf{b} = \mid\mid\mathbf{a}\mid\mid \cdot \mid\mid\mathbf{b}\mid\mid\cos(\theta)

Product of magnitudes times the cosine of the angle between them.

Positive

Vectors point in roughly the same direction (\theta < 90{^\circ}

Zero

Vectors are orthogonal (perpendicular) (\theta = 90{^\circ}

Negative

Vectors point in opposite directions (\theta > 90{^\circ}

The Projection View

Think of it as casting a shadow of vector a onto b. The dot product tells you: "How much of a goes in the direction of b?". This "projection" concept is why dot products measure similarity.

Properties of the Dot Product

Commutative

\mathbf{a} \cdot \mathbf{b} = \mathbf{b} \cdot \mathbf{a}

Order does not matter.

Distributive

\mathbf{a} \cdot (\mathbf{b} + \mathbf{c}) = \mathbf{a} \cdot \mathbf{b} + \mathbf{a} \cdot \mathbf{c}

Distributes over vector addition.

Self Dot Product

\mathbf{a} \cdot \mathbf{a} = \mid\mid\mathbf{a}\mid\mid^{2}

Dotting a vector with itself gives the squared norm (magnitude).

Interactive: Dot Product

Drag the sliders to change vectors. See how the dot product relates to angle, cosine, and projection.

Dot Product

Lock

Tilt

Cross

Split

|a|3|b|3θ45°

Vector Norms

A norm measures the "size" or "magnitude" of a vector. Different norms measure size in different ways, each with unique geometric interpretations and ML applications.

L2 Norm (Euclidean)

\mid\mid\mathbf{x}\mid\mid_{2} = \sqrt{\sum x_{i}^{2}}

Straight-line distance. Used in Ridge Regression, KNN.

L1 Norm (Manhattan)

\mid\mid\mathbf{x}\mid\mid_{1} = \sum\mid x_{i}\mid

Sum of absolute values (taxi-cab distance). Promotes sparsity (Lasso).

L-infinity Norm (Max)

\mid\mid\mathbf{x}\mid\mid_{\infty} = \max\mid x_{i}\mid

Largest single element. Used in adversarial robustness.

Properties of Norms

For a function to be a valid norm, it must satisfy three axioms:

The p-Norm Family

All common norms are special cases of the generalized p-norm (where p \geq 1

\mid\mid\mathbf{x}\mid\mid_{p} = \left( \sum_{i = 1}^{n}\mid x_{i}\mid^{p} \right)^{1/p}

p = 1

L1 Norm (Manhattan)

p = 2

L2 Norm (Euclidean)

p = 3,4,\ldots

Higher-order norms

\left. p\rightarrow\infty \right.

L-infinity (Max)

Interactive: Unit Balls

The "unit ball" (vectors with norm = 1) has a different shape for each p-norm. This shape explains why L1 regularization promotes sparsity (sharp corners on axes).

Vector Norms & Unit Balls

Visualizing \parallel x\parallel_{p} = 1 for different p-norms.

Select Norm

L1

L2

L∞

Vector Components

X Coordinate 3

Y Coordinate 4

L2 Norm (Euclidean)

Points with constant sum of squares form a circle. Measures straight-line distance.

Grid: 1 unit steps

L1

7.00

L2

5.00

L∞

4.00

Normalization (Unit Vectors)

Normalization converts a vector to a unit vector (norm = 1) pointing in the same direction, isolating direction from magnitude.

\hat{\mathbf{x}} = \frac{\mathbf{x}}{\mid\mid\mathbf{x}\mid\mid}

Divide by the norm.

Applications

ML Applications

Regularization

Penalizing the norm of weights prevents overfitting.

Attention Mechanism

Transformer attention is a scaled dot product. It calculates relevance scores between tokens.

Distance Metrics

KNN and K-Means rely on L2 distance \mid\mid\mathbf{x} - \mathbf{y}\mid\mid_{2} Using different norms here changes the algorithm's behavior drastically.

Computational Considerations

Time Complexity

Dot Product (n-dim)O(n)

Norm CalculationO(n)

Hardware

GPUs optimize these O(n) ops via massive parallelism (SIMD). Always use vectorized libraries (PyTorch/NumPy), never loops.