import Mathlib.LinearAlgebra.Matrix.Rank open scoped BigOperators namespace TilesRank open Matrix Module /-- The all-ones matrix. -/ noncomputable def allOnes (n : ℕ) : Matrix (Fin n) (Fin n) ℚ := fun _ _ => 1 /-- The matrix $J-I$, i.e. $1$ off-diagonal and $0$ on the diagonal. -/ noncomputable def offDiagOnes (n : ℕ) : Matrix (Fin n) (Fin n) ℚ := allOnes n - 1 lemma allOnes_mulVec {n : ℕ} (v : Fin n → ℚ) : allOnes n *ᵥ v = fun _ : Fin n => ∑ j : Fin n, v j := by classical ext i -- For the all-ones matrix, every row is the constant-`1` vector. simpa [allOnes, Matrix.mulVec] using (one_dotProduct (v := v) : (1 : Fin n → ℚ) ⬝ᵥ v = _) /-- For $n \neq 1$, the linear map given by multiplying by $J-I$ has trivial kernel. -/ theorem ker_offDiagOnes_mulVecLin_eq_bot {n : ℕ} (hn : n ≠ 1) : LinearMap.ker (offDiagOnes n).mulVecLin = ⊥ := by classical -- Use the characterization `ker = ⊥ ↔ ∀ v, f v = 0 → v = 0`. refine LinearMap.ker_eq_bot'.mpr ?_ intro v hv -- Let `s` be the sum of the coordinates of `v`. let s : ℚ := ∑ j : Fin n, v j have hvi : ∀ i : Fin n, v i = s := by intro i have hi : ((offDiagOnes n) *ᵥ v) i = 0 := by simpa [Matrix.mulVecLin_apply] using congrFun hv i have hi' : s - v i = 0 := by -- Expand `(J - I) * v = J*v - v`. simpa [offDiagOnes, Matrix.sub_mulVec, allOnes_mulVec, Matrix.one_mulVec, s] using hi exact (sub_eq_zero.mp hi').symm have hs : s = (n : ℚ) * s := by -- Rewrite the sum defining `s` using `hvi`. have hsum : (∑ j : Fin n, v j) = ∑ _j : Fin n, s := by -- unfold the `Fintype` sum to `Finset.univ.sum` show (Finset.univ.sum fun j : Fin n => v j) = Finset.univ.sum (fun _j : Fin n => s) refine Finset.sum_congr rfl ?_ intro j hj simpa using hvi j -- Now compute the constant sum. have hconst : (∑ _j : Fin n, s) = (n : ℚ) * s := by -- `∑ _j, s = (Fintype.card (Fin n)) • s = n • s = (n:ℚ) * s`. calc (∑ _j : Fin n, s) = (Fintype.card (Fin n) : ℕ) • s := by simpa using (Finset.sum_const (a := s) (s := (Finset.univ : Finset (Fin n)))) _ = (n : ℚ) * s := by simp [Fintype.card_fin, nsmul_eq_mul] -- Combine. simpa [s] using hsum.trans hconst have hs0 : s = 0 := by have hs' : (n : ℚ) * s - s = 0 := by simpa using congrArg (fun t => (n : ℚ) * s - t) hs have hs'' : ((n : ℚ) - 1) * s = 0 := by simpa [sub_mul, one_mul] using hs' have hn' : ((n : ℚ) - 1) ≠ 0 := by have : (n : ℚ) ≠ (1 : ℚ) := by exact_mod_cast hn exact sub_ne_zero.mpr this exact (mul_eq_zero.mp hs'').resolve_left hn' ext i simp [hvi i, hs0] /-- For $n \neq 1$, the rank of the matrix $J-I$ is $n$. -/ theorem rank_offDiagOnes {n : ℕ} (hn : n ≠ 1) : (offDiagOnes n).rank = n := by classical have hinj : Function.Injective (offDiagOnes n).mulVecLin := by -- `ker = ⊥` implies injective. exact LinearMap.ker_eq_bot.mp (ker_offDiagOnes_mulVecLin_eq_bot (n := n) hn) -- rank is the `finrank` of the range simpa [Matrix.rank, LinearMap.finrank_range_of_inj hinj, finrank_pi, Fintype.card_fin] end TilesRank