Documentation

CslibFrontier.Foundations.Data.Matrix.RowTable

Polymorphic row-major runtime matrices (Cslib.RowTable) #

Cslib.RowTable α represents a dense matrix of runtime dimensions numRows × numCols stored in row-major order as rows : List (List α). Out-of-bounds reads via RowTable.entry default to 0, making RowTable.toMatrix total on arbitrary (possibly ragged) tables while forming a two-sided equivalence with Matrix (Fin m) (Fin n) α on RowTable.WellFormed tables.

Main definitions #

Main results #

structure Cslib.RowTable (α : Type u_1) :
Type u_1

Explicit row-major matrix of runtime dimensions numRows × numCols.

  • numRows : ℕ

    Declared number of rows.

  • numCols : ℕ

    Declared number of columns.

  • rows : List (List α)

    Row-major list of rows.

Instances For
    theorem Cslib.RowTable.ext {α : Type u_1} {x y : RowTable α} (numRows : x.numRows = y.numRows) (numCols : x.numCols = y.numCols) (rows : x.rows = y.rows) :
    x = y
    theorem Cslib.RowTable.ext_iff {α : Type u_1} {x y : RowTable α} :
    def Cslib.instReprRowTable.repr {α✝ : Type u_1} [Repr α✝] :
    RowTable α✝ → ℕ → Std.Format
    @[instance_reducible]
    instance Cslib.instReprRowTable {α✝ : Type u_1} [Repr α✝] :
    Repr (RowTable α✝)
    def Cslib.instDecidableEqRowTable.decEq {α✝ : Type u_1} [DecidableEq α✝] (x✝ x✝¹ : RowTable α✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    instance Cslib.instDecidableEqRowTable {α✝ : Type u_1} [DecidableEq α✝] :

    Rectangular shape invariant and functorial map #

    def Cslib.RowTable.WellFormed {α : Type u_1} (T : RowTable α) :

    Rectangular shape invariant: T.rows has T.numRows rows, each of length T.numCols.

    Instances For
      @[instance_reducible]
      def Cslib.RowTable.map {α : Type u_1} {β : Type u_2} (f : α → β) (T : RowTable α) :

      Entrywise map on a RowTable, preserving declared dimensions.

      @[simp]
      theorem Cslib.RowTable.numRows_map {α : Type u_1} {β : Type u_2} (f : α → β) (T : RowTable α) :

      map preserves numRows.

      @[simp]
      theorem Cslib.RowTable.numCols_map {α : Type u_1} {β : Type u_2} (f : α → β) (T : RowTable α) :

      map preserves numCols.

      @[simp]
      theorem Cslib.RowTable.rows_map {α : Type u_1} {β : Type u_2} (f : α → β) (T : RowTable α) :

      Rows of T.map f.

      @[simp]
      theorem Cslib.RowTable.map_id {α : Type u_1} (T : RowTable α) :
      map id T = T

      Mapping the identity function returns T unchanged.

      theorem Cslib.RowTable.map_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} (g : β → γ) (f : α → β) (T : RowTable α) :
      map g (map f T) = map (g ∘ f) T

      Composition law for RowTable.map.

      theorem Cslib.RowTable.WellFormed.map {α : Type u_1} {β : Type u_2} {T : RowTable α} (hT : T.WellFormed) (f : α → β) :

      map preserves WellFormed.

      def Cslib.RowTable.tabulate {α : Type u_1} (m n : ℕ) (g : ℕ → ℕ → α) :

      Construct the m × n RowTable whose (i, j) entry is g i j for i < m and j < n.

      @[simp]
      theorem Cslib.RowTable.numRows_tabulate {α : Type u_1} (m n : ℕ) (g : ℕ → ℕ → α) :
      (tabulate m n g).numRows = m

      tabulate m n g has m rows.

      @[simp]
      theorem Cslib.RowTable.numCols_tabulate {α : Type u_1} (m n : ℕ) (g : ℕ → ℕ → α) :
      (tabulate m n g).numCols = n

      tabulate m n g has n columns.

      theorem Cslib.RowTable.length_of_mem_rows_tabulate {α : Type u_1} {m n : ℕ} {g : ℕ → ℕ → α} {row : List α} (hrow : row ∈ (tabulate m n g).rows) :
      row.length = n

      Every row of (tabulate m n g).rows has length n.

      theorem Cslib.RowTable.wellFormed_tabulate {α : Type u_1} (m n : ℕ) (g : ℕ → ℕ → α) :

      Every tabulated m × n RowTable is WellFormed.

      def Cslib.RowTable.ofMatrix {α : Type u_1} {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) :

      Convert a Matrix (Fin m) (Fin n) α into an m × n RowTable α.

      @[simp]
      theorem Cslib.RowTable.numRows_ofMatrix {α : Type u_1} {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) :

      ofMatrix A has m rows.

      @[simp]
      theorem Cslib.RowTable.numCols_ofMatrix {α : Type u_1} {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) :

      ofMatrix A has n columns.

      theorem Cslib.RowTable.wellFormed_ofMatrix {α : Type u_1} {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) :

      ofMatrix A is always WellFormed.

      @[simp]
      theorem Cslib.RowTable.map_ofMatrix {α : Type u_1} {β : Type u_2} {m n : ℕ} (f : α → β) (A : Matrix (Fin m) (Fin n) α) :
      map f (ofMatrix A) = ofMatrix (A.map f)

      map commutes with ofMatrix.

      Zero-defaulting entry lookup and Matrix conversion #

      def Cslib.RowTable.entry {α : Type u_1} [Zero α] (T : RowTable α) (i j : ℕ) :
      α

      Entry (i, j) of T, defaulting to 0 out of bounds.

      def Cslib.RowTable.toMatrix {α : Type u_1} [Zero α] (T : RowTable α) :

      Interpret T : RowTable α as a Matrix (Fin T.numRows) (Fin T.numCols) α.

      @[simp]
      theorem Cslib.RowTable.toMatrix_apply {α : Type u_1} [Zero α] (T : RowTable α) (i : Fin T.numRows) (j : Fin T.numCols) :
      T.toMatrix i j = T.entry ↑i ↑j

      Matrix application on T.toMatrix reads T.entry.

      theorem Cslib.RowTable.entry_tabulate {α : Type u_1} [Zero α] (m n : ℕ) (g : ℕ → ℕ → α) (i j : ℕ) :
      (tabulate m n g).entry i j = if i < m ∧ j < n then g i j else 0

      Entry lookup on tabulate m n g returns g i j in bounds and 0 out of bounds.

      @[simp]
      theorem Cslib.RowTable.entry_tabulate_of_lt {α : Type u_1} [Zero α] {m n : ℕ} (g : ℕ → ℕ → α) {i j : ℕ} (hi : i < m) (hj : j < n) :
      (tabulate m n g).entry i j = g i j

      In-bounds entry lookup on tabulate m n g.

      @[simp]
      theorem Cslib.RowTable.toMatrix_tabulate {α : Type u_1} [Zero α] (m n : ℕ) (g : ℕ → ℕ → α) :
      (tabulate m n g).toMatrix = Matrix.of fun (i : Fin (tabulate m n g).numRows) (j : Fin (tabulate m n g).numCols) => g ↑i ↑j

      Matrix interpretation of tabulate m n g.

      @[simp]
      theorem Cslib.RowTable.entry_ofMatrix_of_lt {α : Type u_1} [Zero α] {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) {i j : ℕ} (hi : i < m) (hj : j < n) :
      (ofMatrix A).entry i j = A ⟨i, hi⟩ ⟨j, hj⟩

      In-bounds entry lookup on ofMatrix A.

      @[simp]
      theorem Cslib.RowTable.toMatrix_ofMatrix {α : Type u_1} [Zero α] {m n : ℕ} (A : Matrix (Fin m) (Fin n) α) :

      Converting A : Matrix (Fin m) (Fin n) α to RowTable α and back recovers A.

      theorem Cslib.RowTable.WellFormed.entry_eq_zero {α : Type u_1} [Zero α] {T : RowTable α} (hT : T.WellFormed) {i j : ℕ} (h : ¬(i < T.numRows ∧ j < T.numCols)) :
      T.entry i j = 0

      Out-of-bounds reads on a WellFormed table return 0.

      theorem Cslib.RowTable.exists_entry_eq_of_mem {α : Type u_1} [Zero α] {T : RowTable α} {row : List α} {x : α} (hrow : row ∈ T.rows) (hx : x ∈ row) :
      ∃ (i : ℕ) (j : ℕ), i < T.rows.length ∧ j < row.length ∧ T.entry i j = x

      Every stored element of T.rows occurs as T.entry i j at valid row/column indices.

      theorem Cslib.RowTable.entry_rel_of_mem {α : Type u_1} [Zero α] {P : α → Prop} (h0 : P 0) {T : RowTable α} (h : ∀ row ∈ T.rows, ∀ x ∈ row, P x) (i j : ℕ) :
      P (T.entry i j)

      Any predicate holding for 0 and for every stored element of T.rows holds for every T.entry i j.

      theorem Cslib.RowTable.eq_of_wellFormed_of_entry_eq {α : Type u_1} [Zero α] {A B : RowTable α} (hA : A.WellFormed) (hB : B.WellFormed) (hr : A.numRows = B.numRows) (hc : A.numCols = B.numCols) (h : ∀ i < A.numRows, ∀ j < A.numCols, A.entry i j = B.entry i j) :
      A = B

      Two WellFormed tables of equal dimensions are equal when their in-bounds entries agree.

      theorem Cslib.RowTable.tabulate_entry {α : Type u_1} [Zero α] {T : RowTable α} (hT : T.WellFormed) :

      Re-tabulating a WellFormed table from entry recovers it unchanged.

      theorem Cslib.RowTable.ofMatrix_toMatrix {α : Type u_1} [Zero α] {T : RowTable α} (hT : T.WellFormed) :

      Converting a WellFormed table to a Matrix and back via ofMatrix recovers T.

      @[simp]
      theorem Cslib.RowTable.entry_map {α : Type u_1} {β : Type u_2} [Zero α] [Zero β] (f : α → β) (hf : f 0 = 0) (T : RowTable α) (i j : ℕ) :
      (map f T).entry i j = f (T.entry i j)

      entry commutes with any zero-preserving map f.

      @[simp]
      theorem Cslib.RowTable.toMatrix_map {α : Type u_1} {β : Type u_2} [Zero α] [Zero β] (f : α → β) (hf : f 0 = 0) (T : RowTable α) :

      toMatrix commutes with Matrix.map for any zero-preserving f.