Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 2568d41

Browse files
committed
feat(data/matrix/basic): Add bundled versions of matrix.diagonal (#8510)
Also shows injectivity of `diagonal`.
1 parent 77d6c8e commit 2568d41

File tree

1 file changed

+60
-5
lines changed

1 file changed

+60
-5
lines changed

src/data/matrix/basic.lean

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,16 @@ section diagonal
154154
variables [decidable_eq n]
155155

156156
/-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0`
157-
if `i ≠ j`. -/
158-
def diagonal [has_zero α] (d : n → α) : matrix n n α := λ i j, if i = j then d i else 0
157+
if `i ≠ j`.
158+
159+
Note that bundled versions exist as:
160+
* `matrix.diagonal_add_monoid_hom`
161+
* `matrix.diagonal_linear_map`
162+
* `matrix.diagonal_ring_hom`
163+
* `matrix.diagonal_alg_hom`
164+
-/
165+
def diagonal [has_zero α] (d : n → α) : matrix n n α
166+
| i j := if i = j then d i else 0
159167

160168
@[simp] theorem diagonal_apply_eq [has_zero α] {d : n → α} (i : n) : (diagonal d) i i = d i :=
161169
by simp [diagonal]
@@ -166,8 +174,11 @@ by simp [diagonal]
166174
theorem diagonal_apply_ne' [has_zero α] {d : n → α} {i j : n} (h : j ≠ i) :
167175
(diagonal d) i j = 0 := diagonal_apply_ne h.symm
168176

177+
lemma diagonal_injective [has_zero α] : function.injective (diagonal : (n → α) → matrix n n α) :=
178+
λ d₁ d₂ h, funext $ λ i, by simpa using matrix.ext_iff.mpr h i i
179+
169180
@[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 :=
170-
by simp [diagonal]; refl
181+
by { ext, simp [diagonal] }
171182

172183
@[simp] lemma diagonal_transpose [has_zero α] (v : n → α) :
173184
(diagonal v)ᵀ = diagonal v :=
@@ -178,10 +189,35 @@ begin
178189
{ simp [h, transpose, diagonal_apply_ne' h] }
179190
end
180191

181-
@[simp] theorem diagonal_add [add_monoid α] (d₁ d₂ : n → α) :
192+
@[simp] theorem diagonal_add [add_zero_class α] (d₁ d₂ : n → α) :
182193
diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) :=
183194
by ext i j; by_cases h : i = j; simp [h]
184195

196+
@[simp] theorem diagonal_smul [monoid R] [add_monoid α] [distrib_mul_action R α] (r : R)
197+
(d : n → α) :
198+
diagonal (r • d) = r • diagonal d :=
199+
by ext i j; by_cases h : i = j; simp [h]
200+
201+
variables (n α)
202+
203+
/-- `matrix.diagonal` as an `add_monoid_hom`. -/
204+
@[simps]
205+
def diagonal_add_monoid_hom [add_zero_class α] : (n → α) →+ matrix n n α :=
206+
{ to_fun := diagonal,
207+
map_zero' := diagonal_zero,
208+
map_add' := λ x y, (diagonal_add x y).symm,}
209+
210+
variables (R)
211+
212+
/-- `matrix.diagonal` as a `linear_map`. -/
213+
@[simps]
214+
def diagonal_linear_map [semiring R] [add_comm_monoid α] [module R α] :
215+
(n → α) →ₗ[R] matrix n n α :=
216+
{ map_smul' := diagonal_smul,
217+
.. diagonal_add_monoid_hom n α,}
218+
219+
variables {n α R}
220+
185221
@[simp] lemma diagonal_map [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} :
186222
(diagonal d).map f = diagonal (λ m, f (d m)) :=
187223
by { ext, simp only [diagonal, map_apply], split_ifs; simp [h], }
@@ -354,7 +390,7 @@ theorem mul_apply' [has_mul α] [add_comm_monoid α] {M : matrix l m α} {N : ma
354390

355391
@[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) :
356392
-diagonal d = diagonal (λ i, -d i) :=
357-
by ext i j; by_cases i = j; simp [h]
393+
((diagonal_add_monoid_hom n α).map_neg d).symm
358394

359395
lemma sum_apply [add_comm_monoid α] (i : m) (j : n)
360396
(s : finset β) (g : β → matrix m n α) :
@@ -446,6 +482,16 @@ lemma map_mul {L : matrix m n α} {M : matrix n o α} [non_assoc_semiring β] {f
446482
(L ⬝ M).map f = L.map f ⬝ M.map f :=
447483
by { ext, simp [mul_apply, ring_hom.map_sum], }
448484

485+
variables (α n)
486+
487+
/-- `matrix.diagonal` as a `ring_hom`. -/
488+
@[simps]
489+
def diagonal_ring_hom [decidable_eq n] : (n → α) →+* matrix n n α :=
490+
{ to_fun := diagonal,
491+
map_one' := diagonal_one,
492+
map_mul' := λ _ _, (diagonal_mul_diagonal' _ _).symm,
493+
.. diagonal_add_monoid_hom n α }
494+
449495
end non_assoc_semiring
450496

451497
section non_unital_semiring
@@ -768,6 +814,15 @@ end
768814
@[simp] lemma algebra_map_eq_smul (r : R) :
769815
algebra_map R (matrix n n R) r = r • (1 : matrix n n R) := rfl
770816

817+
variables (R)
818+
819+
/-- `matrix.diagonal` as an `alg_hom`. -/
820+
@[simps]
821+
def diagonal_alg_hom : (n → α) →ₐ[R] matrix n n α :=
822+
{ to_fun := diagonal,
823+
commutes' := λ r, by { ext, rw [algebra_map_matrix_apply, diagonal, pi.algebra_map_apply] },
824+
.. diagonal_ring_hom n α }
825+
771826
end algebra
772827

773828
/-- For two vectors `w` and `v`, `vec_mul_vec w v i j` is defined to be `w i * v j`.

0 commit comments

Comments
 (0)