Theorem (Matrix.PosSemidef.exists_eq_mul_diagonal_mul_conjTranspose). Simultaneous congruence normal form for a positive semidefinite pair. For real or complex positive semidefinite \mathbf{A},\mathbf{B} there is an invertible \mathbf{S} with \mathbf{A}=\mathbf{S}\operatorname{diag}(\mathbf{a})\mathbf{S}^{\mathsf{H}},\qquad \mathbf{B}=\mathbf{S}\operatorname{diag}(\mathbf{b})\mathbf{S}^{\mathsf{H}}\,. Each a_i is zero or one. Each \mathbf{b}_i is nonnegative and is zero or one when a_i=0. The indices with a_i=1 number \operatorname{rank}(\mathbf{A}). Together with the indices where (a_i,\mathbf{b}_i)=(0,1) they number \operatorname{rank}(\mathbf{A}+\mathbf{B}). The remaining indices number \dim(\ker\mathbf{A}\cap\ker\mathbf{B}). Thus reordering the three index sets gives the identity, zero, and common-kernel blocks, including empty blocks. [HornJohnson2013, §7.6], with the ranks and common nullity stated explicitly.
Headline General form: sizes are index types
Lean statement and details
Matrix.PosSemidef.exists_eq_mul_diagonal_mul_conjTranspose.{u_1, u_2} {𝕜 : Type u_1} [RCLike 𝕜]
{n : Type u_2} [Fintype n] [DecidableEq n] {A B : Matrix n n 𝕜} (hA : A.PosSemidef)
(hB : B.PosSemidef) :
∃ S a b,
IsUnit S.det ∧
A = (S * diagonal fun i ↦ ↑(a i)) * Sᴴ ∧
B = (S * diagonal fun i ↦ ↑(b i)) * Sᴴ ∧
(∀ (i : n), a i = 0 ∨ a i = 1) ∧
(∀ (i : n), 0 ≤ b i) ∧
(∀ (i : n), a i = 0 → b i = 0 ∨ b i = 1) ∧
A.rank = Fintype.card { i // a i = 1 } ∧
(A + B).rank =
Fintype.card { i // a i = 1 } + Fintype.card { i // a i = 0 ∧ b i = 1 } ∧
Module.finrank 𝕜 ↥(A.mulVecLin.ker ⊓ B.mulVecLin.ker) =
Fintype.card { i // a i = 0 ∧ b i = 0 }