12–15 min read Linear Algebra & Similarity Metrics Lesson 15 / 32
Part 3 — Linear Algebra (Lesson 2 of 7)

Lesson 14.15 — Vector Operations, Dot Product & Norms

How do multiple vectors interact algebraically and geometrically? How does vector addition and subtraction model physical displacement, how does the dot product gauge directional alignment and orthogonality, and how do Manhattan and Euclidean norms quantify magnitude and distance in modern Machine Learning?

1 Introduction: Computing with Multiple Vectors

Imagine a character in a 2D game whose current location is $\mathbf{p} = (3, 2)$. The player presses the movement controls, prompting a translation of 4 units to the right and 3 units upwards.

Both the current position and the translation can be represented as vectors:

  • Initial position vector: $\mathbf{p} = (3, 2)$
  • Displacement translation vector: $\mathbf{v} = (4, 3)$

The player's new position is computed through straightforward vector addition: $$\mathbf{p} + \mathbf{v} = (3 + 4, \, 2 + 3) = (7, 5)$$

However, modern engineering and artificial intelligence tasks require far more than basic addition:

  • How far has an autonomous robot advanced along a desired trajectory? (Vector Projection).
  • How semantically aligned are two text document embeddings in an AI model? (Cosine Similarity).
  • How does an input feature vector interact with a neural network's weight vector? (Dot Product).
  • How large or dispersed is a data point in high-dimensional space? (Vector Norms — $L_1, L_2, L_\infty$).
Vector Operations constitute the operational foundation of linear algebra, providing the algebraic and geometric machinery to add, subtract, scale, measure lengths, and calculate spatial angles and similarities between vectors.

2 Vector Addition

Vector Addition combines two or more vectors of identical dimension by summing their corresponding components to produce a new resultant vector.

Given two 2D vectors $\mathbf{a} = (a_1, a_2)$ and $\mathbf{b} = (b_1, b_2)$, their algebraic sum is:

Vector Addition Formula
$$\mathbf{a} + \mathbf{b} = (a_1 + b_1, \, a_2 + b_2)$$

Numerical Example: If $\mathbf{a} = (3, 4)$ and $\mathbf{b} = (2, 1)$: $$\mathbf{a} + \mathbf{b} = (3 + 2, \, 4 + 1) = (5, 5)$$

2.1 Geometric Interpretation: The Head-to-Tail Rule

Geometrically, vector addition follows the Head-to-Tail (Triangle) rule. Placing the tail (initial point) of vector $\mathbf{b}$ at the head (terminal tip) of vector $\mathbf{a}$ produces the resultant vector $\mathbf{a} + \mathbf{b}$, which connects the origin of $\mathbf{a}$ directly to the tip of $\mathbf{b}$.

Vector a Vector b Resultant a + b O (0,0)
Commutative Property
$$\mathbf{a} + \mathbf{b} = \mathbf{b} + \mathbf{a}$$

Reversing the order of addition preserves the identical resultant vector.

Associative Property
$$(\mathbf{a} + \mathbf{b}) + \mathbf{c} = \mathbf{a} + (\mathbf{b} + \mathbf{c})$$

Grouping order does not alter the cumulative sum across multiple vectors.

3 Vector Subtraction

Vector Subtraction subtracts corresponding components of the second vector from the first: $$\mathbf{a} - \mathbf{b} = (a_1 - b_1, \, a_2 - b_2)$$

Example: If $\mathbf{a} = (7, 5)$ and $\mathbf{b} = (3, 2)$: $$\mathbf{a} - \mathbf{b} = (7 - 3, \, 5 - 2) = (4, 3)$$

3.1 Geometric Meaning: Direction and Relative Displacement

Vector subtraction is algebraically equivalent to adding the opposite vector: $$\mathbf{a} - \mathbf{b} = \mathbf{a} + (-\mathbf{b})$$ where $-\mathbf{b}$ has identical length but points in the opposite direction ($180^\circ$ reversed).

Measuring Spatial Shift: If two position vectors $\mathbf{p}_1$ and $\mathbf{p}_2$ emanate from the origin, the difference vector $\mathbf{p}_2 - \mathbf{p}_1$ points directly from the terminal tip of $\mathbf{p}_1$ to the tip of $\mathbf{p}_2$.
For a robot transitioning from $\mathbf{p}_1 = (2, 3)$ to $\mathbf{p}_2 = (8, 7)$, the required displacement vector is: $$\mathbf{d} = \mathbf{p}_2 - \mathbf{p}_1 = (8 - 2, \, 7 - 3) = (6, 4)$$

