Chapter 1 · PDF Pages 1-33

Vectors, Norms, and the Cost of Doing AI

The whole book is also in the PDF.

This chapter builds upon our foundational understanding of vector spaces to introduce the tools necessary for measuring complexity, evaluating data and comparing algorithms operating on vectors. We begin by formally defining vectors and their fundamental operations, paying close attention to the computational cost of each operation using Big-O notation. We then introduce the concept of a function mapping vectors to scalars, which forms the basis for all cost, loss, and objective functions in AI. Finally, we develop the crucial geometric concepts of norms and distances. These tools allow us to quantify the magnitude of vectors, the distance between them, and the angle that separates them, leading to a geometric intuition for optimization. We culminate with proofs of the fundamental Cauchy-Schwarz and triangle inequalities, which underpin much of modern machine learning theory.

Keywords. Vectors | Inner Product | Linear Functions | Norms | Distance | Cauchy-Schwarz Inequality

1.1 Notation

Throughout these notes and our lectures, we will adhere to a consistent system of mathematical notation to ensure clarity. Scalars, which are single numerical quantities, will be denoted by lowercase or uppercase plain letters, either from the Latin alphabet (e.g., \(a, b, C\)) or the Greek alphabet (e.g., \(\alpha, \beta, \gamma, \Delta\)). Whether a letter represents a variable or a constant will be clear from the context. Vectors, which are ordered lists of scalars, will be represented exclusively by boldface lowercase letters (e.g., \(\mathbf{x}, \mathbf{y}, \mathbf{a}\)).

Matrices, which are two-dimensional arrays of scalars, will be denoted by boldface uppercase letters (e.g., \(\mathbf{A}, \mathbf{B}, \mathbf{X}\)).1 A matrix with \(m\) rows and \(n\) columns is an element of \(\mathbb{R}^{m \times n}\). The scalar entry in the \(i\)-th row and \(j\)-th column of a matrix \(\mathbf{A}\) is denoted by \(A_{ij}\) or \((\mathbf{A})_{ij}\). Sets will be represented by uppercase calligraphic letters (e.g., \(\mathcal{S}, \mathcal{C}\)). We write \(\log\) for the natural logarithm (base \(e\)), which some texts write as \(\ln\). Functions are typically denoted by lowercase letters such as \(f\) or \(g\), and the distinction between a scalar function \(f(x)\) and a vector function \(f(\mathbf{x})\) will be clear from the type of its argument. This notational framework is designed to make the type of every mathematical object –scalar, vector, matrix, or set– immediately apparent from its typography.

1.2 A Prelude on Computational Cost

In the study of modern AI, an algorithm’s correctness is only half the story. The other, equally important half is its efficiency. The models we design and study in AI have billions (if not trillions) of parameters, are often trained on datasets with millions or billions of entries, and an algorithm’s feasibility is determined by how its computational requirements scale with the size of the problem. Before we define our first mathematical objects, we must first establish a formal language for discussing this cost.

1.2.1 FLOPs: The Unit of Work

The fundamental operations in numerical algorithms are arithmetic. A floating-point operation, or FLOP, is a single arithmetic operation on floating-point numbers (the standard computer representation for real numbers). This includes addition, subtraction, multiplication, and division.

When we analyze an algorithm, our primary goal is to count the total number of FLOPs it requires to complete. This count serves as a useful, hardware-independent proxy for the algorithm’s runtime. While the actual time an operation takes can depend on the specific processor, memory access patterns, and other low-level factors, the FLOP count provides a standardized way to compare the intrinsic computational cost of different methods.

1.2.2 Big-O Notation: The Language of Scaling

Counting the exact number of FLOPs for every operation can be tedious. For an operation requiring \(3p^2 - 10p + 5\) FLOPs on an input of size \(p\), the \(10p\) and \(5\) terms become negligible as \(p\) grows large. The constant factor of \(3\) is also less important than the fact that the cost grows quadratically with \(p\). Big-O notation is the formal language we use to capture this dominant, long-term scaling behavior.

Definition 1.1 (Big-O Notation). Let \(f(n)\) and \(g(n)\) be two functions defined on the positive integers. We say that \(f(n)\) is of the order of \(g(n)\), written as \(f(n) = O(g(n))\), if there exist a positive constant \(M\) and a positive integer \(n_0\) such that:

\[ |f(n)| \le M \cdot |g(n)| \quad \text{for all } n \ge n_0. \]

In essence, Big-O notation provides an asymptotic upper bound on the growth rate of a function. It tells us how the cost of an algorithm behaves as the input size \(n\) approaches infinity, while ignoring constant factors and lower-order terms.

1.2.3 Big-O in Theory vs. Practice

Students with a background in Algorithmic Thinking, particularly in algorithms on graphs, may be accustomed to a specific use of Big-O notation that differs subtly from its application in numerical analysis and optimization.

  • The Computer Science Algorithmic Perspective: In an algorithms course, the input size \(n\) typically refers to a discrete quantity, such as the number of vertices (\(|\mathcal{V}|\)) or edges (\(|\mathcal{E}|\)) in a graph. The operations being counted are often comparisons, swaps, or pointer dereferences. The analysis focuses on worst-case, average-case, or best-case scenarios, and the distinction between complexity classes like \(O(n \log n)\) and \(O(n^2)\) is paramount.
  • The Numerical Analysis Perspective (Our Focus): In this course, the input size, which we will often denote with \(p\), \(n\), or \(m\), almost always refers to the dimension of vectors or matrices. The fundamental operations we count are FLOPs. While the asymptotic growth rate is still key, the constant factors hidden by Big-O notation can have a significant real-world impact. An algorithm with a cost of \(2p^3\) is technically the same \(O(p^3)\) as one costing \(100p^3\), but the former will run 50 times faster. Therefore, while we use Big-O for classification, when we can, we will be more precise in our FLOP counts to better understand practical performance.
Code session · Section 1.2.3 Skip to Section 1.2.4

Small vectors: constants beat Big-O

Both functions do O(p) work. NumPy pays a fixed start-up cost on every call; the loop pays about 26 nanoseconds per entry. The chart shows which one wins at each size.

show setup
import timeit
import numpy as np
1def inner_product_loop(x, y):
2    result = x[0] * y[0]
3    for i in range(1, len(x)):
4        result += x[i] * y[i]
5    return result
6
7for p in [1, 2, 4, 8, 16, 32, 64, 128]:
8    x_list = [float(i) for i in range(p)]
9    y_list = [float(i) for i in range(p)]
10    x_np, y_np = np.array(x_list), np.array(y_list)
11    t_loop = min(timeit.repeat(lambda: inner_product_loop(x_list, y_list), number=10000, repeat=5)) / 10000
12    t_numpy = min(timeit.repeat(lambda: np.dot(x_np, y_np), number=10000, repeat=5)) / 10000
13    print(f"p = {p:>4}   loop {t_loop * 1e9:6.0f} ns   NumPy {t_numpy * 1e9:6.0f} ns")
Recorded output
p =    1   loop    118 ns   NumPy    390 ns
p =    2   loop    144 ns   NumPy    399 ns
p =    4   loop    196 ns   NumPy    397 ns
p =    8   loop    304 ns   NumPy    401 ns
p =   16   loop    507 ns   NumPy    403 ns
p =   32   loop    935 ns   NumPy    405 ns
p =   64   loop   1805 ns   NumPy    408 ns
p =  128   loop   3466 ns   NumPy    410 ns
100 ns 200 ns 400 ns 1,000 ns 2,000 ns 4,000 ns 1 2 4 8 16 32 64 128 vector length p (log scale) crossover Python loop NumPy

Time for one inner product against p. NumPy stays near 400 ns at every size: almost all of it is start-up cost. The loop grows by about 26 ns per entry, so the loop wins up to p = 8 and loses from p = 16 on. Big-O hides exactly these constants.

Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-11-30; each time is the best of 5 runs of 10,000 calls.

1.2.4 Conventions for this Course

To maintain clarity and consistency, we will adhere to the following conventions when analyzing computational complexity:

  1. FLOPs are our currency. Our primary measure of cost will be the total number of floating-point operations.
  2. Addition and Multiplication have equal cost. A student may rightly wonder if an addition is as costly as a multiplication on a real CPU. While there can be differences in clock cycles, for the purposes of algorithmic analysis, they are on the same order of magnitude: they are considered as constant operations (not equal but still constants). In other words, whether you add \(1 + 1\) or multiply \(345 \times 1,900\) is the same operation as additions and multiplications between floating point scalars. Therefore, we will make the standard simplifying assumption that one addition, one subtraction, one multiplication, or one division each counts as one FLOP.
  3. Other operations are constant-cost. More complex operations like square roots (\(\sqrt{x}\)), exponentials (\(e^x\)), or trigonometric functions are also treated as costing a small, constant number of FLOPs, and thus do not change the overall Big-O complexity.
  4. Focus on the dominant term. The goal of our analysis is to understand how an algorithm scales. Therefore, an operation with a true cost of \(2p^3 + 10p^2 - 5p\) will be simplified to have a complexity of \(O(p^3)\).

By following these conventions, we can develop a clear framework for comparing the efficiency of the optimization algorithms that form the engine of modern artificial intelligence.

1.3 Vectors: The Atoms of AI

A vector is the fundamental data structure in AI, representing everything from a user’s preferences to the pixels of an image or the weights of a neural network.

1.3.1 Definition and Notation

A vector is an ordered, finite list of numbers. For our purposes, these numbers (called scalars) will be from the field of real numbers, \(\mathbb{R}\). A vector of size \(p\), also called a \(p\)-vector, is an element of the vector space \(\mathbb{R}^p\). We denote a vector, by convention, as a column:

\[ \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_p \end{bmatrix} \in \mathbb{R}^p. \]

The \(i\)-th element, or component, of \(\mathbf{x}\) is denoted by \(x_i\).

Example 1.1 (A Concrete Vector). The vector \(\mathbf{v} = \begin{bmatrix} -1.1 \\ 0.0 \\ 3.6 \\ -7.2 \end{bmatrix}\) is a 4-vector, so \(\mathbf{v} \in \mathbb{R}^4\). Its size is 4, and its third component is \(v_3 = 3.6\).

1.3.2 Special Vectors

Certain vectors are so common that they have special names and notation.

  • Zero Vector (\(\mathbf{0}\)): A vector with all its elements equal to zero.
  • Ones Vector (\(\mathbf{1}\)): A vector with all elements equal to one.
  • Standard Unit Vector (\(\mathbf{e}_i\)): A vector with a one in the \(i\)-th position and zeros everywhere else.
  • Sparse Vector: A vector where most of its elements are zero.
Figure 1.1: An illustration of special vectors in a 3-dimensional space (R³).
Figure 1.1: An illustration of special vectors in a 3-dimensional space (\(\mathbb{R}^3\)). The zero vector (\(\mathbf{0}\)) is the origin. The standard unit vectors (\(\mathbf{e}_1, \mathbf{e}_2, \mathbf{e}_3\), in red, blue, and orange) are vectors of length 1 along each axis. The ones vector (\(\mathbf{1}\), in purple) points to the coordinate \((1,1,1)\). The vector \(\mathbf{s}\) (in teal) is an example of a sparse vector that is not a unit vector, as two of its three components are zero.

