mathlib4 review helperopen PRs by @themathqueen on leanprover-community/mathlib4

← queue

#35249 feat(Analysis/Matrix): linear functional on matrices is positive iff its weight is positive semi-definite

WIP merge-conflict t-analysis draft CI failure CI failing merge conflict

+167 −60 · 3 files · 9 commits · head 658fffd · updated 271 days ago · Files changed on GitHub

In plain words

Adds a new file Analysis/Matrix/GNS.lean. It defines the weight of a linear functional on matrices: the matrix with . It then shows two things. First, is star-preserving iff is Hermitian. Second, is monotone and star-preserving iff is positive semidefinite; over , monotone alone is enough. So positive functionals on correspond to PSD matrices, which is the setting of the GNS inner product . The existing toMatrixInnerProductSpace constructions move unchanged from Analysis/Matrix/Order.lean into the new file.

Transcriptions are drafted with an LLM from the diff and are a reading aid, not a verified statement: the Lean code is the reference.

PR description

No comments yet, I'm still thinking of the best thing to do here.


![Open in Gitpod](gitpod.io/from-referrer/)

Conversation on GitHub 6
github-actions[bot] · comment · 2026-02-13
PR summary 32af1e5dd5
Import changes for modified files

No significant changes to the import graph

<details><summary>Import changes for all files</summary>

|Files|Import difference| |-|-| | Mathlib.Analysis.Matrix.GNS (new file)|2440|

</details>


<details><summary>

Declarations diff

</summary>

+ Module.Dual.eq_trace_mul_weight + Module.Dual.isSelfAdjoint_toConv_iff_isHermitian_weight + Module.Dual.monotone_and_isSelfAdjoint_toConv_iff_posSemidef_weight + Module.Dual.monotone_iff_posSemidef_weight + Module.Dual.weight + isHermitian_iff_star_dotProduct + posSemidef_iff_complex <details> <summary>You can run this locally as follows</summary>

## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

</details>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.

</details>


<details><summary>No changes to technical debt.</summary>

You can run this locally as

./scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

</details>

mathlib-merge-conflicts[bot] · comment · 2026-02-14

This pull request has conflicts, please merge master and resolve them.

mathlib-merge-conflicts[bot] · comment · 2026-02-15

This pull request has conflicts, please merge master and resolve them.

mathlib-merge-conflicts[bot] · comment · 2026-02-18

This pull request has conflicts, please merge master and resolve them.

mathlib-merge-conflicts[bot] · comment · 2026-03-03

This pull request has conflicts, please merge master and resolve them.

mathlib-merge-conflicts[bot] · comment · 2026-03-10

This pull request has conflicts, please merge master and resolve them.

Mathlib.lean +1 −0

public import Mathlib.Analysis.LocallyConvex.WeakOperatorTopology
1934public import Mathlib.Analysis.LocallyConvex.WeakSpace
1935public import Mathlib.Analysis.LocallyConvex.WithSeminorms
1936public import Mathlib.Analysis.Matrix
1937public import Mathlib.Analysis.Matrix.GNS
1938public import Mathlib.Analysis.Matrix.Hermitian
1939public import Mathlib.Analysis.Matrix.HermitianFunctionalCalculus
1940public import Mathlib.Analysis.Matrix.LDL

Mathlib/Analysis/Matrix/GNS.lean +166 −0 new file