4 Scalar Multiplication

Multiplying a vector $\mathbf{a} = (a_1, a_2)$ by a real scalar $k \in \mathbb{R}$ scales each component independently by $k$: $$k\mathbf{a} = (k a_1, \, k a_2)$$

The magnitude of the new vector scales proportionally: $$|k\mathbf{a}| = |k| \cdot |\mathbf{a}|$$

$k > 1$
2 × (3, 4) = (6, 8)

Length increases (stretched).

$0 < k < 1$
0.5 × (6, 8) = (3, 4)

Length decreases (shrunk).

$k = 0$
0 × (3, 4) = (0, 0)

Yields the Zero Vector.

$k < 0$
-1 × (3, 4) = (-3, -4)

Direction flips $180^\circ$.

Distributive Laws:
• $k(\mathbf{a} + \mathbf{b}) = k\mathbf{a} + k\mathbf{b}$
• $(k + m)\mathbf{a} = k\mathbf{a} + m\mathbf{a}$

5 What is a Vector Norm?

A Vector Norm is a mathematical function that maps a vector to a non-negative real number ($\mathbb{R}_{\ge 0}$), quantifying its magnitude, size, or distance from the origin.

Denoted by $\|\mathbf{v}\|$, any valid norm must satisfy four fundamental mathematical axioms:

1
Non-negativity: $\|\mathbf{v}\| \ge 0$. The length of any vector is always greater than or equal to zero.
2
Definiteness: $\|\mathbf{v}\| = 0 \iff \mathbf{v} = \mathbf{0}$. Only the Zero Vector has zero length.
3
Absolute Homogeneity: $\|k\mathbf{v}\| = |k| \cdot \|\mathbf{v}\|$. Scaling a vector by scalar $k$ scales its norm by $|k|$.
4
Triangle Inequality: $\|\mathbf{u} + \mathbf{v}\| \le \|\mathbf{u}\| + \|\mathbf{v}\|$. The norm of a sum never exceeds the sum of the individual norms.

6 Core Vector Norms ($L_1, L_2, L_\infty$)

Different machine learning models and optimization frameworks leverage different norms:

6.1 $L_1$ Norm (Manhattan / Taxicab Norm) $\|\mathbf{v}\|_1$

The sum of the absolute values of all components: $$\|\mathbf{v}\|_1 = \sum_{i=1}^n |v_i| = |v_1| + |v_2| + \dots + |v_n|$$

Worked Example: For $\mathbf{v} = (3, -4, 2)$: $$\|\mathbf{v}\|_1 = |3| + |-4| + |2| = 3 + 4 + 2 = 9$$

Applications: Grid pathfinding, Lasso Regression ($L_1$ regularization) which induces sparsity by forcing non-essential weights strictly to zero.

6.2 $L_2$ Norm (Euclidean Norm / Geometric Length) $\|\mathbf{v}\|_2$

The square root of the sum of squared components (generalized Pythagorean length): $$\|\mathbf{v}\|_2 = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}$$

Worked Example: For $\mathbf{v} = (3, -4)$: $$\|\mathbf{v}\|_2 = \sqrt{3^2 + (-4)^2} = \sqrt{9 + 16} = \sqrt{25} = 5$$

Applications: Standard physical distance, 3D graphics, and Ridge Regression ($L_2$ weight decay) which shrinks parameters smoothly.

6.3 $L_\infty$ Norm (Maximum / Chebyshev Norm) $\|\mathbf{v}\|_\infty$

The largest single absolute component in the vector: $$\|\mathbf{v}\|_\infty = \max_{1 \le i \le n} |v_i|$$

Worked Example: For $\mathbf{v} = (3, -4, 2)$: $$\|\mathbf{v}\|_\infty = \max(|3|, |-4|, |2|) = \max(3, 4, 2) = 4$$

Applications: Adversarial ML attacks (e.g. Fast Gradient Sign Method - FGSM), bounding worst-case error, and robotic joint tolerance bounds.

