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