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$).
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:
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}$.
Reversing the order of addition preserves the identical resultant vector.
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).
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}|$$
Length increases (stretched).
Length decreases (shrunk).
Yields the Zero Vector.
Direction flips $180^\circ$.
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:
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|$$
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}$$
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|$$
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:
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:
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$:
Because magnitudes $\|\mathbf{a}\|_2$ and $\|\mathbf{b}\|_2$ are strictly positive, the sign of the dot product is determined entirely by $\cos heta$:
Vectors point in the same general direction.
Vectors are strictly orthogonal (perpendicular).
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)$$
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.
11 Vector Projections
Projection measures how much of vector $\mathbf{a}$ lies directly along the directional axis of vector $\mathbf{b}$:
The signed length of $\mathbf{a}$'s shadow cast onto $\mathbf{b}$.
The actual vector representing that shadow along $\mathbf{b}$.
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 values are bounded strictly within $[-1, +1]$:
Identical direction ($ heta = 0^\circ$). Maximum alignment.
Orthogonal ($ heta = 90^\circ$). No semantic overlap.
Directly opposite direction ($ heta = 180^\circ$).
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:
Grid-based step distance. For $(1,2)$ to $(4,6)$: $$|1-4| + |2-6| = 3 + 4 = 7$$
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:
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)
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 (ChromaDB, Pinecone) index billions of text passages and execute sub-millisecond retrieval using Cosine Similarity and $L_2$ distance.
Cluster assignments and nearest-neighbor classification rely on Euclidean distance ($L_2$) or outlier-resilient Manhattan distance ($L_1$).
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, 2) + (3, 4, 5)$ is undefined. Vector addition and dot products require identical dimensionality.
$\mathbf{a} - \mathbf{b} e \mathbf{b} - \mathbf{a}$. They have identical lengths but point in opposite directions.
The dot product of two vectors always evaluates to a single real scalar number.
Two massive non-zero vectors produce a zero dot product if they are perpendicular ($ heta = 90^\circ$).
$L_1, L_2$, and $L_\infty$ measure length differently. Choose the norm appropriate to the modeling objective.
The zero vector has norm zero; division by zero is undefined, so it has no unit vector.
Cosine similarity measures orientation only; two collinear vectors of radically different sizes have similarity 1.0.
Multiplying a standard Python list duplicates elements. Always use np.ndarray for vector algebra.
Vector Operations Workbench (3-in-1 Explorer)
18 Key Takeaways
Components sum or subtract element-wise. $\mathbf{a} - \mathbf{b} = \mathbf{a} + (-\mathbf{b})$ provides relative displacement.
$k\mathbf{a} = (ka_1, ka_2)$. Scales length by $|k|$. Negative scalars ($k < 0$) invert direction by $180^\circ$.
Satisfies non-negativity ($\ge 0$), definiteness ($\|0\|=0$), absolute homogeneity, and the triangle inequality.
$L_1$ sums absolute values (Lasso/Sparsity); $L_2$ computes root-of-squares (Ridge/Distance); $L_\infty$ selects max component.
$\hat{\mathbf{v}} = \mathbf{v} / \|\mathbf{v}\|_2$. Has length 1. Represents pure direction without magnitude bias.
$\mathbf{a} \cdot \mathbf{b} = \sum a_i b_i = \|a\| \|b\| \cos heta$. Takes two vectors and yields a single real scalar.
$\mathbf{a} \cdot \mathbf{b} = 0 \iff heta = 90^\circ$. A zero dot product between non-zero vectors proves they are perpendicular.
$\cos heta = (\mathbf{a}\cdot\mathbf{b}) / (\|a\|\|b\|)$. Bounded in $[-1, 1]$. The standard for text embedding search.
Mastering vector operations provides the exact mathematical foundation to unlock multidimensional Matrices next!