Example 1.2 (Special Vectors in \(\mathbb{R}^3\)). For the vector space \(\mathbb{R}^3\), the key special vectors are:

\[ \mathbf{0} = \begin{bmatrix} 0 \\ 0 \\ 0 \end{bmatrix}, \quad \mathbf{1} = \begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}, \quad \mathbf{e}_1 = \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}, \quad \mathbf{e}_2 = \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix}, \quad \mathbf{e}_3 = \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}. \]

The vector \(\mathbf{e}_2\) is sparse, as its number of non-zero elements is \(\text{nnz}(\mathbf{e}_2)=1\), which is much smaller than its dimension as \(p\) increases (here, \(p = 3\)).

1.3.3 Fundamental Vector Operations and Their Complexity

1.3.4 Vector Addition and Subtraction

Given \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\), their sum \(\mathbf{z} = \mathbf{x} + \mathbf{y}\) is defined element-wise: \(z_i = x_i + y_i\).

  • Properties: Commutative (\(\mathbf{x}+\mathbf{y} = \mathbf{y}+\mathbf{x}\)) and associative.
  • Complexity: Requires \(p\) additions. Complexity is \(O(p)\).

Example 1.3 (Vector Addition). Let \(\mathbf{x} = \begin{bmatrix} 1 \\ -2 \\ 5 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 3 \\ 1 \\ -3 \end{bmatrix}\). Then their sum is:

\[ \mathbf{x} + \mathbf{y} = \begin{bmatrix} 1+3 \\ -2+1 \\ 5+(-3) \end{bmatrix} = \begin{bmatrix} 4 \\ -1 \\ 2 \end{bmatrix}. \]

This operation requires 3 additions, consistent with \(O(p)\) complexity for \(p=3\).

Figure 1.2: A geometric illustration of vector addition in R³.
Figure 1.2: A geometric illustration of vector addition in \(\mathbb{R}^3\). The blue vector \(\mathbf{x} = [1, -2, 5]^\top\) and the red vector \(\mathbf{y} = [3, 1, -3]^\top\) are added together, with their positions clarified by the axis ticks. By placing a copy of \(\mathbf{y}\) (dashed red) at the head of \(\mathbf{x}\), we arrive at the endpoint of the sum vector \(\mathbf{x}+\mathbf{y} = [4, -1, 2]^\top\) (in purple). This demonstrates the “head-to-tail” rule, which forms a parallelogram in space.

1.3.5 Scalar-Vector Multiplication

Given \(\alpha \in \mathbb{R}\) and \(\mathbf{x} \in \mathbb{R}^p\), their product \(\mathbf{z} = \alpha\mathbf{x}\) is defined as \(z_i = \alpha x_i\).

  • Properties: Associative and distributive.
  • Complexity: Requires \(p\) multiplications. Complexity is \(O(p)\).

Example 1.4 (Scalar-Vector Multiplication). Let \(\alpha = -2\) and \(\mathbf{x} = \begin{bmatrix} 1 \\ -2 \\ 5 \end{bmatrix}\). Then:

\[ \alpha\mathbf{x} = -2 \begin{bmatrix} 1 \\ -2 \\ 5 \end{bmatrix} = \begin{bmatrix} -2 \cdot 1 \\ -2 \cdot (-2) \\ -2 \cdot 5 \end{bmatrix} = \begin{bmatrix} -2 \\ 4 \\ -10 \end{bmatrix}. \]

Geometrically, multiplying by a negative scalar reverses the vector’s direction and scales its length.

Figure 1.3: A geometric illustration of scalar-vector multiplication in R³.
Figure 1.3: A geometric illustration of scalar-vector multiplication in \(\mathbb{R}^3\). The original vector \(\mathbf{x} = [1, -2, 5]^\top\) (blue) is multiplied by the scalar \(\alpha = -2\). The resulting vector \(\alpha\mathbf{x} = [-2, 4, -10]^\top\) (teal) points in the exact opposite direction and is twice as long. The dashed gray line highlights that both vectors are collinear, lying on the same line through the origin.

1.3.6 Linear Combination

A linear combination of vectors \(\mathbf{x}_1, \dots, \mathbf{x}_k \in \mathbb{R}^p\) with scalars \(\alpha_1, \dots, \alpha_k \in \mathbb{R}\) is the vector \(\mathbf{y} = \sum_{i=1}^k \alpha_i \mathbf{x}_i\).

  • Complexity: Requires \(k\) scalar-vector multiplications (\(kp\) flops) and \(k-1\) vector additions (\((k-1)p\) flops). Total complexity is \(O(kp)\).

Example 1.5 (Linear Combination). Express the vector \(\mathbf{y} = \begin{bmatrix} 5 \\ 4 \end{bmatrix}\) as a linear combination of \(\mathbf{x}_1 = \begin{bmatrix} 2 \\ 0 \end{bmatrix}\) and \(\mathbf{x}_2 = \begin{bmatrix} -1 \\ 2 \end{bmatrix}\). We seek scalars \(\alpha_1, \alpha_2\) such that \(\alpha_1\mathbf{x}_1 + \alpha_2\mathbf{x}_2 = \mathbf{y}\). This yields a system of linear equations:

\[ \begin{aligned} 2\alpha_1 - \alpha_2 &= 5 \\ 2\alpha_2 &= 4 \end{aligned} \]

Solving gives \(\alpha_2=2\) and \(\alpha_1=3.5\). Thus, \(\mathbf{y} = 3.5\mathbf{x}_1 + 2\mathbf{x}_2\).

Figure 1.4: Step 1: The initial basis vectors.
Figure 1.4: Step 1: The initial basis vectors.
Figure 1.5: Step 2: Scaling the basis vectors.
Figure 1.5: Step 2: Scaling the basis vectors.
Figure 1.6: Step 3: Adding the scaled vectors.
Figure 1.6: Step 3: Adding the scaled vectors.
Figure 1.7: A step-by-step visualization of forming the linear combination \(\mathbf{y} = 3.5\mathbf{x}_1 + 2\mathbf{x}_2\). All three panels share one coordinate frame so the vectors can be compared directly. (Step 1) We start with the basis vectors \(\mathbf{x}_1\) and \(\mathbf{x}_2\). (Step 2) We scale each vector by its corresponding scalar coefficient. (Step 3) We perform head-to-tail addition of the scaled vectors to construct the final target vector \(\mathbf{y} = [5, 4]^\top\).

Example 1.6 (An Unreachable Vector (Linear Combination Failure)). Now, consider a case in 3D. Can we express the vector \(\mathbf{y} = \begin{bmatrix} 0 \\ 2 \\ -1 \end{bmatrix}\) as a linear combination of the basis vectors \(\mathbf{x}_1 = \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}\) and \(\mathbf{x}_2 = \begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix}\)?

Algebraic Approach: We seek scalars \(\alpha_1, \alpha_2\) such that \(\alpha_1\mathbf{x}_1 + \alpha_2\mathbf{x}_2 = \mathbf{y}\). Let’s write out the vector equation:

\[ \alpha_1\begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix} + \alpha_2\begin{bmatrix} 0 \\ 1 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 2 \\ -1 \end{bmatrix} \]

Combining the terms on the left side gives:

\[ \begin{bmatrix} \alpha_1 \\ \alpha_2 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 2 \\ -1 \end{bmatrix} \]

For these vectors to be equal, each component must be equal. This gives us a system of three equations:

\[ \begin{aligned} \alpha_1 &= 0 \\ \alpha_2 &= 2 \\ 0 &= -1 \quad \text{\textbf{(Contradiction!)}} \end{aligned} \]

The third equation, \(0 = -1\), is a mathematical contradiction. This means there are no scalars \(\alpha_1, \alpha_2\) that can satisfy the original equation. Therefore, \(\mathbf{y}\) cannot be expressed as a linear combination of \(\mathbf{x}_1\) and \(\mathbf{x}_2\).

Geometric Interpretation: The vectors \(\mathbf{x}_1\) and \(\mathbf{x}_2\) are the standard unit vectors for the first and second axes. The set of all their possible linear combinations, their span, forms the entire \(x_1\)/\(x_2\) plane. Any vector created by combining \(\mathbf{x}_1\) and \(\mathbf{x}_2\) must lie within this plane; its third component will always be zero.

Our target vector, \(\mathbf{y} = \begin{bmatrix} 0 \\ 2 \\ -1 \end{bmatrix}\), has a non-zero third component (\(y_3 = -1\)). This means it lies outside the \(x_1/x_2\) plane. It is geometrically impossible to reach a point outside of a plane using only vectors that are confined to that plane.

Figure 1.8: A step-by-step visualization of why the vector y=[0,2,-1]ᵀ cannot be represented as a linear combination of x₁=[1,0,0]ᵀ and x₂=[0,1,0]ᵀ.
Figure 1.8: A step-by-step visualization of why the vector \(\mathbf{y}=[0,2,-1]^\top\) cannot be represented as a linear combination of \(\mathbf{x}_1=[1,0,0]^\top\) and \(\mathbf{x}_2=[0,1,0]^\top\). Left: the span of \(\mathbf{x}_1\) and \(\mathbf{x}_2\) is the shaded \(x_1\)–\(x_2\) plane. Middle: every linear combination \(\mathbf{z}\) stays inside that plane. Right: \(\mathbf{y}\) has a nonzero third component, so it sits off the plane and only its projection \(\mathbf{y}_{\text{proj}}\) is reachable.

1.3.7 The Inner Product: The Crux of AI Computation

If vectors are the atoms of AI, the inner product is the fundamental force that governs their interactions. At its core, the inner product is a simple computation, yet it is arguably one of the most important operations in all of AI/ML. It provides a formal, quantitative answer to the qualitative question: “How related are these two vectors?” This question of relation, similarity, or alignment is the crux of countless AI tasks.

Whether we are predicting a house price, recommending a movie, or translating a sentence, the underlying mechanism often boils down to a massive number of inner product calculations. Mastering its algebraic properties and geometric intuition is just the first small step towards fundamental AI.

1.3.8 Formal Definition and Complexity

The inner product (or dot product) of two vectors \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\) is a scalar defined as:

\[ \langle \mathbf{x}, \mathbf{y} \rangle = \mathbf{x}^\top\mathbf{y} = \sum_{i=1}^p x_i y_i. \]
  • Properties: The inner product is commutative (\(\langle \mathbf{x}, \mathbf{y} \rangle = \langle \mathbf{y}, \mathbf{x} \rangle\)), distributive (\(\langle \mathbf{x}, \mathbf{y}+\mathbf{z} \rangle = \langle \mathbf{x}, \mathbf{y} \rangle + \langle \mathbf{x}, \mathbf{z} \rangle\)), and bilinear (\(\langle \alpha\mathbf{x}, \beta\mathbf{y} \rangle = \alpha\beta\langle \mathbf{x}, \mathbf{y} \rangle\)).
  • Complexity: The computation requires \(p\) multiplications and \(p-1\) additions, for a total of \(2p-1\) flops. Its complexity is therefore \(O(p)\).

Example 1.7 (Inner Product Calculation). Let \(\mathbf{x} = \begin{bmatrix} 1 \\ -2 \\ 5 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 3 \\ 1 \\ -3 \end{bmatrix}\). Their inner product is:

