Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Lean/Mathlib/Mathlib/LinearAlgebra/Matrix/     Datei vom 15.10.2026 mit Größe 21 kB image not shown  

Quelle  Block.lean   Sprache: Lean

 

/-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Patrick Massot, Casper Putz, Anne Baanen, Wen Yang
-/

module

public import Mathlib.LinearAlgebra.Matrix.NonsingularInverse
public import Mathlib.LinearAlgebra.Matrix.Transvection

/-!
# Block matrices and their determinant

This file defines a predicate `Matrix.BlockTriangular` saying a matrix
is block triangular, and proves the value of the determinant for various
matrices built out of blocks.

## Main definitions

* `Matrix.BlockTriangular` expresses that an `o` by `o` matrix is block triangular,
  if the rows and columns are ordered according to some order `b : o → α`
* `Matrix.IsUpperTriangular` and `Matrix.IsLowerTriangular`, as abbreviations for
  `Matrix.BlockTriangular` with the identity and the dual order, respectively.

## Main results

* `Matrix.BlockTriangular.det`: the determinant of a block triangular matrix
  is equal to the product of the determinants of all the blocks
* `Matrix.det_of_isUpperTriangular` and `Matrix.det_of_isLowerTriangular`: the determinant of
  a triangular matrix is the product of the entries along the diagonal

## Tags

matrix, diagonal, det, block triangular
-/


@[expose] public section


open Finset Function OrderDual

open Matrix

universe v