1/-
2Copyright (c) 2025 Monica Omar. All rights reserved.
3Released under Apache 2.0 license as described in the file LICENSE.
4Authors: Monica Omar
5-/
6module
7
8public import Mathlib.Algebra.Star.LinearMap
9public import Mathlib.Analysis.InnerProductSpace.Positive
10public import Mathlib.Analysis.Matrix.Order
12/-! The GNS-inner product on matrices
14This file shows that the GNS-inner product space on matrices is equivalent
15to `Matrix.toMatrixInnerProductSpace`.
16So choosing a positive linear map `Matrix n n ℂ →ₚ[ℂ] ℂ` is equivalent to choosing
17a positive semi-definite matrix `Q`. -/
19@[expose] public section
21variable {n m R 𝕜 : Type*} [Fintype n] [DecidableEq n] [RCLike 𝕜]
23open scoped MatrixOrder ComplexOrder
25namespace Matrix
27set_option backward.isDefEq.respectTransparency false in
28set_option backward.privateInPublic true in
abbrev PosSemidef.matrixPreInnerProductSpace newL29–40
29/-- The pre-inner product space structure implementation. Only an auxiliary for
30`Matrix.toMatrixSeminormedAddCommGroup`, `Matrix.toMatrixNormedAddCommGroup`,
31and `Matrix.toMatrixInnerProductSpace`. -/
32private abbrev PosSemidef.matrixPreInnerProductSpace {M : Matrix n n 𝕜} (hM : M.PosSemidef) :
33 PreInnerProductSpace.Core 𝕜 (Matrix n n 𝕜) where
34 inner x y := (y * M * xᴴ).trace
35 conj_inner_symm _ _ := by
36 simp only [mul_assoc, starRingEnd_apply, ← trace_conjTranspose, conjTranspose_mul,
37 conjTranspose_conjTranspose, hM.isHermitian.eq]
38 re_inner_nonneg x := RCLike.nonneg_iff.mp (hM.mul_mul_conjTranspose_same x).trace_nonneg |>.1
39 add_left := by simp [mul_add]
40 smul_left := by simp
42set_option backward.isDefEq.respectTransparency false in
43set_option backward.privateInPublic true in
44set_option backward.privateInPublic.warn false in
def toMatrixSeminormedAddCommGroup newL45–49
45/-- A positive definite matrix `M` induces a norm on `Matrix n n 𝕜`
46`‖x‖ = sqrt (x * M * xᴴ).trace`. -/
47noncomputable def toMatrixSeminormedAddCommGroup (M : Matrix n n 𝕜) (hM : M.PosSemidef) :
48 SeminormedAddCommGroup (Matrix n n 𝕜) :=
49 @InnerProductSpace.Core.toSeminormedAddCommGroup _ _ _ _ _ hM.matrixPreInnerProductSpace
51set_option backward.isDefEq.respectTransparency false in
52set_option backward.privateInPublic true in
53set_option backward.privateInPublic.warn false in
def toMatrixNormedAddCommGroup newL54–69
54/-- A positive definite matrix `M` induces a norm on `Matrix n n 𝕜`:
55`‖x‖ = sqrt (x * M * xᴴ).trace`. -/
56noncomputable def toMatrixNormedAddCommGroup (M : Matrix n n 𝕜) (hM : M.PosDef) :
57 NormedAddCommGroup (Matrix n n 𝕜) :=
58 letI : InnerProductSpace.Core 𝕜 (Matrix n n 𝕜) :=
59 { __ := hM.posSemidef.matrixPreInnerProductSpace
60 definite x hx := by
61 classical
62 obtain ⟨y, hy, rfl⟩ := CStarAlgebra.isStrictlyPositive_iff_eq_star_mul_self.mp
63 hM.isStrictlyPositive
64 simp +instances only at hx
65 rw [← mul_assoc, ← conjTranspose_conjTranspose x, star_eq_conjTranspose, ← conjTranspose_mul,
66 conjTranspose_conjTranspose, mul_assoc, trace_conjTranspose_mul_self_eq_zero_iff] at hx
67 lift y to (Matrix n n 𝕜)ˣ using hy
68 simpa [← mul_assoc] using congr(y⁻¹ * $hx) }
69 this.toNormedAddCommGroup
71set_option backward.isDefEq.respectTransparency false in
def toMatrixInnerProductSpace newL72–77
72/-- A positive semi-definite matrix `M` induces an inner product on `Matrix n n 𝕜`:
73`⟪x, y⟫ = (y * M * xᴴ).trace`. -/
74def toMatrixInnerProductSpace (M : Matrix n n 𝕜) (hM : M.PosSemidef) :
75 letI : SeminormedAddCommGroup (Matrix n n 𝕜) := M.toMatrixSeminormedAddCommGroup hM
76 InnerProductSpace 𝕜 (Matrix n n 𝕜) :=
77 InnerProductSpace.ofCore _
79@[deprecated (since := "2025-11-18")] alias PosDef.matrixNormedAddCommGroup :=
80 toMatrixNormedAddCommGroup
81@[deprecated (since := "2025-11-12")] alias PosDef.matrixInnerProductSpace :=
82 toMatrixInnerProductSpace
84open Matrix
def Module.Dual.weight newL86–89
86/-- The *weight* of a linear functional `f` on matrices is the matrix such that
87`f a = trace (a * f.weight)` for all `a`. -/
88def Module.Dual.weight [Semiring R] (f : Module.Dual R (Matrix n n R)) :
89 Matrix n n R := ∑ i, ∑ j, .single j i (f (.single i j 1))
theorem Module.Dual.eq_trace_mul_weight newL91–97
91theorem Module.Dual.eq_trace_mul_weight [Semiring R] (f : Module.Dual R (Matrix n n R))
92 (a : Matrix n n R) : f a = (a * f.weight).trace := by
93 conv_lhs => rw [matrix_eq_sum_single a]
94 simp_rw [fun i j ↦ show single i j (a i j) = a i j • single i j 1 by simp,
95 map_sum, map_smul]
96 simp [weight, Finset.mul_sum, trace, sum_apply, mul_apply,
97 Matrix.single_apply, ite_and]
theorem Module.Dual.isSelfAdjoint_toConv_iff_isHermitian_weight newL99–105
99/-- `f` is star-preserving iff its weight is Hermitian. -/
100theorem Module.Dual.isSelfAdjoint_toConv_iff_isHermitian_weight
101 [CommSemiring R] [StarRing R] (f : Module.Dual R (Matrix n n R)) :
102 IsSelfAdjoint (WithConv.toConv f) ↔ f.weight.IsHermitian := by
103 simp only [IsSelfAdjoint, WithConv.ext_iff, LinearMap.ext_iff, LinearMap.intrinsicStar_apply]
104 simp_rw [eq_trace_mul_weight, star_eq_conjTranspose, ← trace_conjTranspose, conjTranspose_mul,
105 conjTranspose_conjTranspose, trace_mul_comm _ f.weight, ← ext_iff_trace_mul_right, IsHermitian]
107open scoped ComplexOrder MatrixOrder
109-- `f` is a positive and star-preserving iff `f.weight` is positive semi-definite
110-- when `R = ℂ`, we can drop `toConv f` is self-adjoint (but a lot of missing API ugh)
theorem Module.Dual.monotone_and_isSelfAdjoint_toConv_iff_posSemidef_weight newL111–129
111theorem Module.Dual.monotone_and_isSelfAdjoint_toConv_iff_posSemidef_weight
112 {𝕜 : Type*} [RCLike 𝕜] (f : Module.Dual 𝕜 (Matrix n n 𝕜)) :
113 Monotone f ∧ IsSelfAdjoint (WithConv.toConv f) ↔ f.weight.PosSemidef := by
114 rw [posSemidef_iff_dotProduct_mulVec, isSelfAdjoint_toConv_iff_isHermitian_weight,
115 and_comm, and_congr_right_iff]
116 intro h
117 simp_rw [fun y ↦ show star y ⬝ᵥ f.weight *ᵥ y = (f.weight * vecMulVec y (star y)).trace by
118 rw [vecMulVec_eq Unit, trace_mul_cycle', ← replicateCol_mulVec, trace_mul_comm,
119 trace_replicateCol_mul_replicateRow, dotProduct_comm]]
120 refine ⟨fun hs ↦ ?_, fun H x y ↦ ?_⟩
121 · simp only [Monotone, eq_trace_mul_weight] at hs
122 intro y
123 simpa [trace_mul_comm f.weight] using hs (posSemidef_vecMulVec_self_star y).nonneg
124 · rw [← sub_nonneg, ← sub_nonneg (b := f x), ← map_sub, eq_trace_mul_weight,
125 trace_mul_comm]
126 intro h
127 obtain ⟨m, e, he⟩ := posSemidef_iff_eq_sum_vecMulVec.mp h.posSemidef
128 simp [he, Matrix.mul_sum]
129 exact Finset.sum_nonneg fun _ _ ↦ H _
theorem isHermitian_iff_star_dotProduct newL131–138
131theorem isHermitian_iff_star_dotProduct {n} [Fintype n]
132 {A : Matrix n n ℂ} :
133 A.IsHermitian ↔ ∀ x, star (star x ⬝ᵥ A *ᵥ x) = star x ⬝ᵥ A *ᵥ x := by
134 conv_lhs => rw [← conjTranspose_conjTranspose A, isHermitian_conjTranspose_iff]
135 classical
136 simp [isHermitian_iff_isSymmetric, LinearMap.isSymmetric_iff_inner_map_self_real,
137 EuclideanSpace.inner_eq_star_dotProduct, star_mulVec, dotProduct_comm _ (_ ᵥ* _),
138 ← dotProduct_mulVec, (WithLp.toLp_surjective _).forall]
theorem posSemidef_iff_complex newL140–143
140theorem posSemidef_iff_complex {n} [Fintype n] (x : Matrix n n ℂ) :
141 x.PosSemidef ↔ ∀ (y : n → ℂ), 0 ≤ star y ⬝ᵥ x.mulVec y := by
142 rw [posSemidef_iff_dotProduct_mulVec, isHermitian_iff_star_dotProduct]
143 simp_all [Complex.nonneg_iff, eq_comm (a := (0 : ℝ)), ← Complex.conj_eq_iff_im]
theorem Module.Dual.monotone_iff_posSemidef_weight newL145–164
145theorem Module.Dual.monotone_iff_posSemidef_weight (f : Module.Dual ℂ (Matrix n n ℂ)) :
146 Monotone f ↔ f.weight.PosSemidef := by
147 -- rw [← monotone_and_isSelfAdjoint_toConv_iff_posSemidef_weight, iff_self_and]
148 -- intro hf
149 -- rw [isSelfAdjoint_toConv_iff_isHermitian_weight]
150 rw [posSemidef_iff_complex]
151 -- literally the same exact code as above...
152 simp_rw [fun y ↦ show star y ⬝ᵥ f.weight *ᵥ y = (f.weight * vecMulVec y (star y)).trace by
153 rw [vecMulVec_eq Unit, trace_mul_cycle', ← replicateCol_mulVec, trace_mul_comm,
154 trace_replicateCol_mul_replicateRow, dotProduct_comm]]
155 refine ⟨fun hs ↦ ?_, fun H x y ↦ ?_⟩
156 · simp only [Monotone, eq_trace_mul_weight] at hs
157 intro y
158 simpa [trace_mul_comm f.weight] using hs (posSemidef_vecMulVec_self_star y).nonneg
159 · rw [← sub_nonneg, ← sub_nonneg (b := f x), ← map_sub, eq_trace_mul_weight,
160 trace_mul_comm]
161 intro h
162 obtain ⟨m, e, he⟩ := posSemidef_iff_eq_sum_vecMulVec.mp h.posSemidef
163 simp [he, Matrix.mul_sum]
164 exact Finset.sum_nonneg fun _ _ ↦ H _
166end Matrix