\[ \langle \mathbf{x}, \mathbf{y} \rangle = (1)(3) + (-2)(1) + (5)(-3) = 3 - 2 - 15 = -14. \]

Since the inner product is negative, we can infer that the angle between these vectors is obtuse (\(> 90^\circ\)).

Code session · Section 1.3.8 Skip to Section 1.3.9

Loop versus NumPy: same O(p), very different speed

Both functions compute the inner product of Section 1.3.8 with O(p) work. The loop runs one Python step per term; NumPy runs compiled code over the whole array.

show setup
import timeit
import numpy as np
1def inner_product_loop(x, y):
2    result = x[0] * y[0]
3    for i in range(1, len(x)):
4        result += x[i] * y[i]
5    return result
6
7for p in [10**3, 10**4, 10**5, 10**6, 10**7]:
8    x_list = [float(i) for i in range(p)]
9    y_list = [float(i) for i in range(p)]
10    x_np, y_np = np.array(x_list), np.array(y_list)
11    t_loop = min(timeit.repeat(lambda: inner_product_loop(x_list, y_list), number=1, repeat=5))
12    t_numpy = min(timeit.repeat(lambda: np.dot(x_np, y_np), number=1, repeat=5))
13    print(f"p = {p:>10,}   loop {t_loop:.2e} s   NumPy {t_numpy:.2e} s   {t_loop / t_numpy:,.0f}x")
Recorded output
p =      1,000   loop 3.10e-05 s   NumPy 6.25e-07 s   50x
p =     10,000   loop 3.31e-04 s   NumPy 2.67e-06 s   124x
p =    100,000   loop 3.47e-03 s   NumPy 2.10e-05 s   165x
p =  1,000,000   loop 3.44e-02 s   NumPy 3.10e-04 s   111x
p = 10,000,000   loop 3.45e-01 s   NumPy 2.62e-03 s   132x
10⁻⁷ s 10⁻⁶ s 10⁻⁵ s 10⁻⁴ s 10⁻³ s 10⁻² s 10⁻¹ s 1 s 10³ 10⁴ 10⁵ 10⁶ 10⁷ vector length p (log scale) 132x Python loop NumPy

Time for one inner product, on log scales. The loop's time grows tenfold with each tenfold step in p, as O(p) predicts. NumPy grows about as fast once p passes 10,000; below that, a fixed start-up cost dominates. At ten million entries, NumPy is 132 times faster: the same O(p), with a much smaller constant.

Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-11-29; each time is the best of 5 runs.

1.3.9 The Inner Product at the Heart of Modern AI

Let’s see how this simple \(O(p)\) operation powers some of the most advanced AI systems.

Example 1.8 (Linear Regression: Weighted Sums for Prediction). In a linear regression model (not our job to define it explicitly but it will be introduced later on), we predict an outcome (e.g., the price of a house, \(y\)) based on a vector of its features (e.g., area, number of bedrooms, etc, represented as a vector \(\mathbf{x} \in \mathbb{R}^p\)). The model learns a weight vector, \(\mathbf{w} \in \mathbb{R}^p\), where each weight signifies the importance of a feature. The prediction, \(\hat{y}\), is simply the inner product of the weights and the features:

\[ \hat{y} = \mathbf{w}^\top\mathbf{x} = w_1x_1 + w_2x_2 + \dots + w_px_p. \]

The inner product acts as a “scoring” mechanism, where features that align with large positive weights increase the score, and those that align with large negative weights decrease it.

Figure 1.9: The inner product in action across different AI models.
Figure 1.9: The inner product in action across different AI models. (a) In linear regression, the inner product \(\mathbf{w}^\top\mathbf{x}\) projects a feature vector onto the weight vector to produce a prediction \(\hat y\). (b) For recommender systems, the small angle between the user vector \(\mathbf{p}\) and the movie vector \(\mathbf{q}_1\) means a high inner product, and so a high predicted rating; the movie \(\mathbf{q}_2\) at a large angle scores low. (c) In attention, the high alignment between a query \(\mathbf{q}\) and a key \(\mathbf{k}_1\) signals high relevance between two words in a sentence, while \(\mathbf{k}_2\) is nearly orthogonal and so nearly irrelevant.

Example 1.9 (Matrix Factorization: Recommendations). How does Netflix know you’ll like a certain movie? It might use matrix factorization. The system learns a short vector of latent features for every user (\(\mathbf{p}_u\)) and every movie (\(\mathbf{q}_i\)). To predict the rating user \(u\) would give to movie \(i\), it simply computes their inner product (a very rough description of what really happens):

\[ \text{PredictedRating}_{ui} = \mathbf{p}_u^\top \mathbf{q}_i. \]

A high, positive inner product means the user’s “taste vector” aligns well with the movie’s “characteristic vector,” resulting in a strong recommendation.

Example 1.10 (Attention Mechanisms: Transformers and LLMs). The attention mechanism that powers models like ChatGPT relies on inner products to weigh the importance of words in a sentence. To understand a word (the “query”), the model compares it to every other word (the “keys”). This comparison is an inner product:

\[ \text{AttentionScore}(\text{query}, \text{key}_i) = \mathbf{q}^\top \mathbf{k}_i. \]

A high score means the key word is highly relevant to the query word. By computing billions or even trillions of these inner products, the model builds a rich contextual understanding of language.

1.3.10 Geometric Examples

To understand how the inner product works in those complex models, we must build our intuition from simple, geometric cases.

Example 1.11 (Inner Product with Itself: Squared Length). Let \(\mathbf{x} = \begin{bmatrix} 3 \\ 4 \end{bmatrix}\). What is the geometric meaning of its inner product with itself?

\[ \langle \mathbf{x}, \mathbf{x} \rangle = \mathbf{x}^\top\mathbf{x} = (3)(3) + (4)(4) = 9 + 16 = 25. \]

This value, 25, is the square of the vector’s Euclidean length. By the Pythagorean theorem, the length of \(\mathbf{x}\) is \(\sqrt{3^2 + 4^2} = \sqrt{25} = 5\). Thus, the inner product of a vector with itself gives its squared Euclidean length. This is a crucial concept we will formalize soon when we define norms.

Figure 1.10: The inner product of a vector x=[3,4]ᵀ with itself is ⟨ x, x ⟩ = 25.
Figure 1.10: The inner product of a vector \(\mathbf{x}=[3,4]^\top\) with itself is \(\langle \mathbf{x}, \mathbf{x} \rangle = 25\). The square root of this value corresponds to the vector’s length, as given by the Pythagorean theorem.

Example 1.12 (Inner Product of Orthogonal Vectors). Two vectors are orthogonal if their inner product is zero. Geometrically, this means they are perpendicular. Let \(\mathbf{x} = \begin{bmatrix} 2 \\ 3 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} -3 \\ 2 \end{bmatrix}\).

\[ \langle \mathbf{x}, \mathbf{y} \rangle = (2)(-3) + (3)(2) = -6 + 6 = 0. \]

Because their inner product is zero, we know the vectors form a \(90^\circ\) angle. This is a fundamental property used throughout signal processing and machine learning to identify independent or uncorrelated components.

Figure 1.11: The vectors x=[2,3]ᵀ and y=[-3,2]ᵀ are orthogonal.
Figure 1.11: The vectors \(\mathbf{x}=[2,3]^\top\) and \(\mathbf{y}=[-3,2]^\top\) are orthogonal. Their inner product is zero, corresponding to a \(90^\circ\) angle between them.

1.4 Functions of Vectors

In optimization, our goal is to minimize or maximize a function. In AI, these are often called cost, loss, or objective functions. A function \(f: \mathbb{R}^p \to \mathbb{R}\) takes a vector as input and produces a single scalar as output. This scalar tells us how “good” or “bad” the input vector is with respect to our goal.

Example 1.13 (A Simple Cost Function). Let a company’s profit be a function of its spending on digital advertising, \(x_1\), and print advertising, \(x_2\). A simple (non-linear) model could be:

\[ f(\mathbf{x}) = 1000 - (10-x_1)^2 - (5-x_2)^2. \]

Here, \(f(\begin{bmatrix} 10 \\ 5 \end{bmatrix}) = 1000\) represents the maximum profit. The function’s value decreases as spending deviates from this optimal point.

1.4.1 Linear Functions

A function \(f: \mathbb{R}^p \to \mathbb{R}\) is linear if it satisfies superposition: \(f(\alpha\mathbf{x} + \beta\mathbf{y}) = \alpha f(\mathbf{x}) + \beta f(\mathbf{y})\). Any such function can be represented as an inner product: \(f(\mathbf{x}) = \mathbf{a}^\top\mathbf{x}\) for some fixed vector \(\mathbf{a}\). Adding a constant gives an affine function, \(f(\mathbf{x}) = \mathbf{a}^\top\mathbf{x} + b\), which is linear only when \(b = 0\).

  • Complexity: Evaluating a linear function, i.e., finding its function value for a given input \(\mathbf{x}\), costs \(O(p)\).

Example 1.14 (Evaluating a Linear Function). Let a linear function be defined by the weight vector \(\mathbf{a} = \begin{bmatrix} 0.5 \\ -1 \end{bmatrix}\). Evaluate this function for the input \(\mathbf{x} = \begin{bmatrix} 10 \\ 4 \end{bmatrix}\).

\[ f(\mathbf{x}) = \mathbf{a}^\top\mathbf{x} = (0.5)(10) + (-1)(4) = 5 - 4 = 1. \]

1.4.2 Optimizing a Linear Function

From an optimization perspective, linear functions are deceptively simple. Consider the task of minimizing \(f(\mathbf{x}) = \mathbf{a}^\top\mathbf{x}\) over all \(\mathbf{x} \in \mathbb{R}^p\). Geometrically, the function \(f\) represents a tilted plane (or hyperplane in higher dimensions). The vector \(\mathbf{a}\) points in the direction of the steepest ascent of this plane. Consequently, the direction of steepest descent is \(-\mathbf{a}\).

Since the slope of this plane is constant everywhere, there is no point where the function “flattens out” to form a minimum or maximum. If we take any step in the direction \(-\mathbf{a}\), the function value will decrease. We can continue taking steps of any size in this direction indefinitely, and the function value will continue to decrease without bound. Therefore, for any non-zero \(\mathbf{a}\), the unconstrained minimum of \(f(\mathbf{x})\) is \(-\infty\), and its maximum is \(+\infty\). This is not a very interesting optimization problem.

This reveals a critical insight: for the optimization of a linear function to be meaningful, we must operate on a constrained domain. For example, minimizing \(\mathbf{a}^\top\mathbf{x}\) subject to \(\mathbf{x} \in \mathcal{C}\) (i.e., inside a range of values) is a well-posed problem. We will see examples of these in later chapters of the class.

Figure 1.12: The graph of a univariate affine function f(x)=ax+b (with a>0).
Figure 1.12: The graph of a univariate affine function \(f(x)=ax+b\) (with \(a>0\)). The function has a constant slope and never changes curvature. An unconstrained minimization will follow the slope down to \(-\infty\), while a maximization will lead to \(+\infty\).

1.4.3 Quadratic Functions: The Simplest Interesting Case

To find a meaningful minimum or maximum, we need functions that curve. The simplest such functions are quadratic. They form the basis for many foundational optimization problems, including the least-squares problem or linear regression, and serve as excellent local approximations for more complex functions. To properly define these functions, we require more material covered (e.g., norms), but we will mention them here given what we have learned thus far.

