/
matrix.lean
266 lines (211 loc) · 9.43 KB
/
matrix.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Johannes Hölzl, Casper Putz
The equivalence between matrices and linear maps.
-/
import data.matrix.basic
import linear_algebra.dimension linear_algebra.tensor_product
/-!
# Linear maps and matrices
This file defines the maps to send matrices to a linear map,
and to send linear maps between modules with a finite bases
to matrices. This defines a linear equivalence between linear maps
between finite-dimensional vector spaces and matrices indexed by
the respective bases.
Some results are proved about the linear map corresponding to a
diagonal matrix (range, ker and rank).
## Main definitions
to_lin, to_matrix, lin_equiv_matrix
## Tags
linear_map, matrix, linear_equiv, diagonal
-/
noncomputable theory
open set submodule
universes u v
variables {l m n : Type u} [fintype l] [fintype m] [fintype n]
namespace matrix
variables {α : Type v} [comm_ring α]
instance [decidable_eq m] [decidable_eq n] (α) [fintype α] : fintype (matrix m n α) :=
by unfold matrix; apply_instance
/-- Evaluation of matrices gives a linear map from matrix m n α to
linear maps (n → α) →ₗ[α] (m → α). -/
def eval : (matrix m n α) →ₗ[α] ((n → α) →ₗ[α] (m → α)) :=
begin
refine linear_map.mk₂ α mul_vec _ _ _ _,
{ assume M N v, funext x,
change finset.univ.sum (λy:n, (M x y + N x y) * v y) = _,
simp only [_root_.add_mul, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change finset.univ.sum (λy:n, (c * M x y) * v y) = _,
simp only [_root_.mul_assoc, finset.mul_sum.symm],
refl },
{ assume M v w, funext x,
change finset.univ.sum (λy:n, M x y * (v y + w y)) = _,
simp [_root_.mul_add, finset.sum_add_distrib],
refl },
{ assume c M v, funext x,
change finset.univ.sum (λy:n, M x y * (c * v y)) = _,
rw [show (λy:n, M x y * (c * v y)) = (λy:n, c * (M x y * v y)), { funext n, ac_refl },
← finset.mul_sum],
refl }
end
/-- Evaluation of matrices gives a map from matrix m n α to
linear maps (n → α) →ₗ[α] (m → α). -/
def to_lin : matrix m n α → (n → α) →ₗ[α] (m → α) := eval.to_fun
lemma to_lin_add (M N : matrix m n α) : (M + N).to_lin = M.to_lin + N.to_lin :=
matrix.eval.map_add M N
@[simp] lemma to_lin_zero : (0 : matrix m n α).to_lin = 0 :=
matrix.eval.map_zero
instance to_lin.is_linear_map :
@is_linear_map α (matrix m n α) ((n → α) →ₗ[α] (m → α)) _ _ _ _ _ to_lin :=
matrix.eval.is_linear
instance to_lin.is_add_monoid_hom :
@is_add_monoid_hom (matrix m n α) ((n → α) →ₗ[α] (m → α)) _ _ to_lin :=
{ map_zero := to_lin_zero, map_add := to_lin_add }
@[simp] lemma to_lin_apply (M : matrix m n α) (v : n → α) :
(M.to_lin : (n → α) → (m → α)) v = mul_vec M v := rfl
lemma mul_to_lin [decidable_eq l] (M : matrix m n α) (N : matrix n l α) :
(M.mul N).to_lin = M.to_lin.comp N.to_lin :=
begin
ext v x,
simp [to_lin_apply, mul_vec, matrix.mul, finset.sum_mul, finset.mul_sum],
rw [finset.sum_comm],
congr, funext x, congr, funext y,
rw [mul_assoc]
end
end matrix
namespace linear_map
variables {α : Type v} [comm_ring α]
/-- The linear map from linear maps (n → α) →ₗ[α] (m → α) to matrix m n α. -/
def to_matrixₗ [decidable_eq n] : ((n → α) →ₗ[α] (m → α)) →ₗ[α] matrix m n α :=
begin
refine linear_map.mk (λ f i j, f (λ n, ite (j = n) 1 0) i) _ _,
{ assume f g, simp only [add_apply], refl },
{ assume f g, simp only [smul_apply], refl }
end
/-- The map from linear maps (n → α) →ₗ[α] (m → α) to matrix m n α. -/
def to_matrix [decidable_eq n] : ((n → α) →ₗ[α] (m → α)) → matrix m n α := to_matrixₗ.to_fun
end linear_map
section lin_equiv_matrix
variables {α : Type v} [comm_ring α] [decidable_eq n]
open finsupp matrix linear_map
/-- to_lin is the left inverse of to_matrix. -/
lemma to_matrix_to_lin [decidable_eq α] {f : (n → α) →ₗ[α] (m → α)} :
to_lin (to_matrix f) = f :=
begin
ext : 1,
-- Show that the two sides are equal by showing that they are equal on a basis
convert linear_eq_on (set.range _) _ (is_basis.mem_span (@pi.is_basis_fun α _ n _ _ _) _),
assume e he,
rw [@std_basis_eq_single α _ _ _ _ 1] at he,
cases (set.mem_range.mp he) with i h,
ext j,
change finset.univ.sum (λ k, (f.to_fun (single k 1).to_fun) j * (e k)) = _,
rw [←h],
conv_lhs { congr, skip, funext,
rw [mul_comm, ←smul_eq_mul, ←pi.smul_apply, ←linear_map.smul, single_apply],
rw [show f.to_fun (ite (i = k) (1:α) 0 • (single k 1).to_fun) = ite (i = k) (f.to_fun ((single k 1).to_fun)) 0,
{ split_ifs, { rw [one_smul] }, { rw [zero_smul], exact linear_map.map_zero f } }] },
convert finset.sum_eq_single i _ _,
{ rw [if_pos rfl], refl },
{ assume _ _ hbi, rw [if_neg $ ne.symm hbi], refl },
{ assume hi, exact false.elim (hi $ finset.mem_univ i) }
end
/-- to_lin is the right inverse of to_matrix. -/
lemma to_lin_to_matrix {M : matrix m n α} : to_matrix (to_lin M) = M :=
begin
ext,
change finset.univ.sum (λ y, M i y * ite (j = y) 1 0) = M i j,
have h1 : (λ y, M i y * ite (j = y) 1 0) = (λ y, ite (j = y) (M i y) 0),
{ ext, split_ifs, exact mul_one _, exact ring.mul_zero _ },
have h2 : finset.univ.sum (λ y, ite (j = y) (M i y) 0) = (finset.singleton j).sum (λ y, ite (j = y) (M i y) 0),
{ refine (finset.sum_subset _ _).symm,
{ intros _ H, rwa finset.mem_singleton.1 H, exact finset.mem_univ _ },
{ exact λ _ _ H, if_neg (mt (finset.mem_singleton.2 ∘ eq.symm) H) } },
rw [h1, h2, finset.sum_singleton],
exact if_pos rfl
end
/-- Linear maps (n → α) →ₗ[α] (m → α) are linearly equivalent to matrix m n α. -/
def lin_equiv_matrix' [decidable_eq α] : ((n → α) →ₗ[α] (m → α)) ≃ₗ[α] matrix m n α :=
{ to_fun := to_matrix,
inv_fun := to_lin,
right_inv := λ _, to_lin_to_matrix,
left_inv := λ _, to_matrix_to_lin,
add := to_matrixₗ.add,
smul := to_matrixₗ.smul }
/-- Given a basis of two modules β and γ over a commutative ring α, we get a linear equivalence
between linear maps β →ₗ γ and matrices over α indexed by the bases. -/
def lin_equiv_matrix {ι κ β γ : Type*} [decidable_eq α]
[add_comm_group β] [decidable_eq β] [module α β]
[add_comm_group γ] [decidable_eq γ] [module α γ]
[fintype ι] [decidable_eq ι] [fintype κ] [decidable_eq κ]
{v₁ : ι → β} {v₂ : κ → γ} (hv₁ : is_basis α v₁) (hv₂ : is_basis α v₂) :
(β →ₗ[α] γ) ≃ₗ[α] matrix κ ι α :=
linear_equiv.trans (linear_equiv.arrow_congr (equiv_fun_basis hv₁) (equiv_fun_basis hv₂)) lin_equiv_matrix'
end lin_equiv_matrix
namespace matrix
section ring
variables {α : Type v} [comm_ring α]
open linear_map matrix
lemma proj_diagonal [decidable_eq m] (i : m) (w : m → α) :
(proj i).comp (to_lin (diagonal w)) = (w i) • proj i :=
by ext j; simp [mul_vec_diagonal]
lemma diagonal_comp_std_basis [decidable_eq n] (w : n → α) (i : n) :
(diagonal w).to_lin.comp (std_basis α (λ_:n, α) i) = (w i) • std_basis α (λ_:n, α) i :=
begin
ext a j,
simp only [linear_map.comp_apply, smul_apply, to_lin_apply, mul_vec_diagonal, smul_apply,
pi.smul_apply, smul_eq_mul],
by_cases i = j,
{ subst h },
{ rw [std_basis_ne α (λ_:n, α) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] }
end
end ring
section vector_space
variables {α : Type u} [discrete_field α] -- maybe try to relax the universe constraint
open linear_map matrix
lemma rank_vec_mul_vec [decidable_eq n] (w : m → α) (v : n → α) :
rank (vec_mul_vec w v).to_lin ≤ 1 :=
begin
rw [vec_mul_vec_eq, mul_to_lin],
refine le_trans (rank_comp_le1 _ _) _,
refine le_trans (rank_le_domain _) _,
rw [dim_fun', ← cardinal.fintype_card],
exact le_refl _
end
set_option class.instance_max_depth 100
lemma diagonal_to_lin [decidable_eq m] (w : m → α) :
(diagonal w).to_lin = linear_map.pi (λi, w i • linear_map.proj i) :=
by ext v j; simp [mul_vec_diagonal]
lemma ker_diagonal_to_lin [decidable_eq m] (w : m → α) :
ker (diagonal w).to_lin = (⨆i∈{i | w i = 0 }, range (std_basis α (λi, α) i)) :=
begin
rw [← comap_bot, ← infi_ker_proj],
simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'],
have : univ ⊆ {i : m | w i = 0} ∪ -{i : m | w i = 0}, { rw set.union_compl_self },
exact (supr_range_std_basis_eq_infi_ker_proj α (λi:m, α)
(disjoint_compl {i | w i = 0}) this (finite.of_fintype _)).symm
end
lemma range_diagonal [decidable_eq m] (w : m → α) :
(diagonal w).to_lin.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis α (λi, α) i).range) :=
begin
dsimp only [mem_set_of_eq],
rw [← map_top, ← supr_range_std_basis, map_supr],
congr, funext i,
rw [← linear_map.range_comp, diagonal_comp_std_basis, range_smul'],
end
lemma rank_diagonal [decidable_eq m] [decidable_eq α] (w : m → α) :
rank (diagonal w).to_lin = fintype.card { i // w i ≠ 0 } :=
begin
have hu : univ ⊆ - {i : m | w i = 0} ∪ {i : m | w i = 0}, { rw set.compl_union_self },
have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl {i | w i = 0}).symm,
have h₁ := supr_range_std_basis_eq_infi_ker_proj α (λi:m, α) hd hu (finite.of_fintype _),
have h₂ := @infi_ker_proj_equiv α _ _ (λi:m, α) _ _ _ _ (by simp; apply_instance) hd hu,
rw [rank, range_diagonal, h₁, ←@dim_fun' α],
apply linear_equiv.dim_eq,
apply h₂,
end
end vector_space
end matrix