Mathlib/Analysis/Matrix/Order.lean +0 −60

This allows us to use more general results from C⋆-algebras, like `CFC.sqrt`.
23* `Matrix.instPartialOrder`: the partial order on matrices given by `x ≤ y := (y - x).PosSemidef`.
24* `Matrix.PosSemidef.dotProduct_mulVec_zero_iff`: for a positive semi-definite matrix `A`,
25 we have `x⋆ A x = 0` iff `A x = 0`.
−* `Matrix.toMatrixInnerProductSpace`: the inner product on matrices induced by a
− positive semi-definite matrix `M`: `⟪x, y⟫ = (y * M * xᴴ).trace`.
27## Implementation notes
def tracePositiveLinearMap : Matrix n n 𝕜 →ₚ[α] 𝕜 :=
335@[simp] lemma tracePositiveLinearMap_apply (x) : tracePositiveLinearMap n α 𝕜 x = trace x := rfl
337end tracePositiveLinearMap
−
−set_option backward.isDefEq.respectTransparency false in
−set_option backward.privateInPublic true in
abbrev PosSemidef.matrixPreInnerProductSpace removedLundefined–undefined
−/-- The pre-inner product space structure implementation. Only an auxiliary for
−`Matrix.toMatrixSeminormedAddCommGroup`, `Matrix.toMatrixNormedAddCommGroup`,
−and `Matrix.toMatrixInnerProductSpace`. -/
−private abbrev PosSemidef.matrixPreInnerProductSpace {M : Matrix n n 𝕜} (hM : M.PosSemidef) :
− PreInnerProductSpace.Core 𝕜 (Matrix n n 𝕜) where
− inner x y := (y * M * xᴴ).trace
− conj_inner_symm _ _ := by
− simp only [mul_assoc, starRingEnd_apply, ← trace_conjTranspose, conjTranspose_mul,
− conjTranspose_conjTranspose, hM.isHermitian.eq]
− re_inner_nonneg x := RCLike.nonneg_iff.mp (hM.mul_mul_conjTranspose_same x).trace_nonneg |>.1
− add_left := by simp [mul_add]
− smul_left := by simp
−set_option backward.isDefEq.respectTransparency false in
−set_option backward.privateInPublic true in
−set_option backward.privateInPublic.warn false in
def toMatrixSeminormedAddCommGroup removedLundefined–undefined
−/-- A positive definite matrix `M` induces a norm on `Matrix n n 𝕜`
−`‖x‖ = sqrt (x * M * xᴴ).trace`. -/
−noncomputable def toMatrixSeminormedAddCommGroup (M : Matrix n n 𝕜) (hM : M.PosSemidef) :
− SeminormedAddCommGroup (Matrix n n 𝕜) :=
− @InnerProductSpace.Core.toSeminormedAddCommGroup _ _ _ _ _ hM.matrixPreInnerProductSpace
−set_option backward.isDefEq.respectTransparency false in
−set_option backward.privateInPublic true in
−set_option backward.privateInPublic.warn false in
def toMatrixNormedAddCommGroup removedLundefined–undefined
−/-- A positive definite matrix `M` induces a norm on `Matrix n n 𝕜`:
−`‖x‖ = sqrt (x * M * xᴴ).trace`. -/
−noncomputable def toMatrixNormedAddCommGroup (M : Matrix n n 𝕜) (hM : M.PosDef) :
− NormedAddCommGroup (Matrix n n 𝕜) :=
− letI : InnerProductSpace.Core 𝕜 (Matrix n n 𝕜) :=
− { __ := hM.posSemidef.matrixPreInnerProductSpace
− definite x hx := by
− classical
− obtain ⟨y, hy, rfl⟩ := CStarAlgebra.isStrictlyPositive_iff_eq_star_mul_self.mp
− hM.isStrictlyPositive
− simp +instances only at hx
− rw [← mul_assoc, ← conjTranspose_conjTranspose x, star_eq_conjTranspose, ← conjTranspose_mul,
− conjTranspose_conjTranspose, mul_assoc, trace_conjTranspose_mul_self_eq_zero_iff] at hx
− lift y to (Matrix n n 𝕜)ˣ using hy
− simpa [← mul_assoc] using congr(y⁻¹ * $hx) }
− this.toNormedAddCommGroup
−set_option backward.isDefEq.respectTransparency false in
def toMatrixInnerProductSpace removedLundefined–undefined
−/-- A positive semi-definite matrix `M` induces an inner product on `Matrix n n 𝕜`:
−`⟪x, y⟫ = (y * M * xᴴ).trace`. -/
−def toMatrixInnerProductSpace (M : Matrix n n 𝕜) (hM : M.PosSemidef) :
− letI : SeminormedAddCommGroup (Matrix n n 𝕜) := M.toMatrixSeminormedAddCommGroup hM
− InnerProductSpace 𝕜 (Matrix n n 𝕜) :=
− InnerProductSpace.ofCore _
−@[deprecated (since := "2025-11-18")] alias PosDef.matrixNormedAddCommGroup :=
− toMatrixNormedAddCommGroup
−@[deprecated (since := "2025-11-12")] alias PosDef.matrixInnerProductSpace :=
− toMatrixInnerProductSpace
−
338end Matrix
0/15 declarations checked

Your review, as Markdown

Paste it as a comment or a review on the PR. Line links to the head commit unfold into code snippets on GitHub.