1.4.4 The Univariate Case

A univariate quadratic function has the familiar form \(f(x) = ax^2 + bx + c\), where \(a, b, c \in \mathbb{R}\) and \(a \neq 0\). From single-variable calculus, we can analyze this function completely.

  • Finding the Extremum: The function’s extremum (minimum or maximum) occurs where the derivative is zero. \[ f'(x) = 2ax + b = 0 \implies x^\star = -\frac{b}{2a}. \]
  • Nature of the Extremum: The second derivative, \(f''(x) = 2a\), tells us about the function’s curvature.
    • If \(a > 0\), the parabola opens upwards (we say, it is convex). The point \(x^\star\) is a unique global minimum.
    • If \(a < 0\), the parabola opens downwards (we say, it is concave). The point \(x^\star\) is a unique global maximum.
Figure 1.13: The two cases for a univariate quadratic function f(x)=ax²+bx+c.
Figure 1.13: The two cases for a univariate quadratic function \(f(x)=ax^2+bx+c\). On the left (\(a>0\)), the function is convex and has a unique global minimum. On the right (\(a<0\)), the function is concave and has a unique global maximum.

1.4.5 The Multivariate Case

Extending the quadratic form to multiple dimensions gives us a rich and powerful class of functions. For now, we will avoid general matrix notation and define a multivariate quadratic function using inner products. A common and useful form is:

\[ f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle, \]

where \(\mathbf{x}, \mathbf{b} \in \mathbb{R}^p\). This function is fundamental to solving systems of linear equations and least-squares problems; it is not obvious now as we obscured the use of matrices, but stay tuned! More general quadratic functions exist, which we will explore in detail once we have developed the necessary matrix tools (like positive definite matrices).

What changes when we move from one dimension to \(p\) dimensions?

  • The input \(x\) is now a vector \(\mathbf{x}\).
  • The single minimum point \(x^\star\) becomes a minimum vector \(\mathbf{x}^\star\).
  • The shape of the function is no longer a parabola but a paraboloid: a multi-dimensional “bowl”.
  • The “level sets” of the function (the sets of points where \(f(\mathbf{x})\) is constant) are ellipses or circles.

Example 1.15 (A 2D Quadratic Function). Consider the function \(f: \mathbb{R}^2 \to \mathbb{R}\) defined by

\[ f(x_1, x_2) = \frac{1}{2}(x_1^2 + x_2^2) - (3x_1 + 2x_2). \]

This can be written using inner products with \(\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}\) and \(\mathbf{b} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}\) as \(f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle\). By a process analogous to “completing the square” from single-variable algebra, we can rewrite the function to make its minimum obvious. Our goal is to manipulate the expression into a form that includes a squared term like \(\langle \mathbf{x}-\mathbf{b}, \mathbf{x}-\mathbf{b} \rangle\).

Let’s start reversely with the definition of this squared term, written as a sum of squares:

\[ \sum_{i=1}^p (x_i - b_i)^2 = \langle \mathbf{x}-\mathbf{b}, \mathbf{x}-\mathbf{b} \rangle \]

Using the distributive property of the inner product, we can expand this:

\[ \begin{aligned} \langle \mathbf{x}-\mathbf{b}, \mathbf{x}-\mathbf{b} \rangle &= \langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{x}, \mathbf{b} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle + \langle \mathbf{b}, \mathbf{b} \rangle \\ &= \langle \mathbf{x}, \mathbf{x} \rangle - 2\langle \mathbf{b}, \mathbf{x} \rangle + \langle \mathbf{b}, \mathbf{b} \rangle \end{aligned} \]

Notice that our original function, \(f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle\), contains two of the three terms from this expansion. We can now rewrite \(f(\mathbf{x})\) by adding and subtracting the missing term, \(\frac{1}{2}\langle \mathbf{b}, \mathbf{b} \rangle\):

\[ \begin{aligned} f(\mathbf{x}) &= \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle \\ &= \left( \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle + \frac{1}{2}\langle \mathbf{b}, \mathbf{b} \rangle \right) - \frac{1}{2}\langle \mathbf{b}, \mathbf{b} \rangle \\ &= \frac{1}{2} \left( \langle \mathbf{x}, \mathbf{x} \rangle - 2\langle \mathbf{b}, \mathbf{x} \rangle + \langle \mathbf{b}, \mathbf{b} \rangle \right) - \frac{1}{2}\langle \mathbf{b}, \mathbf{b} \rangle \end{aligned} \]

The expression inside the parentheses is exactly the expansion of \(\langle \mathbf{x}-\mathbf{b}, \mathbf{x}-\mathbf{b} \rangle\). Therefore, we have:

\[ f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}-\mathbf{b}, \mathbf{x}-\mathbf{b} \rangle - \frac{1}{2}\langle \mathbf{b}, \mathbf{b} \rangle \]

Written out using sums, this is:

\[ f(\mathbf{x}) = \frac{1}{2}\sum_{i=1}^p (x_i - b_i)^2 - \frac{1}{2}\sum_{i=1}^p b_i^2 \]

The second term, \(-\frac{1}{2}\sum b_i^2\), is a constant value that does not depend on our choice of \(\mathbf{x}\). To minimize \(f(\mathbf{x})\), we must therefore minimize the first term, \(\frac{1}{2}\sum (x_i - b_i)^2\). Since this is a sum of squared numbers, it is always non-negative. Its smallest possible value is 0, which occurs if and only if every term in the sum is zero, i.e., when \((x_i - b_i)^2 = 0\) for all \(i=1, \dots, p\). This happens precisely when \(x_i = b_i\) for all \(i\), or in other words, when the vector \(\mathbf{x}\) is equal to the vector \(\mathbf{b}\).

This shows that the unique global minimum of the function occurs at \(\mathbf{x}^* = \mathbf{b} = \begin{bmatrix} 3 \\ 2 \end{bmatrix}\). The function’s level sets are circles centered at this point.

Figure 1.14: A contour plot of the multivariate quadratic function f(x) = (1)/(2)⟨ x, x⟩ - ⟨ b, x⟩ with b=[3,2]ᵀ.
Figure 1.14: A contour plot of the multivariate quadratic function \(f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x}\rangle - \langle \mathbf{b}, \mathbf{x}\rangle\) with \(\mathbf{b}=[3,2]^\top\). Completing the square gives \(f(\mathbf{x}) = \frac{1}{2}\|\mathbf{x}-\mathbf{b}\|_2^2 - \frac{1}{2}\|\mathbf{b}\|_2^2\), so the level sets are concentric circles centred on \(\mathbf{b}\). The function forms a “bowl” whose bottom sits at the minimum \(\mathbf{x}^\star = \mathbf{b} = [3,2]^\top\) (dashed guides), where \(f(\mathbf{x}^\star) = -\frac{13}{2}\). Along each blue circle the function is constant, and it grows as we move outward.

1.5 Norms and Distances: Formalizing “Closeness”

Thus far, we have explored functions of vectors, such as linear and quadratic forms. While useful, these functions were defined somewhat arbitrarily. In the vast majority of AI and machine learning applications, the objective functions we wish to minimize are not arbitrary at all; they are carefully constructed measures of distance or error. The core task of “learning” is often equivalent to finding a model whose output is as “close” as possible to the true data.

But what does it mean for two high-dimensional vectors to be “close”? Our intuitive, geometric understanding of distance needs to be formalized into a rigorous mathematical tool that we can write down, analyze, and, most importantly, optimize. This tool is the norm. A norm gives us a formal way to measure the “length” or “magnitude” of a vector. Once we can measure the length of a vector, we can measure the length of the difference between two vectors, which is precisely our definition of distance.

Before we introduce the formal axioms, consider the following examples where the central task is to minimize a notion of distance:

Example 1.16 (Image Denoising). Imagine you have a corrupted, noisy image, represented as a vector \(\mathbf{y}\). You believe that the true, clean image, \(\mathbf{x}\), should be “smooth”; meaning that the values of adjacent pixels should be very close to each other. The optimization task is to find a vector \(\mathbf{x}\) that satisfies two conflicting goals simultaneously: it must be close to the noisy observation \(\mathbf{y}\), while also being internally smooth. We can phrase this as minimizing a combined distance: a term measuring the sum of squared differences between the pixels of \(\mathbf{x}\) and \(\mathbf{y}\), plus a second term measuring the sum of squared differences between adjacent pixels within \(\mathbf{x}\). The algorithm must find the best trade-off to recover the clean image.

Example 1.17 (Word Embeddings). In natural language processing, we represent words as vectors in a high-dimensional space (e.g., Word2Vec or GloVe). The goal of the learning algorithm is to arrange these vectors such that their distances reflect semantic meaning. The algorithm is trained to minimize the distance between vectors of words that appear in similar contexts (like “king” and “queen”) while simultaneously maximizing the distance between vectors of unrelated words (like “king” and “walrus”). The resulting geometry of these word vectors, governed entirely by their pairwise distances, captures a remarkable amount of meaning.

Example 1.18 (Clustering). The K-Means clustering algorithm is a classic learning method that aims to partition a set of data points (vectors) into \(K\) distinct groups. The core of the algorithm is a distance minimization problem. It tries to find a set of \(K\) “center” vectors such that the sum of the squared distances from each data point to its nearest center is as small as possible. The algorithm iteratively refines the positions of these centers to pull them closer to the data they represent, using distance as its only guide.

These examples highlight a universal principle: to make a machine learn, we must first define what a “mistake” looks like. In most cases, a mistake is a measure of distance. The formal machinery for this measurement begins with the definition of a norm (but not the only way!).

1.5.1 Norms

A function \(\|\cdot\|: \mathbb{R}^p \to \mathbb{R}\) is a norm if it provides a valid measure of a vector’s “length” or “magnitude”. To be considered a valid norm, it must satisfy four formal properties for all vectors \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\) and any scalar \(\alpha \in \mathbb{R}\):

  1. Non-negativity: \(\|\mathbf{x}\| \ge 0\).
  2. Definiteness: \(\|\mathbf{x}\| = 0\) if and only if \(\mathbf{x} = \mathbf{0}\).
  3. Positive Homogeneity: \(\|\alpha\mathbf{x}\| = |\alpha|\|\mathbf{x}\|\).
  4. Triangle Inequality: \(\|\mathbf{x}+\mathbf{y}\| \le \|\mathbf{x}\| + \|\mathbf{y}\|\).

These axioms ensure that a norm behaves in a way that is consistent with our geometric intuition of length. While many functions can satisfy these properties, three norms are overwhelmingly common in AI and machine learning.

1.5.2 The \(\ell_2\)-norm (Euclidean Norm)

The most important and intuitive norm is the Euclidean norm, which corresponds to the standard straight-line distance in space.

\[ \|\mathbf{x}\|_2 = \sqrt{\sum_{i=1}^p x_i^2} = \sqrt{\mathbf{x}^\top\mathbf{x}}. \]
  • Complexity Analysis: To compute the \(\ell_2\)-norm, we must perform:
    • \(p\) multiplications to square each element \(x_i\).
    • \(p-1\) additions to sum the squared elements.
    • 1 square root operation.

    Following our convention, we count this as approximately \(2p\) floating-point operations (FLOPs). The dominant term scales linearly with the dimension \(p\), so the complexity is \(O(p)\).

