๐ŸŽ“ Matrix Learning Lab

Interactive visualizations and explorations for linear algebra โ€” from first principles to matrix decompositions

๐ŸŽฏ What Are Matrices?

Welcome to your interactive journey into linear algebra! Let's start with the absolute basics and build up from there.

Part 1: A Matrix is Just a Grid of Numbers

Think of it like a table or spreadsheet:

โ”‚  1   2  โ”‚
โ”‚  3   4  โ”‚

This is a 2ร—2 matrix (2 rows, 2 columns).
We read it as "2 by 2".

Part 2: Larger Matrices and Real Data

Matrices can be any size! Here's a 3ร—3 matrix:

โ”‚  5   8   3  โ”‚
โ”‚  2   9   7  โ”‚
โ”‚  6   1   4  โ”‚

This is a 3ร—3 matrix (3 rows, 3 columns).

In the real world, matrices store actual data. Each row might represent a person, and each column a different feature:

๐Ÿ“Š Real-Life Data Matrix Example:
Person Age Income ($) Health Score Zipcode Population Density
Person 1 25 45,000 85 10001 12,500
Person 2 42 78,000 92 90210 8,200
Person 3 67 95,000 78 60601 15,800
Person 4 31 62,000 88 77001 9,100

As a matrix: 4 people ร— 5 features = 4ร—5 matrix

โ”‚ 25   45000   85   10001   12500 โ”‚
โ”‚ 42   78000   92    90210    8200 โ”‚
โ”‚ 67   95000   78    60601   15800 โ”‚
โ”‚ 31   62000   88    77001    9100 โ”‚

Real-World Applications

๐ŸŽฎ Computer Graphics

Rotate, scale, and move 3D game objects. Every rotation in a video game uses matrices!

๐Ÿ“ฑ Image Filters

Instagram filters? Matrices! They transform pixel colors to create effects like blur, sharpen, and edge detection.

๐Ÿค– Machine Learning

Neural networks are built from matrices. Every AI model you use (ChatGPT, image recognition) runs on matrix math!

๐Ÿ“Š Data Analysis

Organize and analyze large datasets. Matrices help find patterns in millions of data points (like Netflix recommendations).

โšก Engineering

Model electrical circuits, structural forces, and quantum mechanics. Engineers use matrices to predict how systems behave.

๐Ÿ” Cryptography

Encrypt secret messages! Some encryption methods multiply your message by a secret matrix to scramble it.

๐ŸŽฏ Lesson 2: Matrix Arithmetic

