-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson7.v
422 lines (325 loc) · 10.3 KB
/
lesson7.v
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
From elpi Require Import elpi.
From HB Require Import structures.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import ssralg finalg countalg zmodp matrix mxalgebra. (* all_algebra *)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Import GRing.Theory.
Local Open Scope ring_scope.
(** #<div class='slide'>#
* Linear algebra in mathematical components
Extensive documentation in the header of:
- the library #<a href="https://math-comp.github.io/htmldoc_2_0_alpha1/mathcomp.algebra.matrix.html">matrix</a>#
- and the library #<a href="https://math-comp.github.io/htmldoc_2_0_alpha1/mathcomp.algebra.mxalgebra.html">mxalgebra</a>#
* Roadmap for the lesson:
- definition of matrices
- main theorems
- help with depend types
- vector spaces as matrices
*)
(** #<div># *)
Module DefinitionMatrices.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
* Defining Matrices
(Credits "ITP 2013 tutorial: The Mathematical Components library" by Enrico Tassi and Assia Mahboubi)
*)
(** #<div># *)
Reserved Notation "''M[' R ]_ n"
(at level 8, n at level 2, format "''M[' R ]_ n").
Reserved Notation "''M[' R ]_ ( m , n )"
(at level 8, format "''M[' R ]_ ( m , n )").
Reserved Notation "\matrix_ ( i , j ) E"
(at level 36, E at level 36, i, j at level 50,
format "\matrix_ ( i , j ) E").
Reserved Notation "x %:M" (at level 8, format "x %:M").
Reserved Notation "A *m B" (at level 40, left associativity, format "A *m B").
Reserved Notation "A ^T" (at level 8, format "A ^T").
Reserved Notation "\tr A" (at level 10, A at level 8, format "\tr A").
(** #</div># *)
(**
** A matrix is a 2-dimension array
*)
(** #<div># *)
Inductive matrix (R : Type) (m n : nat) : Type :=
Matrix of {ffun 'I_m * 'I_n -> R}.
(** #</div># *)
(**
Some notations : size inside parentheses, type of coefficients inside
square brackets. NB: In the library, the type of coefficients can also
be ommitted.
*)
(** #<div># *)
Notation "''M[' R ]_ ( m , n )" := (matrix R m n) : type_scope.
Notation "''M[' R ]_ n" := 'M[R]_(n, n) : type_scope.
(* Test *)
Check 'M[nat]_(2,3).
Check 'M[nat]_2.
(** #</div># *)
(**
The type "matrix" is just a tag over ffun: it inherits from its structure.
We can "transfer" automatically all structures from the type of finite
functions by "trivial subTyping".
*)
(** #<div># *)
Definition mx_val R m n (A : 'M[R]_(m,n)) : {ffun 'I_m * 'I_n -> R} :=
let: Matrix g := A in g.
HB.instance Definition _ R m n := [isNew for @mx_val R m n].
HB.instance Definition _ (R : eqType) m n := [Equality of 'M[R]_(m, n) by <:].
HB.instance Definition _ (R : choiceType) m n := [Choice of 'M[R]_(m, n) by <:].
HB.instance Definition _ (R : countType) m n := [Countable of 'M[R]_(m, n) by <:].
HB.instance Definition _ (R : finType) m n := [Finite of 'M[R]_(m, n) by <:].
(** #</div># *)
(**
Test overloaded "_ == _" notation
*)
(** #<div># *)
Check 'M[nat]_2 : eqType.
Check forall A : 'M[nat]_2, A == A.
(** #</div># *)
(**
Since matrices over nat are comparable with _ == _, matrices over
matrices over nat are also comparable
*)
(** #<div># *)
Check forall AA : 'M[ 'M[nat]_3 ]_2, AA == AA.
(** #</div># *)
(**
We define a coercion in order to access elements as if matrices were
functions.
*)
(** #<div># *)
Definition fun_of_mx R m n (A : 'M[R]_(m,n)) : 'I_m -> 'I_n -> R :=
fun i j => mx_val A (i, j). Coercion fun_of_mx : matrix >-> Funclass.
Check forall (A : 'M[nat]_3) i j, A i j == 37%N.
(** #</div># *)
(**
We provide a notation to build matrices from a general term.
*)
(** #<div># *)
Definition mx_of_fun R m n (F : 'I_m -> 'I_n -> R) : 'M[R]_(m,n) :=
Matrix [ffun ij => F ij.1 ij.2].
Notation "\matrix_ ( i , j ) E" := (mx_of_fun (fun i j => E))
(at level 36, E at level 36, i, j at level 50) : ring_scope.
Check \matrix_(i,j) (i - j)%N : 'M[nat]_(3,4).
End DefinitionMatrices.
Module MatrixProperties.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
* Main Theorems
We now show the most used theorems for matrix manipulation.
** mxE
mxE is an equation to compute a term in the matrix at given
coordinates: it extracts the general term of the matrix and compute
the substitution of indexes. It is generally the right move when you
have <pre>(A complicated matrix) i j</pre>
in your goal.
*)
(** #<div># *)
Check mxE.
(** #</div># *)
(**
** matrixP
matrixP is the "extensionality theorem" for matrices, it says two
matrices are equal if and only if all their coefficients are pairwise
equal.
*)
(** #<div># *)
Check matrixP.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
** Operations on matrices
*** Specific operation: trace and transpose
(do not confuse the names)
*)
(** #<div># *)
Print mxtrace.
Locate "\tr".
Print trmx.
Locate "^T".
(** #</div># *)
(**
*** Specific operation scalar matrix
*)
(** #<div># *)
Print scalar_mx.
Locate "%:M".
(** #</div># *)
(**
*** Matrices on rings are provided with a R-module canonical structure.
But not a ring as the multiplication is heterogeneous.
*)
(** #<div># *)
Lemma test1 (R : ringType) m n (A B : 'M[R]_(m,n)) : A + B = B + A.
Proof. exact: addrC. Qed.
Print mulmx.
Lemma test2 (R : ringType) m n (a : R) (A : 'M[R]_(m,n)) : a *: A = a%:M *m A.
Proof. by rewrite mul_scalar_mx. Qed.
(** #</div># *)
(**
*** Square matrices with explicit non zero size have a ring canonical structure.
This ring product coincides with the matrix product.
*)
(** #<div># *)
Lemma test3 (R : ringType) n (A B : 'M[R]_n.+1) : A * B = A *m B.
Proof. reflexivity. Qed.
(** #</div># *)
(**
*** Specific operation: the determinant.
*)
(** #<div># *)
Print determinant.
Locate "\det".
(** #</div># *)
(**
*** Square matrices on a commutative unit ring with explicit non zero size have a unit ring canonical structure.
and these notions of inversibility are definitionally equivalent.
*)
(** #<div># *)
Lemma test4 (R : comUnitRingType) n (A : 'M[R]_n.+1) :
(unitmx A) = (A \is a GRing.unit)
/\ (A \is a GRing.unit) = (\det A \is a GRing.unit).
Proof. split; reflexivity. Qed.
End MatrixProperties.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
* SUB VECTOR SPACES AS MATRICES
** General understanding
- A specificity of the mathematical components library is to allow to
reason on matrices as if they represented their own image.
- The doc and the code are in #<a href="https://math-comp.github.io/htmldoc_2_0_alpha1/mathcomp.algebra.mxalgebra.html">mxalgebra</a>#
- rows can be seen as vectors, and matrix can be seen as the familiy
of its row vectors.
- #<b>WARNING</b># Following the diagramatic convention (which is
opposite to the usual convention), composition/application of
linear maps represented by matrices should be done left to right:
applying A to u is <pre>u *m A</pre>
- the scope MS (matrix space) contains all notions about this vision
of matrices (comparison, addition, intersection of spaces).
- as a consequence, membership to a space is the same operation as
comparison of spaces.
*** The rank of a matrix is also the dimension of the space it represents
*)
(** #<div># *)
Locate "\rank".
About mxrank.
(** #</div># *)
(**
*** Inclusion can be used both for elements (row vectors) and subspaces (matrices).
*)
(** #<div># *)
Locate "_ <= _".
About submx.
(** #</div># *)
(**
*** The total space is represented by 1, and the trivial space by 0.
*)
(** #<div># *)
About submx1.
About sub1mx.
About sub0mx.
About submx0.
(** #</div># *)
(**
*** Addition of subspaces is not the same thing as addition of matrices.
(In Coq: same notation, different scope)
*)
(** #<div># *)
Locate "_ + _".
About addsmx.
(** #</div># *)
(**
*** Intersection of subspaces
*)
(** #<div># *)
Locate "_ :&: _".
About capmx.
About sub_capmx.
(** #</div># *)
(**
*** Equality of subspaces is double inclusion.
Alternatively, the library provides an equivalent definition (via
eqmxP) that can be used for rewriting in inclusion statements or rank.
*)
(** #<div># *)
Locate "_ == _".
Check (_ == _)%MS.
Locate "_ :=: _".
About eqmx.
Print eqmx.
About mxdirectE.
About mxdirect_addsP.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
** Usage.
- Im A is represented by the matrix A itself.
- vectors of a space represented by the matrix A are linear
combinations of the rows of A, which amount to making a product by
an element (i.e. row of coefficients, or coordinates in the family
generated by A) on the left.
*)
(** #<div># *)
About submxP.
About row_subP.
About submxMl.
(** #</div># *)
(**
- Ker A is represented by the square matrix kermx A.
*)
(** #<div># *)
About kermx.
About sub_kermxP.
(** #</div># *)
(** #</div># *)
(** -------------------------------------------- *)
(** #<div class='slide'>#
** Let's do an example together
*)
(** #<div># *)
Section ex_6_12.
Variables (F : fieldType) (n' : nat).
Let n := n'.+1.
Variable (u : 'M[F]_n) (S : 'M[F]_n).
Hypothesis eq_keru_imu : (kermx u :=: u)%MS.
Hypothesis S_u_direct : (S :&: u)%MS = 0.
Hypothesis S_u_eq1 : (S + u :=: 1)%MS.
Implicit Types (x y z : 'rV[F]_n).
Lemma Su_rect x : exists2 yz, x = yz.1 + yz.2 *m u
& (yz.1 <= S)%MS && (yz.2 <= S)%MS.
Proof.
pose y := x *m proj_mx S u.
have /submxP [z'] := proj_mx_sub u S x => xpu.
pose z := z' *m proj_mx S u.
exists (y, z) => /=; last by rewrite !proj_mx_sub.
rewrite -{1}(@add_proj_mx _ _ _ S u x) ?S_u_direct ?S_u_eq1 ?submx1 //.
congr (_ + _); apply/eqP; rewrite xpu -subr_eq0 -mulmxBl.
apply/eqP/sub_kermxP.
by rewrite eq_keru_imu proj_mx_compl_sub ?S_u_eq1 ?submx1.
Qed.
Lemma Su_dec_eq0 y z : (y <= S)%MS -> (z <= S)%MS ->
(y + z *m u == 0) = (y == 0) && (z == 0).
Proof.
move=> yS zS; apply/idP/idP; last first.
by move=> /andP[/eqP -> /eqP ->]; rewrite add0r mul0mx.
rewrite addr_eq0 -mulNmx => /eqP eq_y_Nzu.
have : (y <= S :&: u)%MS by rewrite sub_capmx yS eq_y_Nzu submxMl.
rewrite S_u_direct // submx0 => /eqP y_eq0.
move/eqP: eq_y_Nzu; rewrite y_eq0 eq_sym mulNmx oppr_eq0 eqxx /= => /eqP.
move=> /sub_kermxP; rewrite eq_keru_imu => z_keru.
have : (z <= S :&: u)%MS by rewrite sub_capmx zS.
by rewrite S_u_direct // submx0.
Qed.
End ex_6_12.
(** #</div># *)
(** #</div># *)