/-Copyright (c) 2026 Carles MarΓn. All rights reserved.Released under Apache 2.0 license as described in the file LICENSE.Authors: Carles MarΓn-/module public import Mathlib.LinearAlgebra.Matrix.Rankpublic import Mathlib.LinearAlgebra.Matrix.Kroneckerpublic import Mathlib.LinearAlgebra.TensorProduct.Matrixpublic import Mathlib.RingTheory.Flat.Basicpublic import Mathlib.LinearAlgebra.Dimension.Constructionspublic import Mathlib.Data.Fin.Tuple.Basic /-!# Rank of Kronecker products ## Main results * `LinearMap.finrank_range_tensorProductMap`: over a field, `dim range (f β g) = dim range f Β· dim range g`.* `Matrix.rank_kronecker`: `(A ββ B).rank = A.rank * B.rank` over a field.* `Matrix.rank_piKronecker`: the `k`-th Kronecker power `piKronecker k A` (indexed by `k`-tuples) has rank `A.rank ^ k`.* `Matrix.rank_kronecker_one`: `(A ββ (1 : Matrix p p K)).rank = Fintype.card p * A.rank`. The general statement goes through linear maps: with standard bases `A ββ B` is the matrix of`TensorProduct.map (toLin A) (toLin B)` (`Matrix.toLin_kronecker`), and `map f g` factors as the surjection`map f.rangeRestrict g.rangeRestrict` followed by the inclusion `range f β range g β P β Q`, which is injectivebecause vector spaces are flat.-/ @[expose] public section open Moduleopen scoped Kronecker namespace LinearMap variable {K : Type*} [Field K] /-- Over a field, the image of `f β g` has dimension `dim range f Β· dim range g`. -/theorem finrank_range_tensorProductMap {M N P Q : Type*} [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [AddCommGroup Q] [Module K M] [Module K N] [Module K P] [Module K Q] [FiniteDimensional K M] [FiniteDimensional K N] (f : M ββ[K] P) (g : N ββ[K] Q) : finrank K (range (TensorProduct.map f g)) = finrank K (range f) * finrank K (range g) := by have hcomp : TensorProduct.map f g = (TensorProduct.map (range f).subtype (range g).subtype).comp (TensorProduct.map f.rangeRestrict g.rangeRestrict) := by rw [β TensorProduct.map_comp] rfl have hsurj : Function.Surjective (TensorProduct.map f.rangeRestrict g.rangeRestrict) := TensorProduct.map_surjective f.surjective_rangeRestrict g.surjective_rangeRestrict have hinj : Function.Injective (TensorProduct.map (range f).subtype (range g).subtype) := TensorProduct.map_injective_of_flat_flat _ _ Subtype.val_injective Subtype.val_injective rw [hcomp, range_comp_of_range_eq_top _ (range_eq_top.2 hsurj), finrank_range_of_inj hinj, Module.finrank_tensorProduct] end LinearMap namespace Matrix /-- **The rank of a Kronecker product is the product of the ranks** (over a field). -/theorem rank_kronecker {m n p q K : Type*} [Field K] [Fintype m] [Fintype n] [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q] (A : Matrix m n K) (B : Matrix p q K) : (A ββ B).rank = A.rank * B.rank := by rw [rank_eq_finrank_range_toLin (A ββ B) ((Pi.basisFun K m).tensorProduct (Pi.basisFun K p)) ((Pi.basisFun K n).tensorProduct (Pi.basisFun K q)), show A ββ B = kroneckerMap (Β· * Β·) A B from rfl, toLin_kronecker, LinearMap.finrank_range_tensorProductMap, β rank_eq_finrank_range_toLin A (Pi.basisFun K m) (Pi.basisFun K n), β rank_eq_finrank_range_toLin B (Pi.basisFun K p) (Pi.basisFun K q)] variable {m n p K : Type*} [Field K] section Kronecker variable [Fintype n] [Fintype p] [DecidableEq p] private theorem kronecker_one_mulVec_apply (A : Matrix m n K) (v : n Γ p β K) (i : m) (k : p) : ((A ββ (1 : Matrix p p K)) *α΅₯ v) (i, k) = (A *α΅₯ fun j => v (j, k)) i := by simp [mulVec, dotProduct, kroneckerMap_apply, one_apply, Fintype.sum_prod_type] /-- `ker (A ββ 1) β (p β ker A)`. -/private noncomputable def kerKroneckerOneEquiv (A : Matrix m n K) : LinearMap.ker (A ββ (1 : Matrix p p K)).mulVecLin ββ[K] (p β LinearMap.ker A.mulVecLin) where toFun v k := β¨fun j => (v : n Γ p β K) (j, k), by rw [LinearMap.mem_ker, mulVecLin_apply] funext i have hv : (A ββ (1 : Matrix p p K)) *α΅₯ (v : n Γ p β K) = 0 := v.2 rw [β kronecker_one_mulVec_apply, hv] rflβ© invFun w := β¨fun q => (w q.2 : n β K) q.1, by rw [LinearMap.mem_ker, mulVecLin_apply] funext β¨i, kβ© rw [kronecker_one_mulVec_apply] exact congrFun (w k).2 iβ© map_add' _ _ := rfl map_smul' _ _ := rfl left_inv _ := rfl right_inv _ := rfl /-- The rank of `A ββ 1` is `Fintype.card p` times the rank of `A`. -/theorem rank_kronecker_one (A : Matrix m n K) : (A ββ (1 : Matrix p p K)).rank = Fintype.card p * A.rank := by have hk := (kerKroneckerOneEquiv (p := p) A).finrank_eq rw [Module.finrank_pi_fintype, Finset.sum_const, Finset.card_univ, smul_eq_mul] at hk have h1 : (A ββ (1 : Matrix p p K)).rank + finrank K (LinearMap.ker (A ββ (1 : Matrix p p K)).mulVecLin) = Fintype.card (n Γ p) := by rw [rank, LinearMap.finrank_range_add_finrank_ker, Module.finrank_fintype_fun_eq_card] have h2 : A.rank + finrank K (LinearMap.ker A.mulVecLin) = Fintype.card n := by rw [rank, LinearMap.finrank_range_add_finrank_ker, Module.finrank_fintype_fun_eq_card] rw [Fintype.card_prod, hk] at h1 have h3 : Fintype.card p * (A.rank + finrank K (LinearMap.ker A.mulVecLin)) = Fintype.card p * Fintype.card n := by rw [h2] rw [Nat.mul_add] at h3 rw [Nat.mul_comm (Fintype.card n)] at h1 omega end Kronecker section PiKronecker /-- The `k`-th Kronecker power of `A`, indexed by tuples: entry `β i, A (r i) (c i)`. -/def piKronecker {Ξ± Ξ² R : Type*} [CommMonoid R] (k : β) (A : Matrix Ξ± Ξ² R) : Matrix (Fin k β Ξ±) (Fin k β Ξ²) R := fun r c => β i, A (r i) (c i) theorem piKronecker_succ {Ξ± Ξ² R : Type*} [CommMonoid R] (k : β) (A : Matrix Ξ± Ξ² R) : piKronecker (k + 1) A = (A ββ piKronecker k A).submatrix (Fin.consEquiv fun _ => Ξ±).symm (Fin.consEquiv fun _ => Ξ²).symm := by ext r c simp [piKronecker, Fin.prod_univ_succ, kroneckerMap_apply, Fin.consEquiv, Fin.tail] theorem rank_piKronecker {Ξ± Ξ² K : Type*} [Field K] [Fintype Ξ±] [Fintype Ξ²] [DecidableEq Ξ±] [DecidableEq Ξ²] (k : β) (A : Matrix Ξ± Ξ² K) : (piKronecker k A).rank = A.rank ^ k := by induction k with | zero => have h1 : piKronecker 0 A = (1 : Matrix (Fin 0 β Ξ±) (Fin 0 β Ξ±) K).submatrix (Equiv.refl _) (Equiv.ofUnique (Fin 0 β Ξ²) (Fin 0 β Ξ±)) := by ext r c rw [submatrix_apply, Subsingleton.elim ((Equiv.ofUnique (Fin 0 β Ξ²) (Fin 0 β Ξ±)) c) ((Equiv.refl _) r), one_apply_eq] simp [piKronecker] rw [h1, rank_submatrix _ (Equiv.refl _), rank_one] simp | succ k ih => rw [piKronecker_succ, rank_submatrix, rank_kronecker, ih, pow_succ, mul_comm] end PiKronecker end Matrix