1.5.3 The \(\ell_1\)-norm (Manhattan Norm)

The \(\ell_1\)-norm measures distance by summing the absolute axial distances, much like navigating a city grid (hence the name “Manhattan”):

\[ \|\mathbf{x}\|_1 = \sum_{i=1}^p |x_i|. \]
  • Complexity Analysis: Computing the \(\ell_1\)-norm requires:
    • \(p\) absolute value operations. (We can consider this equivalent to \(p\) comparisons or a negligible cost).
    • \(p-1\) additions to sum the absolute values.

    The total number of FLOPs is approximately \(p\). Therefore, the complexity is \(O(p)\).

1.5.4 The \(\ell_\infty\)-norm (Max Norm)

The \(\ell_\infty\)-norm is defined as the maximum absolute value of any element in the vector.

\[ \|\mathbf{x}\|_\infty = \max_{i \in \{1, \dots, p\}} |x_i|. \]
  • Complexity Analysis: Computing the \(\ell_\infty\)-norm requires:
    • \(p\) absolute value operations.
    • \(p-1\) comparisons to find the maximum value among the \(p\) absolute values.

    While comparisons are not strictly FLOPs, their cost scales linearly with \(p\). In the context of algorithmic analysis, we consider this to have a complexity of \(O(p)\).

Although all three norms have the same Big-O complexity, their specific FLOP counts and the types of operations differ slightly. In practice on modern hardware, all are very fast to compute, but their mathematical properties lead to profoundly different behaviors when used in optimization objectives.

Figure 1.15: The unit ball in R² for the ℓ₂, ℓ₁, and ℓ∞ norms.
Figure 1.15: The unit ball in \(\mathbb{R}^2\) for the \(\ell_2\), \(\ell_1\), and \(\ell_\infty\) norms. Each shaded region represents the set of all vectors \(\mathbf{x}\) that satisfy \(\|\mathbf{x}\| \le 1\) for the respective norm. The shape of the unit ball reveals the geometry of the norm: the \(\ell_2\)-norm is perfectly round, the \(\ell_1\)-norm favors points on the axes (promoting sparsity), and the \(\ell_\infty\)-norm forms a square.
Figure 1.16: The unit ball in R³ for the ℓ₂, ℓ₁, and ℓ∞ norms.
Figure 1.16: The unit ball in \(\mathbb{R}^3\) for the \(\ell_2\), \(\ell_1\), and \(\ell_\infty\) norms. The 2D shapes generalize to a sphere (\(\ell_2\)), an octahedron (\(\ell_1\)), and a cube (\(\ell_\infty\)). The “pointy” corners of the \(\ell_1\) ball and the flat faces of the \(\ell_\infty\) ball are geometric properties that have significant implications when these norms are used as constraints in optimization problems.

Example 1.19 (Norm Calculation). Let \(\mathbf{x} = \begin{bmatrix} 3 \\ -4 \\ 0 \end{bmatrix}\). Let’s compute its three common norms.

  • \(\ell_2\)-norm: \(\|\mathbf{x}\|_2 = \sqrt{3^2 + (-4)^2 + 0^2} = \sqrt{9 + 16} = \sqrt{25} = 5\).
  • \(\ell_1\)-norm: \(\|\mathbf{x}\|_1 = |3| + |-4| + |0| = 3 + 4 + 0 = 7\).
  • \(\ell_\infty\)-norm: \(\|\mathbf{x}\|_\infty = \max(|3|, |-4|, |0|) = 4\).

Notice that for the same vector, these different norms give different measures of magnitude.

1.5.5 Algebraic Properties of the \(\ell_2\)-norm

The identity \(\|\mathbf{x}\|_2^2 = \langle \mathbf{x}, \mathbf{x} \rangle\) is more than just a definition; it is a bridge that allows us to move between the algebraic world of inner products and the geometric world of norms. Using this bridge, we can derive several fundamental properties that are essential for optimization.

1.5.6 Norm of a Sum and Difference

A crucial identity allows us to expand the squared norm of a sum of two vectors, analogous to expanding \((a+b)^2\) in scalar algebra.

Identity (Norm of a Sum): For any two vectors \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\),

\[ \|\mathbf{x} + \mathbf{y}\|_2^2 = \|\mathbf{x}\|_2^2 + 2\langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2. \]

Proof. We start with the left-hand side and apply the definition of the norm in terms of the inner product.

\[ \begin{aligned} \|\mathbf{x} + \mathbf{y}\|_2^2 &= \langle \mathbf{x} + \mathbf{y}, \mathbf{x} + \mathbf{y} \rangle \\ &= \langle \mathbf{x}, \mathbf{x} + \mathbf{y} \rangle + \langle \mathbf{y}, \mathbf{x} + \mathbf{y} \rangle \quad (\text{Distributivity}) \\ &= \langle \mathbf{x}, \mathbf{x} \rangle + \langle \mathbf{x}, \mathbf{y} \rangle + \langle \mathbf{y}, \mathbf{x} \rangle + \langle \mathbf{y}, \mathbf{y} \rangle \quad (\text{Distributivity again}) \\ &= \|\mathbf{x}\|_2^2 + \langle \mathbf{x}, \mathbf{y} \rangle + \langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2 \quad (\text{Commutativity of inner product}) \\ &= \|\mathbf{x}\|_2^2 + 2\langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2. \quad \square \end{aligned} \]

Example 1.20 (Numerical Verification and Geometry). Let \(\mathbf{x} = \begin{bmatrix} 3 \\ 1 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 1 \\ 2 \end{bmatrix}\). Then \(\mathbf{x} + \mathbf{y} = \begin{bmatrix} 4 \\ 3 \end{bmatrix}\). Verify the above identity by:

  • LHS (direct calculation): \(\|\mathbf{x}+\mathbf{y}\|_2^2 = 4^2 + 3^2 = 16+9 = 25\).
  • RHS (using the identity):
    • \(\|\mathbf{x}\|_2^2 = 3^2 + 1^2 = 10\).
    • \(\|\mathbf{y}\|_2^2 = 1^2 + 2^2 = 5\).
    • \(\langle \mathbf{x}, \mathbf{y} \rangle = (3)(1) + (1)(2) = 5\).
    • Sum: \(10 + 2(5) + 5 = 25\).

The results match. The geometry of this identity forms the basis of the Law of Cosines for triangles.

Figure 1.17: Geometric representation of the norm of a sum, for x=[3,1]ᵀ and y=[1,2]ᵀ.
Figure 1.17: Geometric representation of the norm of a sum, for \(\mathbf{x}=[3,1]^\top\) and \(\mathbf{y}=[1,2]^\top\). Each vector is labelled with its \(\ell_2\) length in parentheses: \(\|\mathbf{x}\|_2=\sqrt{10}\), \(\|\mathbf{y}\|_2=\sqrt{5}\), and \(\|\mathbf{x}+\mathbf{y}\|_2=5\). The squared length of the diagonal of the parallelogram (\(\|\mathbf{x}+\mathbf{y}\|_2^2\)) is related to the squared lengths of the sides (\(\|\mathbf{x}\|_2^2, \|\mathbf{y}\|_2^2\)) and the angle between them (captured by the inner product term \(2\langle \mathbf{x}, \mathbf{y} \rangle\)).

A direct corollary gives us the squared norm of a difference. By substituting \(-\mathbf{y}\) for \(\mathbf{y}\) in the identity above and using the linearity of the inner product, we get:

\[ \|\mathbf{x} - \mathbf{y}\|_2^2 = \|\mathbf{x}\|_2^2 - 2\langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2. \]

Norm of a Triple Sum

This principle can be extended to sums of more than two vectors. For three vectors \(\mathbf{x}, \mathbf{y}, \mathbf{z} \in \mathbb{R}^p\):

\[ \|\mathbf{x}+\mathbf{y}+\mathbf{z}\|_2^2 = \|\mathbf{x}\|_2^2 + \|\mathbf{y}\|_2^2 + \|\mathbf{z}\|_2^2 + 2\langle\mathbf{x},\mathbf{y}\rangle + 2\langle\mathbf{x},\mathbf{z}\rangle + 2\langle\mathbf{y},\mathbf{z}\rangle. \]

Proof Sketch. We prove this by treating \((\mathbf{x}+\mathbf{y})\) as a single vector and applying the two-vector identity twice.

\[ \begin{aligned} &\|\underbrace{(\mathbf{x}+\mathbf{y})}_{\mathbf{u}} + \mathbf{z}\|_2^2 \\ &= \|\mathbf{u}+\mathbf{z}\|_2^2 \\ &= \|\mathbf{u}\|_2^2 + 2\langle\mathbf{u},\mathbf{z}\rangle + \|\mathbf{z}\|_2^2 \\ &= \|\mathbf{x}+\mathbf{y}\|_2^2 + 2\langle\mathbf{x}+\mathbf{y},\mathbf{z}\rangle + \|\mathbf{z}\|_2^2 \\ &= (\|\mathbf{x}\|_2^2 + 2\langle\mathbf{x},\mathbf{y}\rangle + \|\mathbf{y}\|_2^2) + 2(\langle\mathbf{x},\mathbf{z}\rangle + \langle\mathbf{y},\mathbf{z}\rangle) + \|\mathbf{z}\|_2^2. \end{aligned} \]

Rearranging the terms gives the stated result. The pattern is clear: the squared norm of a sum is the sum of the squared norms plus twice the sum of all unique pairwise inner products.

1.5.7 Revisiting the Quadratic Example with Norms

We can now use this more powerful notation to re-analyze the multivariate quadratic function from before.

Example 1.21 (Quadratic Function using Norms). Consider the function \(f: \mathbb{R}^p \to \mathbb{R}\) defined by:

\[ f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{b}, \mathbf{x} \rangle. \]

Using our new identity, we can immediately write this as:

\[ f(\mathbf{x}) = \frac{1}{2}\|\mathbf{x}\|_2^2 - \langle \mathbf{b}, \mathbf{x} \rangle. \]

Our goal is to “complete the square” to reveal the function’s minimum. We know from our derivation above that the expansion of \(\|\mathbf{x}-\mathbf{b}\|_2^2\) is \(\|\mathbf{x}\|_2^2 - 2\langle \mathbf{x}, \mathbf{b} \rangle + \|\mathbf{b}\|_2^2\). Let’s rearrange this to isolate the terms we have in our function:

\[ \|\mathbf{x}\|_2^2 - 2\langle \mathbf{x}, \mathbf{b} \rangle = \|\mathbf{x}-\mathbf{b}\|_2^2 - \|\mathbf{b}\|_2^2. \]

Our function \(f(\mathbf{x})\) contains \(\frac{1}{2}\) of the left side. So, we can substitute:

\[ \begin{aligned} f(\mathbf{x}) &= \frac{1}{2} (\|\mathbf{x}\|_2^2 - 2\langle \mathbf{b}, \mathbf{x} \rangle) \\ &= \frac{1}{2} \left( \|\mathbf{x}-\mathbf{b}\|_2^2 - \|\mathbf{b}\|_2^2 \right) \\ &= \frac{1}{2}\|\mathbf{x}-\mathbf{b}\|_2^2 - \frac{1}{2}\|\mathbf{b}\|_2^2. \end{aligned} \]

This is the same result as before, but derived more elegantly using norm properties. The second term, \(-\frac{1}{2}\|\mathbf{b}\|_2^2\), is a constant. To minimize \(f(\mathbf{x})\), we must minimize the first term, \(\frac{1}{2}\|\mathbf{x}-\mathbf{b}\|_2^2\). By the non-negativity and definiteness properties of norms, this term is always non-negative, and its minimum value is 0, which is achieved precisely when \(\mathbf{x}-\mathbf{b} = \mathbf{0}\). Therefore, the unique global minimum occurs at \(\mathbf{x}^* = \mathbf{b}\).

1.5.8 The Triangle Inequality

For any \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\): \(\|\mathbf{x}+\mathbf{y}\|_2 \le \|\mathbf{x}\|_2 + \|\mathbf{y}\|_2\). The proof to be provided later on.

Example 1.22 (Triangle Inequality Verification). Let \(\mathbf{x} = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} -6 \\ 4 \\ 1 \end{bmatrix}\). Then:

\[ \mathbf{x} + \mathbf{y} = \begin{bmatrix} 1-6 \\ 2+4 \\ 3+1 \end{bmatrix} = \begin{bmatrix} -5 \\ 6 \\ 4 \end{bmatrix}. \]
  • Left-hand side: \(\|\mathbf{x}+\mathbf{y}\|_2 = \sqrt{(-5)^2+6^2+4^2} = \sqrt{25+36+16} = \sqrt{77} \approx 8.77\).
  • Right-hand side: \(\|\mathbf{x}\|_2 + \|\mathbf{y}\|_2 = \sqrt{14} + \sqrt{53} \approx 3.74 + 7.28 = 11.02\).
  • Verification: \(8.77 \le 11.02\). The inequality holds.

1.5.9 Norm as Distance

The most powerful application of a norm is in defining the Euclidean distance between two vectors. This formalizes our intuitive concept of “closeness” in a way that is computationally and analytically tractable. The distance between vectors \(\mathbf{x}\) and \(\mathbf{y}\) is defined as the norm of their difference:

\[ \text{dist}(\mathbf{x}, \mathbf{y}) = \|\mathbf{x} - \mathbf{y}\|_2. \]
  • Complexity: This requires one vector subtraction (\(O(p)\)) followed by one norm calculation (\(O(p)\)), for a total complexity of \(O(p)\).

Example 1.23 (Distance Calculation). Let \(\mathbf{x} = \begin{bmatrix} 3 \\ 1 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 1 \\ 2 \end{bmatrix}\). The distance is:

\[ \begin{aligned} \text{dist}(\mathbf{x}, \mathbf{y}) &= \left\|\begin{bmatrix} 3-1 \\ 1-2 \end{bmatrix}\right\|_2 = \left\|\begin{bmatrix} 2 \\ -1 \end{bmatrix}\right\|_2 \\ &= \sqrt{2^2 + (-1)^2} = \sqrt{5} \approx 2.236. \end{aligned} \]

1.5.10 The Triangle Inequality for Distances

One of the foundational axioms of a norm is the triangle inequality:

\[ \begin{aligned} \|\mathbf{a}+\mathbf{b}\| \le \|\mathbf{a}\| + \|\mathbf{b}\|. \end{aligned} \]

When we interpret the norm as a measure of distance, this axiom takes on a deeply intuitive geometric meaning: the shortest distance between two points is a straight line.

Consider three points in space, represented by the vectors \(\mathbf{x}\), \(\mathbf{y}\), and \(\mathbf{z}\). The direct distance between \(\mathbf{x}\) and \(\mathbf{z}\) is \(\|\mathbf{x} - \mathbf{z}\|_2\). A path that goes from \(\mathbf{x}\) to \(\mathbf{z}\) by “stopping” at \(\mathbf{y}\) has a total length of \(\|\mathbf{x} - \mathbf{y}\|_2 + \|\mathbf{y} - \mathbf{z}\|_2\). The triangle inequality guarantees that this detour is never shorter than the direct path.

Theorem (Triangle Inequality for Distances): For any three vectors \(\mathbf{x}, \mathbf{y}, \mathbf{z} \in \mathbb{R}^p\),

\[ \|\mathbf{x} - \mathbf{z}\|_2 \le \|\mathbf{x} - \mathbf{y}\|_2 + \|\mathbf{y} - \mathbf{z}\|_2. \]

Proof. The proof relies on a simple algebraic manipulation and the base triangle inequality property of norms. We can cleverly rewrite the vector \(\mathbf{x}-\mathbf{z}\) by adding and subtracting \(\mathbf{y}\):

\[ \mathbf{x} - \mathbf{z} = (\mathbf{x} - \mathbf{y}) + (\mathbf{y} - \mathbf{z}). \]

Now, we take the norm of both sides. Let \(\mathbf{a} = (\mathbf{x} - \mathbf{y})\) and \(\mathbf{b} = (\mathbf{y} - \mathbf{z})\). The left side becomes \(\|\mathbf{a}+\mathbf{b}\|_2\). By the axiomatic triangle inequality for norms, we know that \(\|\mathbf{a}+\mathbf{b}\|_2 \le \|\mathbf{a}\|_2 + \|\mathbf{b}\|_2\). Substituting our definitions of \(\mathbf{a}\) and \(\mathbf{b}\) back in gives:

\[ \|\underbrace{(\mathbf{x} - \mathbf{y})}_{\mathbf{a}} + \underbrace{(\mathbf{y} - \mathbf{z})}_{\mathbf{b}}\|_2 \le \|\mathbf{x} - \mathbf{y}\|_2 + \|\mathbf{y} - \mathbf{z}\|_2. \]

Simplifying the left side gives the desired result.  \(\square\)

Figure 1.18: A geometric illustration of the triangle inequality for distances.
Figure 1.18: A geometric illustration of the triangle inequality for distances. The direct path from the point represented by \(\mathbf{x}\) to the point represented by \(\mathbf{z}\) (purple line) is always shorter than or equal to the path that takes a detour through the point represented by \(\mathbf{y}\) (the sum of the blue and red line lengths). This holds true for vectors of any dimension.

1.5.11 Fundamental Inequalities and Geometric Interpretation

1.5.12 The Cauchy-Schwarz Inequality

This is a very important inequality:

Lemma 1.1 (Cauchy-Schwarz Inequality). For any two vectors \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\), the following holds:

\[ |\langle \mathbf{x}, \mathbf{y} \rangle| \le \|\mathbf{x}\|_2 \|\mathbf{y}\|_2. \]

Proof. The inequality is trivially true if \(\mathbf{x} = \mathbf{0}\) or \(\mathbf{y} = \mathbf{0}\), since both sides are zero. Let us therefore assume \(\mathbf{x}, \mathbf{y} \neq \mathbf{0}\).

Consider an arbitrary scalar \(t \in \mathbb{R}\) and form the vector \(\mathbf{z} = \mathbf{x} - t\mathbf{y}\). By the non-negativity property of norms, the squared norm of any vector must be non-negative. Therefore:

\[ \|\mathbf{x} - t\mathbf{y}\|_2^2 \ge 0. \]

We can now expand this expression using the properties of the inner product:

\[ \begin{aligned} \|\mathbf{x} - t\mathbf{y}\|_2^2 &= \langle \mathbf{x} - t\mathbf{y}, \mathbf{x} - t\mathbf{y} \rangle \\ &= \langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{x}, t\mathbf{y} \rangle - \langle t\mathbf{y}, \mathbf{x} \rangle + \langle t\mathbf{y}, t\mathbf{y} \rangle \\ &= \langle \mathbf{x}, \mathbf{x} \rangle - t\langle \mathbf{x}, \mathbf{y} \rangle - t\langle \mathbf{y}, \mathbf{x} \rangle + t^2\langle \mathbf{y}, \mathbf{y} \rangle \\ &= \|\mathbf{x}\|_2^2 - 2t\langle \mathbf{x}, \mathbf{y} \rangle + t^2\|\mathbf{y}\|_2^2. \end{aligned} \]

Let’s define a function of \(t\), \(p(t) = \|\mathbf{y}\|_2^2 t^2 - 2\langle \mathbf{x}, \mathbf{y} \rangle t + \|\mathbf{x}\|_2^2\). This is a quadratic polynomial in the variable \(t\). Our initial observation that the norm is non-negative means that \(p(t) \ge 0\) for all possible values of \(t\).

From elementary algebra, a quadratic polynomial \(At^2 + Bt + C\) is always non-negative if and only if it has at most one real root. This occurs when its discriminant, \(B^2 - 4AC\), is less than or equal to zero. For our polynomial \(p(t)\), the coefficients are:

\[ A = \|\mathbf{y}\|_2^2, \quad B = -2\langle \mathbf{x}, \mathbf{y} \rangle, \quad C = \|\mathbf{x}\|_2^2. \]

The discriminant condition is therefore:

\[ \begin{aligned} B^2 - 4AC &\le 0 \\ (-2\langle \mathbf{x}, \mathbf{y} \rangle)^2 - 4(\|\mathbf{y}\|_2^2)(\|\mathbf{x}\|_2^2) &\le 0 \\ 4\langle \mathbf{x}, \mathbf{y} \rangle^2 - 4\|\mathbf{x}\|_2^2 \|\mathbf{y}\|_2^2 &\le 0 \\ \langle \mathbf{x}, \mathbf{y} \rangle^2 &\le \|\mathbf{x}\|_2^2 \|\mathbf{y}\|_2^2. \end{aligned} \]

Taking the square root of both sides yields the Cauchy-Schwarz inequality:

\[ |\langle \mathbf{x}, \mathbf{y} \rangle| \le \|\mathbf{x}\|_2 \|\mathbf{y}\|_2. \quad \square \]

We have proven that \(|\langle \mathbf{x}, \mathbf{y} \rangle| \le \|\mathbf{x}\|_2 \|\mathbf{y}\|_2\). A crucial part of understanding this inequality is to analyze the specific case where it becomes an equality. The geometric intuition is that this happens only when the vectors are perfectly aligned (or anti-aligned). Let’s prove this formally.

Theorem: Equality holds in the Cauchy-Schwarz inequality, i.e., \(|\langle \mathbf{x}, \mathbf{y} \rangle| = \|\mathbf{x}\|_2 \|\mathbf{y}\|_2\), if and only if one vector is a scalar multiple of the other (i.e., \(\mathbf{x} = \alpha\mathbf{y}\) or \(\mathbf{y} = \alpha\mathbf{x}\) for some scalar \(\alpha \in \mathbb{R}\)).

Proof. This is an “if and only if” statement, so we must prove both directions.

Part 1: Scalar multiple implies equality (\(\impliedby\)). Assume one vector is a scalar multiple of the other. Without loss of generality, let \(\mathbf{x} = \alpha\mathbf{y}\) for some scalar \(\alpha\). Let’s evaluate both sides of the Cauchy-Schwarz inequality.

Left-hand side (LHS):

\[ |\langle \mathbf{x}, \mathbf{y} \rangle| = |\langle \alpha\mathbf{y}, \mathbf{y} \rangle| = |\alpha \langle \mathbf{y}, \mathbf{y} \rangle| = |\alpha| \|\mathbf{y}\|_2^2. \]

Right-hand side (RHS):

