Mathematics as Code · Math as Code
Lesson 2: Algebraic Representation in Computing - Vectors, Matrices, and Arrays
Explanation
Introduction & Intuition
In linear algebra, we are accustomed to working with vectors and matrices as abstract mathematical entities that can be added, multiplied, and transformed. However, when we want to write code to perform these operations (such as in computer graphics engines or neural networks), we must translate these two-dimensional structures into the computer's memory architecture.
Intuition: Physical computer memory is not a 2D grid; it is a long, 1D "tape" of addresses. Therefore, any mathematical structure must ultimately be represented as a 1D sequence of cells. The most fundamental structure for this in programming languages is the Array – a contiguous block of memory where each element is instantly accessible via its index.
Formal Definition & Theorems
1. Vector Representation
A vector translates into an array of length .Engineering Baseline: While in math it is customary to start indices at (i.e., ), most modern programming languages (C, Python, JS, TypeScript) are 0-indexed. Thus, the -th element of the mathematical vector is accessed in the array at position `v[i-1]`.
2. Matrix Flattening
A matrix with rows and columns can be represented in two main ways:1. Array of Arrays: Each row is a separate array. Access pattern: `A[i][j]`.
2. 1D Flattened Array: The entire matrix is stored in one contiguous memory block of length . This approach is significantly more efficient in terms of performance.
The Mapping Theorem (Row-Major Order):
In a row-by-row storage format (common in C and Python), the mathematical element (assuming 0-based indexing) will be mapped to the 1D array at index according to the formula:
where is the total number of columns (the width of the matrix).
Performance Analysis & Edge Cases
Cache Locality
Because computer memory is loaded into the CPU in blocks (Cache Lines), reading adjacent data is extremely fast (up to 100x faster than random access).Consequently, if we iterate over a matrix (stored in Row-Major) row-by-row, the code runs blazingly fast. Conversely, if we iterate column-by-column (causing large memory jumps), we lose the cache advantage, resulting in a dramatic performance penalty (Cache Misses).
Edge Cases
- Out-of-Bounds Dot Product: Attempting a dot product between vectors of different dimensions will trigger an "Index Out of Bounds" exception or undefined behavior.- Matrix Multiplication: To perform , the prerequisite is that the width of equals the height of . In code, this must be explicitly validated before starting the execution loops.
Visual UI Component Ideas
- Flattening Animator: An animation of a grid where rows break apart, are highlighted in different colors, and seamlessly link end-to-end to form a single 1D memory strip.- Cache Hit/Miss Visualizer: A pointer traversing a 1D array; when scanning properly (row-by-row), the pointer moves in quick, continuous green strides. When scanning column-by-column, the pointer jumps erratically back and forth, pausing with red warnings.
Interactive Visualization
Loading visualization
Worked Examples
Practice
Exercise 1 of 5
1
Given a matrix represented as a 1D array in Row-Major order, with rows and columns. What is the correct formula to find the 1D index for the element at row and column (assuming 0-indexing)?
★Quiz
Test your knowledge from this lesson
A mix of questions from the lesson — each attempt varies slightly
Answer at least 4 out of 5 questions correctly to pass (70%)