Learning Objectives:
โœ“ Master matrix addition and subtraction
โœ“ Understand matrix multiplication (it's different!)
โœ“ Learn transpose, determinant, and inverse operations

Part 1: Addition & Subtraction (The Easy Ones!)

Good news: Adding matrices is like adding regular numbers โ€” just do it element-by-element!

Example: Add these 2ร—2 matrices
โ”‚ 1  2 โ”‚
โ”‚ 3  4 โ”‚
Matrix A
+
โ”‚ 5  6 โ”‚
โ”‚ 7  8 โ”‚
Matrix B
=
โ”‚  6   8 โ”‚
โ”‚ 10  12 โ”‚
Result

Each element adds: (1+5=6, 2+6=8, 3+7=10, 4+8=12)

โš ๏ธ Important Rule:
You can only add/subtract matrices with the SAME dimensions!
Valid: A 2ร—3 matrix + 2ร—3 matrix.
Invalid: A 2ร—2 matrix + 3ร—3 matrix.
Not Possible: Adding different-sized matrices
โ”‚ 1  2 โ”‚
โ”‚ 3  4 โ”‚
Matrix A
+
โ”‚ 5  6  7 โ”‚
โ”‚ 8  9 10 โ”‚
โ”‚11 12 13 โ”‚
Matrix B
ร—
Cannot add
Different dimensions

Part 2: Matrix Multiplication (The Tricky One!)

Warning: Matrix multiplication is NOT element-by-element! It's more like "mix and combine".

The Rule: To multiply matrices A ร— B:
Take a row from A (shown above: Row 1 of A = [1, 2])
Take a column from B (shown above: Column 1 of B = [5, 7])
Multiply corresponding elements and add them up: (1ร—5) + (2ร—7) = 5 + 14 = 19
That's one entry in the result!
๐Ÿ’ก Dimension Rule:
You can only multiply A ร— B if columns_of_A = rows_of_B.
Valid: A (2ร—3) ร— B (3ร—4) = โœ“ Valid (result is 2ร—4).
Invalid: A (2ร—3) ร— B (4ร—2) = โœ— Invalid!
Worked Example: A valid matrix multiplication
โ”‚ 1  2  3 โ”‚
โ”‚ 4  5  6 โ”‚
Matrix A (2ร—3)
ร—
โ”‚ 7   8 โ”‚
โ”‚ 9  10 โ”‚
โ”‚11  12 โ”‚
Matrix B (3ร—2)
=
โ”‚  58   64 โ”‚
โ”‚ 139  154 โ”‚
Result (2ร—2)

Part 3: Transpose (The Flipper!)

The transpose of a matrix flips rows into columns (and columns into rows). In real life, you might need to transpose data when your features are stored as rows but your algorithm expects them as columns (or vice versa).

โ”‚ 1  2  3 โ”‚
โ”‚ 4  5  6 โ”‚
Original (2ร—3)
โ†’ Aแต€ โ†’
โ”‚ 1  4 โ”‚
โ”‚ 2  5 โ”‚
โ”‚ 3  6 โ”‚
Transposed (3ร—2)

Notice: Rows became columns! The first row [1,2,3] became the first column.

Part 4: Determinant (The "Size" of a Matrix)

The determinant tells you whether a matrix is invertible, meaning the transformation preserves information so the original input can be uniquely recovered, and it also measures how the transformation scales area. For 2ร—2 matrices

โ”‚  a   b  โ”‚
โ”‚  c   d  โ”‚
det(A) = ad - bc
Example:
Matrix: โ”‚  2   3  โ”‚
        โ”‚  1   4  โ”‚

det = (2ร—4) - (3ร—1) = 8 - 3 = 5

This means the area or volume would scale by 5.

๐Ÿ” What does determinant mean?
โ€ข det = 0 โ†’ Matrix is "singular" (not invertible, collapses space)
โ€ข det > 0 โ†’ Matrix preserves orientation
โ€ข det < 0 โ†’ Matrix flips orientation (mirrors)
โ€ข |det| = scaling factor (how much the matrix stretches area/volume)
๐Ÿ“ Determinant Examples
Case 1: det > 0 โ€” Preserves Orientation
โ”‚ 2 0 โ”‚ โ”‚ 0 3 โ”‚
Scaling
ร—
โ”‚ 1 โ”‚ โ”‚ 1 โ”‚
Vector
=
โ”‚ 2 โ”‚ โ”‚ 3 โ”‚
Stretched

det = (2ร—3) โˆ’ (0ร—0) = 6 โ€” stretches but keeps orientation

Case 2: det < 0 โ€” Flips Orientation
โ”‚ 1 0 โ”‚ โ”‚ 0 -1 โ”‚
Reflection
ร—
โ”‚ 1 โ”‚ โ”‚ 1 โ”‚
Vector
=
โ”‚ 1 โ”‚ โ”‚ -1 โ”‚
Flipped

det = (1ร—โˆ’1) โˆ’ (0ร—0) = โˆ’1 โ€” inverts orientation (mirror)

Case 3: det = 0 โ€” Singular (Collapses Space)
โ”‚ 1 2 โ”‚ โ”‚ 2 4 โ”‚
Dep. rows
ร—
โ”‚ 1 โ”‚ โ”‚ 1 โ”‚
Any vector
=
โ”‚ 3 โ”‚ โ”‚ 6 โ”‚
Same dir.

det = (1ร—4) โˆ’ (2ร—2) = 0 โ€” not invertible, collapses to a line

Exercise 3: Determinant

Find det(A) for:

          A =       
              โ”‚ 3  -2โ”‚
              โ”‚ 1   4โ”‚
                    
Show Solution
det(A) = (3ร—4) - (-2ร—1) = 12 - (-2) = 12 + 2 = 14
Exercise 4: Determinant & Area

Calculate det(B) and predict how it transforms the area of a 2ร—2 square:

          B =
              โ”‚ 0.5   0 โ”‚
              โ”‚ 0    0.5โ”‚
                    
Show Solution
det(B) = (0.5ร—0.5) - (0ร—0) = 0.25

Area scales by factor of 0.25 (shrinks to 1/4 the original area)

๐Ÿ’ป Interactive Matrix Calculator

ร—

๐ŸŽฏ Lesson 3: Matrix Transformations

A matrix is an operator that rotates vectors and shapes in geometric transformations.

Vector Rotation Card

Enter a vector and angle to see the rotation matrix act on the vector in real time.

โŒ„

Rotation Angle

Rotation Matrix

0.707 -0.707 0.707 0.707

Vector Input and Output

Vector Input
Vector Output
x-component 0.71
y-component 3.54

Live Rotation Preview

Original Rotated

Rotation uses $R(\theta)=\begin{bmatrix}\cos\theta & -\sin\theta \\ \sin\theta & \cos\theta\end{bmatrix}$, so changing the angle updates the matrix entries and the vector output together.

Scaling + Rotation Card

Use a generic matrix to rotate and scale a vector at the same time.

โŒ„

Angle and Scaling

Generic Matrix

1.299 -0.400 0.750 0.693

Vector Input and Output

Input Vector
Transformed Vector
x-component 1.998
y-component 2.539

Generic Matrix Preview

This matrix combines rotation with independent scaling along the x and y directions.

Input Transformed

A generic transform can be written as $A = R(\theta)\begin{bmatrix}s_x & 0 \\ 0 & s_y\end{bmatrix}$, which means the vector is rotated and scaled in one transformation.

Image Rotation Card

Use separate image controls to rotate a cartoon frame the way image coordinates are rotated in geometric processing.

โŒ„

Image Rotation Controls

Figure Readout

Figure Type Image card
Current Rotation 45.00ยฐ

Image Rotation Matrix

0.707 -0.707 0.707 0.707

Image and Shape Transformation Preview

Switch between an image card and simple shapes to see that the same rotation matrix applies to many different kinds of figures.

Original image Transformed image 45ยฐ rotation

The same rotation matrix can act on image coordinates or on geometric shapes, which is why both pictures and abstract figures can be rotated with the same transformation rule.

๐ŸŽฏ Lesson 4: Solving Systems with Matrices

Matrices are used to solve equations with many variables.

Step 1: Convert to Matrix Form
System of Equations:
2x + 3y = 8
x - y = 1
โ†’
Matrix Form (AV = b):
                  
โ”‚ 2  3โ”‚ ยท โ”‚ xโ”‚ = โ”‚ 8โ”‚
โ”‚ 1 -1โ”‚   โ”‚ yโ”‚   โ”‚ 1โ”‚
                  
   A        V       b

A = coefficients arranged as a matrix, V = unknown variables (X, Y), b = right-hand side

Step 2: Solve using Matrix Inverse

If A is invertible, the solution is: V = Aโปยนb

Example: Solve the system above
Aโปยน =         
โ”‚ 0.2  0.6โ”‚
โ”‚ 0.2 -0.4โ”‚

V = Aโปยนb

โ”‚ xโ”‚ = โ”‚ 0.2  0.6โ”‚ ยท โ”‚ 8โ”‚ = โ”‚2.2โ”‚
โ”‚ yโ”‚   โ”‚ 0.2 -0.4โ”‚   โ”‚ 1โ”‚   โ”‚1.2โ”‚

The Three Cases: How Many Solutions?

Quick summary: A system can have one solution, infinitely many solutions, or no solution. If det(A) โ‰  0, there is one unique solution. If det(A) = 0, the system either has infinitely many solutions or no solution, depending on whether the equations are consistent.

๐Ÿ’ป Interactive System Solver

Solve your own linear systems! Enter the number of equations and let the matrix solver work its magic.

๏ฟฝ Lesson 5: Eigenvalues โ€” The Magic Directions

Learning Objectives:
โœ“ Understand what eigenvalues and eigenvectors represent geometrically
โœ“ Learn how to find eigenvalues (det(A - ฮปI) = 0)
โœ“ Recognize why eigenvalues matter in applications

The Mind-Blowing Idea

Most matrices rotate AND stretch vectors. But there are special directions where the matrix ONLY stretches!

The Eigenvalue Equation
Av = ฮปv
A
The matrix
v
Eigenvector
(special direction)
ฮป
Eigenvalue
(stretch factor)

"Matrix A just scales vector v by ฮป โ€” no rotation!"

Example: See It In Action

Matrix A =          Eigenvector v =        Eigenvalue ฮป = 4
           โ”‚ 3 1โ”‚                    โ”‚ 1โ”‚
           โ”‚ 0 4โ”‚                    โ”‚ 0โ”‚
                                       

Let's verify Av = ฮปv:

Av =       ร—     =         =    
     โ”‚ 3 1โ”‚   โ”‚ 1โ”‚   โ”‚ 3ร—1+1ร—0โ”‚   โ”‚ 3โ”‚
     โ”‚ 0 4โ”‚   โ”‚ 0โ”‚   โ”‚ 0ร—1+4ร—0โ”‚   โ”‚ 0โ”‚
                                   โ† This should equal 4v

ฮปv = 4 ร—     =       โ† They DON'T match! So v=[1,0] is NOT an eigenvector.
         โ”‚ 1โ”‚   โ”‚ 4โ”‚
         โ”‚ 0โ”‚   โ”‚ 0โ”‚
                  

Try v =    :
        โ”‚ 1โ”‚
        โ”‚ 1โ”‚
           

Av =       ร—     =        and    4v =       โœ“ Match! This IS an eigenvector!
     โ”‚ 3 1โ”‚   โ”‚ 1โ”‚   โ”‚ 4โ”‚                  โ”‚ 4โ”‚
     โ”‚ 0 4โ”‚   โ”‚ 1โ”‚   โ”‚ 4โ”‚                  โ”‚ 4โ”‚
                                         

How to Find Eigenvalues (The Algorithm)

Step 1: Set up the characteristic equation
det(A - ฮปI) = 0

Why? We need Av = ฮปv, which rearranges to (A - ฮปI)v = 0.
This only has non-trivial solutions when det(A - ฮปI) = 0.
Step 2: Calculate the determinant (gives a polynomial)
Example: A =      
             โ”‚ 4 2โ”‚
             โ”‚ 1 3โ”‚
                  

A - ฮปI =          
         โ”‚ 4-ฮป   2โ”‚
         โ”‚  1  3-ฮปโ”‚
                  

det(A - ฮปI) = (4-ฮป)(3-ฮป) - (2)(1)
            = 12 - 4ฮป - 3ฮป + ฮปยฒ - 2
            = ฮปยฒ - 7ฮป + 10
Step 3: Solve for ฮป (find roots)
ฮปยฒ - 7ฮป + 10 = 0
(ฮป - 5)(ฮป - 2) = 0

Eigenvalues: ฮปโ‚ = 5, ฮปโ‚‚ = 2
Step 4: Find eigenvectors (plug ฮป back in)
For ฮป = 5:  (A - 5I)v = 0  โ†’      v = 0  โ†’  Eigenvector vโ‚ =    
                                   โ”‚-1  2โ”‚                       โ”‚ 2โ”‚
                                   โ”‚ 1 -2โ”‚                       โ”‚ 1โ”‚
                                                                 

Why Eigenvalues Matter

๐ŸŽฌ Google PageRank
The web is a giant matrix! Google finds the "importance" eigenvector of this matrix.
๐Ÿ”ฌ Physics & Engineering
Vibration modes, quantum mechanics, stability analysis โ€” all use eigenvalues!
๐Ÿ“Š Principal Component Analysis (PCA)
Data science's favorite tool! Uses eigenvectors to find important patterns in data.
๐ŸŽฎ Computer Graphics
Eigenvectors define "principal axes" for 3D object rotations.
๐Ÿ” Quick Facts:
โ€ข An nร—n matrix has n eigenvalues (counting multiplicities)
โ€ข Symmetric matrices โ†’ always real eigenvalues
โ€ข Trace(A) = sum of eigenvalues
โ€ข det(A) = product of eigenvalues
โ€ข Eigenvalues of diagonal matrices = the diagonal entries!

๐Ÿ’ป Compute & Visualize

Generate a random square matrix to explore its eigenvalues and eigenvectors.

๐Ÿ“Š Visualization

๐Ÿ’ก How to read this plot:
โ€ข Arrows (Real Eigenvalues): Show the eigenvectors ($v$) and how they get scaled ($\lambda v$).
โ€ข Points (Complex Eigenvalues): Show the eigenvalues on the complex plane (Rotation + Scaling).

๐ŸŽฏ Lesson 6: SVD & PCA โ€” Data Science Superpowers

Learning Objectives:
โœ“ Understand Singular Value Decomposition (A = UฮฃVแต€)
โœ“ Learn how SVD enables data compression
โœ“ Master Principal Component Analysis (PCA) for dimensionality reduction

SVD: The Ultimate Matrix Factorization

Every matrix can be broken down into three simple pieces!

A = U ฮฃ Vแต€
A
Original
Matrix
=
U
Rotate
(left)
ฮฃ
Stretch
(diagonal)
Vแต€
Rotate
(right)

Any transformation = Rotate โ†’ Stretch โ†’ Rotate!

The Three Pieces Explained

U (Left Singular Vectors)
โ€ข Orthogonal matrix (columns are perpendicular)
โ€ข Defines output space directions
โ€ข Size: m ร— m
ฮฃ (Singular Values)
โ€ข Diagonal matrix with ฯƒโ‚ โ‰ฅ ฯƒโ‚‚ โ‰ฅ ... โ‰ฅ 0
โ€ข Shows "importance" of each direction
โ€ข Large ฯƒ = important, small ฯƒ = can be dropped!
Vแต€ (Right Singular Vectors)
โ€ข Orthogonal matrix (rows are perpendicular)
โ€ข Defines input space directions
โ€ข Size: n ร— n

๐Ÿ—œ๏ธ The Magic: Data Compression!

Keep only the TOP k singular values โ†’ throw away the rest โ†’ get close approximation with less data!

Example: Compress an Image
Original 1000ร—1000 image matrix = 1,000,000 numbers

After SVD: Keep top 50 singular values
โ†’ Storage needed: 50 ร— (1000 + 1000 + 1) โ‰ˆ 100,000 numbers
โ†’ Compression ratio: 10x smaller! Only 10% of original size

Result: Image looks almost identical but file is 90% smaller! ๐ŸŽ‰

This is exactly what JPEG compression does! (with a twist using DCT, a cousin of SVD)

๐Ÿ“Š PCA: Finding Patterns in Data

Principal Component Analysis uses SVD to find the "most important directions" in your data.

The PCA Recipe (4 Steps)
  1. Center the data: Subtract mean from each feature
  2. Compute covariance matrix: Shows how features vary together
  3. Find eigenvalues & eigenvectors: Get principal components (eigenvectors of covariance matrix)
  4. Project onto top components: Reduce from 100D โ†’ 2D!
Real Example: Netflix Recommendations
โ€ข You have 10,000 movies (10,000 dimensions!)
โ€ข PCA finds ~50 "hidden factors" (action, romance, comedy, etc.)
โ€ข Each user's taste = combination of these 50 factors
โ€ข 10,000D โ†’ 50D = 200x compression! ๐Ÿš€
๐Ÿ’ก Key Insight: Not all directions matter equally! SVD/PCA find the directions with the MOST variation. Drop the boring directions, keep the interesting ones.

๐Ÿ’ป Interactive SVD Decomposition

Generate a random matrix or enter one to see its Singular Value Decomposition.

๐ŸŽฏ Interactive PCA Analysis

Generate a random dataset (rows=samples, cols=features) and find its principal components.