\[ \|\mathbf{x}\|_2 \|\mathbf{y}\|_2 = \|\alpha\mathbf{y}\|_2 \|\mathbf{y}\|_2 = (|\alpha|\|\mathbf{y}\|_2) \|\mathbf{y}\|_2 = |\alpha| \|\mathbf{y}\|_2^2. \]

Since LHS = RHS, we have shown that if the vectors are scalar multiples, the equality holds.

Part 2: Equality implies scalar multiple (\(\implies\)). Now, assume that the equality \(|\langle \mathbf{x}, \mathbf{y} \rangle| = \|\mathbf{x}\|_2 \|\mathbf{y}\|_2\) holds. If \(\mathbf{y} = \mathbf{0}\), then \(\mathbf{y} = 0\mathbf{x}\) and we are done. Otherwise \(\mathbf{y} \neq \mathbf{0}\), and we trace back through our original proof of the inequality.

The key step in the proof was the discriminant condition for the quadratic polynomial \(p(t) = \|\mathbf{x} - t\mathbf{y}\|_2^2\). We argued that since \(p(t) \ge 0\) for all \(t\), its discriminant must be less than or equal to zero.

\[ \langle \mathbf{x}, \mathbf{y} \rangle^2 - \|\mathbf{x}\|_2^2 \|\mathbf{y}\|_2^2 \le 0. \]

The condition for equality, \(|\langle \mathbf{x}, \mathbf{y} \rangle| = \|\mathbf{x}\|_2 \|\mathbf{y}\|_2\), is equivalent to saying that the discriminant is exactly zero:

\[ \langle \mathbf{x}, \mathbf{y} \rangle^2 - \|\mathbf{x}\|_2^2 \|\mathbf{y}\|_2^2 = 0. \]

From algebra, a quadratic polynomial with a zero discriminant has exactly one real root. This means there is a unique value of \(t\), let’s call it \(t^\star\), for which \(p(t^\star) = 0\). By the definition of our polynomial, this means:

\[ p(t^\star) = \|\mathbf{x} - t^\star\mathbf{y}\|_2^2 = 0. \]

By the definiteness property of norms (a norm is zero if and only if the vector is the zero vector), this implies:

\[ \mathbf{x} - t^\star\mathbf{y} = \mathbf{0}. \]

Rearranging this equation gives:

\[ \mathbf{x} = t^\star\mathbf{y}. \]

This shows that if the equality holds, then \(\mathbf{x}\) must be a scalar multiple of \(\mathbf{y}\). (The specific scalar is \(t^\star = \frac{\langle \mathbf{x}, \mathbf{y} \rangle}{\|\mathbf{y}\|_2^2}\), which is the value that minimizes the quadratic \(p(t)\)). This completes the proof.  \(\square\)

Example 1.24 (Cauchy-Schwarz Verification). Let \(\mathbf{x} = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} -6 \\ 4 \\ 1 \end{bmatrix}\).

  • Left-hand side: \(|\langle \mathbf{x}, \mathbf{y} \rangle| = |(1)(-6) + (2)(4) + (3)(1)| = |-6+8+3| = |5| = 5\).
  • Right-hand side: \(\|\mathbf{x}\|_2 = \sqrt{1^2+2^2+3^2} = \sqrt{14}\). \(\|\mathbf{y}\|_2 = \sqrt{(-6)^2+4^2+1^2} = \sqrt{36+16+1} = \sqrt{53}\).
  • Product: \(\|\mathbf{x}\|_2 \|\mathbf{y}\|_2 = \sqrt{14 \cdot 53} = \sqrt{742} \approx 27.24\).
  • Verification: \(5 \le 27.24\). The inequality holds.

1.5.13 Proving the Triangle Inequality

Now, armed with the Cauchy-Schwarz inequality that we have just proven from first principles, we can prove the triangle inequality.

Proof. We start by expanding the squared norm of the sum, which we know is easier to work with than the norm itself.

\[ \begin{aligned} \|\mathbf{x}+\mathbf{y}\|_2^2 &= \langle \mathbf{x}+\mathbf{y}, \mathbf{x}+\mathbf{y} \rangle \\ &= \langle \mathbf{x}, \mathbf{x} \rangle + 2\langle \mathbf{x}, \mathbf{y} \rangle + \langle \mathbf{y}, \mathbf{y} \rangle \\ &= \|\mathbf{x}\|_2^2 + 2\langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2 \end{aligned} \]

The inner product term \(\langle \mathbf{x}, \mathbf{y} \rangle\) is a real number, and any real number is less than or equal to its absolute value, so \(\langle \mathbf{x}, \mathbf{y} \rangle \le |\langle \mathbf{x}, \mathbf{y} \rangle|\). Therefore:

\[ \|\mathbf{x}+\mathbf{y}\|_2^2 \le \|\mathbf{x}\|_2^2 + 2|\langle \mathbf{x}, \mathbf{y} \rangle| + \|\mathbf{y}\|_2^2. \]

Now, we apply the Cauchy-Schwarz inequality, which we just proved, to bound the inner product term:

\[ \|\mathbf{x}+\mathbf{y}\|_2^2 \le \|\mathbf{x}\|_2^2 + 2\|\mathbf{x}\|_2 \|\mathbf{y}\|_2 + \|\mathbf{y}\|_2^2. \]

We recognize the right-hand side as the expansion of a perfect square:

\[ \|\mathbf{x}+\mathbf{y}\|_2^2 \le (\|\mathbf{x}\|_2 + \|\mathbf{y}\|_2)^2. \]

Since the norm is always non-negative, we can take the square root of both sides without changing the direction of the inequality, which yields the triangle inequality:

\[ \|\mathbf{x} + \mathbf{y}\|_2 \le \|\mathbf{x}\|_2 + \|\mathbf{y}\|_2. \quad \square \]

This completes the proof, showing how the triangle inequality is a direct consequence of the non-negativity of the norm and the fundamental properties of the inner product.

1.5.14 The Angle Between Vectors: A Unified Geometric View

The inner product, which we have treated primarily as an algebraic operation, has an equivalent geometric interpretation rooted in the Law of Cosines. This connection allows us to formally define the angle between any two vectors, regardless of their dimension, and provides a powerful lens through which to view concepts like similarity and distance.

Derivation from the Law of Cosines

Consider a triangle formed by two non-zero vectors, \(\mathbf{x}\) and \(\mathbf{y}\), and their difference, \(\mathbf{x}-\mathbf{y}\). The sides of this triangle have lengths \(\|\mathbf{x}\|_2\), \(\|\mathbf{y}\|_2\), and \(\|\mathbf{x}-\mathbf{y}\|_2\).

The Law of Cosines from geometry states that for a triangle with sides \(a, b, c\), and angle \(\theta\) opposite side \(c\), we have \(c^2 = a^2 + b^2 - 2ab\cos\theta\). Applying this to our vector triangle:

\[ \|\mathbf{x}-\mathbf{y}\|_2^2 = \|\mathbf{x}\|_2^2 + \|\mathbf{y}\|_2^2 - 2\|\mathbf{x}\|_2\|\mathbf{y}\|_2\cos\theta. \]

We also know from the algebraic properties of the norm (which we derived from the inner product) that:

\[ \|\mathbf{x}-\mathbf{y}\|_2^2 = \|\mathbf{x}\|_2^2 - 2\langle \mathbf{x}, \mathbf{y} \rangle + \|\mathbf{y}\|_2^2. \]

By equating these two expressions for \(\|\mathbf{x}-\mathbf{y}\|_2^2\), we can see that the \(\|\mathbf{x}\|_2^2\) and \(\|\mathbf{y}\|_2^2\) terms on both sides cancel out, leaving:

\[ -2\langle \mathbf{x}, \mathbf{y} \rangle = -2\|\mathbf{x}\|_2\|\mathbf{y}\|_2\cos\theta. \]

Dividing by \(-2\) gives us the fundamental geometric definition of the inner product:

\[ \langle \mathbf{x}, \mathbf{y} \rangle = \|\mathbf{x}\|_2 \|\mathbf{y}\|_2 \cos\theta. \]

This identity is not an approximation; it is an exact restatement of the algebraic inner product in geometric terms.

1.5.15 An Alternative Proof of the Cauchy-Schwarz Inequality

This geometric identity provides an immediate and intuitive proof of the Cauchy-Schwarz inequality. Since the cosine function is bounded by \(-1 \le \cos\theta \le 1\) for any angle \(\theta\), we can write:

\[ |\cos\theta| \le 1. \]

Multiplying by the non-negative quantities \(\|\mathbf{x}\|_2\) and \(\|\mathbf{y}\|_2\):

\[ \|\mathbf{x}\|_2 \|\mathbf{y}\|_2 |\cos\theta| \le \|\mathbf{x}\|_2 \|\mathbf{y}\|_2. \]

From our derived identity, the left side is simply \(|\langle \mathbf{x}, \mathbf{y} \rangle|\). Therefore:

\[ |\langle \mathbf{x}, \mathbf{y} \rangle| \le \|\mathbf{x}\|_2 \|\mathbf{y}\|_2. \]

This shows that the Cauchy-Schwarz inequality is a direct consequence of the geometric fact that the cosine of any angle cannot exceed 1 in magnitude.

1.5.16 Defining the Angle and Its Relation to Alignment

Because the Cauchy-Schwarz inequality guarantees that the term \(\frac{\langle \mathbf{x}, \mathbf{y} \rangle}{\|\mathbf{x}\|_2 \|\mathbf{y}\|_2}\) is always in the range \([-1, 1]\), we can formally define the angle \(\theta\) between any two non-zero vectors \(\mathbf{x}\) and \(\mathbf{y}\) as:

\[ \theta = \arccos\left(\frac{\langle \mathbf{x}, \mathbf{y} \rangle}{\|\mathbf{x}\|_2 \|\mathbf{y}\|_2}\right). \]

This angle perfectly captures the notion of vector alignment:

  • Aligned (\(\theta = 0^\circ\)): \(\cos\theta = 1 \implies \langle \mathbf{x}, \mathbf{y} \rangle = \|\mathbf{x}\|_2 \|\mathbf{y}\|_2\). This occurs when \(\mathbf{x}\) and \(\mathbf{y}\) point in the same direction.
  • Orthogonal (\(\theta = 90^\circ\)): \(\cos\theta = 0 \implies \langle \mathbf{x}, \mathbf{y} \rangle = 0\). This occurs when the vectors are perpendicular.
  • Anti-aligned (\(\theta = 180^\circ\)): \(\cos\theta = -1 \implies \langle \mathbf{x}, \mathbf{y} \rangle = -\|\mathbf{x}\|_2 \|\mathbf{y}\|_2\). This occurs when \(\mathbf{x}\) and \(\mathbf{y}\) point in opposite directions.

1.5.17 The Relationship Between Distance and Angle

The distance between two vectors depends on both their lengths and the angle between them. We can see this by re-examining the Law of Cosines:

\[ \text{dist}(\mathbf{x}, \mathbf{y})^2 = \|\mathbf{x}-\mathbf{y}\|_2^2 = \|\mathbf{x}\|_2^2 + \|\mathbf{y}\|_2^2 - 2\|\mathbf{x}\|_2\|\mathbf{y}\|_2\cos\theta. \]

