Linear Algebra Essentials
Vectors: The Building Block
A vector is an ordered list of numbers. In RAG systems, every piece of text is converted into a vector—these are called embeddings.
Vector Representation
A vector \(\vec{v}\) with \(n\) dimensions:
Example: A 3-dimensional vector representing a point in space:
In Python:
Vector Operations
Addition
Vectors add element-wise:
Example: $\(\begin{bmatrix} 1 \\ 2 \end{bmatrix} + \begin{bmatrix} 3 \\ 4 \end{bmatrix} = \begin{bmatrix} 4 \\ 6 \end{bmatrix}\)$
Scalar Multiplication
Multiply each element by a scalar (single number):
Example: $\(2 \cdot \begin{bmatrix} 1 \\ 3 \end{bmatrix} = \begin{bmatrix} 2 \\ 6 \end{bmatrix}\)$
The Dot Product: The Heart of Similarity
The dot product (also called inner product) is THE fundamental operation for finding similarity between vectors.
Definition
For vectors \(\vec{u}\) and \(\vec{v}\) of length \(n\):
Why It Measures Similarity
The dot product is large and positive when vectors point in the same direction (similar).
The dot product is small or negative when vectors point in different directions (dissimilar).
Example
u = np.array([1, 0])
v = np.array([1, 0])
dot_product = np.dot(u, v) # 1
w = np.array([0, 1])
dot_product_2 = np.dot(u, w) # 0
Magnitude (Norm) of a Vector
The magnitude or norm of a vector \(\vec{v}\) is its length:
This is also called the L2 norm or Euclidean norm.
Example
Normalization: The Key to Fair Comparison
Here's a critical insight: the dot product alone doesn't measure similarity fairly if vectors have different magnitudes.
For example, a long vector pointing in a similar direction as a short vector will have a larger dot product, even though they point the same direction.
Solution: Normalize vectors to unit length (magnitude = 1):
This gives us the unit vector, which points in the same direction but has length 1.
Example
Verify: \(\|\hat{\vec{v}}\| = \sqrt{0.6^2 + 0.8^2} = \sqrt{0.36 + 0.64} = \sqrt{1} = 1\) ✓
v = np.array([3, 4])
v_normalized = v / np.linalg.norm(v) # [0.6, 0.8]
print(np.linalg.norm(v_normalized)) # 1.0
Cosine Similarity: The Standard for Embeddings
When vectors are normalized, the dot product has a special name and property:
Why "cosine"? Because it equals the cosine of the angle between the vectors:
where \(\theta\) is the angle between them.
Properties
- Range: \([-1, 1]\)
- 1: vectors point exactly the same direction (identical)
- 0: vectors are perpendicular (unrelated)
- -1: vectors point opposite directions (opposite meaning)
Example
u = np.array([1, 0, 0])
v = np.array([1, 1, 0])
# Manual calculation
dot = np.dot(u, v)
norm_u = np.linalg.norm(u)
norm_v = np.linalg.norm(v)
cosine_sim = dot / (norm_u * norm_v)
print(cosine_sim) # 0.707...
# Or use sklearn
from sklearn.metrics.pairwise import cosine_similarity
sim = cosine_similarity([u], [v])
print(sim) # [[0.707...]]
Dot Product in High Dimensions
In RAG systems, embeddings are hundreds or thousands of dimensions (e.g., 384-dim or 1536-dim). The dot product still works the same way mathematically, but:
- Intuition: Gets harder to visualize (you can't easily imagine 1536-dimensional space)
- Computation: Is very fast with modern hardware
- Behavior: In very high dimensions, random vectors are often surprisingly perpendicular and dissimilar
This is sometimes called the curse of dimensionality.
Matrices: Collections of Vectors
A matrix is an organized collection of vectors. Each row (or column) is a vector.
For example, if you have 100 text documents, each embedded into 384 dimensions, you get a 100 × 384 matrix.
Matrix-Vector Multiplication
Multiplying a matrix by a vector applies the dot product to each row:
This is extremely useful for RAG: compute the similarity of a query vector to all stored document vectors in one operation!
# 100 documents, each 384-dim
documents = np.random.randn(100, 384)
# 1 query, 384-dim
query = np.random.randn(1, 384)
# Compute similarity to all documents (1 operation!)
similarities = np.dot(documents, query.T) # Shape: (100, 1)
print(similarities.shape)
Summary
| Concept | What It Is | Why It Matters |
|---|---|---|
| Vector | Ordered list of numbers | Each embedding is a vector |
| Dot Product | \(\sum u_i v_i\) | Measures similarity |
| Magnitude (Norm) | \(\sqrt{\sum v_i^2}\) | Measures vector length |
| Normalization | Divide by magnitude | Makes fair comparisons |
| Cosine Similarity | Normalized dot product | Standard similarity metric in RAG |
| Matrix | Table of numbers (vectors as rows) | Efficient batch operations |
Practice Problems
Problem 1
Calculate the dot product of \(\vec{u} = [2, 3]\) and \(\vec{v} = [4, 1]\).
Solution: \(2 \cdot 4 + 3 \cdot 1 = 8 + 3 = 11\)
Problem 2
Find the magnitude of \(\vec{v} = [3, 4, 12]\).
Solution: \(\|\vec{v}\| = \sqrt{9 + 16 + 144} = \sqrt{169} = 13\)
Problem 3
Normalize \(\vec{v} = [3, 4]\).
Solution: \(\|\vec{v}\| = 5\), so \(\hat{\vec{v}} = [0.6, 0.8]\)
Next Steps
Now that you understand the math, move to Probability & Statistics to understand TF-IDF and term importance, or jump directly to Embeddings.