doc-next-gen

Mathlib.Data.Matrix.Block

Module docstring

{"# Block Matrices

Main definitions

  • Matrix.fromBlocks: build a block matrix out of 4 blocks
  • Matrix.toBlocks₁₁, Matrix.toBlocks₁₂, Matrix.toBlocks₂₁, Matrix.toBlocks₂₂: extract each of the four blocks from Matrix.fromBlocks.
  • Matrix.blockDiagonal: block diagonal of equally sized blocks. On square blocks, this is a ring homomorphisms, Matrix.blockDiagonalRingHom.
  • Matrix.blockDiag: extract the blocks from the diagonal of a block diagonal matrix.
  • Matrix.blockDiagonal': block diagonal of unequally sized blocks. On square blocks, this is a ring homomorphisms, Matrix.blockDiagonal'RingHom.
  • Matrix.blockDiag': extract the blocks from the diagonal of a block diagonal matrix. "}
Matrix.dotProduct_block theorem
[Fintype m] [Fintype n] [Mul α] [AddCommMonoid α] (v w : m ⊕ n → α) : v ⬝ᵥ w = v ∘ Sum.inl ⬝ᵥ w ∘ Sum.inl + v ∘ Sum.inr ⬝ᵥ w ∘ Sum.inr
Full source
theorem dotProduct_block [Fintype m] [Fintype n] [Mul α] [AddCommMonoid α] (v w : m ⊕ n → α) :
    v ⬝ᵥ w = v ∘ Sum.inlv ∘ Sum.inl ⬝ᵥ w ∘ Sum.inl + v ∘ Sum.inrv ∘ Sum.inr ⬝ᵥ w ∘ Sum.inr :=
  Fintype.sum_sum_type _
Dot Product Decomposition for Block Vectors
Informal description
Let $m$ and $n$ be finite types, and let $\alpha$ be a type equipped with a multiplication operation and an additive commutative monoid structure. For any two vectors $v, w : m \oplus n \to \alpha$, the dot product $v \cdot w$ is equal to the sum of the dot products of the left components $(v \circ \text{inl}) \cdot (w \circ \text{inl})$ and the right components $(v \circ \text{inr}) \cdot (w \circ \text{inr})$.
Matrix.fromBlocks definition
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : Matrix (n ⊕ o) (l ⊕ m) α
Full source
/-- We can form a single large matrix by flattening smaller 'block' matrices of compatible
dimensions. -/
@[pp_nodot]
def fromBlocks (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) :
    Matrix (n ⊕ o) (l ⊕ m) α :=
  of <| Sum.elim (fun i => Sum.elim (A i) (B i)) (fun j => Sum.elim (C j) (D j))
Block matrix construction from four submatrices
Informal description
Given four matrices $A : \text{Matrix}\, n\, l\, \alpha$, $B : \text{Matrix}\, n\, m\, \alpha$, $C : \text{Matrix}\, o\, l\, \alpha$, and $D : \text{Matrix}\, o\, m\, \alpha$, the function constructs a block matrix of type $\text{Matrix}\, (n \oplus o)\, (l \oplus m)\, \alpha$ by combining them. The resulting matrix has $A$ in the top-left block, $B$ in the top-right block, $C$ in the bottom-left block, and $D$ in the bottom-right block.
Matrix.fromBlocks_apply₁₁ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (i : n) (j : l) : fromBlocks A B C D (Sum.inl i) (Sum.inl j) = A i j
Full source
@[simp]
theorem fromBlocks_apply₁₁ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (i : n) (j : l) : fromBlocks A B C D (Sum.inl i) (Sum.inl j) = A i j :=
  rfl
Block Matrix Entry Formula for Top-Left Block: $(\text{fromBlocks}\, A\, B\, C\, D)_{i,j} = A_{i,j}$
Informal description
Let $A$ be an $n \times l$ matrix, $B$ an $n \times m$ matrix, $C$ an $o \times l$ matrix, and $D$ an $o \times m$ matrix over a type $\alpha$. For any indices $i \in n$ and $j \in l$, the $(i,j)$-entry of the block matrix constructed from $A$, $B$, $C$, and $D$ (using the left inclusion for both row and column indices) equals the $(i,j)$-entry of $A$, i.e., \[ \text{fromBlocks}\, A\, B\, C\, D\, (\text{inl}\, i)\, (\text{inl}\, j) = A_{i,j}. \]
Matrix.fromBlocks_apply₁₂ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (i : n) (j : m) : fromBlocks A B C D (Sum.inl i) (Sum.inr j) = B i j
Full source
@[simp]
theorem fromBlocks_apply₁₂ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (i : n) (j : m) : fromBlocks A B C D (Sum.inl i) (Sum.inr j) = B i j :=
  rfl
Top-right block entry in block matrix construction
Informal description
For any matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, and for any indices $i \in n$ and $j \in m$, the $(i,j)$-th entry of the top-right block of the block matrix $\text{fromBlocks}\, A\, B\, C\, D$ is equal to the $(i,j)$-th entry of $B$. In other words, if $M = \begin{pmatrix} A & B \\ C & D \end{pmatrix}$ is the block matrix formed from $A, B, C, D$, then $M_{i,j} = B_{i,j}$ when $i$ corresponds to a row in the top block and $j$ corresponds to a column in the right block.
Matrix.fromBlocks_apply₂₁ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (i : o) (j : l) : fromBlocks A B C D (Sum.inr i) (Sum.inl j) = C i j
Full source
@[simp]
theorem fromBlocks_apply₂₁ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (i : o) (j : l) : fromBlocks A B C D (Sum.inr i) (Sum.inl j) = C i j :=
  rfl
Bottom-left Block Entry in Block Matrix Construction
Informal description
Let $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$ be matrices. For any indices $i \in o$ and $j \in l$, the $(i,j)$-th entry of the block matrix constructed from $A$, $B$, $C$, and $D$ at position $(\text{inr }i, \text{inl }j)$ is equal to the $(i,j)$-th entry of $C$, i.e., \[ \text{fromBlocks}\, A\, B\, C\, D\, (\text{inr }i)\, (\text{inl }j) = C_{ij}. \]
Matrix.fromBlocks_apply₂₂ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (i : o) (j : m) : fromBlocks A B C D (Sum.inr i) (Sum.inr j) = D i j
Full source
@[simp]
theorem fromBlocks_apply₂₂ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (i : o) (j : m) : fromBlocks A B C D (Sum.inr i) (Sum.inr j) = D i j :=
  rfl
Bottom-Right Block Entry in Block Matrix Construction
Informal description
Given four matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, the $(i,j)$-th entry of the bottom-right block of the block matrix constructed by `fromBlocks A B C D` is equal to the $(i,j)$-th entry of $D$. Specifically, for any $i \in o$ and $j \in m$, we have $(\text{fromBlocks}\, A\, B\, C\, D)\, (\text{inr}\, i)\, (\text{inr}\, j) = D\, i\, j$.
Matrix.toBlocks₁₁ definition
(M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix n l α
Full source
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"top left" submatrix. -/
def toBlocks₁₁ (M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix n l α :=
  of fun i j => M (Sum.inl i) (Sum.inl j)
Top-left block of a block matrix
Informal description
Given a block matrix $M$ with row indices in $n \oplus o$ and column indices in $l \oplus m$, the function extracts the top-left submatrix of $M$ as a matrix with row indices in $n$ and column indices in $l$. Specifically, for any $i \in n$ and $j \in l$, the $(i,j)$-th entry of the extracted submatrix is equal to the $(\text{inl }i, \text{inl }j)$-th entry of $M$.
Matrix.toBlocks₁₂ definition
(M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix n m α
Full source
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"top right" submatrix. -/
def toBlocks₁₂ (M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix n m α :=
  of fun i j => M (Sum.inl i) (Sum.inr j)
Top-right block of a block matrix
Informal description
Given a block matrix $M$ with row indices in $n \oplus o$ and column indices in $l \oplus m$, the function extracts the top-right submatrix of $M$, which has row indices in $n$ and column indices in $m$. Specifically, for each $i \in n$ and $j \in m$, the entry at position $(i,j)$ in the extracted submatrix is equal to $M(\text{inl}\, i, \text{inr}\, j)$ in the original matrix.
Matrix.toBlocks₂₁ definition
(M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix o l α
Full source
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"bottom left" submatrix. -/
def toBlocks₂₁ (M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix o l α :=
  of fun i j => M (Sum.inr i) (Sum.inl j)
Bottom-left block of a block matrix
Informal description
Given a block matrix $M$ with row indices in the disjoint union $n \oplus o$ and column indices in the disjoint union $l \oplus m$, the function extracts the bottom-left submatrix of $M$, which has row indices in $o$ and column indices in $l$. Specifically, for $i \in o$ and $j \in l$, the entry at position $(i,j)$ of the extracted submatrix is equal to $M(\text{inr } i, \text{inl } j)$, where $\text{inr}$ and $\text{inl}$ are the canonical injections into the disjoint union.
Matrix.toBlocks₂₂ definition
(M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix o m α
Full source
/-- Given a matrix whose row and column indexes are sum types, we can extract the corresponding
"bottom right" submatrix. -/
def toBlocks₂₂ (M : Matrix (n ⊕ o) (l ⊕ m) α) : Matrix o m α :=
  of fun i j => M (Sum.inr i) (Sum.inr j)
Bottom-right block of a block matrix
Informal description
Given a block matrix $M$ of type $\text{Matrix}\, (n \oplus o)\, (l \oplus m)\, \alpha$, the function extracts the bottom-right submatrix of $M$, which is a matrix of type $\text{Matrix}\, o\, m\, \alpha$. The entries of this submatrix correspond to the rows indexed by $\text{Sum.inr}\, i$ and columns indexed by $\text{Sum.inr}\, j$ in the original matrix $M$.
Matrix.fromBlocks_toBlocks theorem
(M : Matrix (n ⊕ o) (l ⊕ m) α) : fromBlocks M.toBlocks₁₁ M.toBlocks₁₂ M.toBlocks₂₁ M.toBlocks₂₂ = M
Full source
theorem fromBlocks_toBlocks (M : Matrix (n ⊕ o) (l ⊕ m) α) :
    fromBlocks M.toBlocks₁₁ M.toBlocks₁₂ M.toBlocks₂₁ M.toBlocks₂₂ = M := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> rfl
Reconstruction of Block Matrix from Submatrices
Informal description
For any block matrix $M$ with row indices in $n \oplus o$ and column indices in $l \oplus m$, the reconstruction of $M$ from its four submatrices (top-left, top-right, bottom-left, and bottom-right) via `fromBlocks` yields the original matrix $M$. That is, \[ \text{fromBlocks}\, M_{11}\, M_{12}\, M_{21}\, M_{22} = M \] where $M_{11}$, $M_{12}$, $M_{21}$, and $M_{22}$ are the submatrices extracted from $M$ by `toBlocks₁₁`, `toBlocks₁₂`, `toBlocks₂₁`, and `toBlocks₂₂` respectively.
Matrix.toBlocks_fromBlocks₁₁ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₁₁ = A
Full source
@[simp]
theorem toBlocks_fromBlocks₁₁ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₁₁ = A :=
  rfl
Top-left Block Extraction from Block Matrix Construction
Informal description
Given matrices $A \in \text{Matrix}(n, l, \alpha)$, $B \in \text{Matrix}(n, m, \alpha)$, $C \in \text{Matrix}(o, l, \alpha)$, and $D \in \text{Matrix}(o, m, \alpha)$, the top-left block of the block matrix constructed by $\text{fromBlocks}(A, B, C, D)$ is equal to $A$.
Matrix.toBlocks_fromBlocks₁₂ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₁₂ = B
Full source
@[simp]
theorem toBlocks_fromBlocks₁₂ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₁₂ = B :=
  rfl
Top-Right Block Extraction from Block Matrix Construction
Informal description
For any matrices $A \in \text{Matrix}(n, l, \alpha)$, $B \in \text{Matrix}(n, m, \alpha)$, $C \in \text{Matrix}(o, l, \alpha)$, and $D \in \text{Matrix}(o, m, \alpha)$, the top-right block of the block matrix constructed by $\text{fromBlocks}(A, B, C, D)$ is equal to $B$.
Matrix.toBlocks_fromBlocks₂₁ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₂₁ = C
Full source
@[simp]
theorem toBlocks_fromBlocks₂₁ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₂₁ = C :=
  rfl
Bottom-left Block Extraction from Block Matrix Construction
Informal description
Given four matrices $A \in \text{Matrix}(n, l, \alpha)$, $B \in \text{Matrix}(n, m, \alpha)$, $C \in \text{Matrix}(o, l, \alpha)$, and $D \in \text{Matrix}(o, m, \alpha)$, the bottom-left block of the block matrix constructed via $\text{fromBlocks}(A, B, C, D)$ is equal to $C$.
Matrix.toBlocks_fromBlocks₂₂ theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₂₂ = D
Full source
@[simp]
theorem toBlocks_fromBlocks₂₂ (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D).toBlocks₂₂ = D :=
  rfl
Bottom-Right Block Extraction from Block Matrix Construction
Informal description
For any matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, the bottom-right block of the block matrix constructed by $\text{fromBlocks}\, A\, B\, C\, D$ is equal to $D$.
Matrix.ext_iff_blocks theorem
{A B : Matrix (n ⊕ o) (l ⊕ m) α} : A = B ↔ A.toBlocks₁₁ = B.toBlocks₁₁ ∧ A.toBlocks₁₂ = B.toBlocks₁₂ ∧ A.toBlocks₂₁ = B.toBlocks₂₁ ∧ A.toBlocks₂₂ = B.toBlocks₂₂
Full source
/-- Two block matrices are equal if their blocks are equal. -/
theorem ext_iff_blocks {A B : Matrix (n ⊕ o) (l ⊕ m) α} :
    A = B ↔
      A.toBlocks₁₁ = B.toBlocks₁₁ ∧
        A.toBlocks₁₂ = B.toBlocks₁₂ ∧ A.toBlocks₂₁ = B.toBlocks₂₁ ∧ A.toBlocks₂₂ = B.toBlocks₂₂ :=
  ⟨fun h => h ▸ ⟨rfl, rfl, rfl, rfl⟩, fun ⟨h₁₁, h₁₂, h₂₁, h₂₂⟩ => by
    rw [← fromBlocks_toBlocks A, ← fromBlocks_toBlocks B, h₁₁, h₁₂, h₂₁, h₂₂]⟩
Equality of Block Matrices via Block Equality
Informal description
For any two block matrices $A$ and $B$ with row indices in $n \oplus o$ and column indices in $l \oplus m$, the matrices are equal if and only if their corresponding blocks are equal. That is, \[ A = B \leftrightarrow A_{11} = B_{11} \land A_{12} = B_{12} \land A_{21} = B_{21} \land A_{22} = B_{22}, \] where $A_{11}, A_{12}, A_{21}, A_{22}$ and $B_{11}, B_{12}, B_{21}, B_{22}$ are the top-left, top-right, bottom-left, and bottom-right blocks of $A$ and $B$ respectively.
Matrix.fromBlocks_inj theorem
{A : Matrix n l α} {B : Matrix n m α} {C : Matrix o l α} {D : Matrix o m α} {A' : Matrix n l α} {B' : Matrix n m α} {C' : Matrix o l α} {D' : Matrix o m α} : fromBlocks A B C D = fromBlocks A' B' C' D' ↔ A = A' ∧ B = B' ∧ C = C' ∧ D = D'
Full source
@[simp]
theorem fromBlocks_inj {A : Matrix n l α} {B : Matrix n m α} {C : Matrix o l α} {D : Matrix o m α}
    {A' : Matrix n l α} {B' : Matrix n m α} {C' : Matrix o l α} {D' : Matrix o m α} :
    fromBlocksfromBlocks A B C D = fromBlocks A' B' C' D' ↔ A = A' ∧ B = B' ∧ C = C' ∧ D = D' :=
  ext_iff_blocks
Injectivity of Block Matrix Construction: $\text{fromBlocks}\, A\, B\, C\, D = \text{fromBlocks}\, A'\, B'\, C'\, D' \leftrightarrow A = A' \land B = B' \land C = C' \land D = D'$
Informal description
For any matrices $A, A' \in \text{Matrix}\, n\, l\, \alpha$, $B, B' \in \text{Matrix}\, n\, m\, \alpha$, $C, C' \in \text{Matrix}\, o\, l\, \alpha$, and $D, D' \in \text{Matrix}\, o\, m\, \alpha$, the block matrix formed by $A, B, C, D$ is equal to the block matrix formed by $A', B', C', D'$ if and only if $A = A'$, $B = B'$, $C = C'$, and $D = D'$. In other words, the construction of a block matrix from four submatrices is injective, meaning that two block matrices are equal precisely when their corresponding blocks are equal.
Matrix.fromBlocks_map theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (f : α → β) : (fromBlocks A B C D).map f = fromBlocks (A.map f) (B.map f) (C.map f) (D.map f)
Full source
theorem fromBlocks_map (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α)
    (f : α → β) : (fromBlocks A B C D).map f =
      fromBlocks (A.map f) (B.map f) (C.map f) (D.map f) := by
  ext i j; rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp [fromBlocks]
Mapping a Function Over a Block Matrix Preserves Block Structure
Informal description
Let $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$ be matrices, and let $f : \alpha \to \beta$ be a function. Then the entry-wise application of $f$ to the block matrix formed from $A$, $B$, $C$, and $D$ is equal to the block matrix formed from the entry-wise applications of $f$ to each of $A$, $B$, $C$, and $D$. In other words, $$ \text{map}\left(\text{fromBlocks}\, A\, B\, C\, D\right) f = \text{fromBlocks}\, (\text{map}\, A\, f)\, (\text{map}\, B\, f)\, (\text{map}\, C\, f)\, (\text{map}\, D\, f). $$
Matrix.fromBlocks_transpose theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D)ᵀ = fromBlocks Aᵀ Cᵀ Bᵀ Dᵀ
Full source
theorem fromBlocks_transpose (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D)ᵀ = fromBlocks Aᵀ Cᵀ Bᵀ Dᵀ := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp [fromBlocks]
Transpose of a Block Matrix
Informal description
For any matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, the transpose of the block matrix formed by $A, B, C, D$ is equal to the block matrix formed by the transposes of $A$ and $C$ in the top row and $B$ and $D$ in the bottom row. That is, $$ \begin{pmatrix} A & B \\ C & D \end{pmatrix}^\top = \begin{pmatrix} A^\top & C^\top \\ B^\top & D^\top \end{pmatrix}. $$
Matrix.fromBlocks_conjTranspose theorem
[Star α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D)ᴴ = fromBlocks Aᴴ Cᴴ Bᴴ Dᴴ
Full source
theorem fromBlocks_conjTranspose [Star α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : (fromBlocks A B C D)ᴴ = fromBlocks Aᴴ Cᴴ Bᴴ Dᴴ := by
  simp only [conjTranspose, fromBlocks_transpose, fromBlocks_map]
Conjugate Transpose of a Block Matrix
Informal description
Let $\alpha$ be a type equipped with a star operation (conjugation), and let $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$ be matrices. The conjugate transpose of the block matrix formed by $A, B, C, D$ is equal to the block matrix formed by the conjugate transposes of $A$ and $C$ in the top row and $B$ and $D$ in the bottom row. That is, $$ \begin{pmatrix} A & B \\ C & D \end{pmatrix}^H = \begin{pmatrix} A^H & C^H \\ B^H & D^H \end{pmatrix}. $$
Matrix.fromBlocks_submatrix_sum_swap_left theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (f : p → l ⊕ m) : (fromBlocks A B C D).submatrix Sum.swap f = (fromBlocks C D A B).submatrix id f
Full source
@[simp]
theorem fromBlocks_submatrix_sum_swap_left (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (f : p → l ⊕ m) :
    (fromBlocks A B C D).submatrix Sum.swap f = (fromBlocks C D A B).submatrix id f := by
  ext i j
  cases i <;> dsimp <;> cases f j <;> rfl
Submatrix of Block Matrix with Swapped Rows Equals Submatrix of Swapped Blocks
Informal description
Let $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$ be four matrices. For any function $f : p \to l \oplus m$, the submatrix obtained by applying the row reindexing function $\text{Sum.swap}$ (which swaps the summands) and column reindexing function $f$ to the block matrix $\text{fromBlocks}\, A\, B\, C\, D$ is equal to the submatrix obtained by applying the identity row reindexing function and column reindexing function $f$ to the block matrix $\text{fromBlocks}\, C\, D\, A\, B$. In other words, swapping the row indices of a block matrix via $\text{Sum.swap}$ is equivalent to swapping the top and bottom blocks of the original matrix before taking the submatrix.
Matrix.fromBlocks_submatrix_sum_swap_right theorem
(A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (f : p → n ⊕ o) : (fromBlocks A B C D).submatrix f Sum.swap = (fromBlocks B A D C).submatrix f id
Full source
@[simp]
theorem fromBlocks_submatrix_sum_swap_right (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (f : p → n ⊕ o) :
    (fromBlocks A B C D).submatrix f Sum.swap = (fromBlocks B A D C).submatrix f id := by
  ext i j
  cases j <;> dsimp <;> cases f i <;> rfl
Submatrix Column Swap Property for Block Matrices
Informal description
Let $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$ be matrices. For any function $f : p \to n \oplus o$, the submatrix of the block matrix $\text{fromBlocks}\, A\, B\, C\, D$ obtained by reindexing columns with $\text{Sum.swap}$ is equal to the submatrix of $\text{fromBlocks}\, B\, A\, D\, C$ obtained by reindexing columns with the identity function. In other words: $$(\text{fromBlocks}\, A\, B\, C\, D).\text{submatrix}\, f\, \text{Sum.swap} = (\text{fromBlocks}\, B\, A\, D\, C).\text{submatrix}\, f\, \text{id}$$
Matrix.fromBlocks_submatrix_sum_swap_sum_swap theorem
{l m n o α : Type*} (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : (fromBlocks A B C D).submatrix Sum.swap Sum.swap = fromBlocks D C B A
Full source
theorem fromBlocks_submatrix_sum_swap_sum_swap {l m n o α : Type*} (A : Matrix n l α)
    (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) :
    (fromBlocks A B C D).submatrix Sum.swap Sum.swap = fromBlocks D C B A := by simp
Block Matrix Submatrix Double Swap Equals Reverse Block Order
Informal description
For any matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, the submatrix of the block matrix $\text{fromBlocks}\, A\, B\, C\, D$ obtained by reindexing both rows and columns with $\text{Sum.swap}$ is equal to the block matrix $\text{fromBlocks}\, D\, C\, B\, A$. In other words, swapping both row and column indices of a block matrix via $\text{Sum.swap}$ results in a block matrix with the four blocks in reverse order (bottom-right, bottom-left, top-right, top-left).
Matrix.IsTwoBlockDiagonal definition
[Zero α] (A : Matrix (n ⊕ o) (l ⊕ m) α) : Prop
Full source
/-- A 2x2 block matrix is block diagonal if the blocks outside of the diagonal vanish -/
def IsTwoBlockDiagonal [Zero α] (A : Matrix (n ⊕ o) (l ⊕ m) α) : Prop :=
  toBlocks₁₂toBlocks₁₂ A = 0 ∧ toBlocks₂₁ A = 0
Block Diagonal Matrix (2×2 Case)
Informal description
A block matrix $A$ with row indices in $n \oplus o$ and column indices in $l \oplus m$ is called *block diagonal* if its off-diagonal blocks (the top-right and bottom-left blocks) are zero matrices. That is, $A$ is block diagonal if $A_{12} = 0$ and $A_{21} = 0$, where $A_{12}$ and $A_{21}$ are the top-right and bottom-left blocks of $A$ respectively.
Matrix.toBlock definition
(M : Matrix m n α) (p : m → Prop) (q : n → Prop) : Matrix { a // p a } { a // q a } α
Full source
/-- Let `p` pick out certain rows and `q` pick out certain columns of a matrix `M`. Then
  `toBlock M p q` is the corresponding block matrix. -/
def toBlock (M : Matrix m n α) (p : m → Prop) (q : n → Prop) : Matrix { a // p a } { a // q a } α :=
  M.submatrix (↑) (↑)
Submatrix defined by row and column predicates
Informal description
Given a matrix $M$ of size $m \times n$ with entries in $\alpha$, and predicates $p$ on rows and $q$ on columns, the function constructs the submatrix $M.\text{toBlock}\, p\, q$ consisting of entries $M_{i,j}$ where $i$ satisfies $p$ and $j$ satisfies $q$. The resulting submatrix has rows indexed by $\{a \mid p\, a\}$ and columns indexed by $\{a \mid q\, a\}$.
Matrix.toBlock_apply theorem
(M : Matrix m n α) (p : m → Prop) (q : n → Prop) (i : { a // p a }) (j : { a // q a }) : toBlock M p q i j = M ↑i ↑j
Full source
@[simp]
theorem toBlock_apply (M : Matrix m n α) (p : m → Prop) (q : n → Prop) (i : { a // p a })
    (j : { a // q a }) : toBlock M p q i j = M ↑i ↑j :=
  rfl
Submatrix Entry Formula: $(\mathrm{toBlock}\, M\, p\, q)_{i j} = M_{i j}$
Informal description
Given a matrix $M \colon m \times n \to \alpha$, predicates $p \colon m \to \mathrm{Prop}$ and $q \colon n \to \mathrm{Prop}$, and indices $i \in \{a \mid p\, a\}$, $j \in \{a \mid q\, a\}$, the entry of the submatrix $\mathrm{toBlock}\, M\, p\, q$ at position $(i, j)$ is equal to the entry $M_{i j}$ of the original matrix.
Matrix.toSquareBlockProp definition
(M : Matrix m m α) (p : m → Prop) : Matrix { a // p a } { a // p a } α
Full source
/-- Let `p` pick out certain rows and columns of a square matrix `M`. Then
  `toSquareBlockProp M p` is the corresponding block matrix. -/
def toSquareBlockProp (M : Matrix m m α) (p : m → Prop) : Matrix { a // p a } { a // p a } α :=
  toBlock M _ _
Square submatrix defined by a predicate
Informal description
Given a square matrix $M$ of size $m \times m$ with entries in $\alpha$ and a predicate $p$ on the row/column indices, the function constructs the square submatrix $M.\text{toSquareBlockProp}\, p$ consisting of entries $M_{i,j}$ where both $i$ and $j$ satisfy $p$. The resulting submatrix has rows and columns indexed by $\{a \mid p\, a\}$.
Matrix.toSquareBlockProp_def theorem
(M : Matrix m m α) (p : m → Prop) : toSquareBlockProp M p = of (fun i j : { a // p a } => M ↑i ↑j)
Full source
theorem toSquareBlockProp_def (M : Matrix m m α) (p : m → Prop) :
    toSquareBlockProp M p = of (fun i j : { a // p a } => M ↑i ↑j) :=
  rfl
Definition of Square Submatrix via Predicate: $\text{toSquareBlockProp}\, M\, p = \text{of}\, (\lambda i j, M_{i j})$
Informal description
Given a square matrix $M$ of size $m \times m$ with entries in $\alpha$ and a predicate $p$ on the row/column indices, the submatrix $\text{toSquareBlockProp}\, M\, p$ is equal to the matrix constructed from the entries $M_{i j}$ where both $i$ and $j$ satisfy $p$. Formally, $\text{toSquareBlockProp}\, M\, p = \text{of}\, (\lambda (i j : \{a \mid p\, a\}), M_{i j})$.
Matrix.toSquareBlock definition
(M : Matrix m m α) (b : m → β) (k : β) : Matrix { a // b a = k } { a // b a = k } α
Full source
/-- Let `b` map rows and columns of a square matrix `M` to blocks. Then
  `toSquareBlock M b k` is the block `k` matrix. -/
def toSquareBlock (M : Matrix m m α) (b : m → β) (k : β) :
    Matrix { a // b a = k } { a // b a = k } α :=
  toSquareBlockProp M _
Square submatrix for a given block index
Informal description
Given a square matrix $M$ of size $m \times m$ with entries in $\alpha$, a function $b$ mapping row/column indices to blocks, and a block index $k$, the function constructs the square submatrix $M.\text{toSquareBlock}\, b\, k$ consisting of entries $M_{i,j}$ where both $i$ and $j$ are mapped to the block $k$ by $b$. The resulting submatrix has rows and columns indexed by $\{a \mid b\, a = k\}$.
Matrix.toSquareBlock_def theorem
(M : Matrix m m α) (b : m → β) (k : β) : toSquareBlock M b k = of (fun i j : { a // b a = k } => M ↑i ↑j)
Full source
theorem toSquareBlock_def (M : Matrix m m α) (b : m → β) (k : β) :
    toSquareBlock M b k = of (fun i j : { a // b a = k } => M ↑i ↑j) :=
  rfl
Definition of Block Submatrix via Index Partitioning
Informal description
Given a square matrix $M$ of size $m \times m$ with entries in $\alpha$, a function $b : m \to \beta$ mapping indices to blocks, and a block index $k : \beta$, the submatrix $\text{toSquareBlock}\, M\, b\, k$ is equal to the matrix constructed from entries $M_{i j}$ where both $i$ and $j$ satisfy $b(i) = b(j) = k$. Formally, $$\text{toSquareBlock}\, M\, b\, k = \text{of}\, (\lambda (i j : \{a \mid b(a) = k\}), M_{i j}).$$
Matrix.fromBlocks_smul theorem
[SMul R α] (x : R) (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) : x • fromBlocks A B C D = fromBlocks (x • A) (x • B) (x • C) (x • D)
Full source
theorem fromBlocks_smul [SMul R α] (x : R) (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) : x • fromBlocks A B C D = fromBlocks (x • A) (x • B) (x • C) (x • D) := by
  ext i j; rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp [fromBlocks]
Scalar Multiplication Distributes Over Block Matrix Construction
Informal description
Let $R$ be a type with a scalar multiplication operation on a type $\alpha$, and let $x \in R$. For any matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, the scalar multiplication of $x$ with the block matrix formed from $A$, $B$, $C$, $D$ is equal to the block matrix formed from the scalar multiples $x \cdot A$, $x \cdot B$, $x \cdot C$, and $x \cdot D$. That is, $$ x \cdot \begin{pmatrix} A & B \\ C & D \end{pmatrix} = \begin{pmatrix} x \cdot A & x \cdot B \\ x \cdot C & x \cdot D \end{pmatrix}. $$
Matrix.fromBlocks_neg theorem
[Neg R] (A : Matrix n l R) (B : Matrix n m R) (C : Matrix o l R) (D : Matrix o m R) : -fromBlocks A B C D = fromBlocks (-A) (-B) (-C) (-D)
Full source
theorem fromBlocks_neg [Neg R] (A : Matrix n l R) (B : Matrix n m R) (C : Matrix o l R)
    (D : Matrix o m R) : -fromBlocks A B C D = fromBlocks (-A) (-B) (-C) (-D) := by
  ext i j
  cases i <;> cases j <;> simp [fromBlocks]
Negation of Block Matrix Equals Block Matrix of Negations
Informal description
For any type $R$ with a negation operation and matrices $A \in \text{Matrix}\, n\, l\, R$, $B \in \text{Matrix}\, n\, m\, R$, $C \in \text{Matrix}\, o\, l\, R$, and $D \in \text{Matrix}\, o\, m\, R$, the negation of the block matrix formed from $A$, $B$, $C$, $D$ is equal to the block matrix formed from the negations $-A$, $-B$, $-C$, $-D$. That is, $$ -\begin{pmatrix} A & B \\ C & D \end{pmatrix} = \begin{pmatrix} -A & -B \\ -C & -D \end{pmatrix}. $$
Matrix.fromBlocks_zero theorem
[Zero α] : fromBlocks (0 : Matrix n l α) 0 0 (0 : Matrix o m α) = 0
Full source
@[simp]
theorem fromBlocks_zero [Zero α] : fromBlocks (0 : Matrix n l α) 0 0 (0 : Matrix o m α) = 0 := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> rfl
Zero Block Matrix Construction Yields Zero Matrix
Informal description
For any type $\alpha$ with a zero element, the block matrix constructed from four zero submatrices (of appropriate dimensions) is equal to the zero matrix. That is, if $A : \text{Matrix}\, n\, l\, \alpha$, $B : \text{Matrix}\, n\, m\, \alpha$, $C : \text{Matrix}\, o\, l\, \alpha$, and $D : \text{Matrix}\, o\, m\, \alpha$ are all zero matrices, then the block matrix $\begin{pmatrix} A & B \\ C & D \end{pmatrix}$ is equal to the zero matrix of type $\text{Matrix}\, (n \oplus o)\, (l \oplus m)\, \alpha$.
Matrix.fromBlocks_add theorem
[Add α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (A' : Matrix n l α) (B' : Matrix n m α) (C' : Matrix o l α) (D' : Matrix o m α) : fromBlocks A B C D + fromBlocks A' B' C' D' = fromBlocks (A + A') (B + B') (C + C') (D + D')
Full source
theorem fromBlocks_add [Add α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α)
    (D : Matrix o m α) (A' : Matrix n l α) (B' : Matrix n m α) (C' : Matrix o l α)
    (D' : Matrix o m α) : fromBlocks A B C D + fromBlocks A' B' C' D' =
      fromBlocks (A + A') (B + B') (C + C') (D + D') := by
  ext i j; rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> rfl
Additivity of Block Matrix Construction: $\begin{pmatrix} A & B \\ C & D \end{pmatrix} + \begin{pmatrix} A' & B' \\ C' & D' \end{pmatrix} = \begin{pmatrix} A + A' & B + B' \\ C + C' & D + D' \end{pmatrix}$
Informal description
Let $\alpha$ be a type equipped with an addition operation. Given matrices $A, A' \in \mathrm{Matrix}\, n\, l\, \alpha$, $B, B' \in \mathrm{Matrix}\, n\, m\, \alpha$, $C, C' \in \mathrm{Matrix}\, o\, l\, \alpha$, and $D, D' \in \mathrm{Matrix}\, o\, m\, \alpha$, the sum of the block matrices constructed from these submatrices equals the block matrix constructed from the sums of corresponding submatrices. That is, \[ \begin{pmatrix} A & B \\ C & D \end{pmatrix} + \begin{pmatrix} A' & B' \\ C' & D' \end{pmatrix} = \begin{pmatrix} A + A' & B + B' \\ C + C' & D + D' \end{pmatrix} \]
Matrix.fromBlocks_multiply theorem
[Fintype l] [Fintype m] [NonUnitalNonAssocSemiring α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (A' : Matrix l p α) (B' : Matrix l q α) (C' : Matrix m p α) (D' : Matrix m q α) : fromBlocks A B C D * fromBlocks A' B' C' D' = fromBlocks (A * A' + B * C') (A * B' + B * D') (C * A' + D * C') (C * B' + D * D')
Full source
theorem fromBlocks_multiply [Fintype l] [Fintype m] [NonUnitalNonAssocSemiring α] (A : Matrix n l α)
    (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (A' : Matrix l p α) (B' : Matrix l q α)
    (C' : Matrix m p α) (D' : Matrix m q α) :
    fromBlocks A B C D * fromBlocks A' B' C' D' =
      fromBlocks (A * A' + B * C') (A * B' + B * D') (C * A' + D * C') (C * B' + D * D') := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp only [fromBlocks, mul_apply, of_apply,
      Sum.elim_inr, Fintype.sum_sum_type, Sum.elim_inl, add_apply]
Block Matrix Multiplication Formula
Informal description
Let $l$, $m$, $n$, $o$, $p$, $q$ be finite types, and let $\alpha$ be a non-unital non-associative semiring. Given block matrices $M = \begin{pmatrix} A & B \\ C & D \end{pmatrix}$ and $N = \begin{pmatrix} A' & B' \\ C' & D' \end{pmatrix}$, where: - $A, A' \in \text{Matrix}\, n\, l\, \alpha$, - $B, B' \in \text{Matrix}\, n\, m\, \alpha$, - $C, C' \in \text{Matrix}\, o\, l\, \alpha$, - $D, D' \in \text{Matrix}\, o\, m\, \alpha$, - $A' \in \text{Matrix}\, l\, p\, \alpha$, - $B' \in \text{Matrix}\, l\, q\, \alpha$, - $C' \in \text{Matrix}\, m\, p\, \alpha$, - $D' \in \text{Matrix}\, m\, q\, \alpha$, the product $M \cdot N$ is given by the block matrix: \[ \begin{pmatrix} A A' + B C' & A B' + B D' \\ C A' + D C' & C B' + D D' \end{pmatrix}. \]
Matrix.fromBlocks_mulVec theorem
[Fintype l] [Fintype m] [NonUnitalNonAssocSemiring α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (x : l ⊕ m → α) : (fromBlocks A B C D) *ᵥ x = Sum.elim (A *ᵥ (x ∘ Sum.inl) + B *ᵥ (x ∘ Sum.inr)) (C *ᵥ (x ∘ Sum.inl) + D *ᵥ (x ∘ Sum.inr))
Full source
theorem fromBlocks_mulVec [Fintype l] [Fintype m] [NonUnitalNonAssocSemiring α] (A : Matrix n l α)
    (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (x : l ⊕ m → α) :
    (fromBlocks A B C D) *ᵥ x =
      Sum.elim (A *ᵥ (x ∘ Sum.inl) + B *ᵥ (x ∘ Sum.inr))
        (C *ᵥ (x ∘ Sum.inl) + D *ᵥ (x ∘ Sum.inr)) := by
  ext i
  cases i <;> simp [mulVec, dotProduct]
Matrix-vector multiplication formula for block matrices
Informal description
Let $A$ be an $n \times l$ matrix, $B$ an $n \times m$ matrix, $C$ an $o \times l$ matrix, and $D$ an $o \times m$ matrix over a non-unital non-associative semiring $\alpha$, where $l$ and $m$ are finite types. For any vector $x : l \oplus m \to \alpha$, the matrix-vector product of the block matrix $\begin{pmatrix} A & B \\ C & D \end{pmatrix}$ with $x$ is given by: \[ \begin{pmatrix} A & B \\ C & D \end{pmatrix} \cdot x = \begin{cases} A \cdot x_{\text{left}} + B \cdot x_{\text{right}} & \text{for rows in } n \\ C \cdot x_{\text{left}} + D \cdot x_{\text{right}} & \text{for rows in } o \end{cases} \] where $x_{\text{left}} = x \circ \text{inl}$ and $x_{\text{right}} = x \circ \text{inr}$ are the left and right components of $x$ respectively.
Matrix.vecMul_fromBlocks theorem
[Fintype n] [Fintype o] [NonUnitalNonAssocSemiring α] (A : Matrix n l α) (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (x : n ⊕ o → α) : x ᵥ* fromBlocks A B C D = Sum.elim ((x ∘ Sum.inl) ᵥ* A + (x ∘ Sum.inr) ᵥ* C) ((x ∘ Sum.inl) ᵥ* B + (x ∘ Sum.inr) ᵥ* D)
Full source
theorem vecMul_fromBlocks [Fintype n] [Fintype o] [NonUnitalNonAssocSemiring α] (A : Matrix n l α)
    (B : Matrix n m α) (C : Matrix o l α) (D : Matrix o m α) (x : n ⊕ o → α) :
    x ᵥ* fromBlocks A B C D =
      Sum.elim ((x ∘ Sum.inl) ᵥ* A + (x ∘ Sum.inr) ᵥ* C)
        ((x ∘ Sum.inl) ᵥ* B + (x ∘ Sum.inr) ᵥ* D) := by
  ext i
  cases i <;> simp [vecMul, dotProduct]
Left Multiplication of a Vector by a Block Matrix
Informal description
Let $n, o, l, m$ be finite types, and let $\alpha$ be a non-unital non-associative semiring. Given matrices $A \in \text{Matrix}\, n\, l\, \alpha$, $B \in \text{Matrix}\, n\, m\, \alpha$, $C \in \text{Matrix}\, o\, l\, \alpha$, and $D \in \text{Matrix}\, o\, m\, \alpha$, and a vector $x : n \oplus o \to \alpha$, the left multiplication of the block matrix $\begin{pmatrix} A & B \\ C & D \end{pmatrix}$ by $x$ is given by: \[ x \cdot \begin{pmatrix} A & B \\ C & D \end{pmatrix} = \left( (x \circ \text{inl}) \cdot A + (x \circ \text{inr}) \cdot C, (x \circ \text{inl}) \cdot B + (x \circ \text{inr}) \cdot D \right), \] where $\text{inl}$ and $\text{inr}$ are the canonical injections into the coproduct $n \oplus o$, and $\cdot$ denotes the left multiplication of a vector by a matrix.
Matrix.toBlock_diagonal_self theorem
(d : m → α) (p : m → Prop) : Matrix.toBlock (diagonal d) p p = diagonal fun i : Subtype p => d ↑i
Full source
theorem toBlock_diagonal_self (d : m → α) (p : m → Prop) :
    Matrix.toBlock (diagonal d) p p = diagonal fun i : Subtype p => d ↑i := by
  ext i j
  by_cases h : i = j
  · simp [h]
  · simp [One.one, h, Subtype.val_injective.ne h]
Diagonal Submatrix Equality for Predicate-Restricted Blocks
Informal description
Let $d : m \to \alpha$ be a vector and $p : m \to \text{Prop}$ be a predicate on $m$. The submatrix of the diagonal matrix $\text{diagonal } d$ obtained by restricting to rows and columns where $p$ holds is equal to the diagonal matrix constructed from the restriction of $d$ to the subtype $\{i \in m \mid p(i)\}$. That is, \[ \text{toBlock } (\text{diagonal } d) \ p \ p = \text{diagonal } (\lambda (i : \{i \in m \mid p(i)\}), \ d(i)). \]
Matrix.toBlock_diagonal_disjoint theorem
(d : m → α) {p q : m → Prop} (hpq : Disjoint p q) : Matrix.toBlock (diagonal d) p q = 0
Full source
theorem toBlock_diagonal_disjoint (d : m → α) {p q : m → Prop} (hpq : Disjoint p q) :
    Matrix.toBlock (diagonal d) p q = 0 := by
  ext ⟨i, hi⟩ ⟨j, hj⟩
  have : i ≠ j := fun heq => hpq.le_bot i ⟨hi, heq.symm ▸ hj⟩
  simp [diagonal_apply_ne d this]
Off-diagonal Block of a Diagonal Matrix is Zero for Disjoint Predicates
Informal description
Let $d : m \to \alpha$ be a vector and $p, q : m \to \mathrm{Prop}$ be two predicates on $m$ such that $p$ and $q$ are disjoint (i.e., there is no $x \in m$ satisfying both $p(x)$ and $q(x)$). Then the submatrix of the diagonal matrix $\mathrm{diagonal}\, d$ formed by rows satisfying $p$ and columns satisfying $q$ is the zero matrix. That is, $(\mathrm{diagonal}\, d).\mathrm{toBlock}\, p\, q = 0$.
Matrix.fromBlocks_diagonal theorem
(d₁ : l → α) (d₂ : m → α) : fromBlocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (Sum.elim d₁ d₂)
Full source
@[simp]
theorem fromBlocks_diagonal (d₁ : l → α) (d₂ : m → α) :
    fromBlocks (diagonal d₁) 0 0 (diagonal d₂) = diagonal (Sum.elim d₁ d₂) := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp [diagonal]
Block Diagonal Matrix Construction via Sum Elimination
Informal description
Given two vectors $d_1 : l \to \alpha$ and $d_2 : m \to \alpha$, the block diagonal matrix constructed from the diagonal matrices $\mathrm{diagonal}\, d_1$ and $\mathrm{diagonal}\, d_2$ (with zero matrices in the off-diagonal blocks) is equal to the diagonal matrix formed by combining $d_1$ and $d_2$ via the sum elimination function. That is, \[ \mathrm{fromBlocks}\, (\mathrm{diagonal}\, d_1)\, 0\, 0\, (\mathrm{diagonal}\, d_2) = \mathrm{diagonal}\, (\mathrm{Sum.elim}\, d_1\, d_2). \]
Matrix.toBlocks₁₁_diagonal theorem
(v : l ⊕ m → α) : toBlocks₁₁ (diagonal v) = diagonal (fun i => v (Sum.inl i))
Full source
@[simp]
lemma toBlocks₁₁_diagonal (v : l ⊕ m → α) :
    toBlocks₁₁ (diagonal v) = diagonal (fun i => v (Sum.inl i)) := by
  unfold toBlocks₁₁
  funext i j
  simp only [ne_eq, Sum.inl.injEq, of_apply, diagonal_apply]
Top-left Block of a Diagonal Block Matrix is Diagonal on Left Summand
Informal description
For any vector $v : l \oplus m \to \alpha$, the top-left block of the diagonal matrix constructed from $v$ is equal to the diagonal matrix constructed from the restriction of $v$ to the left summand $l$. That is, $\text{toBlocks₁₁}(\text{diagonal}(v)) = \text{diagonal}(i \mapsto v(\text{inl }i))$.
Matrix.toBlocks₂₂_diagonal theorem
(v : l ⊕ m → α) : toBlocks₂₂ (diagonal v) = diagonal (fun i => v (Sum.inr i))
Full source
@[simp]
lemma toBlocks₂₂_diagonal (v : l ⊕ m → α) :
    toBlocks₂₂ (diagonal v) = diagonal (fun i => v (Sum.inr i)) := by
  unfold toBlocks₂₂
  funext i j
  simp only [ne_eq, Sum.inr.injEq, of_apply, diagonal_apply]
Bottom-Right Block of a Block Diagonal Matrix
Informal description
For any vector $v : l \oplus m \to \alpha$, the bottom-right block of the block diagonal matrix $\text{diagonal}\, v$ is equal to the diagonal matrix formed by the components of $v$ corresponding to the right summand $m$, i.e., $\text{diagonal}\, (i \mapsto v (\text{Sum.inr}\, i))$.
Matrix.toBlocks₁₂_diagonal theorem
(v : l ⊕ m → α) : toBlocks₁₂ (diagonal v) = 0
Full source
@[simp]
lemma toBlocks₁₂_diagonal (v : l ⊕ m → α) : toBlocks₁₂ (diagonal v) = 0 := rfl
Top-right Block of Diagonal Matrix is Zero
Informal description
For any vector $v : l \oplus m \to \alpha$, the top-right block of the block diagonal matrix $\mathrm{diagonal}\,v$ is the zero matrix, i.e., $\mathrm{toBlocks}_{12}(\mathrm{diagonal}\,v) = 0$.
Matrix.toBlocks₂₁_diagonal theorem
(v : l ⊕ m → α) : toBlocks₂₁ (diagonal v) = 0
Full source
@[simp]
lemma toBlocks₂₁_diagonal (v : l ⊕ m → α) : toBlocks₂₁ (diagonal v) = 0 := rfl
Bottom-left Block of Diagonal Matrix is Zero
Informal description
For any vector $v : l \oplus m \to \alpha$, the bottom-left block of the diagonal matrix constructed from $v$ is the zero matrix. That is, if $M$ is the diagonal matrix with entries $v_i$ on the diagonal, then the submatrix corresponding to rows in $m$ and columns in $l$ satisfies $M_{m \times l} = 0$.
Matrix.fromBlocks_one theorem
: fromBlocks (1 : Matrix l l α) 0 0 (1 : Matrix m m α) = 1
Full source
@[simp]
theorem fromBlocks_one : fromBlocks (1 : Matrix l l α) 0 0 (1 : Matrix m m α) = 1 := by
  ext i j
  rcases i with ⟨⟩ <;> rcases j with ⟨⟩ <;> simp [one_apply]
Block Diagonal Identity Matrix Construction
Informal description
Let $l$ and $m$ be types, and let $\alpha$ be a type with a multiplicative identity. The block matrix constructed from the identity matrix of size $l \times l$ in the top-left block, the zero matrix of size $l \times m$ in the top-right block, the zero matrix of size $m \times l$ in the bottom-left block, and the identity matrix of size $m \times m$ in the bottom-right block is equal to the identity matrix of size $(l \oplus m) \times (l \oplus m)$. In other words, the block diagonal matrix formed by two identity matrices is itself an identity matrix: $$ \begin{pmatrix} I_l & 0 \\ 0 & I_m \end{pmatrix} = I_{l \oplus m} $$ where $I_k$ denotes the identity matrix of size $k \times k$ and $0$ denotes the appropriately sized zero matrix.
Matrix.toBlock_one_self theorem
(p : m → Prop) : Matrix.toBlock (1 : Matrix m m α) p p = 1
Full source
@[simp]
theorem toBlock_one_self (p : m → Prop) : Matrix.toBlock (1 : Matrix m m α) p p = 1 :=
  toBlock_diagonal_self _ p
Identity Submatrix Equality for Predicate-Restricted Blocks
Informal description
For any predicate $p$ on the index set $m$, the submatrix of the identity matrix (of size $m \times m$) restricted to rows and columns where $p$ holds is itself an identity matrix. That is, if $I$ is the identity matrix, then \[ I_{\{i \mid p(i)\} \times \{i \mid p(i)\}} = I_{\{i \mid p(i)\}}. \]
Matrix.toBlock_one_disjoint theorem
{p q : m → Prop} (hpq : Disjoint p q) : Matrix.toBlock (1 : Matrix m m α) p q = 0
Full source
theorem toBlock_one_disjoint {p q : m → Prop} (hpq : Disjoint p q) :
    Matrix.toBlock (1 : Matrix m m α) p q = 0 :=
  toBlock_diagonal_disjoint _ hpq
Zero Submatrix Property for Disjoint Predicates on Identity Matrix
Informal description
For any predicates $p$ and $q$ on the index set $m$ that are disjoint (i.e., there is no $x \in m$ satisfying both $p(x)$ and $q(x)$), the submatrix of the identity matrix (of size $m \times m$) restricted to rows where $p$ holds and columns where $q$ holds is the zero matrix. That is, if $I$ is the identity matrix, then \[ I_{\{i \mid p(i)\} \times \{j \mid q(j)\}} = 0. \]
Matrix.blockDiagonal definition
(M : o → Matrix m n α) : Matrix (m × o) (n × o) α
Full source
/-- `Matrix.blockDiagonal M` turns a homogeneously-indexed collection of matrices
`M : o → Matrix m n α'` into an `m × o`-by-`n × o` block matrix which has the entries of `M` along
the diagonal and zero elsewhere.

See also `Matrix.blockDiagonal'` if the matrices may not have the same size everywhere.
-/
def blockDiagonal (M : o → Matrix m n α) : Matrix (m × o) (n × o) α :=
  of <| (fun ⟨i, k⟩ ⟨j, k'⟩ => if k = k' then M k i j else 0 : m × on × o → α)
Block diagonal matrix construction
Informal description
Given a collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ indexed by $o$, the function $\text{blockDiagonal}\, M$ constructs a block diagonal matrix of size $(m \times o) \times (n \times o)$. The entries of each matrix $M_k$ (for $k \in o$) are placed along the diagonal of the resulting matrix, and all other entries are zero. Specifically, for indices $(i, k)$ and $(j, k')$ in $m \times o$ and $n \times o$ respectively, the entry is $M_k\, i\, j$ if $k = k'$, and $0$ otherwise.
Matrix.blockDiagonal_apply' theorem
(M : o → Matrix m n α) (i k j k') : blockDiagonal M ⟨i, k⟩ ⟨j, k'⟩ = if k = k' then M k i j else 0
Full source
theorem blockDiagonal_apply' (M : o → Matrix m n α) (i k j k') :
    blockDiagonal M ⟨i, k⟩ ⟨j, k'⟩ = if k = k' then M k i j else 0 :=
  rfl
Entry Formula for Block Diagonal Matrix
Informal description
For a collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ indexed by $o$, the entry of the block diagonal matrix $\text{blockDiagonal}\, M$ at position $\langle i, k \rangle$ (row) and $\langle j, k' \rangle$ (column) is given by: \[ (\text{blockDiagonal}\, M)_{\langle i, k \rangle, \langle j, k' \rangle} = \begin{cases} M_k(i, j) & \text{if } k = k', \\ 0 & \text{otherwise.} \end{cases} \] Here, $i \in m$, $j \in n$, and $k, k' \in o$.
Matrix.blockDiagonal_apply theorem
(M : o → Matrix m n α) (ik jk) : blockDiagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0
Full source
theorem blockDiagonal_apply (M : o → Matrix m n α) (ik jk) :
    blockDiagonal M ik jk = if ik.2 = jk.2 then M ik.2 ik.1 jk.1 else 0 := by
  cases ik
  cases jk
  rfl
Entry Formula for Block Diagonal Matrix
Informal description
Let $M : o \to \text{Matrix}\, m\, n\, \alpha$ be a collection of matrices indexed by $o$. For any indices $(i, k) \in m \times o$ and $(j, k') \in n \times o$, the entry of the block diagonal matrix $\text{blockDiagonal}\, M$ at position $((i, k), (j, k'))$ is given by: \[ (\text{blockDiagonal}\, M)_{(i,k),(j,k')} = \begin{cases} M_k(i, j) & \text{if } k = k' \\ 0 & \text{otherwise.} \end{cases} \]
Matrix.blockDiagonal_apply_eq theorem
(M : o → Matrix m n α) (i j k) : blockDiagonal M (i, k) (j, k) = M k i j
Full source
@[simp]
theorem blockDiagonal_apply_eq (M : o → Matrix m n α) (i j k) :
    blockDiagonal M (i, k) (j, k) = M k i j :=
  if_pos rfl
Diagonal Block Entry in Block Diagonal Matrix
Informal description
For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ indexed by $o$, and for any indices $i \in m$, $j \in n$, and $k \in o$, the $(i,k)$-$(j,k)$ entry of the block diagonal matrix $\text{blockDiagonal}\, M$ is equal to the $(i,j)$ entry of the matrix $M_k$, i.e., $$(\text{blockDiagonal}\, M)_{(i,k),(j,k)} = M_k(i,j).$$
Matrix.blockDiagonal_apply_ne theorem
(M : o → Matrix m n α) (i j) {k k'} (h : k ≠ k') : blockDiagonal M (i, k) (j, k') = 0
Full source
theorem blockDiagonal_apply_ne (M : o → Matrix m n α) (i j) {k k'} (h : k ≠ k') :
    blockDiagonal M (i, k) (j, k') = 0 :=
  if_neg h
Off-Diagonal Blocks of Block Diagonal Matrix are Zero
Informal description
For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ and any indices $i \in m$, $j \in n$, if $k \neq k'$ for $k, k' \in o$, then the $(i,k)$-$(j,k')$ entry of the block diagonal matrix $\text{blockDiagonal}\, M$ is zero, i.e., $(\text{blockDiagonal}\, M)_{(i,k),(j,k')} = 0$.
Matrix.blockDiagonal_map theorem
(M : o → Matrix m n α) (f : α → β) (hf : f 0 = 0) : (blockDiagonal M).map f = blockDiagonal fun k => (M k).map f
Full source
theorem blockDiagonal_map (M : o → Matrix m n α) (f : α → β) (hf : f 0 = 0) :
    (blockDiagonal M).map f = blockDiagonal fun k => (M k).map f := by
  ext
  simp only [map_apply, blockDiagonal_apply, eq_comm]
  rw [apply_ite f, hf]
Entry-wise Map Commutes with Block Diagonal Construction
Informal description
Let $M : o \to \text{Matrix}\, m\, n\, \alpha$ be a collection of matrices indexed by $o$, and let $f : \alpha \to \beta$ be a function such that $f(0) = 0$. Then, the entry-wise application of $f$ to the block diagonal matrix $\text{blockDiagonal}\, M$ is equal to the block diagonal matrix formed by applying $f$ to each block $M_k$: \[ (\text{blockDiagonal}\, M).\text{map}\, f = \text{blockDiagonal}\, (k \mapsto (M_k).\text{map}\, f). \]
Matrix.blockDiagonal_transpose theorem
(M : o → Matrix m n α) : (blockDiagonal M)ᵀ = blockDiagonal fun k => (M k)ᵀ
Full source
@[simp]
theorem blockDiagonal_transpose (M : o → Matrix m n α) :
    (blockDiagonal M)ᵀ = blockDiagonal fun k => (M k)ᵀ := by
  ext
  simp only [transpose_apply, blockDiagonal_apply, eq_comm]
  split_ifs with h
  · rw [h]
  · rfl
Transpose of Block Diagonal Matrix Equals Block Diagonal of Transposes
Informal description
Let $M : o \to \text{Matrix}\, m\, n\, \alpha$ be a collection of matrices indexed by $o$. The transpose of the block diagonal matrix $\text{blockDiagonal}\, M$ is equal to the block diagonal matrix formed by the transposes of the individual matrices $M_k$, i.e., \[ (\text{blockDiagonal}\, M)^\top = \text{blockDiagonal}\, (\lambda k, (M_k)^\top). \]
Matrix.blockDiagonal_conjTranspose theorem
{α : Type*} [AddMonoid α] [StarAddMonoid α] (M : o → Matrix m n α) : (blockDiagonal M)ᴴ = blockDiagonal fun k => (M k)ᴴ
Full source
@[simp]
theorem blockDiagonal_conjTranspose {α : Type*} [AddMonoid α] [StarAddMonoid α]
    (M : o → Matrix m n α) : (blockDiagonal M)ᴴ = blockDiagonal fun k => (M k)ᴴ := by
  simp only [conjTranspose, blockDiagonal_transpose]
  rw [blockDiagonal_map _ star (star_zero α)]
Conjugate Transpose of Block Diagonal Matrix Equals Block Diagonal of Conjugate Transposes
Informal description
Let $\alpha$ be a type equipped with an additive monoid structure and a star operation that is additive. For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ indexed by $o$, the conjugate transpose of the block diagonal matrix $\text{blockDiagonal}\, M$ is equal to the block diagonal matrix formed by the conjugate transposes of the individual matrices $M_k$, i.e., \[ (\text{blockDiagonal}\, M)^H = \text{blockDiagonal}\, (\lambda k, (M_k)^H). \]
Matrix.blockDiagonal_zero theorem
: blockDiagonal (0 : o → Matrix m n α) = 0
Full source
@[simp]
theorem blockDiagonal_zero : blockDiagonal (0 : o → Matrix m n α) = 0 := by
  ext
  simp [blockDiagonal_apply]
Block Diagonal of Zero Matrix is Zero
Informal description
The block diagonal matrix constructed from the zero matrix (where all entries are zero) is equal to the zero matrix. That is, $\text{blockDiagonal}\, (0 : o \to \text{Matrix}\, m\, n\, \alpha) = 0$.
Matrix.blockDiagonal_diagonal theorem
[DecidableEq m] (d : o → m → α) : (blockDiagonal fun k => diagonal (d k)) = diagonal fun ik => d ik.2 ik.1
Full source
@[simp]
theorem blockDiagonal_diagonal [DecidableEq m] (d : o → m → α) :
    (blockDiagonal fun k => diagonal (d k)) = diagonal fun ik => d ik.2 ik.1 := by
  ext ⟨i, k⟩ ⟨j, k'⟩
  simp only [blockDiagonal_apply, diagonal_apply, Prod.mk_inj, ← ite_and]
  congr 1
  rw [and_comm]
Block Diagonal of Diagonal Matrices Equals Diagonal Matrix
Informal description
Let $m$ and $o$ be types, $\alpha$ a type with decidable equality on $m$, and $d : o \to m \to \alpha$ a family of vectors. The block diagonal matrix constructed from diagonal matrices (where the $k$-th block is the diagonal matrix formed by $d(k)$) equals the diagonal matrix whose entries are given by $d(k)(i)$ at position $(i,k)$.
Matrix.blockDiagonal_one theorem
[DecidableEq m] [One α] : blockDiagonal (1 : o → Matrix m m α) = 1
Full source
@[simp]
theorem blockDiagonal_one [DecidableEq m] [One α] : blockDiagonal (1 : o → Matrix m m α) = 1 :=
  show (blockDiagonal fun _ : o => diagonal fun _ : m => (1 : α)) = diagonal fun _ => 1 by
    rw [blockDiagonal_diagonal]
Block Diagonal of Identity Matrices is Identity Matrix
Informal description
Let $m$ and $o$ be types with decidable equality on $m$, and let $\alpha$ be a type with a multiplicative identity. The block diagonal matrix constructed from identity matrices (where each block is the identity matrix of size $m \times m$) is equal to the identity matrix of size $(m \times o) \times (m \times o)$. That is, \[ \text{blockDiagonal}\, (1 : o \to \text{Matrix}\, m\, m\, \alpha) = 1. \]
Matrix.blockDiagonal_add theorem
[AddZeroClass α] (M N : o → Matrix m n α) : blockDiagonal (M + N) = blockDiagonal M + blockDiagonal N
Full source
@[simp]
theorem blockDiagonal_add [AddZeroClass α] (M N : o → Matrix m n α) :
    blockDiagonal (M + N) = blockDiagonal M + blockDiagonal N := by
  ext
  simp only [blockDiagonal_apply, Pi.add_apply, add_apply]
  split_ifs <;> simp
Additivity of Block Diagonal Matrix Construction
Informal description
Let $\alpha$ be a type with an addition operation that forms an `AddZeroClass`. For any two collections of matrices $M, N : o \to \text{Matrix}\, m\, n\, \alpha$, the block diagonal matrix of their sum is equal to the sum of their block diagonal matrices: \[ \text{blockDiagonal}\, (M + N) = \text{blockDiagonal}\, M + \text{blockDiagonal}\, N. \]
Matrix.blockDiagonalAddMonoidHom definition
[AddZeroClass α] : (o → Matrix m n α) →+ Matrix (m × o) (n × o) α
Full source
/-- `Matrix.blockDiagonal` as an `AddMonoidHom`. -/
@[simps]
def blockDiagonalAddMonoidHom [AddZeroClass α] :
    (o → Matrix m n α) →+ Matrix (m × o) (n × o) α where
  toFun := blockDiagonal
  map_zero' := blockDiagonal_zero
  map_add' := blockDiagonal_add
Block diagonal matrix additive monoid homomorphism
Informal description
The function `Matrix.blockDiagonalAddMonoidHom` constructs an additive monoid homomorphism that maps a collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ to their block diagonal matrix $\text{blockDiagonal}\, M$ of size $(m \times o) \times (n \times o)$. This homomorphism satisfies: 1. $\text{blockDiagonal}\, 0 = 0$ (preservation of zero) 2. $\text{blockDiagonal}\, (M + N) = \text{blockDiagonal}\, M + \text{blockDiagonal}\, N$ (preservation of addition)
Matrix.blockDiagonal_neg theorem
[AddGroup α] (M : o → Matrix m n α) : blockDiagonal (-M) = -blockDiagonal M
Full source
@[simp]
theorem blockDiagonal_neg [AddGroup α] (M : o → Matrix m n α) :
    blockDiagonal (-M) = -blockDiagonal M :=
  map_neg (blockDiagonalAddMonoidHom m n o α) M
Negation Commutes with Block Diagonal Construction
Informal description
Let $\alpha$ be an additive group. For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$, the block diagonal matrix of the negation $-M$ is equal to the negation of the block diagonal matrix of $M$: \[ \text{blockDiagonal}\, (-M) = -\text{blockDiagonal}\, M. \]
Matrix.blockDiagonal_sub theorem
[AddGroup α] (M N : o → Matrix m n α) : blockDiagonal (M - N) = blockDiagonal M - blockDiagonal N
Full source
@[simp]
theorem blockDiagonal_sub [AddGroup α] (M N : o → Matrix m n α) :
    blockDiagonal (M - N) = blockDiagonal M - blockDiagonal N :=
  map_sub (blockDiagonalAddMonoidHom m n o α) M N
Block Diagonal Preserves Matrix Subtraction
Informal description
Let $\alpha$ be an additive group, and let $m$, $n$, $o$ be arbitrary types. For any two families of matrices $M, N : o \to \text{Matrix}\, m\, n\, \alpha$, the block diagonal matrix of their difference $M - N$ is equal to the difference of their block diagonal matrices: \[ \text{blockDiagonal}\, (M - N) = \text{blockDiagonal}\, M - \text{blockDiagonal}\, N. \]
Matrix.blockDiagonal_mul theorem
[Fintype n] [Fintype o] [NonUnitalNonAssocSemiring α] (M : o → Matrix m n α) (N : o → Matrix n p α) : (blockDiagonal fun k => M k * N k) = blockDiagonal M * blockDiagonal N
Full source
@[simp]
theorem blockDiagonal_mul [Fintype n] [Fintype o] [NonUnitalNonAssocSemiring α]
    (M : o → Matrix m n α) (N : o → Matrix n p α) :
    (blockDiagonal fun k => M k * N k) = blockDiagonal M * blockDiagonal N := by
  ext ⟨i, k⟩ ⟨j, k'⟩
  simp only [blockDiagonal_apply, mul_apply, ← Finset.univ_product_univ, Finset.sum_product]
  split_ifs with h <;> simp [h]
Product of Block Diagonal Matrices Equals Block Diagonal of Products
Informal description
Let $m$, $n$, $p$, and $o$ be finite types, and let $\alpha$ be a non-unital non-associative semiring. Given two families of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ and $N : o \to \text{Matrix}\, n\, p\, \alpha$, the block diagonal matrix constructed from the pointwise product $M_k N_k$ is equal to the product of the block diagonal matrices constructed from $M$ and $N$: \[ \text{blockDiagonal}\, (\lambda k, M_k N_k) = (\text{blockDiagonal}\, M) (\text{blockDiagonal}\, N). \]
Matrix.blockDiagonalRingHom definition
[DecidableEq m] [Fintype o] [Fintype m] [NonAssocSemiring α] : (o → Matrix m m α) →+* Matrix (m × o) (m × o) α
Full source
/-- `Matrix.blockDiagonal` as a `RingHom`. -/
@[simps]
def blockDiagonalRingHom [DecidableEq m] [Fintype o] [Fintype m] [NonAssocSemiring α] :
    (o → Matrix m m α) →+* Matrix (m × o) (m × o) α :=
  { blockDiagonalAddMonoidHom m m o α with
    toFun := blockDiagonal
    map_one' := blockDiagonal_one
    map_mul' := blockDiagonal_mul }
Ring homomorphism of block diagonal matrices
Informal description
Given a finite type `o`, a finite type `m` with decidable equality, and a non-associative semiring `α`, the function `Matrix.blockDiagonalRingHom` is a ring homomorphism that maps a family of square matrices $M : o \to \text{Matrix}\, m\, m\, \alpha$ to their block diagonal matrix $\text{blockDiagonal}\, M$ of size $(m \times o) \times (m \times o)$. This homomorphism satisfies: 1. $\text{blockDiagonal}\, 1 = 1$ (preservation of multiplicative identity) 2. $\text{blockDiagonal}\, (M \cdot N) = (\text{blockDiagonal}\, M) \cdot (\text{blockDiagonal}\, N)$ (preservation of multiplication) 3. $\text{blockDiagonal}\, 0 = 0$ (preservation of additive identity) 4. $\text{blockDiagonal}\, (M + N) = \text{blockDiagonal}\, M + \text{blockDiagonal}\, N$ (preservation of addition)
Matrix.blockDiagonal_pow theorem
[DecidableEq m] [Fintype o] [Fintype m] [Semiring α] (M : o → Matrix m m α) (n : ℕ) : blockDiagonal (M ^ n) = blockDiagonal M ^ n
Full source
@[simp]
theorem blockDiagonal_pow [DecidableEq m] [Fintype o] [Fintype m] [Semiring α]
    (M : o → Matrix m m α) (n : ) : blockDiagonal (M ^ n) = blockDiagonal M ^ n :=
  map_pow (blockDiagonalRingHom m o α) M n
Power of Block Diagonal Matrix Equals Block Diagonal of Powers
Informal description
Let $m$ and $o$ be finite types with decidable equality on $m$, and let $\alpha$ be a semiring. For any family of square matrices $M : o \to \text{Matrix}\, m\, m\, \alpha$ and any natural number $n$, the block diagonal matrix formed by the $n$-th powers of the $M_k$ is equal to the $n$-th power of the block diagonal matrix formed by $M$: \[ \text{blockDiagonal}\, (M^n) = (\text{blockDiagonal}\, M)^n. \]
Matrix.blockDiagonal_smul theorem
{R : Type*} [Zero α] [SMulZeroClass R α] (x : R) (M : o → Matrix m n α) : blockDiagonal (x • M) = x • blockDiagonal M
Full source
@[simp]
theorem blockDiagonal_smul {R : Type*} [Zero α] [SMulZeroClass R α] (x : R)
    (M : o → Matrix m n α) : blockDiagonal (x • M) = x • blockDiagonal M := by
  ext
  simp only [blockDiagonal_apply, Pi.smul_apply, smul_apply]
  split_ifs <;> simp
Scalar Multiplication Commutes with Block Diagonal Construction
Informal description
Let $R$ be a type with a scalar multiplication operation on a type $\alpha$ that preserves zero (i.e., $0 \in \alpha$ satisfies $x \cdot 0 = 0$ for all $x \in R$). For any scalar $x \in R$ and any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$, the block diagonal matrix formed by the scalar multiples $x \cdot M_k$ (for $k \in o$) is equal to the scalar multiple $x$ applied to the block diagonal matrix formed by $M$. In other words: \[ \text{blockDiagonal}\, (x \cdot M) = x \cdot \text{blockDiagonal}\, M. \]
Matrix.blockDiag definition
(M : Matrix (m × o) (n × o) α) (k : o) : Matrix m n α
Full source
/-- Extract a block from the diagonal of a block diagonal matrix.

This is the block form of `Matrix.diag`, and the left-inverse of `Matrix.blockDiagonal`. -/
def blockDiag (M : Matrix (m × o) (n × o) α) (k : o) : Matrix m n α :=
  of fun i j => M (i, k) (j, k)
Extraction of diagonal block from a block diagonal matrix
Informal description
For a block diagonal matrix $M$ of type $\text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$, the function extracts the $k$-th diagonal block as a matrix of type $\text{Matrix}\, m\, n\, \alpha$. Specifically, the $(i,j)$-th entry of the extracted block is equal to the $((i,k), (j,k))$-th entry of $M$.
Matrix.blockDiag_apply theorem
(M : Matrix (m × o) (n × o) α) (k : o) (i j) : blockDiag M k i j = M (i, k) (j, k)
Full source
theorem blockDiag_apply (M : Matrix (m × o) (n × o) α) (k : o) (i j) :
    blockDiag M k i j = M (i, k) (j, k) :=
  rfl
Entry-wise Formula for Diagonal Block Extraction
Informal description
For a block diagonal matrix $M \in \text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$, the $(i,j)$-th entry of the $k$-th diagonal block $\text{blockDiag}\, M\, k$ is equal to the $((i,k), (j,k))$-th entry of $M$, i.e., $$(\text{blockDiag}\, M\, k)_{i,j} = M_{(i,k),(j,k)}.$$
Matrix.blockDiag_map theorem
(M : Matrix (m × o) (n × o) α) (f : α → β) : blockDiag (M.map f) = fun k => (blockDiag M k).map f
Full source
theorem blockDiag_map (M : Matrix (m × o) (n × o) α) (f : α → β) :
    blockDiag (M.map f) = fun k => (blockDiag M k).map f :=
  rfl
Commutativity of Block Diagonal Extraction with Entry-wise Mapping
Informal description
For any block diagonal matrix $M \in \text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ and any function $f : \alpha \to \beta$, the $k$-th diagonal block of the matrix obtained by applying $f$ entry-wise to $M$ is equal to the matrix obtained by applying $f$ entry-wise to the $k$-th diagonal block of $M$. In other words, for each $k \in o$, we have $\text{blockDiag}\, (M.map\, f)\, k = (\text{blockDiag}\, M\, k).map\, f$.
Matrix.blockDiag_transpose theorem
(M : Matrix (m × o) (n × o) α) (k : o) : blockDiag Mᵀ k = (blockDiag M k)ᵀ
Full source
@[simp]
theorem blockDiag_transpose (M : Matrix (m × o) (n × o) α) (k : o) :
    blockDiag Mᵀ k = (blockDiag M k)ᵀ :=
  ext fun _ _ => rfl
Transpose Commutes with Block Diagonal Extraction
Informal description
For any block matrix $M \in \text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ and any index $k \in o$, the $k$-th diagonal block of the transpose matrix $M^\top$ is equal to the transpose of the $k$-th diagonal block of $M$. That is, $$(\text{blockDiag}\, M^\top\, k) = (\text{blockDiag}\, M\, k)^\top.$$
Matrix.blockDiag_conjTranspose theorem
{α : Type*} [Star α] (M : Matrix (m × o) (n × o) α) (k : o) : blockDiag Mᴴ k = (blockDiag M k)ᴴ
Full source
@[simp]
theorem blockDiag_conjTranspose {α : Type*} [Star α]
    (M : Matrix (m × o) (n × o) α) (k : o) : blockDiag Mᴴ k = (blockDiag M k)ᴴ :=
  ext fun _ _ => rfl
Conjugate Transpose of Block Diagonal Matrix Extracts to Conjugate Transpose of Blocks
Informal description
For any block diagonal matrix $M \in \text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ with entries in a type $\alpha$ equipped with a star operation, and for any index $k \in o$, the $k$-th diagonal block of the conjugate transpose $M^H$ is equal to the conjugate transpose of the $k$-th diagonal block of $M$. That is, $\text{blockDiag}\, M^H\, k = (\text{blockDiag}\, M\, k)^H$.
Matrix.blockDiag_zero theorem
: blockDiag (0 : Matrix (m × o) (n × o) α) = 0
Full source
@[simp]
theorem blockDiag_zero : blockDiag (0 : Matrix (m × o) (n × o) α) = 0 :=
  rfl
Diagonal Blocks of Zero Matrix are Zero
Informal description
For any type $\alpha$ with a zero element, the diagonal blocks of the zero matrix in $\mathrm{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ are themselves zero matrices in $\mathrm{Matrix}\, m\, n\, \alpha$. That is, $\mathrm{blockDiag}\, 0 = 0$.
Matrix.blockDiag_diagonal theorem
[DecidableEq o] [DecidableEq m] (d : m × o → α) (k : o) : blockDiag (diagonal d) k = diagonal fun i => d (i, k)
Full source
@[simp]
theorem blockDiag_diagonal [DecidableEq o] [DecidableEq m] (d : m × o → α) (k : o) :
    blockDiag (diagonal d) k = diagonal fun i => d (i, k) :=
  ext fun i j => by
    obtain rfl | hij := Decidable.eq_or_ne i j
    · rw [blockDiag_apply, diagonal_apply_eq, diagonal_apply_eq]
    · rw [blockDiag_apply, diagonal_apply_ne _ hij, diagonal_apply_ne _ (mt _ hij)]
      exact Prod.fst_eq_iff.mpr
Diagonal Block Extraction from Block Diagonal Matrix: $\mathrm{blockDiag}\, (\mathrm{diagonal}\, d)\, k = \mathrm{diagonal}\, (\lambda i, d(i, k))$
Informal description
Let $m$ and $o$ be types with decidable equality, and let $\alpha$ be a type. Given a function $d : m \times o \to \alpha$ and an index $k \in o$, the $k$-th diagonal block of the block diagonal matrix $\mathrm{diagonal}\, d$ is equal to the diagonal matrix $\mathrm{diagonal}\, (\lambda i, d(i, k))$. That is, $$\mathrm{blockDiag}\, (\mathrm{diagonal}\, d)\, k = \mathrm{diagonal}\, (\lambda i, d(i, k)).$$
Matrix.blockDiag_blockDiagonal theorem
[DecidableEq o] (M : o → Matrix m n α) : blockDiag (blockDiagonal M) = M
Full source
@[simp]
theorem blockDiag_blockDiagonal [DecidableEq o] (M : o → Matrix m n α) :
    blockDiag (blockDiagonal M) = M :=
  funext fun _ => ext fun i j => blockDiagonal_apply_eq M i j _
Diagonal Blocks of Block Diagonal Matrix Recover Original Matrices
Informal description
For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ indexed by $o$, the diagonal blocks of the block diagonal matrix $\text{blockDiagonal}\, M$ are exactly the original matrices $M$. That is, for each $k \in o$, the $k$-th diagonal block satisfies $(\text{blockDiag}\, (\text{blockDiagonal}\, M))_k = M_k$.
Matrix.blockDiagonal_injective theorem
[DecidableEq o] : Function.Injective (blockDiagonal : (o → Matrix m n α) → Matrix _ _ α)
Full source
theorem blockDiagonal_injective [DecidableEq o] :
    Function.Injective (blockDiagonal : (o → Matrix m n α) → Matrix _ _ α) :=
  Function.LeftInverse.injective blockDiag_blockDiagonal
Injectivity of Block Diagonal Matrix Construction
Informal description
For any type $o$ with decidable equality, the function $\mathrm{blockDiagonal} : (o \to \mathrm{Matrix}\, m\, n\, \alpha) \to \mathrm{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ is injective. That is, if $\mathrm{blockDiagonal}\, M = \mathrm{blockDiagonal}\, N$ for two collections of matrices $M, N : o \to \mathrm{Matrix}\, m\, n\, \alpha$, then $M = N$.
Matrix.blockDiagonal_inj theorem
[DecidableEq o] {M N : o → Matrix m n α} : blockDiagonal M = blockDiagonal N ↔ M = N
Full source
@[simp]
theorem blockDiagonal_inj [DecidableEq o] {M N : o → Matrix m n α} :
    blockDiagonalblockDiagonal M = blockDiagonal N ↔ M = N :=
  blockDiagonal_injective.eq_iff
Equality of Block Diagonal Matrices $\leftrightarrow$ Equality of Component Matrices
Informal description
For any type $o$ with decidable equality and any two collections of matrices $M, N : o \to \text{Matrix}\, m\, n\, \alpha$, the block diagonal matrix constructed from $M$ equals the block diagonal matrix constructed from $N$ if and only if $M$ equals $N$. In other words, $\text{blockDiagonal}\, M = \text{blockDiagonal}\, N \leftrightarrow M = N$.
Matrix.blockDiag_one theorem
[DecidableEq o] [DecidableEq m] [One α] : blockDiag (1 : Matrix (m × o) (m × o) α) = 1
Full source
@[simp]
theorem blockDiag_one [DecidableEq o] [DecidableEq m] [One α] :
    blockDiag (1 : Matrix (m × o) (m × o) α) = 1 :=
  funext <| blockDiag_diagonal _
Diagonal Blocks of Identity Matrix are Identity Matrices: $\mathrm{blockDiag}\, (1) = 1$
Informal description
Let $m$ and $o$ be types with decidable equality, and let $\alpha$ be a type with a multiplicative identity. The diagonal blocks of the identity matrix in $\mathrm{Matrix}\, (m \times o)\, (m \times o)\, \alpha$ are themselves identity matrices. That is, for each $k \in o$, the $k$-th diagonal block satisfies $\mathrm{blockDiag}\, (1) = 1$, where the first $1$ is the identity matrix in $\mathrm{Matrix}\, (m \times o)\, (m \times o)\, \alpha$ and the second $1$ is the identity matrix in $\mathrm{Matrix}\, m\, m\, \alpha$.
Matrix.blockDiag_add theorem
[Add α] (M N : Matrix (m × o) (n × o) α) : blockDiag (M + N) = blockDiag M + blockDiag N
Full source
@[simp]
theorem blockDiag_add [Add α] (M N : Matrix (m × o) (n × o) α) :
    blockDiag (M + N) = blockDiag M + blockDiag N :=
  rfl
Additivity of Diagonal Block Extraction: $\mathrm{blockDiag}\, (M + N) = \mathrm{blockDiag}\, M + \mathrm{blockDiag}\, N$
Informal description
For any type $\alpha$ equipped with an addition operation, and for any two block diagonal matrices $M, N \in \mathrm{Matrix}\, (m \times o)\, (n \times o)\, \alpha$, the diagonal blocks of the sum $M + N$ are equal to the sum of the diagonal blocks of $M$ and $N$. That is, for each $k \in o$, the $k$-th diagonal block of $M + N$ satisfies $(\mathrm{blockDiag}\, (M + N))_k = (\mathrm{blockDiag}\, M)_k + (\mathrm{blockDiag}\, N)_k$.
Matrix.blockDiagAddMonoidHom definition
[AddZeroClass α] : Matrix (m × o) (n × o) α →+ o → Matrix m n α
Full source
/-- `Matrix.blockDiag` as an `AddMonoidHom`. -/
@[simps]
def blockDiagAddMonoidHom [AddZeroClass α] : MatrixMatrix (m × o) (n × o) α →+ o → Matrix m n α where
  toFun := blockDiag
  map_zero' := blockDiag_zero
  map_add' := blockDiag_add
Block Diagonal Extraction as an Additive Monoid Homomorphism
Informal description
The additive monoid homomorphism version of `Matrix.blockDiag`, which maps a block diagonal matrix $M \in \text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$ to a function $o \to \text{Matrix}\, m\, n\, \alpha$ that extracts each diagonal block. Specifically, for a matrix $M$ and index $k \in o$, the $k$-th component of the resulting function gives the $k$-th diagonal block of $M$, which is a matrix in $\text{Matrix}\, m\, n\, \alpha$. This homomorphism preserves addition and the zero matrix.
Matrix.blockDiag_neg theorem
[AddGroup α] (M : Matrix (m × o) (n × o) α) : blockDiag (-M) = -blockDiag M
Full source
@[simp]
theorem blockDiag_neg [AddGroup α] (M : Matrix (m × o) (n × o) α) : blockDiag (-M) = -blockDiag M :=
  map_neg (blockDiagAddMonoidHom m n o α) M
Negation Commutes with Block Diagonal Extraction: $\mathrm{blockDiag}\, (-M) = -\mathrm{blockDiag}\, M$
Informal description
For any additive group $\alpha$ and any block diagonal matrix $M \in \mathrm{Matrix}\, (m \times o)\, (n \times o)\, \alpha$, the diagonal blocks of the negation $-M$ are equal to the negation of the diagonal blocks of $M$. That is, $\mathrm{blockDiag}\, (-M) = -\mathrm{blockDiag}\, M$.
Matrix.blockDiag_sub theorem
[AddGroup α] (M N : Matrix (m × o) (n × o) α) : blockDiag (M - N) = blockDiag M - blockDiag N
Full source
@[simp]
theorem blockDiag_sub [AddGroup α] (M N : Matrix (m × o) (n × o) α) :
    blockDiag (M - N) = blockDiag M - blockDiag N :=
  map_sub (blockDiagAddMonoidHom m n o α) M N
Subtraction Commutes with Block Diagonal Extraction: $\mathrm{blockDiag}\, (M - N) = \mathrm{blockDiag}\, M - \mathrm{blockDiag}\, N$
Informal description
For any additive group $\alpha$ and any block diagonal matrices $M, N \in \mathrm{Matrix}\, (m \times o)\, (n \times o)\, \alpha$, the diagonal blocks of the difference $M - N$ are equal to the difference of the diagonal blocks of $M$ and $N$. That is, $\mathrm{blockDiag}\, (M - N) = \mathrm{blockDiag}\, M - \mathrm{blockDiag}\, N$.
Matrix.blockDiag_smul theorem
{R : Type*} [SMul R α] (x : R) (M : Matrix (m × o) (n × o) α) : blockDiag (x • M) = x • blockDiag M
Full source
@[simp]
theorem blockDiag_smul {R : Type*} [SMul R α] (x : R)
    (M : Matrix (m × o) (n × o) α) : blockDiag (x • M) = x • blockDiag M :=
  rfl
Scalar Multiplication Commutes with Block Diagonal Extraction: $\text{blockDiag}\, (x \cdot M) = x \cdot \text{blockDiag}\, M$
Informal description
Let $R$ be a type with a scalar multiplication operation on a type $\alpha$, and let $M$ be a block diagonal matrix in $\text{Matrix}\, (m \times o)\, (n \times o)\, \alpha$. For any scalar $x \in R$, the diagonal blocks of the scalar multiple $x \cdot M$ are equal to the scalar multiples of the diagonal blocks of $M$. That is, for each block index $k \in o$, we have $\text{blockDiag}\, (x \cdot M)\, k = x \cdot (\text{blockDiag}\, M\, k)$.
Matrix.blockDiagonal' definition
(M : ∀ i, Matrix (m' i) (n' i) α) : Matrix (Σ i, m' i) (Σ i, n' i) α
Full source
/-- `Matrix.blockDiagonal' M` turns `M : Π i, Matrix (m i) (n i) α` into a
`Σ i, m i`-by-`Σ i, n i` block matrix which has the entries of `M` along the diagonal
and zero elsewhere.

This is the dependently-typed version of `Matrix.blockDiagonal`. -/
def blockDiagonal' (M : ∀ i, Matrix (m' i) (n' i) α) : Matrix (Σ i, m' i) (Σ i, n' i) α :=
  of <|
    (fun ⟨k, i⟩ ⟨k', j⟩ => if h : k = k' then M k i (cast (congr_arg n' h.symm) j) else 0 :
      (Σ i, m' i) → (Σ i, n' i) → α)
Dependently typed block diagonal matrix construction
Informal description
Given a family of matrices $M_i$ of type $\text{Matrix}\, (m_i)\, (n_i)\, \alpha$ indexed by $i$, the function $\text{blockDiagonal}'$ constructs a block diagonal matrix of type $\text{Matrix}\, (\Sigma i, m_i)\, (\Sigma i, n_i)\, \alpha$. The resulting matrix has the entries of each $M_i$ along its diagonal and zeros elsewhere. More precisely, for indices $\langle k, i \rangle$ and $\langle k', j \rangle$ in the dependent sum type $\Sigma i, m_i$ and $\Sigma i, n_i$ respectively, the entry at position $(\langle k, i \rangle, \langle k', j \rangle)$ is given by: \[ \text{blockDiagonal}'\, M\, \langle k, i \rangle\, \langle k', j \rangle = \begin{cases} M_k\, i\, j' & \text{if } k = k' \text{ and } j' \text{ is } j \text{ cast along the equality } k = k', \\ 0 & \text{otherwise.} \end{cases} \] This is the dependently typed version of $\text{blockDiagonal}$, allowing for blocks of varying sizes.
Matrix.blockDiagonal'_apply' theorem
(M : ∀ i, Matrix (m' i) (n' i) α) (k i k' j) : blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩ = if h : k = k' then M k i (cast (congr_arg n' h.symm) j) else 0
Full source
theorem blockDiagonal'_apply' (M : ∀ i, Matrix (m' i) (n' i) α) (k i k' j) :
    blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩ =
      if h : k = k' then M k i (cast (congr_arg n' h.symm) j) else 0 :=
  rfl
Entry Formula for Dependently Typed Block Diagonal Matrix
Informal description
Given a family of matrices $M_i \in \text{Matrix}\,(m'_i)\,(n'_i)\,\alpha$ indexed by $i$, the $( \langle k, i \rangle, \langle k', j \rangle )$-entry of the block diagonal matrix $\text{blockDiagonal}'\, M$ is defined as: \[ (\text{blockDiagonal}'\, M)_{\langle k, i \rangle, \langle k', j \rangle} = \begin{cases} M_k\, i\, j' & \text{if } k = k' \text{ where } j' \text{ is } j \text{ cast along } k = k', \\ 0 & \text{otherwise.} \end{cases} \] Here $\langle k, i \rangle$ and $\langle k', j \rangle$ are indices in the dependent sum types $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively.
Matrix.blockDiagonal'_eq_blockDiagonal theorem
(M : o → Matrix m n α) {k k'} (i j) : blockDiagonal M (i, k) (j, k') = blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩
Full source
theorem blockDiagonal'_eq_blockDiagonal (M : o → Matrix m n α) {k k'} (i j) :
    blockDiagonal M (i, k) (j, k') = blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩ :=
  rfl
Equality between block diagonal and dependently typed block diagonal matrices
Informal description
For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$ and indices $k, k' \in o$, $i \in m$, $j \in n$, the entry of the block diagonal matrix $\text{blockDiagonal}\, M$ at position $(i, k)$ and $(j, k')$ is equal to the corresponding entry of the dependently typed block diagonal matrix $\text{blockDiagonal}'\, M$ at positions $\langle k, i \rangle$ and $\langle k', j \rangle$. In other words, for any $i, j, k, k'$, we have: \[ \text{blockDiagonal}\, M\, (i, k)\, (j, k') = \text{blockDiagonal}'\, M\, \langle k, i \rangle\, \langle k', j \rangle. \]
Matrix.blockDiagonal'_submatrix_eq_blockDiagonal theorem
(M : o → Matrix m n α) : (blockDiagonal' M).submatrix (Prod.toSigma ∘ Prod.swap) (Prod.toSigma ∘ Prod.swap) = blockDiagonal M
Full source
theorem blockDiagonal'_submatrix_eq_blockDiagonal (M : o → Matrix m n α) :
    (blockDiagonal' M).submatrix (Prod.toSigmaProd.toSigma ∘ Prod.swap) (Prod.toSigmaProd.toSigma ∘ Prod.swap) =
      blockDiagonal M :=
  Matrix.ext fun ⟨_, _⟩ ⟨_, _⟩ => rfl
Equality of Block Diagonal Matrices under Submatrix and Reindexing
Informal description
For any collection of matrices $M : o \to \text{Matrix}\, m\, n\, \alpha$, the submatrix of the block diagonal matrix $\text{blockDiagonal}'\, M$, obtained by reindexing rows and columns via the composition of swapping and converting to sigma type, is equal to the block diagonal matrix $\text{blockDiagonal}\, M$. More precisely, let $\text{swap} : o \times m \to m \times o$ be the function that swaps the components of a pair, and $\text{toSigma} : m \times o \to \Sigma\, i : o, m$ be the conversion from product type to sigma type. Then: \[ (\text{blockDiagonal}'\, M).\text{submatrix}\, (\text{toSigma} \circ \text{swap})\, (\text{toSigma} \circ \text{swap}) = \text{blockDiagonal}\, M \]
Matrix.blockDiagonal'_apply theorem
(M : ∀ i, Matrix (m' i) (n' i) α) (ik jk) : blockDiagonal' M ik jk = if h : ik.1 = jk.1 then M ik.1 ik.2 (cast (congr_arg n' h.symm) jk.2) else 0
Full source
theorem blockDiagonal'_apply (M : ∀ i, Matrix (m' i) (n' i) α) (ik jk) :
    blockDiagonal' M ik jk =
      if h : ik.1 = jk.1 then M ik.1 ik.2 (cast (congr_arg n' h.symm) jk.2) else 0 := by
  cases ik
  cases jk
  rfl
Entry Formula for Dependently Typed Block Diagonal Matrix
Informal description
Given a family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, the $(ik, jk)$-th entry of the block diagonal matrix $\text{blockDiagonal}'\, M$ is defined as: \[ (\text{blockDiagonal}'\, M)_{ik, jk} = \begin{cases} M_k\, i\, j' & \text{if } k = k' \text{ and } j' \text{ is } j \text{ cast along } k = k', \\ 0 & \text{otherwise}, \end{cases} \] where $ik = (k, i)$ and $jk = (k', j)$ are indices in the dependent sum type $\Sigma i, m_i$ and $\Sigma i, n_i$ respectively.
Matrix.blockDiagonal'_apply_eq theorem
(M : ∀ i, Matrix (m' i) (n' i) α) (k i j) : blockDiagonal' M ⟨k, i⟩ ⟨k, j⟩ = M k i j
Full source
@[simp]
theorem blockDiagonal'_apply_eq (M : ∀ i, Matrix (m' i) (n' i) α) (k i j) :
    blockDiagonal' M ⟨k, i⟩ ⟨k, j⟩ = M k i j :=
  dif_pos rfl
Diagonal Block Preservation in $\text{blockDiagonal}'$ Construction
Informal description
For a family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, the block diagonal matrix $\text{blockDiagonal}'\, M$ satisfies \[ (\text{blockDiagonal}'\, M)_{\langle k, i \rangle, \langle k, j \rangle} = M_k i j \] for any $k$ and indices $i \in m_k$, $j \in n_k$. That is, the diagonal blocks of $\text{blockDiagonal}'\, M$ coincide with the original matrices $M_k$.
Matrix.blockDiagonal'_apply_ne theorem
(M : ∀ i, Matrix (m' i) (n' i) α) {k k'} (i j) (h : k ≠ k') : blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩ = 0
Full source
theorem blockDiagonal'_apply_ne (M : ∀ i, Matrix (m' i) (n' i) α) {k k'} (i j) (h : k ≠ k') :
    blockDiagonal' M ⟨k, i⟩ ⟨k', j⟩ = 0 :=
  dif_neg h
Off-Diagonal Blocks of Block Diagonal Matrix Are Zero
Informal description
For a family of matrices $M_i$ of type $\text{Matrix}\, (m_i)\, (n_i)\, \alpha$ indexed by $i$, the block diagonal matrix $\text{blockDiagonal}'\, M$ has zero entries at all off-diagonal positions. Specifically, for any indices $k \neq k'$, $i \in m_k$, and $j \in n_{k'}$, the entry at position $(\langle k, i \rangle, \langle k', j \rangle)$ is zero, i.e., \[ \text{blockDiagonal}'\, M\, \langle k, i \rangle\, \langle k', j \rangle = 0. \]
Matrix.blockDiagonal'_map theorem
(M : ∀ i, Matrix (m' i) (n' i) α) (f : α → β) (hf : f 0 = 0) : (blockDiagonal' M).map f = blockDiagonal' fun k => (M k).map f
Full source
theorem blockDiagonal'_map (M : ∀ i, Matrix (m' i) (n' i) α) (f : α → β) (hf : f 0 = 0) :
    (blockDiagonal' M).map f = blockDiagonal' fun k => (M k).map f := by
  ext
  simp only [map_apply, blockDiagonal'_apply, eq_comm]
  rw [apply_dite f, hf]
Commutativity of Entrywise Map with Block Diagonal Construction: $(\text{blockDiagonal}'\, M).\text{map}\, f = \text{blockDiagonal}'\, (M.\text{map}\, f)$
Informal description
Given a family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, a function $f : \alpha \to \beta$ with $f(0) = 0$, the entrywise application of $f$ to the block diagonal matrix $\text{blockDiagonal}'\, M$ is equal to the block diagonal matrix formed by applying $f$ entrywise to each $M_i$. That is, \[ (\text{blockDiagonal}'\, M).\text{map}\, f = \text{blockDiagonal}'\, (\lambda k, (M_k).\text{map}\, f). \]
Matrix.blockDiagonal'_transpose theorem
(M : ∀ i, Matrix (m' i) (n' i) α) : (blockDiagonal' M)ᵀ = blockDiagonal' fun k => (M k)ᵀ
Full source
@[simp]
theorem blockDiagonal'_transpose (M : ∀ i, Matrix (m' i) (n' i) α) :
    (blockDiagonal' M)ᵀ = blockDiagonal' fun k => (M k)ᵀ := by
  ext ⟨ii, ix⟩ ⟨ji, jx⟩
  simp only [transpose_apply, blockDiagonal'_apply]
  split_ifs <;> cc
Transpose of Block Diagonal Matrix Equals Block Diagonal of Transposes
Informal description
Let $M_i$ be a family of matrices in $\text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$. The transpose of the block diagonal matrix formed from the $M_i$ is equal to the block diagonal matrix formed from the transposes of the $M_i$. That is, \[ (\text{blockDiagonal}'\, M)^\top = \text{blockDiagonal}'\, (\lambda k, (M_k)^\top). \]
Matrix.blockDiagonal'_conjTranspose theorem
{α} [AddMonoid α] [StarAddMonoid α] (M : ∀ i, Matrix (m' i) (n' i) α) : (blockDiagonal' M)ᴴ = blockDiagonal' fun k => (M k)ᴴ
Full source
@[simp]
theorem blockDiagonal'_conjTranspose {α} [AddMonoid α] [StarAddMonoid α]
    (M : ∀ i, Matrix (m' i) (n' i) α) : (blockDiagonal' M)ᴴ = blockDiagonal' fun k => (M k)ᴴ := by
  simp only [conjTranspose, blockDiagonal'_transpose]
  exact blockDiagonal'_map _ star (star_zero α)
Conjugate Transpose of Block Diagonal Matrix Equals Block Diagonal of Conjugate Transposes
Informal description
Let $\alpha$ be a type equipped with an additive monoid structure and a star operation that is additive. Given a family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, the conjugate transpose of the block diagonal matrix formed from the $M_i$ is equal to the block diagonal matrix formed from the conjugate transposes of the $M_i$. That is, \[ (\text{blockDiagonal}'\, M)^H = \text{blockDiagonal}'\, (\lambda k, (M_k)^H). \]
Matrix.blockDiagonal'_zero theorem
: blockDiagonal' (0 : ∀ i, Matrix (m' i) (n' i) α) = 0
Full source
@[simp]
theorem blockDiagonal'_zero : blockDiagonal' (0 : ∀ i, Matrix (m' i) (n' i) α) = 0 := by
  ext
  simp [blockDiagonal'_apply]
Block Diagonal of Zero Matrices is Zero Matrix
Informal description
The block diagonal matrix constructed from a family of zero matrices is equal to the zero matrix. That is, for any family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ where each $M_i$ is the zero matrix, we have $\text{blockDiagonal}'\, M = 0$.
Matrix.blockDiagonal'_diagonal theorem
[∀ i, DecidableEq (m' i)] (d : ∀ i, m' i → α) : (blockDiagonal' fun k => diagonal (d k)) = diagonal fun ik => d ik.1 ik.2
Full source
@[simp]
theorem blockDiagonal'_diagonal [∀ i, DecidableEq (m' i)] (d : ∀ i, m' i → α) :
    (blockDiagonal' fun k => diagonal (d k)) = diagonal fun ik => d ik.1 ik.2 := by
  ext ⟨i, k⟩ ⟨j, k'⟩
  simp only [blockDiagonal'_apply, diagonal]
  obtain rfl | hij := Decidable.eq_or_ne i j
  · simp
  · simp [hij]
Block Diagonal of Diagonal Matrices Equals Diagonal Matrix
Informal description
Let $\{m_i\}_{i}$ be a family of types with decidable equality, and let $d_i : m_i \to \alpha$ be a family of vectors indexed by $i$. Then the block diagonal matrix constructed from the diagonal matrices $\text{diagonal}(d_i)$ is equal to the diagonal matrix whose entries are given by $d_{ik.1}(ik.2)$ for each index $ik = (i, k) \in \Sigma i, m_i$. In other words: \[ \text{blockDiagonal}'\, (\lambda k \mapsto \text{diagonal}\, (d_k)) = \text{diagonal}\, (\lambda (i, k) \mapsto d_i(k)) \]
Matrix.blockDiagonal'_one theorem
[∀ i, DecidableEq (m' i)] [One α] : blockDiagonal' (1 : ∀ i, Matrix (m' i) (m' i) α) = 1
Full source
@[simp]
theorem blockDiagonal'_one [∀ i, DecidableEq (m' i)] [One α] :
    blockDiagonal' (1 : ∀ i, Matrix (m' i) (m' i) α) = 1 :=
  show (blockDiagonal' fun i : o => diagonal fun _ : m' i => (1 : α)) = diagonal fun _ => 1 by
    rw [blockDiagonal'_diagonal]
Block Diagonal of Identity Matrices is Identity Matrix
Informal description
Let $\{m_i\}_{i}$ be a family of types with decidable equality and let $\alpha$ be a type with a multiplicative identity. Then the block diagonal matrix constructed from identity matrices (of appropriate sizes) is itself the identity matrix. That is: \[ \text{blockDiagonal}'\, (1 : \forall i, \text{Matrix}\, (m_i)\, (m_i)\, \alpha) = 1 \] where the right-hand side $1$ denotes the identity matrix over the sigma type $\Sigma i, m_i$.
Matrix.blockDiagonal'_add theorem
[AddZeroClass α] (M N : ∀ i, Matrix (m' i) (n' i) α) : blockDiagonal' (M + N) = blockDiagonal' M + blockDiagonal' N
Full source
@[simp]
theorem blockDiagonal'_add [AddZeroClass α] (M N : ∀ i, Matrix (m' i) (n' i) α) :
    blockDiagonal' (M + N) = blockDiagonal' M + blockDiagonal' N := by
  ext
  simp only [blockDiagonal'_apply, Pi.add_apply, add_apply]
  split_ifs <;> simp
Additivity of Block Diagonal Matrix Construction: $\text{blockDiagonal}'(M + N) = \text{blockDiagonal}' M + \text{blockDiagonal}' N$
Informal description
Let $\alpha$ be a type with an addition operation that forms an add-zero class. For any family of matrices $M_i, N_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, the block diagonal matrix formed by their sum is equal to the sum of their block diagonal matrices: \[ \text{blockDiagonal}'\, (M + N) = \text{blockDiagonal}'\, M + \text{blockDiagonal}'\, N. \] Here, $\text{blockDiagonal}'$ constructs a block diagonal matrix from a family of matrices with potentially varying dimensions.
Matrix.blockDiagonal'AddMonoidHom definition
[AddZeroClass α] : (∀ i, Matrix (m' i) (n' i) α) →+ Matrix (Σ i, m' i) (Σ i, n' i) α
Full source
/-- `Matrix.blockDiagonal'` as an `AddMonoidHom`. -/
@[simps]
def blockDiagonal'AddMonoidHom [AddZeroClass α] :
    (∀ i, Matrix (m' i) (n' i) α) →+ Matrix (Σ i, m' i) (Σ i, n' i) α where
  toFun := blockDiagonal'
  map_zero' := blockDiagonal'_zero
  map_add' := blockDiagonal'_add
Additive monoid homomorphism for block diagonal matrices with varying block sizes
Informal description
The function `Matrix.blockDiagonal'AddMonoidHom` constructs an additive monoid homomorphism that maps a family of matrices \( M_i \) of type \(\text{Matrix}\, (m_i)\, (n_i)\, \alpha\) (with \(\alpha\) forming an add-zero class) to their block diagonal matrix of type \(\text{Matrix}\, (\Sigma i, m_i)\, (\Sigma i, n_i)\, \alpha\). This homomorphism preserves addition and maps the zero family of matrices to the zero matrix. More precisely, the homomorphism satisfies: 1. \( \text{blockDiagonal'AddMonoidHom}\, (M + N) = \text{blockDiagonal'AddMonoidHom}\, M + \text{blockDiagonal'AddMonoidHom}\, N \) 2. \( \text{blockDiagonal'AddMonoidHom}\, 0 = 0 \) where \( M \) and \( N \) are families of matrices, and addition is defined pointwise.
Matrix.blockDiagonal'_neg theorem
[AddGroup α] (M : ∀ i, Matrix (m' i) (n' i) α) : blockDiagonal' (-M) = -blockDiagonal' M
Full source
@[simp]
theorem blockDiagonal'_neg [AddGroup α] (M : ∀ i, Matrix (m' i) (n' i) α) :
    blockDiagonal' (-M) = -blockDiagonal' M :=
  map_neg (blockDiagonal'AddMonoidHom m' n' α) M
Negation Commutes with Block Diagonal Construction: $\text{blockDiagonal}'(-M) = -\text{blockDiagonal}'(M)$
Informal description
Let $\alpha$ be an additive group. For any family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$, the block diagonal matrix formed by the negation $-M_i$ is equal to the negation of the block diagonal matrix formed by $M_i$. That is, \[ \text{blockDiagonal}'\, (-M) = -\text{blockDiagonal}'\, M. \]
Matrix.blockDiagonal'_sub theorem
[AddGroup α] (M N : ∀ i, Matrix (m' i) (n' i) α) : blockDiagonal' (M - N) = blockDiagonal' M - blockDiagonal' N
Full source
@[simp]
theorem blockDiagonal'_sub [AddGroup α] (M N : ∀ i, Matrix (m' i) (n' i) α) :
    blockDiagonal' (M - N) = blockDiagonal' M - blockDiagonal' N :=
  map_sub (blockDiagonal'AddMonoidHom m' n' α) M N
Block Diagonal Preserves Matrix Subtraction
Informal description
Let $\alpha$ be an additive group, and for each index $i$, let $m_i$ and $n_i$ be types. Given two families of matrices $M_i \in \mathrm{Matrix}(m_i, n_i, \alpha)$ and $N_i \in \mathrm{Matrix}(m_i, n_i, \alpha)$, the block diagonal matrix formed by their difference $M_i - N_i$ is equal to the difference of the block diagonal matrices formed by $M_i$ and $N_i$ respectively. That is, \[ \mathrm{blockDiagonal}'\, (M - N) = \mathrm{blockDiagonal}'\, M - \mathrm{blockDiagonal}'\, N. \]
Matrix.blockDiagonal'_mul theorem
[NonUnitalNonAssocSemiring α] [∀ i, Fintype (n' i)] [Fintype o] (M : ∀ i, Matrix (m' i) (n' i) α) (N : ∀ i, Matrix (n' i) (p' i) α) : (blockDiagonal' fun k => M k * N k) = blockDiagonal' M * blockDiagonal' N
Full source
@[simp]
theorem blockDiagonal'_mul [NonUnitalNonAssocSemiring α] [∀ i, Fintype (n' i)] [Fintype o]
    (M : ∀ i, Matrix (m' i) (n' i) α) (N : ∀ i, Matrix (n' i) (p' i) α) :
    (blockDiagonal' fun k => M k * N k) = blockDiagonal' M * blockDiagonal' N := by
  ext ⟨k, i⟩ ⟨k', j⟩
  simp only [blockDiagonal'_apply, mul_apply, ← Finset.univ_sigma_univ, Finset.sum_sigma]
  rw [Fintype.sum_eq_single k]
  · simp only [if_pos, dif_pos]
    split_ifs <;> simp
  · intro j' hj'
    exact Finset.sum_eq_zero fun _ _ => by rw [dif_neg hj'.symm, zero_mul]
Product of Block Diagonal Matrices Equals Block Diagonal of Products
Informal description
Let $\alpha$ be a non-unital non-associative semiring, and let $o$ be a finite index type. For each $i \in o$, let $m_i$, $n_i$, and $p_i$ be finite types, and let $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ and $N_i \in \text{Matrix}(n_i, p_i, \alpha)$ be matrices. Then the block diagonal matrix formed by the pointwise product $M_i N_i$ is equal to the product of the block diagonal matrices formed by $M_i$ and $N_i$ respectively. That is, \[ \text{blockDiagonal}'\, (M_i N_i) = \text{blockDiagonal}'\, M_i \cdot \text{blockDiagonal}'\, N_i. \]
Matrix.blockDiagonal'RingHom definition
[∀ i, DecidableEq (m' i)] [Fintype o] [∀ i, Fintype (m' i)] [NonAssocSemiring α] : (∀ i, Matrix (m' i) (m' i) α) →+* Matrix (Σ i, m' i) (Σ i, m' i) α
Full source
/-- `Matrix.blockDiagonal'` as a `RingHom`. -/
@[simps]
def blockDiagonal'RingHom [∀ i, DecidableEq (m' i)] [Fintype o] [∀ i, Fintype (m' i)]
    [NonAssocSemiring α] : (∀ i, Matrix (m' i) (m' i) α) →+* Matrix (Σ i, m' i) (Σ i, m' i) α :=
  { blockDiagonal'AddMonoidHom m' m' α with
    toFun := blockDiagonal'
    map_one' := blockDiagonal'_one
    map_mul' := blockDiagonal'_mul }
Ring homomorphism for block diagonal matrices with varying block sizes
Informal description
Given a finite index type $o$, a family of finite types $m_i$ (for each $i \in o$) with decidable equality, and a non-associative semiring $\alpha$, the function $\text{blockDiagonal}'$ forms a ring homomorphism from the ring of square matrices (indexed by $m_i$) to the ring of square matrices indexed by the dependent sum type $\Sigma i, m_i$. Specifically, this homomorphism maps a family of square matrices $M_i \in \text{Matrix}(m_i, m_i, \alpha)$ to their block diagonal matrix in $\text{Matrix}(\Sigma i, m_i, \Sigma i, m_i, \alpha)$. It preserves the multiplicative identity (i.e., $\text{blockDiagonal}'\, 1 = 1$) and matrix multiplication (i.e., $\text{blockDiagonal}'\, (M \cdot N) = \text{blockDiagonal}'\, M \cdot \text{blockDiagonal}'\, N$).
Matrix.blockDiagonal'_pow theorem
[∀ i, DecidableEq (m' i)] [Fintype o] [∀ i, Fintype (m' i)] [Semiring α] (M : ∀ i, Matrix (m' i) (m' i) α) (n : ℕ) : blockDiagonal' (M ^ n) = blockDiagonal' M ^ n
Full source
@[simp]
theorem blockDiagonal'_pow [∀ i, DecidableEq (m' i)] [Fintype o] [∀ i, Fintype (m' i)] [Semiring α]
    (M : ∀ i, Matrix (m' i) (m' i) α) (n : ) : blockDiagonal' (M ^ n) = blockDiagonal' M ^ n :=
  map_pow (blockDiagonal'RingHom m' α) M n
Power of Block Diagonal Matrix Equals Block Diagonal of Powers
Informal description
Let $o$ be a finite index type, and for each $i \in o$, let $m_i$ be a finite type with decidable equality. Given a semiring $\alpha$ and a family of square matrices $M_i \in \text{Matrix}(m_i, m_i, \alpha)$, the block diagonal matrix formed by the $n$-th powers of $M_i$ is equal to the $n$-th power of the block diagonal matrix formed by $M_i$. That is, \[ \text{blockDiagonal}'\, (M_i^n) = (\text{blockDiagonal}'\, M_i)^n. \]
Matrix.blockDiagonal'_smul theorem
{R : Type*} [Zero α] [SMulZeroClass R α] (x : R) (M : ∀ i, Matrix (m' i) (n' i) α) : blockDiagonal' (x • M) = x • blockDiagonal' M
Full source
@[simp]
theorem blockDiagonal'_smul {R : Type*} [Zero α] [SMulZeroClass R α] (x : R)
    (M : ∀ i, Matrix (m' i) (n' i) α) : blockDiagonal' (x • M) = x • blockDiagonal' M := by
  ext
  simp only [blockDiagonal'_apply, Pi.smul_apply, smul_apply]
  split_ifs <;> simp
Scalar Multiplication Commutes with Block Diagonal Construction
Informal description
Let $R$ be a type with a scalar multiplication operation on a type $\alpha$ with zero, and let $M_i$ be a family of matrices in $\text{Matrix}(m'_i, n'_i, \alpha)$ indexed by $i$. Then the block diagonal matrix formed by the scalar multiples $x \cdot M_i$ is equal to the scalar multiple $x$ applied to the block diagonal matrix formed by $M_i$, i.e., \[ \text{blockDiagonal}'\, (x \cdot M) = x \cdot \text{blockDiagonal}'\, M. \]
Matrix.blockDiag' definition
(M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : Matrix (m' k) (n' k) α
Full source
/-- Extract a block from the diagonal of a block diagonal matrix.

This is the block form of `Matrix.diag`, and the left-inverse of `Matrix.blockDiagonal'`. -/
def blockDiag' (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : Matrix (m' k) (n' k) α :=
  of fun i j => M ⟨k, i⟩ ⟨k, j⟩
Extraction of diagonal block from block diagonal matrix
Informal description
For a block diagonal matrix $M$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, and entries of type $\alpha$, the function extracts the $k$-th diagonal block as a matrix of type $\text{Matrix}\, (m'_k)\, (n'_k)\, \alpha$. Specifically, the $(i,j)$-th entry of the extracted block is equal to the $(\langle k, i \rangle, \langle k, j \rangle)$-th entry of $M$.
Matrix.blockDiag'_apply theorem
(M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) (i j) : blockDiag' M k i j = M ⟨k, i⟩ ⟨k, j⟩
Full source
theorem blockDiag'_apply (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) (i j) :
    blockDiag' M k i j = M ⟨k, i⟩ ⟨k, j⟩ :=
  rfl
Diagonal Block Entry Extraction Formula
Informal description
For a block diagonal matrix $M$ with rows indexed by $\Sigma i, m'_i$ and columns indexed by $\Sigma i, n'_i$, and entries of type $\alpha$, the $(i,j)$-th entry of the $k$-th diagonal block of $M$ is equal to the $(\langle k, i \rangle, \langle k, j \rangle)$-th entry of $M$. In other words, for any $k : o$, $i : m'_k$, and $j : n'_k$, we have $(\text{blockDiag}'\, M\, k)_{i,j} = M_{\langle k, i \rangle, \langle k, j \rangle}$.
Matrix.blockDiag'_map theorem
(M : Matrix (Σ i, m' i) (Σ i, n' i) α) (f : α → β) : blockDiag' (M.map f) = fun k => (blockDiag' M k).map f
Full source
theorem blockDiag'_map (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (f : α → β) :
    blockDiag' (M.map f) = fun k => (blockDiag' M k).map f :=
  rfl
Commutation of Block Diagonal Extraction with Entry-wise Map
Informal description
For a block diagonal matrix $M$ with rows indexed by $\Sigma i, m'_i$ and columns indexed by $\Sigma i, n'_i$, and entries of type $\alpha$, and for any function $f : \alpha \to \beta$, the $k$-th diagonal block of the matrix obtained by applying $f$ entry-wise to $M$ is equal to the matrix obtained by applying $f$ entry-wise to the $k$-th diagonal block of $M$. In other words, for each $k$, we have $\text{blockDiag}'\, (M.map\, f)\, k = (\text{blockDiag}'\, M\, k).map\, f$.
Matrix.blockDiag'_transpose theorem
(M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : blockDiag' Mᵀ k = (blockDiag' M k)ᵀ
Full source
@[simp]
theorem blockDiag'_transpose (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) :
    blockDiag' Mᵀ k = (blockDiag' M k)ᵀ :=
  ext fun _ _ => rfl
Transpose Commutes with Block Diagonal Extraction
Informal description
For a block diagonal matrix $M$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, and entries of type $\alpha$, the $k$-th diagonal block of the transpose matrix $M^\top$ is equal to the transpose of the $k$-th diagonal block of $M$. In other words, for each block index $k$, we have $(\text{blockDiag'}\, M^\top)_k = (\text{blockDiag'}\, M)_k^\top$.
Matrix.blockDiag'_conjTranspose theorem
{α : Type*} [Star α] (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : blockDiag' Mᴴ k = (blockDiag' M k)ᴴ
Full source
@[simp]
theorem blockDiag'_conjTranspose {α : Type*} [Star α]
    (M : Matrix (Σ i, m' i) (Σ i, n' i) α) (k : o) : blockDiag' Mᴴ k = (blockDiag' M k)ᴴ :=
  ext fun _ _ => rfl
Conjugate Transpose Commutes with Block Diagonal Extraction
Informal description
Let $\alpha$ be a type equipped with a star operation, and let $M$ be a block diagonal matrix with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, and entries in $\alpha$. For any index $k \in o$, the $k$-th diagonal block of the conjugate transpose $M^H$ is equal to the conjugate transpose of the $k$-th diagonal block of $M$, i.e., \[ \text{blockDiag}'\, M^H\, k = (\text{blockDiag}'\, M\, k)^H. \]
Matrix.blockDiag'_zero theorem
: blockDiag' (0 : Matrix (Σ i, m' i) (Σ i, n' i) α) = 0
Full source
@[simp]
theorem blockDiag'_zero : blockDiag' (0 : Matrix (Σ i, m' i) (Σ i, n' i) α) = 0 :=
  rfl
Zero Matrix Diagonal Block Extraction: $\text{blockDiag}'\, 0 = 0$
Informal description
For any type $\alpha$ with a zero element, the $k$-th diagonal block of the zero matrix (with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively) is equal to the zero matrix of type $\mathrm{Matrix}\, (m'_k)\, (n'_k)\, \alpha$.
Matrix.blockDiag'_diagonal theorem
[DecidableEq o] [∀ i, DecidableEq (m' i)] (d : (Σ i, m' i) → α) (k : o) : blockDiag' (diagonal d) k = diagonal fun i => d ⟨k, i⟩
Full source
@[simp]
theorem blockDiag'_diagonal
    [DecidableEq o] [∀ i, DecidableEq (m' i)] (d : (Σ i, m' i) → α) (k : o) :
    blockDiag' (diagonal d) k = diagonal fun i => d ⟨k, i⟩ :=
  ext fun i j => by
    obtain rfl | hij := Decidable.eq_or_ne i j
    · rw [blockDiag'_apply, diagonal_apply_eq, diagonal_apply_eq]
    · rw [blockDiag'_apply, diagonal_apply_ne _ hij, diagonal_apply_ne _ (mt (fun h => ?_) hij)]
      cases h
      rfl
Diagonal Block Extraction Formula for Block Diagonal Matrix
Informal description
Let $o$ be a type with decidable equality, and for each $i \in o$, let $m'_i$ be a type with decidable equality. Given a function $d : \Sigma i, m'_i \to \alpha$ and an index $k \in o$, the $k$-th diagonal block of the block diagonal matrix constructed from $d$ is equal to the diagonal matrix with entries $d_{\langle k, i \rangle}$ for each $i \in m'_k$. That is, \[ \text{blockDiag}'\, (\text{diagonal}\, d)\, k = \text{diagonal}\, (i \mapsto d_{\langle k, i \rangle}). \]
Matrix.blockDiag'_blockDiagonal' theorem
[DecidableEq o] (M : ∀ i, Matrix (m' i) (n' i) α) : blockDiag' (blockDiagonal' M) = M
Full source
@[simp]
theorem blockDiag'_blockDiagonal' [DecidableEq o] (M : ∀ i, Matrix (m' i) (n' i) α) :
    blockDiag' (blockDiagonal' M) = M :=
  funext fun _ => ext fun _ _ => blockDiagonal'_apply_eq M _ _ _
Diagonal Block Extraction from Block Diagonal Matrix: $\text{blockDiag}' \circ \text{blockDiagonal}' = \text{id}$
Informal description
For a family of matrices $M_i \in \text{Matrix}(m_i, n_i, \alpha)$ indexed by $i$ in a decidable type $o$, the diagonal blocks extracted from the block diagonal matrix $\text{blockDiagonal}'\, M$ are equal to the original matrices $M_i$. That is, $\text{blockDiag}' (\text{blockDiagonal}' M) = M$.
Matrix.blockDiagonal'_injective theorem
[DecidableEq o] : Function.Injective (blockDiagonal' : (∀ i, Matrix (m' i) (n' i) α) → Matrix _ _ α)
Full source
theorem blockDiagonal'_injective [DecidableEq o] :
    Function.Injective (blockDiagonal' : (∀ i, Matrix (m' i) (n' i) α) → Matrix _ _ α) :=
  Function.LeftInverse.injective blockDiag'_blockDiagonal'
Injectivity of Block Diagonal Matrix Construction: $\text{blockDiagonal}'$ is Injective
Informal description
For a decidable type $o$ and families of types $m'_i$ and $n'_i$ indexed by $i \in o$, the function $\text{blockDiagonal}'$ that constructs a block diagonal matrix from a family of matrices $(M_i)_{i \in o}$ (where each $M_i$ has type $\text{Matrix}\, (m'_i)\, (n'_i)\, \alpha$) is injective. That is, if $\text{blockDiagonal}'\, M = \text{blockDiagonal}'\, N$, then $M = N$.
Matrix.blockDiagonal'_inj theorem
[DecidableEq o] {M N : ∀ i, Matrix (m' i) (n' i) α} : blockDiagonal' M = blockDiagonal' N ↔ M = N
Full source
@[simp]
theorem blockDiagonal'_inj [DecidableEq o] {M N : ∀ i, Matrix (m' i) (n' i) α} :
    blockDiagonal'blockDiagonal' M = blockDiagonal' N ↔ M = N :=
  blockDiagonal'_injective.eq_iff
Injectivity of Block Diagonal Construction: $\text{blockDiagonal}'\, M = \text{blockDiagonal}'\, N \leftrightarrow M = N$
Informal description
For a decidable type $o$ and families of types $m'_i$ and $n'_i$ indexed by $i \in o$, the block diagonal construction $\text{blockDiagonal}'$ is injective. That is, for any two families of matrices $M, N \colon \forall i, \text{Matrix}\, (m'_i)\, (n'_i)\, \alpha$, we have $\text{blockDiagonal}'\, M = \text{blockDiagonal}'\, N$ if and only if $M = N$.
Matrix.blockDiag'_one theorem
[DecidableEq o] [∀ i, DecidableEq (m' i)] [One α] : blockDiag' (1 : Matrix (Σ i, m' i) (Σ i, m' i) α) = 1
Full source
@[simp]
theorem blockDiag'_one [DecidableEq o] [∀ i, DecidableEq (m' i)] [One α] :
    blockDiag' (1 : Matrix (Σ i, m' i) (Σ i, m' i) α) = 1 :=
  funext <| blockDiag'_diagonal _
Diagonal Blocks of Identity Matrix are Identity Matrices
Informal description
Let $o$ be a type with decidable equality, and for each $i \in o$, let $m'_i$ be a type with decidable equality. Given a type $\alpha$ with a multiplicative identity, the diagonal blocks of the identity matrix of type $\text{Matrix}\, (\Sigma i, m'_i)\, (\Sigma i, m'_i)\, \alpha$ are equal to the identity matrix in each block. That is, for each index $k \in o$, we have \[ \text{blockDiag}'\, (1 : \text{Matrix}\, (\Sigma i, m'_i)\, (\Sigma i, m'_i)\, \alpha)\, k = 1. \]
Matrix.blockDiag'_add theorem
[Add α] (M N : Matrix (Σ i, m' i) (Σ i, n' i) α) : blockDiag' (M + N) = blockDiag' M + blockDiag' N
Full source
@[simp]
theorem blockDiag'_add [Add α] (M N : Matrix (Σ i, m' i) (Σ i, n' i) α) :
    blockDiag' (M + N) = blockDiag' M + blockDiag' N :=
  rfl
Additivity of Diagonal Block Extraction for Block Diagonal Matrices
Informal description
For any type $\alpha$ with an addition operation, and for any block diagonal matrices $M$ and $N$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, the diagonal blocks of the sum $M + N$ are equal to the sum of the corresponding diagonal blocks of $M$ and $N$. That is, for each index $k$, we have $\text{blockDiag}' (M + N)_k = \text{blockDiag}' M_k + \text{blockDiag}' N_k$.
Matrix.blockDiag'AddMonoidHom definition
[AddZeroClass α] : Matrix (Σ i, m' i) (Σ i, n' i) α →+ ∀ i, Matrix (m' i) (n' i) α
Full source
/-- `Matrix.blockDiag'` as an `AddMonoidHom`. -/
@[simps]
def blockDiag'AddMonoidHom [AddZeroClass α] :
    MatrixMatrix (Σ i, m' i) (Σ i, n' i) α →+ ∀ i, Matrix (m' i) (n' i) α where
  toFun := blockDiag'
  map_zero' := blockDiag'_zero
  map_add' := blockDiag'_add
Additive monoid homomorphism for block diagonal matrix extraction
Informal description
The additive monoid homomorphism version of `Matrix.blockDiag'`, which maps a block diagonal matrix $M$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively to a function that extracts each diagonal block $M_k$ of type $\text{Matrix}\, (m'_k)\, (n'_k)\, \alpha$ for every index $k$. This homomorphism preserves the zero matrix and matrix addition. More precisely, for any type $\alpha$ with an additive zero class structure, this defines a homomorphism from the additive monoid of block diagonal matrices to the additive monoid of functions that assign to each index $k$ a matrix over $\alpha$.
Matrix.blockDiag'_neg theorem
[AddGroup α] (M : Matrix (Σ i, m' i) (Σ i, n' i) α) : blockDiag' (-M) = -blockDiag' M
Full source
@[simp]
theorem blockDiag'_neg [AddGroup α] (M : Matrix (Σ i, m' i) (Σ i, n' i) α) :
    blockDiag' (-M) = -blockDiag' M :=
  map_neg (blockDiag'AddMonoidHom m' n' α) M
Negation Commutes with Block Diagonal Extraction: $\text{blockDiag}'(-M) = -\text{blockDiag}'(M)$
Informal description
For any additive group $\alpha$ and any block diagonal matrix $M$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, the diagonal blocks of the negation $-M$ are equal to the negation of the corresponding diagonal blocks of $M$. That is, for each index $k$, we have $\text{blockDiag}' (-M)_k = -\text{blockDiag}' M_k$.
Matrix.blockDiag'_sub theorem
[AddGroup α] (M N : Matrix (Σ i, m' i) (Σ i, n' i) α) : blockDiag' (M - N) = blockDiag' M - blockDiag' N
Full source
@[simp]
theorem blockDiag'_sub [AddGroup α] (M N : Matrix (Σ i, m' i) (Σ i, n' i) α) :
    blockDiag' (M - N) = blockDiag' M - blockDiag' N :=
  map_sub (blockDiag'AddMonoidHom m' n' α) M N
Subtraction Commutes with Block Diagonal Extraction: $\text{blockDiag}'(M - N) = \text{blockDiag}'(M) - \text{blockDiag}'(N)$
Informal description
For any additive group $\alpha$ and any block diagonal matrices $M$ and $N$ with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, the diagonal blocks of the difference $M - N$ are equal to the difference of the corresponding diagonal blocks of $M$ and $N$. That is, for each index $k$, we have $\text{blockDiag}' (M - N)_k = \text{blockDiag}' M_k - \text{blockDiag}' N_k$.
Matrix.blockDiag'_smul theorem
{R : Type*} [SMul R α] (x : R) (M : Matrix (Σ i, m' i) (Σ i, n' i) α) : blockDiag' (x • M) = x • blockDiag' M
Full source
@[simp]
theorem blockDiag'_smul {R : Type*} [SMul R α] (x : R)
    (M : Matrix (Σ i, m' i) (Σ i, n' i) α) : blockDiag' (x • M) = x • blockDiag' M :=
  rfl
Commutativity of Block Diagonal Extraction with Scalar Multiplication: $\text{blockDiag}'(x \cdot M) = x \cdot \text{blockDiag}'(M)$
Informal description
Let $R$ be a type with a scalar multiplication operation on a type $\alpha$, and let $M$ be a block diagonal matrix with rows and columns indexed by $\Sigma i, m'_i$ and $\Sigma i, n'_i$ respectively, and entries in $\alpha$. For any scalar $x \in R$, the $k$-th diagonal block of the scalar multiple $x \cdot M$ is equal to the scalar multiple $x \cdot (M_k)$, where $M_k$ is the $k$-th diagonal block of $M$. In other words, the operation of extracting diagonal blocks commutes with scalar multiplication.
Matrix.toBlock_mul_eq_mul theorem
{m n k : Type*} [Fintype n] (p : m → Prop) (q : k → Prop) (A : Matrix m n R) (B : Matrix n k R) : (A * B).toBlock p q = A.toBlock p ⊤ * B.toBlock ⊤ q
Full source
theorem toBlock_mul_eq_mul {m n k : Type*} [Fintype n] (p : m → Prop) (q : k → Prop)
    (A : Matrix m n R) (B : Matrix n k R) :
    (A * B).toBlock p q = A.toBlock p  * B.toBlock  q := by
  ext i k
  simp only [toBlock_apply, mul_apply]
  rw [Finset.sum_subtype]
  simp [Pi.top_apply, Prop.top_eq_true]
Submatrix Multiplication Identity: $(AB)_{p,q} = A_{p,\top} \cdot B_{\top,q}$
Informal description
Let $m$, $n$, and $k$ be finite types, and let $A$ be an $m \times n$ matrix and $B$ be an $n \times k$ matrix over a ring $R$. For any predicate $p$ on $m$ and $q$ on $k$, the submatrix of the product $AB$ corresponding to rows satisfying $p$ and columns satisfying $q$ is equal to the product of the submatrix of $A$ with rows satisfying $p$ and all columns, and the submatrix of $B$ with all rows and columns satisfying $q$. In symbols: $$(AB)_{\text{toBlock}\, p\, q} = A_{\text{toBlock}\, p\, \top} \cdot B_{\text{toBlock}\, \top\, q}$$
Matrix.toBlock_mul_eq_add theorem
{m n k : Type*} [Fintype n] (p : m → Prop) (q : n → Prop) [DecidablePred q] (r : k → Prop) (A : Matrix m n R) (B : Matrix n k R) : (A * B).toBlock p r = A.toBlock p q * B.toBlock q r + (A.toBlock p fun i => ¬q i) * B.toBlock (fun i => ¬q i) r
Full source
theorem toBlock_mul_eq_add {m n k : Type*} [Fintype n] (p : m → Prop) (q : n → Prop)
    [DecidablePred q] (r : k → Prop) (A : Matrix m n R) (B : Matrix n k R) : (A * B).toBlock p r =
    A.toBlock p q * B.toBlock q r + (A.toBlock p fun i => ¬q i) * B.toBlock (fun i => ¬q i) r := by
  classical
    ext i k
    simp only [toBlock_apply, mul_apply, Pi.add_apply]
    exact (Fintype.sum_subtype_add_sum_subtype q fun x => A (↑i) x * B x ↑k).symm
Block Decomposition of Matrix Multiplication: $(AB)_{p,r} = A_{p,q}B_{q,r} + A_{p,\neg q}B_{\neg q,r}$
Informal description
Let $m$, $n$, and $k$ be finite types, and let $R$ be a ring. Given predicates $p : m \to \mathrm{Prop}$, $q : n \to \mathrm{Prop}$ (with $q$ decidable), and $r : k \to \mathrm{Prop}$, and matrices $A \in \mathrm{Matrix}\, m\, n\, R$ and $B \in \mathrm{Matrix}\, n\, k\, R$, the submatrix of the product $AB$ defined by $p$ and $r$ satisfies: \[ (AB)_{\text{toBlock}\, p\, r} = A_{\text{toBlock}\, p\, q} \cdot B_{\text{toBlock}\, q\, r} + A_{\text{toBlock}\, p\, (\neg q)} \cdot B_{\text{toBlock}\, (\neg q)\, r} \] where $A_{\text{toBlock}\, p\, q}$ denotes the submatrix of $A$ with rows satisfying $p$ and columns satisfying $q$, and similarly for the other terms.
Matrix.map_toSquareBlock theorem
(f : α → β) {M : Matrix m m α} {ι} {b : m → ι} {i : ι} : (M.map f).toSquareBlock b i = (M.toSquareBlock b i).map f
Full source
lemma Matrix.map_toSquareBlock
    (f : α → β) {M : Matrix m m α} {ι} {b : m → ι} {i : ι} :
    (M.map f).toSquareBlock b i = (M.toSquareBlock b i).map f :=
  submatrix_map _ _ _ _
Commutation of Entry-wise Map and Square Block Extraction: $(f \circ M)_{b,i} = f \circ M_{b,i}$
Informal description
Let $M$ be a square matrix of size $m \times m$ with entries in $\alpha$, $f : \alpha \to \beta$ a function, $b : m \to \iota$ a block assignment function, and $i \in \iota$ a block index. Then the square submatrix of the entry-wise mapped matrix $M.\text{map}\, f$ corresponding to block $i$ is equal to the entry-wise mapped square submatrix of $M$ corresponding to block $i$. In symbols: $$(M.\text{map}\, f).\text{toSquareBlock}\, b\, i = (M.\text{toSquareBlock}\, b\, i).\text{map}\, f$$
Matrix.comp_toSquareBlock theorem
{b : m → α} (M : Matrix m m (Matrix n n R)) (a : α) : letI equiv := Equiv.prodSubtypeFstEquivSubtypeProd.symm (M.comp m m n n R).toSquareBlock (fun i ↦ b i.1) a = ((M.toSquareBlock b a).comp _ _ n n R).reindex equiv equiv
Full source
lemma Matrix.comp_toSquareBlock {b : m → α}
    (M : Matrix m m (Matrix n n R)) (a : α) :
    letI equiv := Equiv.prodSubtypeFstEquivSubtypeProd.symm
    (M.comp m m n n R).toSquareBlock (fun i ↦ b i.1) a =
      ((M.toSquareBlock b a).comp _ _ n n R).reindex equiv equiv :=
  rfl
Equivalence between Flattened Block Submatrix and Flattened Submatrix via Reindexing
Informal description
Let $M$ be an $m \times m$ block matrix where each entry is an $n \times n$ matrix over a ring $R$, and let $b : m \to \alpha$ be a function assigning each row/column index to a block. For any block index $a \in \alpha$, the square submatrix of the flattened matrix $M.\text{comp}$ corresponding to block $a$ is equivalent (via reindexing) to the flattened version of the square submatrix $M.\text{toSquareBlock}\, b\, a$. More precisely, let $\text{equiv}$ be the equivalence that swaps the order of components in a product subtype. Then: \[ (M.\text{comp}).\text{toSquareBlock}\, (\lambda i \mapsto b\, i.1)\, a = \left((M.\text{toSquareBlock}\, b\, a).\text{comp}\right).\text{reindex}\, \text{equiv}\, \text{equiv} \]
Matrix.comp_diagonal theorem
(d) : comp m m n n R (diagonal d) = (blockDiagonal d).reindex (.prodComm ..) (.prodComm ..)
Full source
lemma Matrix.comp_diagonal (d) :
    comp m m n n R (diagonal d) =
      (blockDiagonal d).reindex (.prodComm ..) (.prodComm ..) := by
  ext
  simp [diagonal, blockDiagonal, Matrix.ite_apply]
Equivalence between diagonal and block diagonal matrices under flattening and reindexing
Informal description
Let $d$ be a function mapping indices to elements of type $R$. The equivalence `comp` between block matrices and flattened matrices, when applied to the diagonal matrix constructed from $d$, is equal to the block diagonal matrix constructed from $d$ followed by a reindexing that swaps the product order of the indices. In other words, the following diagram commutes: \[ \text{diagonal}(d) \xrightarrow{\text{comp}} \text{blockDiagonal}(d) \xrightarrow{\text{reindex}} \text{reindexed block diagonal matrix} \]