This equation shows that distance is more than just the difference in lengths; it is intricately linked to alignment.

A particularly important special case arises when we consider vectors that have been normalized to have unit length, i.e., \(\|\mathbf{x}\|_2 = 1\) and \(\|\mathbf{y}\|_2 = 1\). Such vectors lie on the surface of a unit hypersphere. In this case, the distance formula simplifies dramatically:

\[ \begin{aligned} \text{dist}(\mathbf{x}, \mathbf{y})^2 &= 1^2 + 1^2 - 2(1)(1)\cos\theta \\ &= 2 - 2\cos\theta = 2(1 - \cos\theta). \end{aligned} \]

Since \(\cos\theta = \langle \mathbf{x}, \mathbf{y} \rangle\) for unit vectors, we have:

\[ \text{dist}(\mathbf{x}, \mathbf{y})^2 = 2(1 - \langle \mathbf{x}, \mathbf{y} \rangle). \]

This reveals a profound and useful relationship: for normalized vectors, minimizing the Euclidean distance is equivalent to maximizing the inner product. As the inner product (alignment) increases, the distance decreases, and vice versa.

Application: Similarity Search in AI

This equivalence is the cornerstone of modern similarity search in AI. Imagine a database of millions of images, where each image is represented by a high-dimensional feature vector that has been normalized to unit length. When a new query image (also normalized) arrives, our goal is to find the most visually similar images in the database.

  • The Naive Approach (Distance Minimization): We could calculate the Euclidean distance from our query vector to every other vector in the database and find the ones with the smallest distance. This is computationally expensive.
  • The Efficient Approach (Inner Product Maximization): Since the vectors are normalized, we know this is equivalent to finding the vectors with the maximum inner product.

This might not seem like a big advantage until we consider specialized algorithms. Techniques like Locality-Sensitive Hashing (LSH) (not in the scope of this course) are designed specifically to find vectors with high inner products very quickly, without having to compare against every single item. LSH works by cleverly designing “hash functions” such that vectors that are close together (i.e., have a small angle and thus a high inner product) are likely to be hashed into the same bucket. By searching only within the query’s bucket, we can find approximate nearest neighbors in sub-linear time, making search over billions of items feasible. This principle powers systems for image retrieval, document search, and recommendation engines.

Figure 1.19: Similarity search with normalized vectors.
Figure 1.19: Similarity search with normalized vectors. All vectors lie on the unit circle. Finding the document vector (\(\mathbf{d}_1\)) closest to the query (\(\mathbf{q}\)) is equivalent to finding the one with the smallest angle (\(\theta_1\)), which in turn is equivalent to finding the one with the maximum inner product (cosine similarity).

Example 1.25 (Geometric Formula as a Sanity Check). Let’s verify this for \(\mathbf{x} = \begin{bmatrix} 2 \\ 2 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 3 \\ 0 \end{bmatrix}\). From simple geometry, the angle \(\theta\) is \(45^\circ\).

1. Algebraic Calculation:

\[ \langle \mathbf{x}, \mathbf{y} \rangle = (2)(3) + (2)(0) = 6. \]

2. Geometric Calculation:

  • \(\|\mathbf{x}\|_2 = \sqrt{2^2 + 2^2} = \sqrt{8} = 2\sqrt{2}\).
  • \(\|\mathbf{y}\|_2 = \sqrt{3^2 + 0^2} = 3\).
  • \(\cos(45^\circ) = \frac{1}{\sqrt{2}}\).
\[ \|\mathbf{x}\|_2 \|\mathbf{y}\|_2 \cos \theta = (2\sqrt{2})(3)\left(\frac{1}{\sqrt{2}}\right) = 6. \]

The results match, confirming the deep connection between the algebraic sum of products and the geometric concept of projection and angle.

Figure 1.20: The algebraic inner product ⟨ [2,2]ᵀ, [3,0]ᵀ ⟩ = 6 is consistent with the geometric formula ‖x‖₂ ‖y‖₂ cos(45°).
Figure 1.20: The algebraic inner product \(\langle [2,2]^\top, [3,0]^\top \rangle = 6\) is consistent with the geometric formula \(\|\mathbf{x}\|_2 \|\mathbf{y}\|_2 \cos(45^\circ)\).

Example 1.26 (Angle Calculation). Find the angle between \(\mathbf{x} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}\) and \(\mathbf{y} = \begin{bmatrix} 1 \\ 0 \end{bmatrix}\).

  • \(\langle \mathbf{x}, \mathbf{y} \rangle = (1)(1) + (1)(0) = 1\).
  • \(\|\mathbf{x}\|_2 = \sqrt{1^2+1^2} = \sqrt{2}\).
  • \(\|\mathbf{y}\|_2 = \sqrt{1^2+0^2} = 1\).
  • \(\cos \theta = \frac{1}{\sqrt{2} \cdot 1} = \frac{1}{\sqrt{2}}\).
  • \(\theta = \arccos\left(\frac{1}{\sqrt{2}}\right) = 45^\circ\) or \(\pi/4\) radians.

Exercises

Below is a set of exercises related to the content of Chapter 1. The exercises are partitioned by difficulty. Programming questions should be completed in Python using the NumPy library.

EASY EXERCISES

  1. E1.
    Vector Arithmetic. Given the vectors \(\mathbf{x} = \begin{bmatrix} 2 \\ -1 \\ 0 \end{bmatrix}\), \(\mathbf{y} = \begin{bmatrix} -3 \\ 1 \\ 5 \end{bmatrix}\), and the scalar \(\alpha = 3\), compute the vector \(\mathbf{z} = \alpha\mathbf{x} + \mathbf{y}\).
  2. E2.
    Inner Product Calculation. Compute the inner product \(\langle \mathbf{a}, \mathbf{b} \rangle\) for the vectors \(\mathbf{a} = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix}\) and \(\mathbf{b} = \begin{bmatrix} -1 \\ 0 \\ 1 \\ -2 \end{bmatrix}\). Based on the sign of the inner product, is the angle between them acute, obtuse, or exactly \(90^\circ\)?
  3. E3.
    Norm Calculation. For the vector \(\mathbf{v} = \begin{bmatrix} -2 \\ 1 \\ -2 \end{bmatrix}\), compute its \(\ell_1\)-norm, \(\ell_2\)-norm, and \(\ell_\infty\)-norm.
  4. E4.
    Linear Combination Check. Can the vector \(\mathbf{y} = \begin{bmatrix} 3 \\ 5 \end{bmatrix}\) be expressed as a linear combination of \(\mathbf{x}_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}\) and \(\mathbf{x}_2 = \begin{bmatrix} 1 \\ -1 \end{bmatrix}\)? If so, find the coefficients \(\alpha_1\) and \(\alpha_2\).
  5. E5.
    Complexity of Basic Operations. What is the computational complexity in Big-O notation for:
    1. a)
      Computing the sum of three vectors \(\mathbf{x}, \mathbf{y}, \mathbf{z} \in \mathbb{R}^p\)?
    2. b)
      Computing the distance between two vectors \(\mathbf{x}, \mathbf{y} \in \mathbb{R}^p\)?
  6. E6.
    Python: Vector Operations. Write a Python function vector_ops(x, y) that takes two NumPy arrays x and y of the same size and returns a tuple containing their sum, their difference (\(\mathbf{x}-\mathbf{y}\)), and their inner product.

MEDIUM EXERCISES

  1. M1.
    Linear Independence. Determine if the following three vectors in \(\mathbb{R}^3\) are linearly independent. Show your work. \[ \mathbf{v}_1 = \begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix}, \quad \mathbf{v}_2 = \begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix}, \quad \mathbf{v}_3 = \begin{bmatrix} 1 \\ 0 \\ -1 \end{bmatrix}. \]
  2. M2.
    Properties of Inner Product. Prove the distributive property of the inner product, \(\langle \mathbf{x}, \mathbf{y}+\mathbf{z} \rangle = \langle \mathbf{x}, \mathbf{y} \rangle + \langle \mathbf{x}, \mathbf{z} \rangle\), using only its summation definition.
  3. M3.
    Optimizing a Univariate Quadratic. For the function \(f(x) = 2x^2 - 8x + 5\), find the point \(x^*\) where the extremum occurs. Is this point a global minimum or a global maximum? Justify your answer using calculus.
  4. M4.
    Angle and Orthogonality. Given the vectors \(\mathbf{a} = \begin{bmatrix} 4 \\ 0 \end{bmatrix}\) and \(\mathbf{b} = \begin{bmatrix} 1 \\ \sqrt{3} \end{bmatrix}\):
    1. a)
      Calculate the angle between \(\mathbf{a}\) and \(\mathbf{b}\) in degrees.
    2. b)
      Find a non-zero vector \(\mathbf{c} \in \mathbb{R}^2\) that is orthogonal to \(\mathbf{a}\).
  5. M5.
    Relating Distance and Inner Product. Prove the Parallelogram Law for any two vectors \(\mathbf{u}, \mathbf{v} \in \mathbb{R}^p\): \[ \|\mathbf{u} + \mathbf{v}\|_2^2 + \|\mathbf{u} - \mathbf{v}\|_2^2 = 2\|\mathbf{u}\|_2^2 + 2\|\mathbf{v}\|_2^2. \]
  6. M6.
    Python: Finding the Closest Vector. You are given a target vector \(\mathbf{t} = \begin{bmatrix} 1 \\ 1 \end{bmatrix}\) and a list of candidate vectors: \[ \mathcal{S} = \left\{ \begin{bmatrix} 2 \\ 1 \end{bmatrix}, \begin{bmatrix} -1 \\ 0 \end{bmatrix}, \begin{bmatrix} 1 \\ 1.5 \end{bmatrix}, \begin{bmatrix} 0 \\ -2 \end{bmatrix} \right\}. \]

    Write a Python script that iterates through the list \(\mathcal{S}\) and finds the vector that is closest to \(\mathbf{t}\) in terms of Euclidean distance.

HARD EXERCISES

  1. H1.
    Proving Norm Inequalities. Prove that for any vector \(\mathbf{x} \in \mathbb{R}^p\), the following relationship holds between its \(\ell_1\) and \(\ell_2\) norms: \(\|\mathbf{x}\|_2 \le \|\mathbf{x}\|_1\). (Hint: Start by squaring the \(\ell_1\)-norm).
  2. H2.
    Optimizing a Multivariate Quadratic. Find the vector \(\mathbf{x}^* \in \mathbb{R}^p\) that minimizes the function \(f(\mathbf{x}) = \frac{1}{2}\langle \mathbf{x}, \mathbf{x} \rangle + \langle \mathbf{b}, \mathbf{x} \rangle + c\) for a fixed vector \(\mathbf{b} \in \mathbb{R}^p\) and scalar \(c\). Use the method of “completing the square” with inner products and norms, without using calculus.
  3. H3.
    Deriving the Law of Cosines from Algebra. Prove the Law of Cosines for vectors. That is, starting from the algebraic expansion of \(\|\mathbf{x}-\mathbf{y}\|_2^2\), show that it is equivalent to \(\|\mathbf{x}\|_2^2 + \|\mathbf{y}\|_2^2 - 2\|\mathbf{x}\|_2\|\mathbf{y}\|_2\cos\theta\), where \(\theta\) is the angle between \(\mathbf{x}\) and \(\mathbf{y}\).