Documentation

CslibFrontier.Foundations.Data.Matrix.Binary

Binary words for power-of-two residue matrices #

A rectangular matrix over ZMod (2 ^ w) is represented row by row, with w low-order-first bits per entry. The dimensions are external parameters. Decoding arbitrary words pads missing bits with false and ignores excess bits, including when a dimension or the width is zero.

Main definitions #

Main results #

def Cslib.Matrix.toBinary {rows cols w : ℕ} (A : Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))) :

Fixed-width row-major encoding; the digits of each residue are least significant first.

def Cslib.Matrix.ofBinary (rows cols w : ℕ) (word : List Bool) :
Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))

Read the declared matrix dimensions, padding missing bits with false and ignoring excess bits. Zero-width entries are the unique residue modulo one.

def Cslib.Matrix.binaryEntry (cols w : ℕ) (word : List Bool) (i j : ℕ) :

Read one low-order-first residue word at an arbitrary row and column. Missing bits are false; the declared column count determines the stride.

theorem Cslib.Matrix.binaryEntry_lt (cols w : ℕ) (word : List Bool) (i j : ℕ) :
binaryEntry cols w word i j < 2 ^ w

An entry read from w digits is below 2 ^ w.

theorem Cslib.Matrix.ofBinary_apply (rows cols w : ℕ) (word : List Bool) (i : Fin rows) (j : Fin cols) :
ofBinary rows cols w word i j = ↑(binaryEntry cols w word ↑i ↑j)

List slicing and the finite matrix decoder use the same coordinates.

@[simp]
theorem Cslib.Matrix.length_toBinary {rows cols w : ℕ} (A : Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))) :
(toBinary A).length = rows * cols * w

Encoding writes every entry's declared width, including leading zero digits.

theorem Cslib.Matrix.toBinary_eq {rows cols w : ℕ} (A : Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))) :
toBinary A = (List.ofFn fun (i : Fin rows) => (List.ofFn fun (j : Fin cols) => List.ofFn fun (k : Fin w) => (A i j).val.testBit ↑k).flatten).flatten

The row-major representation is the concatenation of each row's low-order-first words.

@[simp]
theorem Cslib.Matrix.ofBinary_toBinary {rows cols w : ℕ} (A : Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))) :
ofBinary rows cols w (toBinary A) = A

Canonical fixed-width words decode exactly, including empty dimensions and width zero.

theorem Cslib.Matrix.toBinary_ofBinary {rows cols w : ℕ} {word : List Bool} (h : word.length = rows * cols * w) :
toBinary (ofBinary rows cols w word) = word

A word of the declared length is the word of the matrix it decodes to.

@[simp]
theorem Cslib.Matrix.binaryEntry_toBinary {rows cols w : ℕ} (A : Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w))) (i : Fin rows) (j : Fin cols) :
↑(binaryEntry cols w (toBinary A) ↑i ↑j) = A i j

Reading an entry of a matrix's word recovers the entry.

theorem Cslib.Matrix.binaryEntry_eq_zero_of_length_le {cols w : ℕ} {word : List Bool} {i j : ℕ} (h : word.length ≤ (i * cols + j) * w) :
binaryEntry cols w word i j = 0

An entry whose digits lie beyond the end of the word reads as 0.

def Cslib.Matrix.encBinary (rows cols w : ℕ) :
Matrix (Fin rows) (Fin cols) (ZMod (2 ^ w)) ↪ List Bool

Fixed-dimensional matrices inject into their complete binary words. This fixed-type embedding alone makes no claim of uniform computational efficiency in the dimensions.

The w low-order digits of a residue modulo 2 ^ w are those of every natural number representing it.

theorem Cslib.BitString.lowBits_eq_ofFn_val (w x : ℕ) :
lowBits w x = List.ofFn fun (t : Fin w) => (↑x).val.testBit ↑t

The w low-order digits of a natural number are the digits of the residue it represents modulo 2 ^ w.