variable {α m n : Type*} {m' : α → Type*}
variable {R : Type v} {A : Type*} {M N : Matrix m m R} {b : m → α}

namespace Matrix

section LT

variable [LT α]

section Zero

variable [Zero R]

/-- Let `b` map rows and columns of a square matrix `M` to blocks indexed by `α`s. Then
`BlockTriangular M n b` says the matrix is block triangular. -/

def BlockTriangular (M : Matrix m m R) (b : m → α) : Prop :=
  ∀ ⦃i j⦄, b j < b i → M i j = 0

/-- `M` is upper triangular: entries below the diagonal vanish. -/
abbrev IsUpperTriangular [LT m] (M : Matrix m m R) : Prop :=
  M.BlockTriangular id

/-- `M` is lower triangular: entries above the diagonal vanish. -/
abbrev IsLowerTriangular [LT m] (M : Matrix m m R) : Prop :=
  M.BlockTriangular toDual

/-- A matrix is indecomposable if it cannot be reindexed to block-triangular form. -/
def IsIndecomposable (M : Matrix m m R) : Prop :=
  ∀ i j A B D (e : Fin i ⊕ Fin j ≃ m), M = (fromBlocks A B 0 D).reindex e e → i = 0 ∨ j = 0

@[simp]
protected theorem BlockTriangular.submatrix {f : n → m} (h : M.BlockTriangular b) :
    (M.submatrix f f).BlockTriangular (b ∘ f) := fun _ _ hij => h hij

theorem blockTriangular_reindex_iff {b : n → α} {e : m ≃ n} :
    (reindex e e M).BlockTriangular b ↔ M.BlockTriangular (b ∘ e) := by
  refine ⟨fun h => ?_, fun h => ?_⟩
  · convert h.submatrix
    simp only [reindex_apply, submatrix_submatrix, submatrix_id_id, Equiv.symm_comp_self]
  · convert! h.submatrix
    simp only [comp_assoc b e e.symm, Equiv.self_comp_symm, comp_id]

protected theorem BlockTriangular.transpose :
    M.BlockTriangular b → Mᵀ.BlockTriangular (toDual ∘ b) :=
  swap

@[simp]
protected theorem blockTriangular_transpose_iff {b : m → αᵒᵈ} :
    Mᵀ.BlockTriangular b ↔ M.BlockTriangular (ofDual ∘ b) :=
  forall_comm

@[simp]
theorem blockTriangular_zero : BlockTriangular (0 : Matrix m m R) b := fun _ _ _ => rfl

instance decidableBlockTriangular [DecidableEq R] [Fintype m] [DecidableLT α] :
    Decidable (M.BlockTriangular b) :=
  decidable_of_iff (∀ ij : m × m, b ij.2 < b ij.1 → M ij.1 ij.2 = 0)
    ⟨fun h i j hij => h (i, j) hij, fun h _ hij => h hij⟩

end Zero

protected theorem BlockTriangular.neg [NegZeroClass R] {M : Matrix m m R}
    (hM : BlockTriangular M b) : BlockTriangular (-M) b :=
  fun _ _ h => by rw [Matrix.neg_apply, hM h, neg_zero]

theorem BlockTriangular.add [AddZeroClass R] (hM : BlockTriangular M b) (hN : BlockTriangular N b) :
    BlockTriangular (M + N) b := fun i j h => by simp_rw [Matrix.add_apply, hM h, hN h, zero_add]

theorem BlockTriangular.sub [SubNegZeroMonoid R]
    (hM : BlockTriangular M b) (hN : BlockTriangular N b) :
    BlockTriangular (M - N) b := fun i j h => by simp_rw [Matrix.sub_apply, hM h, hN h, sub_zero]

lemma BlockTriangular.add_iff_right [AddGroup R] (hM : BlockTriangular M b) :
    BlockTriangular (M + N) b ↔ BlockTriangular N b := ⟨(by simpa using hM.neg.add ·), hM.add⟩

lemma BlockTriangular.add_iff_left [AddGroup R] (hN : BlockTriangular N b) :
    BlockTriangular (M + N) b ↔ BlockTriangular M b := ⟨(by simpa using ·.sub hN), (·.add hN)⟩

lemma BlockTriangular.sub_iff_right [AddGroup R] (hM : BlockTriangular M b) :
    BlockTriangular (M - N) b ↔ BlockTriangular N b := ⟨(by simpa using ·.neg.add hM), hM.sub⟩

lemma BlockTriangular.sub_iff_left [AddGroup R] (hN : BlockTriangular N b) :
    BlockTriangular (M - N) b ↔ BlockTriangular M b := ⟨(by simpa using ·.add hN), (·.sub hN)⟩

lemma BlockTriangular.map {S F} [FunLike F R S] [Zero R] [Zero S] [ZeroHomClass F R S] (f : F)
    (h : BlockTriangular M b) : BlockTriangular (M.map f) b :=
  fun i j lt ↦ by simp [h lt]

lemma BlockTriangular.comp [Zero R] {M : Matrix m m (Matrix n n R)} (h : BlockTriangular M b) :
    BlockTriangular (M.comp m m n n R) fun i ↦ b i.1 :=
  fun i j lt ↦ by simp [h lt]

end LT

section Preorder

variable [Preorder α]

section Zero

variable [Zero R]

theorem blockTriangular_diagonal [DecidableEq m] (d : m → R) : BlockTriangular (diagonal d) b :=
  fun _ _ h => diagonal_apply_ne' d fun h' => ne_of_lt h (congr_arg _ h')

theorem blockTriangular_blockDiagonal' [DecidableEq α] (d : ∀ i : α, Matrix (m' i) (m' i) R) :
    BlockTriangular (blockDiagonal' d) Sigma.fst := by
  rintro ⟨i, i'⟩ ⟨j, j'⟩ h
  apply blockDiagonal'_apply_ne d i' j' fun h' => ne_of_lt h h'.symm

theorem blockTriangular_blockDiagonal [DecidableEq α] (d : α → Matrix m m R) :
    BlockTriangular (blockDiagonal d) Prod.snd := by
  rintro ⟨i, i'⟩ ⟨j, j'⟩ h
  rw [blockDiagonal'_eq_blockDiagonal, blockTriangular_blockDiagonal']
  exact h

variable [DecidableEq m]

theorem blockTriangular_one [One R] : BlockTriangular (1 : Matrix m m R) b :=
  blockTriangular_diagonal _

theorem blockTriangular_single {i j : m} (hij : b i ≤ b j) (c : R) :
    BlockTriangular (single i j c) b := by
  intro r s hrs
  apply single_apply_of_ne
  rintro ⟨rfl, rfl⟩
  exact (hij.trans_lt hrs).false

theorem blockTriangular_single' {i j : m} (hij : b j ≤ b i) (c : R) :
    BlockTriangular (single i j c) (toDual ∘ b) :=
  blockTriangular_single (by exact toDual_le_toDual.mpr hij) _

end Zero

variable [CommRing R] [DecidableEq m]

theorem blockTriangular_transvection {i j : m} (hij : b i ≤ b j) (c : R) :
    BlockTriangular (transvection i j c) b :=
  blockTriangular_one.add (blockTriangular_single hij c)

theorem blockTriangular_transvection' {i j : m} (hij : b j ≤ b i) (c : R) :
    BlockTriangular (transvection i j c) (OrderDual.toDual ∘ b) :=
  blockTriangular_one.add (blockTriangular_single' hij c)

end Preorder

section LinearOrder

variable [LinearOrder α]

theorem BlockTriangular.mul [Fintype m] [NonUnitalNonAssocSemiring R]
    {M N : Matrix m m R} (hM : BlockTriangular M b)
    (hN : BlockTriangular N b) : BlockTriangular (M * N) b := by
  intro i j hij
  apply Finset.sum_eq_zero
  intro k _
  by_cases! hki : b k < b i
  · simp_rw [hM hki, zero_mul]
  · simp_rw [hN (lt_of_lt_of_le hij hki), mul_zero]

variable (R b) in
/-- `BlockTriangular` matrices form a subsemiring. -/
@[simps]
def blockTriangularSubsemiring [DecidableEq m] [Fintype m] [Semiring R] :
    Subsemiring (Matrix m m R) where
  carrier := {M | BlockTriangular M b}
  zero_mem' := blockTriangular_zero
  one_mem' := blockTriangular_one
  mul_mem' := .mul
  add_mem' := .add

@[simp]
theorem mem_blockTriangularSubsemiring [DecidableEq m] [Fintype m] [Semiring R]
    {M : Matrix m m R} :
    M ∈ blockTriangularSubsemiring R b ↔ BlockTriangular M b :=
  Iff.rfl

theorem BlockTriangular.pow [DecidableEq m] [Fintype m] [Semiring R] (hM : BlockTriangular M b)
    (n : ℕ) : BlockTriangular (M ^ n) b :=
  pow_mem (S := blockTriangularSubsemiring R b) hM n

theorem blockTriangular_algebraMap [CommSemiring R] [Semiring A] [Algebra R A]
    [DecidableEq m] [Fintype m] (r : R) : (algebraMap R (Matrix m m A) r).BlockTriangular b :=
  blockTriangular_diagonal _

variable (R A b) in
/-- `BlockTriangular` matrices form a subalgebra. -/
def blockTriangularSubalgebra [CommSemiring R] [Semiring A] [Algebra R A]
    [DecidableEq m] [Fintype m] : Subalgebra R (Matrix m m A) where
  __ := blockTriangularSubsemiring A b
  algebraMap_mem' r := blockTriangular_algebraMap r

@[simp]
theorem mem_blockTriangularSubalgebra [CommSemiring R] [Semiring A] [Algebra R A]
    [DecidableEq m] [Fintype m] {M : Matrix m m A} :
    M ∈ blockTriangularSubalgebra R A b ↔ BlockTriangular M b :=
  Iff.rfl

open Fintype in
lemma isIndecomposable_iff_blockTriangular_const [Nontrivial α] [Finite m] [Zero R]
    (M : Matrix m m R) :
    M.IsIndecomposable ↔ ∀ b : m → α, M.BlockTriangular b → ∃ a, b = const m a := by
  simp_rw [IsIndecomposable]
  refine ⟨fun h b hb ↦ ?_, fun h i j A B D e he ↦ ?_⟩
  · contrapose! h
    have : Fintype m := .ofFinite m
    rcases isEmpty_or_nonempty m with hm | hm
    · exact False.elim <| h (Nonempty.some inferInstance) (IsEmpty.congr_fun _ _)
    obtain ⟨K, k, hKk⟩ : ∃ K k, b k < b K := by
      obtain ⟨K, hK⟩ := Finite.exists_max b
      obtain ⟨k, hk⟩ : ∃ k, b k ≠ b K := by simpa using ne_iff.mp <| h (b K)
      exact ⟨K, k, (hK k).lt_of_ne hk⟩
    let s := {k // b k < b K}
    let t := {k // ¬ b k < b K}
    have : Nonempty s := ⟨⟨k, hKk⟩⟩
    have : Nonempty t := ⟨⟨K, lt_irrefl _⟩⟩
    let e : Fin (card s) ⊕ Fin (card t) ≃ m :=
      ((Fintype.equivFin _).sumCongr (Fintype.equivFin _)).symm.trans (Equiv.sumCompl _)
    let M' := M.submatrix e e
    suffices M'.toBlocks₂₁ = 0 by
      refine ⟨_, _, M'.toBlocks₁₁, M'.toBlocks₁₂, M'.toBlocks₂₂, e, ?_, card_ne_zero, card_ne_zero⟩
      simp [← this, M'.fromBlocks_toBlocks, M']
    have hp (x : Fin (card s)) : b (e (.inl x)) < b K := ((equivFin s).symm x).property
    have hn (x : Fin (card t)) : b K ≤ b (e (.inr x)) := not_lt.mp ((equivFin t).symm x).property
    ext i j
    replace hb : M (e (Sum.inr i)) (e (Sum.inl j)) = 0 := hb (lt_of_lt_of_le (hp j) (hn i))
    simpa [M', toBlocks₂₁]
  · obtain ⟨a₁, a₂, ha₁₂⟩ : ∃ a₁ a₂ : α, a₁ < a₂ := exists_pair_lt α
    set b : m → α := fun k ↦ Sum.elim (fun _ ↦ a₁) (fun _ ↦ a₂) (e.symm k) with hb
    have hBT : M.BlockTriangular b := by
      rw [he, Matrix.blockTriangular_reindex_iff]
      rintro (r | r) (s | s) hrs
      · aesop
      · exact False.elim <| lt_irrefl a₁ <| ha₁₂.trans <| by aesop
      · simp
      · aesop
    contrapose! ha₁₂
    obtain ⟨a, ha⟩ := h b hBT
    rw [hb] at ha
    obtain rfl : a₁ = a := by simpa using congr($ha (e <| .inl <| Nonempty.some ⟨⟨0, by lia⟩⟩))
    obtain rfl : a₂ = a₁ := by simpa using congr($ha (e <| .inr <| Nonempty.some ⟨⟨0, by lia⟩⟩))
    exact le_refl _

open Relation in
lemma isIndecomposable_iff_reflTransGen [Finite m] [Zero R] (M : Matrix m m R) :
    M.IsIndecomposable ↔ ∀ i j, ReflTransGen (fun i j ↦ M i j ≠ 0) i j := by
  classical
  rw [isIndecomposable_iff_blockTriangular_const (α := Bool)]
  refine ⟨fun hconst i j ↦ ?_, fun hreach b hb ↦ ?_⟩
  · obtain ⟨a, ha⟩ := hconst (fun k ↦ decide (ReflTransGen (fun i j ↦ M i j ≠ 0) i k))
      fun x y hxy ↦ by grind [Bool.lt_iff]
    simpa [ReflTransGen.refl] using (congrFun ha j).trans (congrFun ha i).symm
  · have key (x y : m) : b x ≤ b y := by
      induction hreach x y with grind [BlockTriangular]
    rcases isEmpty_or_nonempty m with hm | ⟨⟨x⟩⟩
    · exact ⟨false, funext hm.elim⟩
    · exact ⟨b x, funext fun y ↦ le_antisymm (key y x) (key x y)⟩

end LinearOrder

theorem upper_two_blockTriangular [Zero R] [Preorder α] (A : Matrix m m R) (B : Matrix m n R)
    (D : Matrix n n R) {a b : α} (hab : a < b) :
    BlockTriangular (fromBlocks A B 0 D) (Sum.elim (fun _ => a) fun _ => b) := by
  rintro (c | c) (d | d) hcd <;> first | simp [hab.not_gt] at hcd ⊢

/-! ### Determinant -/


variable [CommRing R] [DecidableEq m] [Fintype m] [DecidableEq n] [Fintype n]

theorem equiv_block_det (M : Matrix m m R) {p q : m → Prop} [DecidablePred p] [DecidablePred q]
    (e : ∀ x, q x ↔ p x) : (toSquareBlockProp M p).det = (toSquareBlockProp M q).det := by
  convert!
    Matrix.det_reindex_self (Equiv.subtypeEquivRight e)
      (toSquareBlockProp M q)
        -- Removed `@[simp]` attribute,
        -- as the LHS simplifies already to `M.toSquareBlock id i ⟨i, ⋯⟩ ⟨i, ⋯⟩`


-- Removed `@[simp]` attribute,
-- as the LHS simplifies already to `M.toSquareBlock id i ⟨i, ⋯⟩ ⟨i, ⋯⟩`
theorem det_toSquareBlock_id (M : Matrix m m R) (i : m) : (M.toSquareBlock id i).det = M i i :=
  letI : Unique { a // id a = i } := ⟨⟨⟨i, rfl⟩⟩, fun j => Subtype.ext j.property⟩
  (det_unique _).trans rfl

theorem det_toBlock (M : Matrix m m R) (p : m → Prop) [DecidablePred p] :
    M.det =
      (fromBlocks (toBlock M p p) (toBlock M p fun j => ¬p j) (toBlock M (fun j => ¬p j) p) <|
          toBlock M (fun j => ¬p j) fun j => ¬p j).det := by
  rw [← Matrix.det_reindex_self (Equiv.sumCompl p).symm M]
  rw [det_apply', det_apply']
  congr; ext σ; congr; ext x
  generalize hy : σ x = y
  cases x <;> cases y <;>
    simp only [Matrix.reindex_apply, toBlock_apply, Equiv.symm_symm, Equiv.sumCompl_apply_inr,
      Equiv.sumCompl_apply_inl, fromBlocks_apply₁₁, fromBlocks_apply₁₂, fromBlocks_apply₂₁,
      fromBlocks_apply₂₂, Matrix.submatrix_apply]

theorem twoBlockTriangular_det (M : Matrix m m R) (p : m → Prop) [DecidablePred p]
    (h : ∀ i, ¬p i → ∀ j, p j → M i j = 0) :
    M.det = (toSquareBlockProp M p).det * (toSquareBlockProp M fun i => ¬p i).det := by
  rw [det_toBlock M p]
  convert!
    det_fromBlocks_zero₂₁ (toBlock M p p) (toBlock M p fun j => ¬p j)
      (toBlock M (fun j => ¬p j) fun j => ¬p j)
  ext i j
  exact h (↑i) i.2 (↑j) j.2

theorem twoBlockTriangular_det' (M : Matrix m m R) (p : m → Prop) [DecidablePred p]
    (h : ∀ i, p i → ∀ j, ¬p j → M i j = 0) :
    M.det = (toSquareBlockProp M p).det * (toSquareBlockProp M fun i => ¬p i).det := by
  rw [M.twoBlockTriangular_det fun i => ¬p i, mul_comm]
  · congr 1
    exact equiv_block_det _ fun _ => not_not.symm
  · simpa only [Classical.not_not] using h

protected theorem BlockTriangular.det [DecidableEq α] [LinearOrder α] (hM : BlockTriangular M b) :
    M.det = ∏ a ∈ univ.image b, (M.toSquareBlock b a).det := by
  suffices ∀ hs : Finset α, univ.image b = hs → M.det = ∏ a ∈ hs, (M.toSquareBlock b a).det by
    exact this _ rfl
  intro s hs
  induction s using Finset.eraseInduction generalizing m with | H s ih =>
  subst hs
  cases isEmpty_or_nonempty m
  · simp
  let k := (univ.image b).max' (univ_nonempty.image _)
  rw [twoBlockTriangular_det' M fun i => b i = k]
  · have : univ.image b = insert k ((univ.image b).erase k) := by
      rw [insert_erase]
      apply max'_mem
    rw [this, prod_insert (notMem_erase _ _)]
    refine congr_arg _ ?_
    let b' := fun i : { a // b a ≠ k } => b ↑i
    have h' : BlockTriangular (M.toSquareBlockProp fun i => b i ≠ k) b' := hM.submatrix
    have hb' : image b' univ = (image b univ).erase k := by
      convert image_subtype_ne_univ_eq_image_erase k b
    rw [ih _ (max'_mem _ _) h' hb']
    refine Finset.prod_congr rfl fun l hl => ?_
    let he : { a // b' a = l } ≃ { a // b a = l } :=
      haveI hc : ∀ i, b i = l → b i ≠ k := fun i hi => ne_of_eq_of_ne hi (ne_of_mem_erase hl)
      Equiv.subtypeSubtypeEquivSubtype @hc
    rw [toSquareBlock_def, ← Matrix.det_reindex_self he.symm]
    rfl
  · intro i hi j hj
    apply hM
    rw [hi]
    apply lt_of_le_of_ne _ hj
    exact Finset.le_max' (univ.image b) _ (mem_image_of_mem _ (mem_univ _))

@[deprecated (since := "2026-08-31")] alias det_of_blockTriangular := Matrix.BlockTriangular.det

theorem BlockTriangular.det_fintype [DecidableEq α] [Fintype α] [LinearOrder α]
    (h : BlockTriangular M b) : M.det = ∏ k : α, (M.toSquareBlock b k).det := by
  refine h.det.trans (prod_subset (subset_univ _) fun a _ ha => ?_)
  have : IsEmpty { i // b i = a } := ⟨fun i => ha <| mem_image.2 ⟨i, mem_univ _, i.2⟩⟩
  exact det_isEmpty

theorem det_of_isUpperTriangular [LinearOrder m] (h : M.IsUpperTriangular) :
    M.det = ∏ i : m, M i i := by
  have : DecidableEq R := Classical.decEq _
  simp_rw [h.det, image_id, det_toSquareBlock_id]

@[deprecated (since := "2026-07-30")] alias det_of_upperTriangular := det_of_isUpperTriangular

theorem det_of_isLowerTriangular [LinearOrder m] (M : Matrix m m R) (h : M.IsLowerTriangular) :
    M.det = ∏ i : m, M i i := by
  rw [← det_transpose]
  exact det_of_isUpperTriangular h.transpose

@[deprecated (since := "2026-07-30")] alias det_of_lowerTriangular := det_of_isLowerTriangular

theorem IsUpperTriangular.det_ne_zero_iff [LinearOrder m] [IsDomain R] (hM : M.IsUpperTriangular) :
    M.det ≠ 0 ↔ ∀ i, M.diag i ≠ 0 := by
  simp [det_of_isUpperTriangular hM, prod_ne_zero_iff]

theorem IsLowerTriangular.det_ne_zero_iff [LinearOrder m] [IsDomain R] (hM : M.IsLowerTriangular) :
    M.det ≠ 0 ↔ ∀ i, M.diag i ≠ 0 := by
  simp [det_of_isLowerTriangular M hM, prod_ne_zero_iff]

open Polynomial

theorem matrixOfPolynomials_isUpperTriangular {R} [Semiring R] {n : ℕ} (p : Fin n → R[X])
    (h_deg : ∀ i, (p i).natDegree ≤ i) :
    (Matrix.of (fun (i j : Fin n) => (p j).coeff i)).IsUpperTriangular :=
  fun _ j h => by
    exact coeff_eq_zero_of_natDegree_lt <| Nat.lt_of_le_of_lt (h_deg j) h

@[deprecated (since := "2026-08-31")]
alias matrixOfPolynomials_blockTriangular := Matrix.matrixOfPolynomials_isUpperTriangular

theorem det_matrixOfPolynomials {n : ℕ} (p : Fin n → R[X])
    (h_deg : ∀ i, (p i).natDegree = i) (h_monic : ∀ i, Monic <| p i) :
    (Matrix.of (fun (i j : Fin n) => (p j).coeff i)).det = 1 := by
  rw [Matrix.det_of_isUpperTriangular (Matrix.matrixOfPolynomials_isUpperTriangular p (fun i ↦
      Nat.le_of_eq (h_deg i)))]
  convert! prod_const_one with x _
  rw [Matrix.of_apply, ← h_deg, coeff_natDegree, (h_monic x).leadingCoeff]

/-! ### Invertible -/


theorem BlockTriangular.toBlock_inverse_mul_toBlock_eq_one [LinearOrder α] [Invertible M]
    (hM : BlockTriangular M b) (k : α) :
    ((M⁻¹.toBlock (fun i => b i < k) fun i => b i < k) *
        M.toBlock (fun i => b i < k) fun i => b i < k) =
      1 := by
  let p i := b i < k
  have h_sum :
    M⁻¹.toBlock p p * M.toBlock p p +
        (M⁻¹.toBlock p fun i => ¬p i) * M.toBlock (fun i => ¬p i) p =
      1 := by
    rw [← toBlock_mul_eq_add, inv_mul_of_invertible M, toBlock_one_self]
  have h_zero : M.toBlock (fun i => ¬p i) p = 0 := by
    ext i j
    simpa using hM (lt_of_lt_of_le j.2 (le_of_not_gt i.2))
  simpa [h_zero] using h_sum

/-- The inverse of an upper-left subblock of a block-triangular matrix `M` is the upper-left
subblock of `M⁻¹`. -/

theorem BlockTriangular.inv_toBlock [LinearOrder α] [Invertible M] (hM : BlockTriangular M b)
    (k : α) :
    (M.toBlock (fun i => b i < k) fun i => b i < k)⁻¹ =
      M⁻¹.toBlock (fun i => b i < k) fun i => b i < k :=
  inv_eq_left_inv <| hM.toBlock_inverse_mul_toBlock_eq_one k

/-- An upper-left subblock of an invertible block-triangular matrix is invertible. -/
@[instance_reducible]
def BlockTriangular.invertibleToBlock [LinearOrder α] [Invertible M] (hM : BlockTriangular M b)
    (k : α) : Invertible (M.toBlock (fun i => b i < k) fun i => b i < k) :=
  invertibleOfLeftInverse _ ((⅟M).toBlock (fun i => b i < k) fun i => b i < k) <| by
    simpa only [invOf_eq_nonsing_inv] using hM.toBlock_inverse_mul_toBlock_eq_one k

/-- A lower-left subblock of the inverse of a block-triangular matrix is zero. This is a first step
towards `BlockTriangular.inv_toBlock` below. -/

theorem toBlock_inverse_eq_zero [LinearOrder α] [Invertible M] (hM : BlockTriangular M b) (k : α) :
    (M⁻¹.toBlock (fun i => k ≤ b i) fun i => b i < k) = 0 := by
  let p i := b i < k
  let q i := ¬b i < k
  have h_sum : M⁻¹.toBlock q p * M.toBlock p p + M⁻¹.toBlock q q * M.toBlock q p = 0 := by
    rw [← toBlock_mul_eq_add, inv_mul_of_invertible M, toBlock_one_disjoint]
    rw [disjoint_iff_inf_le]
    exact fun i h => h.1 h.2
  have h_zero : M.toBlock q p = 0 := by
    ext i j
    simpa using hM (lt_of_lt_of_le j.2 <| le_of_not_gt i.2)
  have h_mul_eq_zero : M⁻¹.toBlock q p * M.toBlock p p = 0 := by simpa [h_zero] using h_sum
  have : Invertible (M.toBlock p p) := hM.invertibleToBlock k
  have : (fun i => k ≤ b i) = q := by
    ext
    exact not_lt.symm
  rw [this, ← Matrix.zero_mul (M.toBlock p p)⁻¹, ← h_mul_eq_zero,
    mul_inv_cancel_right_of_invertible]

/-- The inverse of a block-triangular matrix is block-triangular. -/
theorem blockTriangular_inv_of_blockTriangular [LinearOrder α] [Invertible M]
    (hM : BlockTriangular M b) : BlockTriangular M⁻¹ b := by
  suffices ∀ hs : Finset α, univ.image b = hs → BlockTriangular M⁻¹ b by exact this _ rfl
  intro s hs
  induction s using Finset.strongInduction generalizing m with | H s ih =>
  subst hs
  intro i j hij
  have : Inhabited m := ⟨i⟩
  let k := (univ.image b).max' (univ_nonempty.image _)
  let b' := fun i : { a // b a < k } => b ↑i
  let A := M.toBlock (fun i => b i < k) fun j => b j < k
  obtain hbi | hi : b i = k ∨ _ := (le_max' _ (b i) <| mem_image_of_mem _ <| mem_univ _).eq_or_lt
  · have : M⁻¹.toBlock (fun i => k ≤ b i) (fun i => b i < k) ⟨i, hbi.ge⟩ ⟨j, hbi ▸ hij⟩ = 0 := by
      simp only [toBlock_inverse_eq_zero hM k, Matrix.zero_apply]
    simp [this.symm]
  have : Invertible A := hM.invertibleToBlock _
  have hA : A.BlockTriangular b' := hM.submatrix
  have hb' : image b' univ ⊂ image b univ := by
    convert image_subtype_univ_ssubset_image_univ k b _ (fun a => a < k) (lt_irrefl _)
    convert max'_mem (α := α) _ _
  have hij' : b' ⟨j, hij.trans hi⟩ < b' ⟨i, hi⟩ := by simp_rw [b', hij]
  simp [A, hM.inv_toBlock k, (ih (image b' univ) hb' hA rfl hij').symm]

end Matrix

Messung
CHZG
8710010095

¤ Dauer der Verarbeitung: 0.3 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland