← Matrices · guided lesson

Matrices & Simulation

1 / 19 · story

the story

Row-major layout & index maths

Every tool here exists because something was too slow or broke without it. Here is what.

1957 · 1972 · John Backus's IBM FORTRAN team (column order); Dennis Ritchie at Bell Labs (C, row order)

A grid has rows and columns, but memory is one long line of numbered slots. Every language must decide which cell comes next. FORTRAN (IBM, 1957) chose columns: the 1957 paper's address formula for A(I, J, K) multiplies J by the size of the first dimension, so the first subscript moves fastest. C, which grew up at Bell Labs between 1969 and 1973, chose rows: the C standard says arrays are stored in row-major order, the last subscript varying fastest. Java (1996) made int[][] an array of references to row arrays (JLS chapter 10). Each row is its own int[], so walking along a row is the fast direction.

the problem it solved

Turning a two-dimensional table into addresses in one-dimensional memory, with one multiply and one add per lookup.

why it matters: without it

Every grid would need its own map from (row, col) to memory, and image buffers, flat visited arrays and cell ids like r * cols + c would all be ad hoc.

sources: Backus et al. — The FORTRAN Automatic Coding System (Western Joint Computer Conference, 1957), index formula for A(I, J, K) ↗ · ISO C committee draft N1570, §6.5.2.1 ¶3: "arrays are stored in row-major order" ↗ · Dennis Ritchie — The Development of the C Language (HOPL-II, 1993) ↗ · Java Language Specification (SE 17), Chapter 10: Arrays ↗

also in this chapter: Direction arrays & boundary walks · Rotation by transpose + reverse · In-place markers (row 0 / column 0, spare bits) · Cellular automata & generation updates