Documentation

CslibFrontier.Foundations.Data.Nat.Morton

Morton indexing for square matrices #

Nat.Morton.index a b interleaves the binary row and column digits, with the row digit in the higher bit of each pair. Its inverse projections are row and column. For a square of side 2^(k+1), the four contiguous blocks of length 4^k are exactly its four quadrants.

These elementary digit identities adapt Spec/Sec3/Theorem17/ZOrder.lean, lines 39–151, at anthropics/formal-math revision 16a5ce41304fcdaea4e0be7c132e1c361dc26da5. The native counter will compute the spread table in memory; these definitions do not supply a table oracle.

Move each binary digit to an even position, preserving its least-significant-first order.

def Nat.Morton.index (a b : ℕ) :

Interleaved row and column number, with row bits above column bits.

Recover row bits from a Morton position.

Recover column bits from a Morton position.

theorem Nat.Morton.spread_eq (i : ℕ) :
spread i = i % 2 + 4 * spread (i / 2)

The native spread-table recurrence needs only an earlier entry and the last binary bit.

@[simp]

No bits are set in the spread of zero.

theorem Nat.Morton.row_eq (z : ℕ) :
row z = z % 4 / 2 + 2 * row (z / 4)

Extract the last row bit and recurse on the remaining base-four digits.

theorem Nat.Morton.column_eq (z : ℕ) :
column z = z % 4 % 2 + 2 * column (z / 4)

Extract the last column bit and recurse on the remaining base-four digits.

@[simp]

Position zero has row zero.

@[simp]

Position zero has column zero.

theorem Nat.Morton.index_eq (a b : ℕ) :
index a b = 2 * (a % 2) + b % 2 + 4 * index (a / 2) (b / 2)

The paired last binary bits form the last base-four digit.

theorem Nat.Morton.index_lt {k a b : ℕ} (ha : a < 2 ^ k) (hb : b < 2 ^ k) :
index a b < 4 ^ k

A side-2^k square occupies exactly the first 4^k Morton positions.

theorem Nat.Morton.spread_lt {k i : ℕ} (hi : i < 2 ^ k) :
spread i < 4 ^ k

Each computed spread-table entry fits in the corresponding base-four square span.

@[simp]
theorem Nat.Morton.row_index (a b : ℕ) :
row (index a b) = a

Row projection inverts the interleaved position.

@[simp]
theorem Nat.Morton.column_index (a b : ℕ) :
column (index a b) = b

Column projection inverts the interleaved position.

@[simp]
theorem Nat.Morton.index_row_column (z : ℕ) :
index (row z) (column z) = z

Re-interleaving the projections recovers every original position.

theorem Nat.Morton.row_column_quadrant {k t z : ℕ} (ht : t < 4) (hz : z < 4 ^ k) :
row (t * 4 ^ k + z) = row z + t / 2 * 2 ^ k ∧ column (t * 4 ^ k + z) = column z + t % 2 * 2 ^ k

Reading a contiguous quadrant is the same as shifting its row and column by half a side.

theorem Nat.Morton.row_column_lt {k z : ℕ} (hz : z < 4 ^ k) :
row z < 2 ^ k ∧ column z < 2 ^ k

Projection of an admitted Morton position remains inside the corresponding square.