state
stringlengths 0
159k
| srcUpToTactic
stringlengths 387
167k
| nextTactic
stringlengths 3
9k
| declUpToTactic
stringlengths 22
11.5k
| declId
stringlengths 38
95
| decl
stringlengths 16
1.89k
| file_tag
stringlengths 17
73
|
---|---|---|---|---|---|---|
case a.h
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
i j : Fin (Nat.succ n)
β’ M i j = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k)) i j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
| by_cases hi : i = k.succ | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case pos
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
i j : Fin (Nat.succ n)
hi : i = Fin.succ k
β’ M i j = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k)) i j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· | simp [hi, hM', hsucc, updateRow_self] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
i j : Fin (Nat.succ n)
hi : Β¬i = Fin.succ k
β’ M i j = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k)) i j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
| rw [updateRow_ne hi, hM', updateRow_ne hi] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
β’ det M = det N | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
| have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
β’ det M = det N | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
| have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
β’ det M = det N | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
| rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
β’ β (i : Fin n), Fin.castSucc k < Fin.succ i β update c k 0 i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· | intro i hi | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
hi : Fin.castSucc k < Fin.succ i
β’ update c k 0 i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
| rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
hi : βk β€ βi
β’ update c k 0 i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
| rw [Function.update_apply] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
hi : βk β€ βi
β’ (if i = k then 0 else c i) = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
| split_ifs with hik | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case pos
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
hi : βk β€ βi
hik : i = k
β’ 0 = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· | rfl | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
hi : βk β€ βi
hik : Β¬i = k
β’ c i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
| exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik))) | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._h0
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
β’ β (j : Fin (Nat.succ n)), M' 0 j = N 0 j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· | rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hsucc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
β’ β (i : Fin n) (j : Fin (Nat.succ n)), M' (Fin.succ i) j = N (Fin.succ i) j + update c k 0 i * M' (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
| intro i j | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hsucc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
β’ M' (Fin.succ i) j = N (Fin.succ i) j + update c k 0 i * M' (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
| rw [Function.update_apply] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2._hsucc
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
β’ M' (Fin.succ i) j = N (Fin.succ i) j + (if i = k then 0 else c i) * M' (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
| split_ifs with hik | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case pos
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : i = k
β’ M' (Fin.succ i) j = N (Fin.succ i) j + 0 * M' (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· | rw [zero_mul, add_zero, hM', hik, updateRow_self] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
β’ M' (Fin.succ i) j = N (Fin.succ i) j + c i * M' (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
| rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
β’ N (Fin.succ i) j + c i * M (Fin.castSucc i) j =
N (Fin.succ i) j + c i * updateRow M (Fin.succ k) (N (Fin.succ k)) (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
| by_cases hik2 : k < i | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case pos
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
hik2 : k < i
β’ N (Fin.succ i) j + c i * M (Fin.castSucc i) j =
N (Fin.succ i) j + c i * updateRow M (Fin.succ k) (N (Fin.succ k)) (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· | simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
hik2 : Β¬k < i
β’ N (Fin.succ i) j + c i * M (Fin.castSucc i) j =
N (Fin.succ i) j + c i * updateRow M (Fin.succ k) (N (Fin.succ k)) (Fin.castSucc i) j | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
| rw [updateRow_ne] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
hik2 : Β¬k < i
β’ Fin.castSucc i β Fin.succ k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
| apply ne_of_lt | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
case neg.h
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
kβ : Fin (n + 1)
k : Fin n
ih :
β (c : Fin n β R),
(β (i : Fin n), Fin.castSucc k < Fin.succ i β c i = 0) β
β {M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R},
(β (j : Fin (Nat.succ n)), M 0 j = N 0 j) β
(β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j) β
det M = det N
c : Fin n β R
hc : β (i : Fin n), Fin.succ k < Fin.succ i β c i = 0
M N : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R
h0 : β (j : Fin (Nat.succ n)), M 0 j = N 0 j
hsucc : β (i : Fin n) (j : Fin (Nat.succ n)), M (Fin.succ i) j = N (Fin.succ i) j + c i * M (Fin.castSucc i) j
M' : Matrix (Fin (Nat.succ n)) (Fin (Nat.succ n)) R := updateRow M (Fin.succ k) (N (Fin.succ k))
hM' : M' = updateRow M (Fin.succ k) (N (Fin.succ k))
hM : M = updateRow M' (Fin.succ k) (M' (Fin.succ k) + c k β’ M (Fin.castSucc k))
k_ne_succ : Fin.castSucc k β Fin.succ k
M_k : M (Fin.castSucc k) = M' (Fin.castSucc k)
i : Fin n
j : Fin (Nat.succ n)
hik : Β¬i = k
hik2 : Β¬k < i
β’ Fin.castSucc i < Fin.succ k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
| rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt] | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
| Mathlib.LinearAlgebra.Matrix.Determinant.513_0.U1f6HO8zRbnvZ95 | theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R
c : Fin n β R
A_zero : β (i : Fin (n + 1)), A i 0 = B i 0
A_succ : β (i : Fin (n + 1)) (j : Fin n), A i (Fin.succ j) = B i (Fin.succ j) + c j * A i (Fin.castSucc j)
β’ det A = det B | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
| rw [β det_transpose A, β det_transpose B] | /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
| Mathlib.LinearAlgebra.Matrix.Determinant.560_0.U1f6HO8zRbnvZ95 | /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
nβ : Type u_2
instββ΄ : DecidableEq nβ
instβΒ³ : Fintype nβ
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
n : β
A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R
c : Fin n β R
A_zero : β (i : Fin (n + 1)), A i 0 = B i 0
A_succ : β (i : Fin (n + 1)) (j : Fin n), A i (Fin.succ j) = B i (Fin.succ j) + c j * A i (Fin.castSucc j)
β’ det Aα΅ = det Bα΅ | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
| exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i | /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
| Mathlib.LinearAlgebra.Matrix.Determinant.560_0.U1f6HO8zRbnvZ95 | /-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
β’ det (blockDiagonal M) = β k : o, det (M k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
| simp_rw [det_apply'] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
β’ β Ο : Perm (n Γ o), ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i =
β x : o, β Ο : Perm n, ββ(sign Ο) * β i : n, M x (Ο i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
| rw [Finset.prod_sum] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
β’ β Ο : Perm (n Γ o), ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i =
β p in pi univ fun x => univ,
β x in attach univ, ββ(sign (p βx (_ : βx β univ))) * β i : n, M (βx) ((p βx (_ : βx β univ)) i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
| simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
β’ β Ο : Perm (n Γ o), ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i =
β x : (a : o) β a β univ β Perm n,
β x_1 : o,
ββ(sign (x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ))) *
β x_2 : n, M x_1 ((x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ)) x_2) x_2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
| let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
β’ β Ο : Perm (n Γ o), ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i =
β x : (a : o) β a β univ β Perm n,
β x_1 : o,
ββ(sign (x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ))) *
β x_2 : n, M x_1 ((x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ)) x_2) x_2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
| have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β© | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β Ο : Perm (n Γ o), ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i =
β x : (a : o) β a β univ β Perm n,
β x_1 : o,
ββ(sign (x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ))) *
β x_2 : n, M x_1 ((x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ)) x_2) x_2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
| rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β x in preserving_snd, ββ(sign x) * β i : n Γ o, blockDiagonal M (x i) i =
β x : (a : o) β a β univ β Perm n,
β x_1 : o,
ββ(sign (x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ))) *
β x_2 : n, M x_1 ((x x_1 (_ : β{ val := x_1, property := (_ : x_1 β univ) } β univ)) x_2) x_2
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β x β univ, x β preserving_snd β ββ(sign x) * β i : n Γ o, blockDiagonal M (x i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
| rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β (a : (k : o) β k β univ β Perm n) (ha : a β univ),
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha β preserving_snd | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· | intro Ο _ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : (k : o) β k β univ β Perm n
haβ : Ο β univ
β’ (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ β preserving_snd | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
| rw [mem_preserving_snd] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : (k : o) β k β univ β Perm n
haβ : Ο β univ
β’ β (x : n Γ o), (((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ) x).2 = x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
| rintro β¨-, xβ© | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case mk
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : (k : o) β k β univ β Perm n
haβ : Ο β univ
fstβ : n
x : o
β’ (((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ) (fstβ, x)).2 = (fstβ, x).2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
| simp only [prodCongrLeft_apply] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β (a : (k : o) β k β univ β Perm n) (ha : a β univ),
β x : o,
ββ(sign (a x (_ : β{ val := x, property := (_ : x β univ) } β univ))) *
β x_1 : n, M x ((a x (_ : β{ val := x, property := (_ : x β univ) } β univ)) x_1) x_1 =
ββ(sign ((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha)) *
β i : n Γ o, blockDiagonal M (((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha) i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· | intro Ο _ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : (k : o) β k β univ β Perm n
haβ : Ο β univ
β’ β x : o,
ββ(sign (Ο x (_ : β{ val := x, property := (_ : x β univ) } β univ))) *
β x_1 : n, M x ((Ο x (_ : β{ val := x, property := (_ : x β univ) } β univ)) x_1) x_1 =
ββ(sign ((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ)) *
β i : n Γ o, blockDiagonal M (((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ) i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
| rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : (k : o) β k β univ β Perm n
haβ : Ο β univ
β’ (β x : o, ββ(sign (Ο x (_ : β{ val := x, property := (_ : x β univ) } β univ)))) *
β x : o, β x_1 : n, M x ((Ο x (_ : β{ val := x, property := (_ : x β univ) } β univ)) x_1) x_1 =
ββ(sign ((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ)) *
β y : o, β x : n, blockDiagonal M (((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haβ) (x, y)) (x, y) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
| simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β (aβ aβ : (k : o) β k β univ β Perm n) (haβ : aβ β univ) (haβ : aβ β univ),
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) aβ haβ =
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) aβ haβ β
aβ = aβ | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· | intro Ο Ο' _ _ eq | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq :
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haββ =
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο' haββ
β’ Ο = Ο' | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
| ext x hx k | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.h.H
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq :
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο haββ =
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) Ο' haββ
x : o
hx : x β univ
k : n
β’ (Ο x hx) k = (Ο' x hx) k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
| simp only at eq | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.h.H
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq : (prodCongrLeft fun k => Ο k (_ : k β univ)) = prodCongrLeft fun k => Ο' k (_ : k β univ)
x : o
hx : x β univ
k : n
β’ (Ο x hx) k = (Ο' x hx) k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
| have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq : (prodCongrLeft fun k => Ο k (_ : k β univ)) = prodCongrLeft fun k => Ο' k (_ : k β univ)
xβ : o
hx : xβ β univ
kβ k : n
x : o
β’ (prodCongrLeft fun k => Ο k (_ : k β univ)) (k, x) = (prodCongrLeft fun k => Ο' k (_ : k β univ)) (k, x) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by | rw [eq] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.h.H
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq : (prodCongrLeft fun k => Ο k (_ : k β univ)) = prodCongrLeft fun k => Ο' k (_ : k β univ)
x : o
hx : x β univ
k : n
this :
β (k : n) (x : o),
(prodCongrLeft fun k => Ο k (_ : k β univ)) (k, x) = (prodCongrLeft fun k => Ο' k (_ : k β univ)) (k, x)
β’ (Ο x hx) k = (Ο' x hx) k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
| simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.h.H
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο Ο' : (k : o) β k β univ β Perm n
haββ : Ο β univ
haββ : Ο' β univ
eq : (prodCongrLeft fun k => Ο k (_ : k β univ)) = prodCongrLeft fun k => Ο' k (_ : k β univ)
x : o
hx : x β univ
k : n
this : β (k : n) (x : o), (Ο x (_ : x β univ)) k = (Ο' x (_ : x β univ)) k β§ True
β’ (Ο x hx) k = (Ο' x hx) k | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
| exact (this k x).1 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β b β preserving_snd, β a, β (ha : a β univ), b = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· | intro Ο hΟ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : Ο β preserving_snd
β’ β a, β (ha : a β univ), Ο = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
| rw [mem_preserving_snd] at hΟ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
β’ β a, β (ha : a β univ), Ο = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
| have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
β’ β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
| intro x | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
x : n Γ o
β’ (Οβ»ΒΉ x).2 = x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
| conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
x : n Γ o
| x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | rw [β Perm.apply_inv_self Ο x, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
x : n Γ o
| x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | rw [β Perm.apply_inv_self Ο x, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
x : n Γ o
| x.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | rw [β Perm.apply_inv_self Ο x, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
β’ β a, β (ha : a β univ), Ο = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
| have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
β’ β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
| intro k x | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
k : o
x : n
β’ ((Ο (x, k)).1, k) = Ο (x, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
| ext | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
k : o
x : n
β’ ((Ο (x, k)).1, k).1 = (Ο (x, k)).1 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· | simp only | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
k : o
x : n
β’ ((Ο (x, k)).1, k).2 = (Ο (x, k)).2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· | simp only [hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
β’ β a, β (ha : a β univ), Ο = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
| have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ'] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
β’ β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
| intro k x | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
β’ ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
| conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
| ((Οβ»ΒΉ (x, k)).1, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | rw [β Perm.apply_inv_self Ο (x, k)] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
| ((Οβ»ΒΉ (x, k)).1, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | rw [β Perm.apply_inv_self Ο (x, k)] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
| ((Οβ»ΒΉ (x, k)).1, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | rw [β Perm.apply_inv_self Ο (x, k)] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
β’ ((Οβ»ΒΉ (Ο (Οβ»ΒΉ (x, k)))).1, k) = Οβ»ΒΉ (x, k) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
| ext | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
β’ ((Οβ»ΒΉ (Ο (Οβ»ΒΉ (x, k)))).1, k).1 = (Οβ»ΒΉ (x, k)).1 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· | simp only [apply_inv_self] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
k : o
x : n
β’ ((Οβ»ΒΉ (Ο (Οβ»ΒΉ (x, k)))).1, k).2 = (Οβ»ΒΉ (x, k)).2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· | simp only [hΟ'] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
β’ β a, β (ha : a β univ), Ο = (fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ)) a ha | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
| refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β© | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_1
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : o
xβ : k β univ
β’ LeftInverse (fun x => (Οβ»ΒΉ (x, k)).1) fun x => (Ο (x, k)).1 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· | intro x | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_1
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : o
xβ : k β univ
x : n
β’ (fun x => (Οβ»ΒΉ (x, k)).1) ((fun x => (Ο (x, k)).1) x) = x | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
| simp only [mk_apply_eq, inv_apply_self] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : o
xβ : k β univ
β’ Function.RightInverse (fun x => (Οβ»ΒΉ (x, k)).1) fun x => (Ο (x, k)).1 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· | intro x | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_2
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : o
xβ : k β univ
x : n
β’ (fun x => (Ο (x, k)).1) ((fun x => (Οβ»ΒΉ (x, k)).1) x) = x | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
| simp only [mk_inv_apply_eq, apply_inv_self] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_3
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
β’ (fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) }) β
univ | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· | apply Finset.mem_univ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_4
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
β’ Ο =
(fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ))
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) })
(_ :
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) }) β
univ) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· | ext β¨k, xβ© | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_4.H.mk.a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : n
x : o
β’ (Ο (k, x)).1 =
(((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ))
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) })
(_ :
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) }) β
univ))
(k, x)).1 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· | simp only [coe_fn_mk, prodCongrLeft_apply] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case refine'_4.H.mk.a
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : n Γ o β n Γ o
hΟ : β (x : n Γ o), (Ο x).2 = x.2
hΟ' : β (x : n Γ o), (Οβ»ΒΉ x).2 = x.2
mk_apply_eq : β (k : o) (x : n), ((Ο (x, k)).1, k) = Ο (x, k)
mk_inv_apply_eq : β (k : o) (x : n), ((Οβ»ΒΉ (x, k)).1, k) = Οβ»ΒΉ (x, k)
k : n
x : o
β’ (Ο (k, x)).2 =
(((fun Ο x => prodCongrLeft fun k => Ο k (_ : k β univ))
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) })
(_ :
(fun k x =>
{ toFun := fun x => (Ο (x, k)).1, invFun := fun x => (Οβ»ΒΉ (x, k)).1,
left_inv := (_ : β (x : n), (Οβ»ΒΉ ((Ο (x, k)).1, k)).1 = x),
right_inv := (_ : β (x : n), (Ο ((Οβ»ΒΉ (x, k)).1, k)).1 = x) }) β
univ))
(k, x)).2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· | simp only [prodCongrLeft_apply, hΟ] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
β’ β x β univ, x β preserving_snd β ββ(sign x) * β i : n Γ o, blockDiagonal M (x i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· | intro Ο _ hΟ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : Perm (n Γ o)
aβ : Ο β univ
hΟ : Ο β preserving_snd
β’ ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
| rw [mem_preserving_snd] at hΟ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : Perm (n Γ o)
aβ : Ο β univ
hΟ : Β¬β (x : n Γ o), (Ο x).2 = x.2
β’ ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
| obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case intro.mk
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : Perm (n Γ o)
aβ : Ο β univ
hΟ : Β¬β (x : n Γ o), (Ο x).2 = x.2
k : n
x : o
hkx : Β¬(Ο (k, x)).2 = (k, x).2
β’ ββ(sign Ο) * β i : n Γ o, blockDiagonal M (Ο i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
| rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case intro.mk
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : Perm (n Γ o)
aβ : Ο β univ
hΟ : Β¬β (x : n Γ o), (Ο x).2 = x.2
k : n
x : o
hkx : Β¬(Ο (k, x)).2 = (k, x).2
β’ blockDiagonal M (Ο (k, x)) (k, x) = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
| rw [blockDiagonal_apply_ne] | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
case intro.mk.h
m : Type u_1
n : Type u_2
instββΆ : DecidableEq n
instββ΅ : Fintype n
instββ΄ : DecidableEq m
instβΒ³ : Fintype m
R : Type v
instβΒ² : CommRing R
o : Type u_3
instβΒΉ : Fintype o
instβ : DecidableEq o
M : o β Matrix n n R
preserving_snd : Finset (Perm (n Γ o)) := filter (fun Ο => β (x : n Γ o), (Ο x).2 = x.2) univ
mem_preserving_snd : β {Ο : Perm (n Γ o)}, Ο β preserving_snd β β (x : n Γ o), (Ο x).2 = x.2
Ο : Perm (n Γ o)
aβ : Ο β univ
hΟ : Β¬β (x : n Γ o), (Ο x).2 = x.2
k : n
x : o
hkx : Β¬(Ο (k, x)).2 = (k, x).2
β’ (Ο (k, x)).2 β x | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
| exact hkx | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
| Mathlib.LinearAlgebra.Matrix.Determinant.571_0.U1f6HO8zRbnvZ95 | @[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ det (fromBlocks A B 0 D) = det A * det D | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
| classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ]
rw [mul_mul_mul_comm]
congr
rw [sign_sumCongr, Units.val_mul, Int.cast_mul]
Β· intro Οβ Οβ hβ hβ
dsimp only
intro h
have h2 : β x, Perm.sumCongr Οβ.fst Οβ.snd x = Perm.sumCongr Οβ.fst Οβ.snd x :=
FunLike.congr_fun h
simp only [Sum.map_inr, Sum.map_inl, Perm.sumCongr_apply, Sum.forall, Sum.inl.injEq,
Sum.inr.injEq] at h2
ext x
Β· exact h2.left x
Β· exact h2.right x
Β· intro Ο hΟ
erw [Set.mem_toFinset, MonoidHom.mem_range] at hΟ
obtain β¨Οββ, hΟβββ© := hΟ
use Οββ
rw [β hΟββ]
simp
Β· rintro Ο - hΟn
have h1 : Β¬β x, β y, Sum.inl y = Ο (Sum.inl x) := by
rw [Set.mem_toFinset] at hΟn
-- Porting note: golfed
simpa only [Set.MapsTo, Set.mem_range, forall_exists_index, forall_apply_eq_imp_iff] using
mt mem_sumCongrHom_range_of_perm_mapsTo_inl hΟn
obtain β¨a, haβ© := not_forall.mp h1
cases' hx : Ο (Sum.inl a) with a2 b
Β· have hn := (not_exists.mp ha) a2
exact absurd hx.symm hn
Β· rw [Finset.prod_eq_zero (Finset.mem_univ (Sum.inl a)), mul_zero]
rw [hx, fromBlocks_applyββ, zero_apply] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ det (fromBlocks A B 0 D) = det A * det D | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
| simp_rw [det_apply'] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β Ο : Perm (m β n), ββ(sign Ο) * β i : m β n, fromBlocks A B 0 D (Ο i) i =
(β Ο : Perm m, ββ(sign Ο) * β i : m, A (Ο i) i) * β Ο : Perm n, ββ(sign Ο) * β i : n, D (Ο i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
| convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_ | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.e'_3
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ (β Ο : Perm m, ββ(sign Ο) * β i : m, A (Ο i) i) * β Ο : Perm n, ββ(sign Ο) * β i : n, D (Ο i) i =
β x in Set.toFinset β(MonoidHom.range (sumCongrHom m n)), ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i
case convert_2
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β x β univ,
x β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) β ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
| rw [sum_mul_sum] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.e'_3
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β p in univ ΓΛ’ univ, (ββ(sign p.1) * β i : m, A (p.1 i) i) * (ββ(sign p.2) * β i : n, D (p.2 i) i) =
β x in Set.toFinset β(MonoidHom.range (sumCongrHom m n)), ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i
case convert_2
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β x β univ,
x β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) β ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
| simp_rw [univ_product_univ] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h.e'_3
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β p : Perm m Γ Perm n, (ββ(sign p.1) * β i : m, A (p.1 i) i) * (ββ(sign p.2) * β i : n, D (p.2 i) i) =
β x in Set.toFinset β(MonoidHom.range (sumCongrHom m n)), ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i
case convert_2
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β x β univ,
x β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) β ββ(sign x) * β i : m β n, fromBlocks A B 0 D (x i) i = 0 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
| rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β (a : Perm m Γ Perm n) (ha : a β univ),
(fun Ο x => Equiv.sumCongr Ο.1 Ο.2) a ha β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· | intro Οββ h | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οββ : Perm m Γ Perm n
h : Οββ β univ
β’ (fun Ο x => Equiv.sumCongr Ο.1 Ο.2) Οββ h β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
| simp only | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οββ : Perm m Γ Perm n
h : Οββ β univ
β’ Equiv.sumCongr Οββ.1 Οββ.2 β Set.toFinset β(MonoidHom.range (sumCongrHom m n)) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
| erw [Set.mem_toFinset, MonoidHom.mem_range] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οββ : Perm m Γ Perm n
h : Οββ β univ
β’ β x, (sumCongrHom m n) x = Equiv.sumCongr Οββ.1 Οββ.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
| use Οββ | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
case h
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οββ : Perm m Γ Perm n
h : Οββ β univ
β’ (sumCongrHom m n) Οββ = Equiv.sumCongr Οββ.1 Οββ.2 | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
| simp only [sumCongrHom_apply] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β (a : Perm m Γ Perm n) (ha : a β univ),
(ββ(sign a.1) * β i : m, A (a.1 i) i) * (ββ(sign a.2) * β i : n, D (a.2 i) i) =
ββ(sign ((fun Ο x => Equiv.sumCongr Ο.1 Ο.2) a ha)) *
β i : m β n, fromBlocks A B 0 D (((fun Ο x => Equiv.sumCongr Ο.1 Ο.2) a ha) i) i | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· | simp only [forall_prop_of_true, Prod.forall, mem_univ] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· | Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
β’ β (a : Perm m) (b : Perm n),
(ββ(sign a) * β x : m, A (a x) x) * (ββ(sign b) * β x : n, D (b x) x) =
ββ(sign (Equiv.sumCongr a b)) * β x : m β n, fromBlocks A B 0 D ((Equiv.sumCongr a b) x) x | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
| intro Οβ Οβ | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οβ : Perm m
Οβ : Perm n
β’ (ββ(sign Οβ) * β x : m, A (Οβ x) x) * (ββ(sign Οβ) * β x : n, D (Οβ x) x) =
ββ(sign (Equiv.sumCongr Οβ Οβ)) * β x : m β n, fromBlocks A B 0 D ((Equiv.sumCongr Οβ Οβ) x) x | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
| rw [Fintype.prod_sum_type] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οβ : Perm m
Οβ : Perm n
β’ (ββ(sign Οβ) * β x : m, A (Οβ x) x) * (ββ(sign Οβ) * β x : n, D (Οβ x) x) =
ββ(sign (Equiv.sumCongr Οβ Οβ)) *
((β aβ : m, fromBlocks A B 0 D ((Equiv.sumCongr Οβ Οβ) (Sum.inl aβ)) (Sum.inl aβ)) *
β aβ : n, fromBlocks A B 0 D ((Equiv.sumCongr Οβ Οβ) (Sum.inr aβ)) (Sum.inr aβ)) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
| simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οβ : Perm m
Οβ : Perm n
β’ (ββ(sign Οβ) * β x : m, A (Οβ x) x) * (ββ(sign Οβ) * β x : n, D (Οβ x) x) =
ββ(sign (Equiv.sumCongr Οβ Οβ)) * ((β x : m, A (Οβ x) x) * β x : n, D (Οβ x) x) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ]
| rw [mul_mul_mul_comm] | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
m : Type u_1
n : Type u_2
instββ΄ : DecidableEq n
instβΒ³ : Fintype n
instβΒ² : DecidableEq m
instβΒΉ : Fintype m
R : Type v
instβ : CommRing R
A : Matrix m m R
B : Matrix m n R
D : Matrix n n R
Οβ : Perm m
Οβ : Perm n
β’ ββ(sign Οβ) * ββ(sign Οβ) * ((β x : m, A (Οβ x) x) * β x : n, D (Οβ x) x) =
ββ(sign (Equiv.sumCongr Οβ Οβ)) * ((β x : m, A (Οβ x) x) * β x : n, D (Οβ x) x) | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Chris Hughes, Anne Baanen
-/
import Mathlib.Data.Matrix.PEquiv
import Mathlib.Data.Matrix.Block
import Mathlib.Data.Matrix.Notation
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.Perm.Fin
import Mathlib.GroupTheory.Perm.Sign
import Mathlib.Algebra.Algebra.Basic
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.Alternating.Basic
import Mathlib.LinearAlgebra.Pi
#align_import linear_algebra.matrix.determinant from "leanprover-community/mathlib"@"c3019c79074b0619edb4b27553a91b2e82242395"
/-!
# Determinant of a matrix
This file defines the determinant of a matrix, `Matrix.det`, and its essential properties.
## Main definitions
- `Matrix.det`: the determinant of a square matrix, as a sum over permutations
- `Matrix.detRowAlternating`: the determinant, as an `AlternatingMap` in the rows of the matrix
## Main results
- `det_mul`: the determinant of `A * B` is the product of determinants
- `det_zero_of_row_eq`: the determinant is zero if there is a repeated row
- `det_block_diagonal`: the determinant of a block diagonal matrix is a product
of the blocks' determinants
## Implementation notes
It is possible to configure `simp` to compute determinants. See the file
`test/matrix.lean` for some examples.
-/
universe u v w z
open Equiv Equiv.Perm Finset Function
namespace Matrix
open Matrix BigOperators
variable {m n : Type*} [DecidableEq n] [Fintype n] [DecidableEq m] [Fintype m]
variable {R : Type v} [CommRing R]
-- mathport name: Β«exprΞ΅ Β»
local notation "Ξ΅ " Ο:arg => ((sign Ο : β€) : R)
/-- `det` is an `AlternatingMap` in the rows of the matrix. -/
def detRowAlternating : (n β R) [Ξ^n]ββ[R] R :=
MultilinearMap.alternatization ((MultilinearMap.mkPiAlgebra R n R).compLinearMap LinearMap.proj)
#align matrix.det_row_alternating Matrix.detRowAlternating
/-- The determinant of a matrix given by the Leibniz formula. -/
abbrev det (M : Matrix n n R) : R :=
detRowAlternating M
#align matrix.det Matrix.det
theorem det_apply (M : Matrix n n R) : M.det = β Ο : Perm n, Equiv.Perm.sign Ο β’ β i, M (Ο i) i :=
MultilinearMap.alternatization_apply _ M
#align matrix.det_apply Matrix.det_apply
-- This is what the old definition was. We use it to avoid having to change the old proofs below
theorem det_apply' (M : Matrix n n R) : M.det = β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) i := by
simp [det_apply, Units.smul_def]
#align matrix.det_apply' Matrix.det_apply'
@[simp]
theorem det_diagonal {d : n β R} : det (diagonal d) = β i, d i := by
rw [det_apply']
refine' (Finset.sum_eq_single 1 _ _).trans _
Β· rintro Ο - h2
cases' not_forall.1 (mt Equiv.ext h2) with x h3
convert mul_zero (Ξ΅ Ο)
apply Finset.prod_eq_zero (mem_univ x)
exact if_neg h3
Β· simp
Β· simp
#align matrix.det_diagonal Matrix.det_diagonal
-- @[simp] -- Porting note: simp can prove this
theorem det_zero (_ : Nonempty n) : det (0 : Matrix n n R) = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_zero
#align matrix.det_zero Matrix.det_zero
@[simp]
theorem det_one : det (1 : Matrix n n R) = 1 := by rw [β diagonal_one]; simp [-diagonal_one]
#align matrix.det_one Matrix.det_one
theorem det_isEmpty [IsEmpty n] {A : Matrix n n R} : det A = 1 := by simp [det_apply]
#align matrix.det_is_empty Matrix.det_isEmpty
@[simp]
theorem coe_det_isEmpty [IsEmpty n] : (det : Matrix n n R β R) = Function.const _ 1 := by
ext
exact det_isEmpty
#align matrix.coe_det_is_empty Matrix.coe_det_isEmpty
theorem det_eq_one_of_card_eq_zero {A : Matrix n n R} (h : Fintype.card n = 0) : det A = 1 :=
haveI : IsEmpty n := Fintype.card_eq_zero_iff.mp h
det_isEmpty
#align matrix.det_eq_one_of_card_eq_zero Matrix.det_eq_one_of_card_eq_zero
/-- If `n` has only one element, the determinant of an `n` by `n` matrix is just that element.
Although `Unique` implies `DecidableEq` and `Fintype`, the instances might
not be syntactically equal. Thus, we need to fill in the args explicitly. -/
@[simp]
theorem det_unique {n : Type*} [Unique n] [DecidableEq n] [Fintype n] (A : Matrix n n R) :
det A = A default default := by simp [det_apply, univ_unique]
#align matrix.det_unique Matrix.det_unique
theorem det_eq_elem_of_subsingleton [Subsingleton n] (A : Matrix n n R) (k : n) :
det A = A k k := by
have := uniqueOfSubsingleton k
convert det_unique A
#align matrix.det_eq_elem_of_subsingleton Matrix.det_eq_elem_of_subsingleton
theorem det_eq_elem_of_card_eq_one {A : Matrix n n R} (h : Fintype.card n = 1) (k : n) :
det A = A k k :=
haveI : Subsingleton n := Fintype.card_le_one_iff_subsingleton.mp h.le
det_eq_elem_of_subsingleton _ _
#align matrix.det_eq_elem_of_card_eq_one Matrix.det_eq_elem_of_card_eq_one
theorem det_mul_aux {M N : Matrix n n R} {p : n β n} (H : Β¬Bijective p) :
(β Ο : Perm n, Ξ΅ Ο * β x, M (Ο x) (p x) * N (p x) x) = 0 := by
obtain β¨i, j, hpij, hijβ© : β i j, p i = p j β§ i β j := by
rw [β Finite.injective_iff_bijective, Injective] at H
push_neg at H
exact H
exact
sum_involution (fun Ο _ => Ο * Equiv.swap i j)
(fun Ο _ => by
have : (β x, M (Ο x) (p x)) = β x, M ((Ο * Equiv.swap i j) x) (p x) :=
Fintype.prod_equiv (swap i j) _ _ (by simp [apply_swap_eq_self hpij])
simp [this, sign_swap hij, -sign_swap', prod_mul_distrib])
(fun Ο _ _ => (not_congr mul_swap_eq_iff).mpr hij) (fun _ _ => mem_univ _) fun Ο _ =>
mul_swap_involutive i j Ο
#align matrix.det_mul_aux Matrix.det_mul_aux
@[simp]
theorem det_mul (M N : Matrix n n R) : det (M * N) = det M * det N :=
calc
det (M * N) = β p : n β n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i := by
simp only [det_apply', mul_apply, prod_univ_sum, mul_sum, Fintype.piFinset_univ]
rw [Finset.sum_comm]
_ =
β p in (@univ (n β n) _).filter Bijective,
β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (p i) * N (p i) i :=
(Eq.symm <|
sum_subset (filter_subset _ _) fun f _ hbij =>
det_mul_aux <| by simpa only [true_and_iff, mem_filter, mem_univ] using hbij)
_ = β Ο : Perm n, β Ο : Perm n, Ξ΅ Ο * β i, M (Ο i) (Ο i) * N (Ο i) i :=
(sum_bij (fun p h => Equiv.ofBijective p (mem_filter.1 h).2) (fun _ _ => mem_univ _)
(fun _ _ => rfl) (fun _ _ _ _ h => by injection h) fun b _ =>
β¨b, mem_filter.2 β¨mem_univ _, b.bijectiveβ©, coe_fn_injective rflβ©)
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * Ξ΅ Ο * β j, M (Ο j) (Ο j) := by
simp only [mul_comm, mul_left_comm, prod_mul_distrib, mul_assoc]
_ = β Ο : Perm n, β Ο : Perm n, (β i, N (Ο i) i) * (Ξ΅ Ο * Ξ΅ Ο) * β i, M (Ο i) i :=
(sum_congr rfl fun Ο _ =>
Fintype.sum_equiv (Equiv.mulRight Οβ»ΒΉ) _ _ fun Ο => by
have : (β j, M (Ο j) (Ο j)) = β j, M ((Ο * Οβ»ΒΉ) j) j := by
rw [β (Οβ»ΒΉ : _ β _).prod_comp]
simp only [Equiv.Perm.coe_mul, apply_inv_self, Function.comp_apply]
have h : Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ Ο :=
calc
Ξ΅ Ο * Ξ΅ (Ο * Οβ»ΒΉ) = Ξ΅ (Ο * Οβ»ΒΉ * Ο) := by
rw [mul_comm, sign_mul (Ο * Οβ»ΒΉ)]
simp only [Int.cast_mul, Units.val_mul]
_ = Ξ΅ Ο := by simp only [inv_mul_cancel_right]
simp_rw [Equiv.coe_mulRight, h]
simp only [this])
_ = det M * det N := by
simp only [det_apply', Finset.mul_sum, mul_comm, mul_left_comm, mul_assoc]
#align matrix.det_mul Matrix.det_mul
/-- The determinant of a matrix, as a monoid homomorphism. -/
def detMonoidHom : Matrix n n R β* R where
toFun := det
map_one' := det_one
map_mul' := det_mul
#align matrix.det_monoid_hom Matrix.detMonoidHom
@[simp]
theorem coe_detMonoidHom : (detMonoidHom : Matrix n n R β R) = det :=
rfl
#align matrix.coe_det_monoid_hom Matrix.coe_detMonoidHom
/-- On square matrices, `mul_comm` applies under `det`. -/
theorem det_mul_comm (M N : Matrix m m R) : det (M * N) = det (N * M) := by
rw [det_mul, det_mul, mul_comm]
#align matrix.det_mul_comm Matrix.det_mul_comm
/-- On square matrices, `mul_left_comm` applies under `det`. -/
theorem det_mul_left_comm (M N P : Matrix m m R) : det (M * (N * P)) = det (N * (M * P)) := by
rw [β Matrix.mul_assoc, β Matrix.mul_assoc, det_mul, det_mul_comm M N, β det_mul]
#align matrix.det_mul_left_comm Matrix.det_mul_left_comm
/-- On square matrices, `mul_right_comm` applies under `det`. -/
theorem det_mul_right_comm (M N P : Matrix m m R) : det (M * N * P) = det (M * P * N) := by
rw [Matrix.mul_assoc, Matrix.mul_assoc, det_mul, det_mul_comm N P, β det_mul]
#align matrix.det_mul_right_comm Matrix.det_mul_right_comm
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((M : Matrix _ _ _) * N * (βMβ»ΒΉ : Matrix _ _ _)) = det N := by
rw [det_mul_right_comm, Units.mul_inv, one_mul]
#align matrix.det_units_conj Matrix.det_units_conj
-- TODO(mathlib4#6607): fix elaboration so that the ascription isn't needed
theorem det_units_conj' (M : (Matrix m m R)Λ£) (N : Matrix m m R) :
det ((βMβ»ΒΉ : Matrix _ _ _) * N * (βM : Matrix _ _ _)) = det N :=
det_units_conj Mβ»ΒΉ N
#align matrix.det_units_conj' Matrix.det_units_conj'
/-- Transposing a matrix preserves the determinant. -/
@[simp]
theorem det_transpose (M : Matrix n n R) : Mα΅.det = M.det := by
rw [det_apply', det_apply']
refine' Fintype.sum_bijective _ inv_involutive.bijective _ _ _
intro Ο
rw [sign_inv]
congr 1
apply Fintype.prod_equiv Ο
intros
simp
#align matrix.det_transpose Matrix.det_transpose
/-- Permuting the columns changes the sign of the determinant. -/
theorem det_permute (Ο : Perm n) (M : Matrix n n R) :
(Matrix.det fun i => M (Ο i)) = Perm.sign Ο * M.det :=
((detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_perm M Ο).trans (by simp [Units.smul_def])
#align matrix.det_permute Matrix.det_permute
/-- Permuting rows and columns with the same equivalence has no effect. -/
@[simp]
theorem det_submatrix_equiv_self (e : n β m) (A : Matrix m m R) :
det (A.submatrix e e) = det A := by
rw [det_apply', det_apply']
apply Fintype.sum_equiv (Equiv.permCongr e)
intro Ο
rw [Equiv.Perm.sign_permCongr e Ο]
congr 1
apply Fintype.prod_equiv e
intro i
rw [Equiv.permCongr_apply, Equiv.symm_apply_apply, submatrix_apply]
#align matrix.det_submatrix_equiv_self Matrix.det_submatrix_equiv_self
/-- Reindexing both indices along the same equivalence preserves the determinant.
For the `simp` version of this lemma, see `det_submatrix_equiv_self`; this one is unsuitable because
`Matrix.reindex_apply` unfolds `reindex` first.
-/
theorem det_reindex_self (e : m β n) (A : Matrix m m R) : det (reindex e e A) = det A :=
det_submatrix_equiv_self e.symm A
#align matrix.det_reindex_self Matrix.det_reindex_self
/-- The determinant of a permutation matrix equals its sign. -/
@[simp]
theorem det_permutation (Ο : Perm n) :
Matrix.det (Ο.toPEquiv.toMatrix : Matrix n n R) = Perm.sign Ο := by
rw [β Matrix.mul_one (Ο.toPEquiv.toMatrix : Matrix n n R), PEquiv.toPEquiv_mul_matrix,
det_permute, det_one, mul_one]
#align matrix.det_permutation Matrix.det_permutation
theorem det_smul (A : Matrix n n R) (c : R) : det (c β’ A) = c ^ Fintype.card n * det A :=
calc
det (c β’ A) = det ((diagonal fun _ => c) * A) := by rw [smul_eq_diagonal_mul]
_ = det (diagonal fun _ => c) * det A := (det_mul _ _)
_ = c ^ Fintype.card n * det A := by simp [card_univ]
#align matrix.det_smul Matrix.det_smul
@[simp]
theorem det_smul_of_tower {Ξ±} [Monoid Ξ±] [DistribMulAction Ξ± R] [IsScalarTower Ξ± R R]
[SMulCommClass Ξ± R R] (c : Ξ±) (A : Matrix n n R) : det (c β’ A) = c ^ Fintype.card n β’ det A :=
by rw [β smul_one_smul R c A, det_smul, smul_pow, one_pow, smul_mul_assoc, one_mul]
#align matrix.det_smul_of_tower Matrix.det_smul_of_tower
theorem det_neg (A : Matrix n n R) : det (-A) = (-1) ^ Fintype.card n * det A := by
rw [β det_smul, neg_one_smul]
#align matrix.det_neg Matrix.det_neg
/-- A variant of `Matrix.det_neg` with scalar multiplication by `Units β€` instead of multiplication
by `R`. -/
theorem det_neg_eq_smul (A : Matrix n n R) : det (-A) = (-1 : Units β€) ^ Fintype.card n β’ det A :=
by rw [β det_smul_of_tower, Units.neg_smul, one_smul]
#align matrix.det_neg_eq_smul Matrix.det_neg_eq_smul
/-- Multiplying each row by a fixed `v i` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_row (v : n β R) (A : Matrix n n R) :
det (of fun i j => v j * A i j) = (β i, v i) * det A :=
calc
det (of fun i j => v j * A i j) = det (A * diagonal v) :=
congr_arg det <| by
ext
simp [mul_comm]
_ = (β i, v i) * det A := by rw [det_mul, det_diagonal, mul_comm]
#align matrix.det_mul_row Matrix.det_mul_row
/-- Multiplying each column by a fixed `v j` multiplies the determinant by
the product of the `v`s. -/
theorem det_mul_column (v : n β R) (A : Matrix n n R) :
det (of fun i j => v i * A i j) = (β i, v i) * det A :=
MultilinearMap.map_smul_univ _ v A
#align matrix.det_mul_column Matrix.det_mul_column
@[simp]
theorem det_pow (M : Matrix m m R) (n : β) : det (M ^ n) = det M ^ n :=
(detMonoidHom : Matrix m m R β* R).map_pow M n
#align matrix.det_pow Matrix.det_pow
section HomMap
variable {S : Type w} [CommRing S]
theorem _root_.RingHom.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
by simp [Matrix.det_apply', f.map_sum, f.map_prod]
#align ring_hom.map_det RingHom.map_det
theorem _root_.RingEquiv.map_det (f : R β+* S) (M : Matrix n n R) :
f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align ring_equiv.map_det RingEquiv.map_det
theorem _root_.AlgHom.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T] (f : S ββ[R] T)
(M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toRingHom.map_det _
#align alg_hom.map_det AlgHom.map_det
theorem _root_.AlgEquiv.map_det [Algebra R S] {T : Type z} [CommRing T] [Algebra R T]
(f : S ββ[R] T) (M : Matrix n n S) : f M.det = Matrix.det (f.mapMatrix M) :=
f.toAlgHom.map_det _
#align alg_equiv.map_det AlgEquiv.map_det
end HomMap
@[simp]
theorem det_conjTranspose [StarRing R] (M : Matrix m m R) : det Mα΄΄ = star (det M) :=
((starRingEnd R).map_det _).symm.trans <| congr_arg star M.det_transpose
#align matrix.det_conj_transpose Matrix.det_conjTranspose
section DetZero
/-!
### `det_zero` section
Prove that a matrix with a repeated column has determinant equal to zero.
-/
theorem det_eq_zero_of_row_eq_zero {A : Matrix n n R} (i : n) (h : β j, A i j = 0) : det A = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_coord_zero i (funext h)
#align matrix.det_eq_zero_of_row_eq_zero Matrix.det_eq_zero_of_row_eq_zero
theorem det_eq_zero_of_column_eq_zero {A : Matrix n n R} (j : n) (h : β i, A i j = 0) :
det A = 0 := by
rw [β det_transpose]
exact det_eq_zero_of_row_eq_zero j h
#align matrix.det_eq_zero_of_column_eq_zero Matrix.det_eq_zero_of_column_eq_zero
variable {M : Matrix n n R} {i j : n}
/-- If a matrix has a repeated row, the determinant will be zero. -/
theorem det_zero_of_row_eq (i_ne_j : i β j) (hij : M i = M j) : M.det = 0 :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_eq_zero_of_eq M hij i_ne_j
#align matrix.det_zero_of_row_eq Matrix.det_zero_of_row_eq
/-- If a matrix has a repeated column, the determinant will be zero. -/
theorem det_zero_of_column_eq (i_ne_j : i β j) (hij : β k, M k i = M k j) : M.det = 0 := by
rw [β det_transpose, det_zero_of_row_eq i_ne_j]
exact funext hij
#align matrix.det_zero_of_column_eq Matrix.det_zero_of_column_eq
end DetZero
theorem det_updateRow_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateRow M j <| u + v) = det (updateRow M j u) + det (updateRow M j v) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_add M j u v
#align matrix.det_update_row_add Matrix.det_updateRow_add
theorem det_updateColumn_add (M : Matrix n n R) (j : n) (u v : n β R) :
det (updateColumn M j <| u + v) = det (updateColumn M j u) + det (updateColumn M j v) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_add]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_add Matrix.det_updateColumn_add
theorem det_updateRow_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow M j <| s β’ u) = s * det (updateRow M j u) :=
(detRowAlternating : (n β R) [Ξ^n]ββ[R] R).map_smul M j s u
#align matrix.det_update_row_smul Matrix.det_updateRow_smul
theorem det_updateColumn_smul (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn M j <| s β’ u) = s * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, det_updateRow_smul]
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul Matrix.det_updateColumn_smul
theorem det_updateRow_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateRow (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateRow M j u) :=
MultilinearMap.map_update_smul _ M j s u
#align matrix.det_update_row_smul' Matrix.det_updateRow_smul'
theorem det_updateColumn_smul' (M : Matrix n n R) (j : n) (s : R) (u : n β R) :
det (updateColumn (s β’ M) j u) = s ^ (Fintype.card n - 1) * det (updateColumn M j u) := by
rw [β det_transpose, β updateRow_transpose, transpose_smul, det_updateRow_smul']
simp [updateRow_transpose, det_transpose]
#align matrix.det_update_column_smul' Matrix.det_updateColumn_smul'
section DetEq
/-! ### `det_eq` section
Lemmas showing the determinant is invariant under a variety of operations.
-/
theorem det_eq_of_eq_mul_det_one {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = B * C) : det A = det B :=
calc
det A = det (B * C) := congr_arg _ hA
_ = det B * det C := (det_mul _ _)
_ = det B := by rw [hC, mul_one]
#align matrix.det_eq_of_eq_mul_det_one Matrix.det_eq_of_eq_mul_det_one
theorem det_eq_of_eq_det_one_mul {A B : Matrix n n R} (C : Matrix n n R) (hC : det C = 1)
(hA : A = C * B) : det A = det B :=
calc
det A = det (C * B) := congr_arg _ hA
_ = det C * det B := (det_mul _ _)
_ = det B := by rw [hC, one_mul]
#align matrix.det_eq_of_eq_det_one_mul Matrix.det_eq_of_eq_det_one_mul
theorem det_updateRow_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateRow A i (A i + A j)) = det A := by
simp [det_updateRow_add,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_self Matrix.det_updateRow_add_self
theorem det_updateColumn_add_self (A : Matrix n n R) {i j : n} (hij : i β j) :
det (updateColumn A i fun k => A k i + A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_self Aα΅ hij
#align matrix.det_update_column_add_self Matrix.det_updateColumn_add_self
theorem det_updateRow_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateRow A i (A i + c β’ A j)) = det A := by
simp [det_updateRow_add, det_updateRow_smul,
det_zero_of_row_eq hij (updateRow_self.trans (updateRow_ne hij.symm).symm)]
#align matrix.det_update_row_add_smul_self Matrix.det_updateRow_add_smul_self
theorem det_updateColumn_add_smul_self (A : Matrix n n R) {i j : n} (hij : i β j) (c : R) :
det (updateColumn A i fun k => A k i + c β’ A k j) = det A := by
rw [β det_transpose, β updateRow_transpose, β det_transpose A]
exact det_updateRow_add_smul_self Aα΅ hij c
#align matrix.det_update_column_add_smul_self Matrix.det_updateColumn_add_smul_self
theorem det_eq_of_forall_row_eq_smul_add_const_aux {A B : Matrix n n R} {s : Finset n} :
β (c : n β R) (_ : β i, i β s β c i = 0) (k : n) (_ : k β s)
(_: β i j, A i j = B i j + c i * B k j), det A = det B := by
induction s using Finset.induction_on generalizing B with
| empty =>
rintro c hs k - A_eq
have : β i, c i = 0 := by
intro i
specialize hs i
contrapose! hs
simp [hs]
congr
ext i j
rw [A_eq, this, zero_mul, add_zero]
| @insert i s _hi ih =>
intro c hs k hk A_eq
have hAi : A i = B i + c i β’ B k := funext (A_eq i)
rw [@ih (updateRow B i (A i)) (Function.update c i 0), hAi, det_updateRow_add_smul_self]
Β· exact mt (fun h => show k β insert i s from h βΈ Finset.mem_insert_self _ _) hk
Β· intro i' hi'
rw [Function.update_apply]
split_ifs with hi'i
Β· rfl
Β· exact hs i' fun h => hi' ((Finset.mem_insert.mp h).resolve_left hi'i)
Β· exact k
Β· exact fun h => hk (Finset.mem_insert_of_mem h)
Β· intro i' j'
rw [updateRow_apply, Function.update_apply]
split_ifs with hi'i
Β· simp [hi'i]
rw [A_eq, updateRow_ne fun h : k = i => hk <| h βΈ Finset.mem_insert_self k s]
#align matrix.det_eq_of_forall_row_eq_smul_add_const_aux Matrix.det_eq_of_forall_row_eq_smul_add_const_aux
/-- If you add multiples of row `B k` to other rows, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_const {A B : Matrix n n R} (c : n β R) (k : n)
(hk : c k = 0) (A_eq : β i j, A i j = B i j + c i * B k j) : det A = det B :=
det_eq_of_forall_row_eq_smul_add_const_aux c
(fun i =>
not_imp_comm.mp fun hi =>
Finset.mem_erase.mpr
β¨mt (fun h : i = k => show c i = 0 from h.symm βΈ hk) hi, Finset.mem_univ iβ©)
k (Finset.not_mem_erase k Finset.univ) A_eq
#align matrix.det_eq_of_forall_row_eq_smul_add_const Matrix.det_eq_of_forall_row_eq_smul_add_const
theorem det_eq_of_forall_row_eq_smul_add_pred_aux {n : β} (k : Fin (n + 1)) :
β (c : Fin n β R) (_hc : β i : Fin n, k < i.succ β c i = 0)
{M N : Matrix (Fin n.succ) (Fin n.succ) R} (_h0 : β j, M 0 j = N 0 j)
(_hsucc : β (i : Fin n) (j), M i.succ j = N i.succ j + c i * M (Fin.castSucc i) j),
det M = det N := by
refine' Fin.induction _ (fun k ih => _) k <;> intro c hc M N h0 hsucc
Β· congr
ext i j
refine' Fin.cases (h0 j) (fun i => _) i
rw [hsucc, hc i (Fin.succ_pos _), zero_mul, add_zero]
set M' := updateRow M k.succ (N k.succ) with hM'
have hM : M = updateRow M' k.succ (M' k.succ + c k β’ M (Fin.castSucc k)) := by
ext i j
by_cases hi : i = k.succ
Β· simp [hi, hM', hsucc, updateRow_self]
rw [updateRow_ne hi, hM', updateRow_ne hi]
have k_ne_succ : (Fin.castSucc k) β k.succ := (Fin.castSucc_lt_succ k).ne
have M_k : M (Fin.castSucc k) = M' (Fin.castSucc k) := (updateRow_ne k_ne_succ).symm
rw [hM, M_k, det_updateRow_add_smul_self M' k_ne_succ.symm, ih (Function.update c k 0)]
Β· intro i hi
rw [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff] at hi
rw [Function.update_apply]
split_ifs with hik
Β· rfl
exact hc _ (Fin.succ_lt_succ_iff.mpr (lt_of_le_of_ne hi (Ne.symm hik)))
Β· rwa [hM', updateRow_ne (Fin.succ_ne_zero _).symm]
intro i j
rw [Function.update_apply]
split_ifs with hik
Β· rw [zero_mul, add_zero, hM', hik, updateRow_self]
rw [hM', updateRow_ne ((Fin.succ_injective _).ne hik), hsucc]
by_cases hik2 : k < i
Β· simp [hc i (Fin.succ_lt_succ_iff.mpr hik2)]
rw [updateRow_ne]
apply ne_of_lt
rwa [Fin.lt_iff_val_lt_val, Fin.coe_castSucc, Fin.val_succ, Nat.lt_succ_iff, β not_lt]
#align matrix.det_eq_of_forall_row_eq_smul_add_pred_aux Matrix.det_eq_of_forall_row_eq_smul_add_pred_aux
/-- If you add multiples of previous rows to the next row, the determinant doesn't change. -/
theorem det_eq_of_forall_row_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β j, A 0 j = B 0 j)
(A_succ : β (i : Fin n) (j), A i.succ j = B i.succ j + c i * A (Fin.castSucc i) j) :
det A = det B :=
det_eq_of_forall_row_eq_smul_add_pred_aux (Fin.last _) c
(fun _ hi => absurd hi (not_lt_of_ge (Fin.le_last _))) A_zero A_succ
#align matrix.det_eq_of_forall_row_eq_smul_add_pred Matrix.det_eq_of_forall_row_eq_smul_add_pred
/-- If you add multiples of previous columns to the next columns, the determinant doesn't change. -/
theorem det_eq_of_forall_col_eq_smul_add_pred {n : β} {A B : Matrix (Fin (n + 1)) (Fin (n + 1)) R}
(c : Fin n β R) (A_zero : β i, A i 0 = B i 0)
(A_succ : β (i) (j : Fin n), A i j.succ = B i j.succ + c j * A i (Fin.castSucc j)) :
det A = det B := by
rw [β det_transpose A, β det_transpose B]
exact det_eq_of_forall_row_eq_smul_add_pred c A_zero fun i j => A_succ j i
#align matrix.det_eq_of_forall_col_eq_smul_add_pred Matrix.det_eq_of_forall_col_eq_smul_add_pred
end DetEq
@[simp]
theorem det_blockDiagonal {o : Type*} [Fintype o] [DecidableEq o] (M : o β Matrix n n R) :
(blockDiagonal M).det = β k, (M k).det := by
-- Rewrite the determinants as a sum over permutations.
simp_rw [det_apply']
-- The right hand side is a product of sums, rewrite it as a sum of products.
rw [Finset.prod_sum]
simp_rw [Finset.prod_attach_univ, Finset.univ_pi_univ]
-- We claim that the only permutations contributing to the sum are those that
-- preserve their second component.
let preserving_snd : Finset (Equiv.Perm (n Γ o)) :=
Finset.univ.filter fun Ο => β x, (Ο x).snd = x.snd
have mem_preserving_snd :
β {Ο : Equiv.Perm (n Γ o)}, Ο β preserving_snd β β x, (Ο x).snd = x.snd := fun {Ο} =>
Finset.mem_filter.trans β¨fun h => h.2, fun h => β¨Finset.mem_univ _, hβ©β©
rw [β Finset.sum_subset (Finset.subset_univ preserving_snd) _]
-- And that these are in bijection with `o β Equiv.Perm m`.
rw [(Finset.sum_bij
(fun (Ο : β k : o, k β Finset.univ β Equiv.Perm n) _ =>
prodCongrLeft fun k => Ο k (Finset.mem_univ k))
_ _ _ _).symm]
Β· intro Ο _
rw [mem_preserving_snd]
rintro β¨-, xβ©
simp only [prodCongrLeft_apply]
Β· intro Ο _
rw [Finset.prod_mul_distrib, β Finset.univ_product_univ, Finset.prod_product_right]
simp only [sign_prodCongrLeft, Units.coe_prod, Int.cast_prod, blockDiagonal_apply_eq,
prodCongrLeft_apply]
Β· intro Ο Ο' _ _ eq
ext x hx k
simp only at eq
have :
β k x,
prodCongrLeft (fun k => Ο k (Finset.mem_univ _)) (k, x) =
prodCongrLeft (fun k => Ο' k (Finset.mem_univ _)) (k, x) :=
fun k x => by rw [eq]
simp only [prodCongrLeft_apply, Prod.mk.inj_iff] at this
exact (this k x).1
Β· intro Ο hΟ
rw [mem_preserving_snd] at hΟ
have hΟ' : β x, (Οβ»ΒΉ x).snd = x.snd := by
intro x
conv_rhs => rw [β Perm.apply_inv_self Ο x, hΟ]
have mk_apply_eq : β k x, ((Ο (x, k)).fst, k) = Ο (x, k) := by
intro k x
ext
Β· simp only
Β· simp only [hΟ]
have mk_inv_apply_eq : β k x, ((Οβ»ΒΉ (x, k)).fst, k) = Οβ»ΒΉ (x, k) := by
intro k x
conv_lhs => rw [β Perm.apply_inv_self Ο (x, k)]
ext
Β· simp only [apply_inv_self]
Β· simp only [hΟ']
refine' β¨fun k _ => β¨fun x => (Ο (x, k)).fst, fun x => (Οβ»ΒΉ (x, k)).fst, _, _β©, _, _β©
Β· intro x
simp only [mk_apply_eq, inv_apply_self]
Β· intro x
simp only [mk_inv_apply_eq, apply_inv_self]
Β· apply Finset.mem_univ
Β· ext β¨k, xβ©
Β· simp only [coe_fn_mk, prodCongrLeft_apply]
Β· simp only [prodCongrLeft_apply, hΟ]
Β· intro Ο _ hΟ
rw [mem_preserving_snd] at hΟ
obtain β¨β¨k, xβ©, hkxβ© := not_forall.mp hΟ
rw [Finset.prod_eq_zero (Finset.mem_univ (k, x)), mul_zero]
rw [blockDiagonal_apply_ne]
exact hkx
#align matrix.det_block_diagonal Matrix.det_blockDiagonal
/-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ]
rw [mul_mul_mul_comm]
| congr | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det := by
classical
simp_rw [det_apply']
convert Eq.symm <|
sum_subset (Ξ² := R) (subset_univ ((sumCongrHom m n).range : Set (Perm (Sum m n))).toFinset) ?_
rw [sum_mul_sum]
simp_rw [univ_product_univ]
rw [(sum_bij (fun (Ο : Perm m Γ Perm n) _ => Equiv.sumCongr Ο.fst Ο.snd) _ _ _ _).symm]
Β· intro Οββ h
simp only
erw [Set.mem_toFinset, MonoidHom.mem_range]
use Οββ
simp only [sumCongrHom_apply]
Β· simp only [forall_prop_of_true, Prod.forall, mem_univ]
intro Οβ Οβ
rw [Fintype.prod_sum_type]
simp_rw [Equiv.sumCongr_apply, Sum.map_inr, Sum.map_inl, fromBlocks_applyββ,
fromBlocks_applyββ]
rw [mul_mul_mul_comm]
| Mathlib.LinearAlgebra.Matrix.Determinant.643_0.U1f6HO8zRbnvZ95 | /-- The determinant of a 2Γ2 block matrix with the lower-left block equal to zero is the product of
the determinants of the diagonal blocks. For the generalization to any number of blocks, see
`Matrix.det_of_upper_triangular`. -/
@[simp]
theorem det_fromBlocks_zeroββ (A : Matrix m m R) (B : Matrix m n R) (D : Matrix n n R) :
(Matrix.fromBlocks A B 0 D).det = A.det * D.det | Mathlib_LinearAlgebra_Matrix_Determinant |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.