Norm Type Formula Value for (3, -4) Primary ML & CS Use
$L_1$ Norm $\sum |v_i|$ $|3| + |-4| = 7$ Sparsity, Feature Selection, Lasso
$L_2$ Norm $\sqrt{\sum v_i^2}$ $\sqrt{3^2 + 4^2} = 5$ Standard Euclidean Distance, Ridge
$L_\infty$ Norm $\max |v_i|$ $\max(3, 4) = 4$ Worst-case bounds, Adversarial Robustness

7 Unit Vectors and Normalization

A vector whose $L_2$ norm equals exactly 1 is termed a Unit Vector (written $\hat{\mathbf{v}}$).

Dividing any non-zero vector by its Euclidean norm produces a unit vector pointing in the identical direction, a process known as Vector Normalization:

Unit Vector Normalization Formula
$$\hat{\mathbf{v}} = \frac{\mathbf{v}}{\|\mathbf{v}\|_2}$$
Step-by-step example: Given $\mathbf{v} = (3, 4)$, its norm is $\|\mathbf{v}\|_2 = 5$.
Its unit vector is: $$\hat{\mathbf{v}} = \frac{(3, 4)}{5} = \left( \frac{3}{5}, \, \frac{4}{5} ight) = (0.6, \, 0.8)$$
Verifying its unit length: $$\|\hat{\mathbf{v}}\|_2 = \sqrt{0.6^2 + 0.8^2} = \sqrt{0.36 + 0.64} = \sqrt{1.0} = 1$$

Critical Constraint: The Zero Vector has length zero, and division by zero is undefined. Therefore, the Zero Vector has no defined unit vector.

8 The Dot Product (Scalar Product)

The Dot Product takes two vectors of identical dimension and multiplies corresponding components, summing the products into a single Scalar value:

Algebraic Definition of the Dot Product
$$\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^n a_i b_i = a_1 b_1 + a_2 b_2 + \dots + a_n b_n$$
Numerical Example: If $\mathbf{a} = (2, 3)$ and $\mathbf{b} = (4, 5)$: $$\mathbf{a} \cdot \mathbf{b} = (2 imes 4) + (3 imes 5) = 8 + 15 = 23$$ Notice: The inputs are two vectors, but the output $23$ is purely a scalar!

9 Geometric Meaning and Angle Determination

Beyond raw component multiplication, the dot product has an elegant geometric formulation. For any two non-zero vectors $\mathbf{a}$ and $\mathbf{b}$ separated by angle $ heta$:

$$\mathbf{a} \cdot \mathbf{b} = \|\mathbf{a}\|_2 \|\mathbf{b}\|_2 \cos heta$$

Because magnitudes $\|\mathbf{a}\|_2$ and $\|\mathbf{b}\|_2$ are strictly positive, the sign of the dot product is determined entirely by $\cos heta$:

Acute Angle ($0^\circ \le heta < 90^\circ$)
$$\mathbf{a} \cdot \mathbf{b} > 0$$

Vectors point in the same general direction.

Right Angle ($ heta = 90^\circ$)
$$\mathbf{a} \cdot \mathbf{b} = 0$$

Vectors are strictly orthogonal (perpendicular).

Obtuse Angle ($90^\circ < heta \le 180^\circ$)
$$\mathbf{a} \cdot \mathbf{b} < 0$$

Vectors point in opposing directions.

9.1 Finding the Angle Between Vectors

Rearranging the geometric formula allows us to compute the exact spatial angle $ heta$: $$\cos heta = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\|_2 \|\mathbf{b}\|_2} \implies heta = \arccos\left( \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\|_2 \|\mathbf{b}\|_2} ight)$$

Example: For $\mathbf{a} = (1, 0)$ and $\mathbf{b} = (0, 1)$: $$\mathbf{a} \cdot \mathbf{b} = 1(0) + 0(1) = 0 \implies \cos heta = 0 \implies heta = 90^\circ$$

10 Orthogonal Vectors

Two non-zero vectors $\mathbf{a}$ and $\mathbf{b}$ are called Orthogonal (perpendicular) if and only if their dot product equals zero: $$\mathbf{a} \cdot \mathbf{b} = 0 \iff \mathbf{a} \perp \mathbf{b}$$

Verification: Are $\mathbf{a} = (2, 3)$ and $\mathbf{b} = (3, -2)$ orthogonal? $$\mathbf{a} \cdot \mathbf{b} = (2 imes 3) + (3 imes -2) = 6 - 6 = 0$$ Yes! They meet at an exact $90^\circ$ angle.

Engineering Relevance: In 3D graphics, surface illumination requires calculating the perpendicular surface normal. In machine learning, orthogonal feature vectors indicate zero linear redundancy between features.

11 Vector Projections

Projection measures how much of vector $\mathbf{a}$ lies directly along the directional axis of vector $\mathbf{b}$:

Scalar Projection (Component Length)
$$\operatorname{comp}_{\mathbf{b}}\mathbf{a} = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{b}\|_2}$$

The signed length of $\mathbf{a}$'s shadow cast onto $\mathbf{b}$.

Vector Projection (Directional Vector)
$$\operatorname{proj}_{\mathbf{b}}\mathbf{a} = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{b}\|_2^2} \mathbf{b}$$

The actual vector representing that shadow along $\mathbf{b}$.

Example: For $\mathbf{a} = (3, 4)$ and $\mathbf{b} = (1, 0)$ (positive x-axis): $$\operatorname{proj}_{\mathbf{b}}\mathbf{a} = \frac{3(1) + 4(0)}{1^2}(1, 0) = 3(1, 0) = (3, 0)$$

12 Cosine Similarity

In Natural Language Processing, Recommendation Engines, and Semantic Search, Cosine Similarity measures the directional orientation of two vectors, ignoring differences in their raw magnitude:

Cosine Similarity Equation
$$\operatorname{cosine\_similarity}(\mathbf{a}, \mathbf{b}) = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\|_2 \|\mathbf{b}\|_2} = \hat{\mathbf{a}} \cdot \hat{\mathbf{b}}$$

Cosine similarity values are bounded strictly within $[-1, +1]$:

Value = $+1$

Identical direction ($ heta = 0^\circ$). Maximum alignment.

Value = $0$

Orthogonal ($ heta = 90^\circ$). No semantic overlap.

Value = $-1$

Directly opposite direction ($ heta = 180^\circ$).

Why is this vital for text AI? A 50-word summary and a 5,000-word dissertation on the same subject have drastically different word count vector lengths, but their normalized directional angles remain nearly identical. Cosine similarity correctly captures their semantic unity.

Cosine Distance

To convert similarity into a minimization distance metric: $$\operatorname{cosine\_distance} = 1 - \operatorname{cosine\_similarity}$$

13 $L_1$ Distance vs. $L_2$ Distance

Measuring distance between two vector coordinates $\mathbf{a}$ and $\mathbf{b}$ depends on the chosen metric:

$L_1$ Manhattan Distance
$$d_1(\mathbf{a}, \mathbf{b}) = \sum_{i=1}^n |a_i - b_i|$$

Grid-based step distance. For $(1,2)$ to $(4,6)$: $$|1-4| + |2-6| = 3 + 4 = 7$$

$L_2$ Euclidean Distance
$$d_2(\mathbf{a}, \mathbf{b}) = \|\mathbf{a} - \mathbf{b}\|_2 = \sqrt{\sum (a_i - b_i)^2}$$

Direct straight-line distance: $$\sqrt{(1-4)^2 + (2-6)^2} = \sqrt{9 + 16} = 5$$

14 Vector Operations in Python & NumPy

In Python, NumPy provides vectorized C-level execution for all vector operations:

vector_operations_numpy.py
import numpy as np

a = np.array([3.0, 4.0])
b = np.array([2.0, 1.0])

# 1. Addition & Subtraction
print("a + b =", a + b)  # [5. 5.]
print("a - b =", a - b)  # [1. 3.]

# 2. Scalar Multiplication
print("2 * a =", 2 * a)  # [6. 8.]

# 3. Dot Product
dot_val = np.dot(a, b)
print("Dot Product:", dot_val)  # 3*2 + 4*1 = 10.0

# 4. Vector Norms
norm_l2 = np.linalg.norm(a, ord=2)      # Euclidean = 5.0
norm_l1 = np.linalg.norm(a, ord=1)      # Manhattan = 3 + 4 = 7.0
norm_linf = np.linalg.norm(a, ord=np.inf) # Maximum = 4.0

# 5. Unit Vector Normalization
unit_a = a / norm_l2
print("Unit Vector:", unit_a)  # [0.6 0.8]

# 6. Cosine Similarity (with zero vector guard)
def cosine_sim(u, v):
    norm_u = np.linalg.norm(u)
    norm_v = np.linalg.norm(v)
    if norm_u == 0 or norm_v == 0:
        raise ValueError("Cosine similarity is undefined for zero vectors")
    return np.dot(u, v) / (norm_u * norm_v)

print("Cosine Similarity:", cosine_sim(a, b))  # ~0.894

15 Real-World Applications (Graphics, AI, NLP, Optimization)

3D Shading & Lighting (Computer Graphics)

Lambertian diffuse reflectance: $ ext{Brightness} = \max(0, \, \mathbf{N} \cdot \mathbf{L})$. Light direction $\mathbf{L}$ dotted with surface normal $\mathbf{N}$ governs illumination intensity.

Vector Databases & RAG (AI & NLP)

Vector databases (ChromaDB, Pinecone) index billions of text passages and execute sub-millisecond retrieval using Cosine Similarity and $L_2$ distance.

K-Means & KNN (Data Science)

Cluster assignments and nearest-neighbor classification rely on Euclidean distance ($L_2$) or outlier-resilient Manhattan distance ($L_1$).

Gradient Descent & Neural Nets

Model weights update in the negative gradient direction: $\mathbf{w}_{ ext{new}} = \mathbf{w} - \alpha abla L$ (scalar scaling and vector subtraction).

16 8 Common Pitfalls to Avoid

1. Adding Mismatched Dimensions

$(1, 2) + (3, 4, 5)$ is undefined. Vector addition and dot products require identical dimensionality.

2. Ignoring Order in Subtraction

$\mathbf{a} - \mathbf{b} e \mathbf{b} - \mathbf{a}$. They have identical lengths but point in opposite directions.

3. Expecting a Vector Output from Dot Product

The dot product of two vectors always evaluates to a single real scalar number.

4. Equating Zero Dot Product to Zero Magnitude

Two massive non-zero vectors produce a zero dot product if they are perpendicular ($ heta = 90^\circ$).

5. Assuming All Norms Produce Identical Values

$L_1, L_2$, and $L_\infty$ measure length differently. Choose the norm appropriate to the modeling objective.

6. Attempting to Normalize the Zero Vector

The zero vector has norm zero; division by zero is undefined, so it has no unit vector.

7. Conflating Cosine Similarity with Absolute Distance

Cosine similarity measures orientation only; two collinear vectors of radically different sizes have similarity 1.0.

8. Confusing Python Lists with Mathematical Vectors

Multiplying a standard Python list duplicates elements. Always use np.ndarray for vector algebra.

Interactive Laboratory

Vector Operations Workbench (3-in-1 Explorer)

Select Operation:
Vector a = (3, 2)
Vector b = (2, 3)
Resultant Vector (a + b):
(5, 5)
Magnitude: 7.07 units

18 Key Takeaways

1. Addition & Subtraction

Components sum or subtract element-wise. $\mathbf{a} - \mathbf{b} = \mathbf{a} + (-\mathbf{b})$ provides relative displacement.

2. Scalar Multiplication

$k\mathbf{a} = (ka_1, ka_2)$. Scales length by $|k|$. Negative scalars ($k < 0$) invert direction by $180^\circ$.

3. Norm Axioms

Satisfies non-negativity ($\ge 0$), definiteness ($\|0\|=0$), absolute homogeneity, and the triangle inequality.

4. L1 vs. L2 Norm

$L_1$ sums absolute values (Lasso/Sparsity); $L_2$ computes root-of-squares (Ridge/Distance); $L_\infty$ selects max component.

5. Unit Vectors

$\hat{\mathbf{v}} = \mathbf{v} / \|\mathbf{v}\|_2$. Has length 1. Represents pure direction without magnitude bias.

6. Dot Product

$\mathbf{a} \cdot \mathbf{b} = \sum a_i b_i = \|a\| \|b\| \cos heta$. Takes two vectors and yields a single real scalar.

7. Orthogonality

$\mathbf{a} \cdot \mathbf{b} = 0 \iff heta = 90^\circ$. A zero dot product between non-zero vectors proves they are perpendicular.

8. Cosine Similarity

$\cos heta = (\mathbf{a}\cdot\mathbf{b}) / (\|a\|\|b\|)$. Bounded in $[-1, 1]$. The standard for text embedding search.

9. Next Step

Mastering vector operations provides the exact mathematical foundation to unlock multidimensional Matrices next!