-
Notifications
You must be signed in to change notification settings - Fork 299
/
basic.lean
2199 lines (1665 loc) · 84.1 KB
/
basic.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/-
Copyright (c) 2018 Ellen Arlt. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Ellen Arlt, Blair Shi, Sean Leather, Mario Carneiro, Johan Commelin, Lu-Ming Zhang
-/
import algebra.algebra.pi
import algebra.big_operators.pi
import algebra.big_operators.ring
import algebra.big_operators.ring_equiv
import algebra.module.linear_map
import algebra.module.pi
import algebra.star.big_operators
import algebra.star.module
import algebra.star.pi
import data.fintype.big_operators
/-!
# Matrices
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines basic properties of matrices.
Matrices with rows indexed by `m`, columns indexed by `n`, and entries of type `α` are represented
with `matrix m n α`. For the typical approach of counting rows and columns,
`matrix (fin m) (fin n) α` can be used.
## Notation
The locale `matrix` gives the following notation:
* `⬝ᵥ` for `matrix.dot_product`
* `⬝` for `matrix.mul`
* `ᵀ` for `matrix.transpose`
* `ᴴ` for `matrix.conj_transpose`
## Implementation notes
For convenience, `matrix m n α` is defined as `m → n → α`, as this allows elements of the matrix
to be accessed with `A i j`. However, it is not advisable to _construct_ matrices using terms of the
form `λ i j, _` or even `(λ i j, _ : matrix m n α)`, as these are not recognized by lean as having
the right type. Instead, `matrix.of` should be used.
## TODO
Under various conditions, multiplication of infinite matrices makes sense.
These have not yet been implemented.
-/
universes u u' v w
open_locale big_operators
/-- `matrix m n R` is the type of matrices with entries in `R`, whose rows are indexed by `m`
and whose columns are indexed by `n`. -/
def matrix (m : Type u) (n : Type u') (α : Type v) : Type (max u u' v) :=
m → n → α
variables {l m n o : Type*} {m' : o → Type*} {n' : o → Type*}
variables {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*}
namespace matrix
section ext
variables {M N : matrix m n α}
theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N :=
⟨λ h, funext $ λ i, funext $ h i, λ h, by simp [h]⟩
@[ext] theorem ext : (∀ i j, M i j = N i j) → M = N :=
ext_iff.mp
end ext
/-- Cast a function into a matrix.
The two sides of the equivalence are definitionally equal types. We want to use an explicit cast
to distinguish the types because `matrix` has different instances to pi types (such as `pi.has_mul`,
which performs elementwise multiplication, vs `matrix.has_mul`).
If you are defining a matrix, in terms of its entries, use `of (λ i j, _)`. The
purpose of this approach is to ensure that terms of th
e form `(λ i j, _) * (λ i j, _)` do not
appear, as the type of `*` can be misleading.
Porting note: In Lean 3, it is also safe to use pattern matching in a definition as `| i j := _`,
which can only be unfolded when fully-applied. leanprover/lean4#2042 means this does not
(currently) work in Lean 4.
-/
def of : (m → n → α) ≃ matrix m n α := equiv.refl _
@[simp] lemma of_apply (f : m → n → α) (i j) : of f i j = f i j := rfl
@[simp] lemma of_symm_apply (f : matrix m n α) (i j) : of.symm f i j = f i j := rfl
/-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`.
This is available in bundled forms as:
* `add_monoid_hom.map_matrix`
* `linear_map.map_matrix`
* `ring_hom.map_matrix`
* `alg_hom.map_matrix`
* `equiv.map_matrix`
* `add_equiv.map_matrix`
* `linear_equiv.map_matrix`
* `ring_equiv.map_matrix`
* `alg_equiv.map_matrix`
-/
def map (M : matrix m n α) (f : α → β) : matrix m n β := of (λ i j, f (M i j))
@[simp]
lemma map_apply {M : matrix m n α} {f : α → β} {i : m} {j : n} :
M.map f i j = f (M i j) := rfl
@[simp]
lemma map_id (M : matrix m n α) : M.map id = M :=
by { ext, refl, }
@[simp]
lemma map_map {M : matrix m n α} {β γ : Type*} {f : α → β} {g : β → γ} :
(M.map f).map g = M.map (g ∘ f) :=
by { ext, refl, }
lemma map_injective {f : α → β} (hf : function.injective f) :
function.injective (λ M : matrix m n α, M.map f) :=
λ M N h, ext $ λ i j, hf $ ext_iff.mpr h i j
/-- The transpose of a matrix. -/
def transpose (M : matrix m n α) : matrix n m α :=
of $ λ x y, M y x
-- TODO: set as an equation lemma for `transpose`, see mathlib4#3024
@[simp] lemma transpose_apply (M : matrix m n α) (i j) :
transpose M i j = M j i := rfl
localized "postfix (name := matrix.transpose) `ᵀ`:1500 := matrix.transpose" in matrix
/-- The conjugate transpose of a matrix defined in term of `star`. -/
def conj_transpose [has_star α] (M : matrix m n α) : matrix n m α :=
M.transpose.map star
localized "postfix (name := matrix.conj_transpose) `ᴴ`:1500 := matrix.conj_transpose" in matrix
/-- `matrix.col u` is the column matrix whose entries are given by `u`. -/
def col (w : m → α) : matrix m unit α :=
of $ λ x y, w x
-- TODO: set as an equation lemma for `col`, see mathlib4#3024
@[simp] lemma col_apply (w : m → α) (i j) :
col w i j = w i := rfl
/-- `matrix.row u` is the row matrix whose entries are given by `u`. -/
def row (v : n → α) : matrix unit n α :=
of $ λ x y, v y
-- TODO: set as an equation lemma for `row`, see mathlib4#3024
@[simp] lemma row_apply (v : n → α) (i j) : row v i j = v j := rfl
instance [inhabited α] : inhabited (matrix m n α) := pi.inhabited _
instance [has_add α] : has_add (matrix m n α) := pi.has_add
instance [add_semigroup α] : add_semigroup (matrix m n α) := pi.add_semigroup
instance [add_comm_semigroup α] : add_comm_semigroup (matrix m n α) := pi.add_comm_semigroup
instance [has_zero α] : has_zero (matrix m n α) := pi.has_zero
instance [add_zero_class α] : add_zero_class (matrix m n α) := pi.add_zero_class
instance [add_monoid α] : add_monoid (matrix m n α) := pi.add_monoid
instance [add_comm_monoid α] : add_comm_monoid (matrix m n α) := pi.add_comm_monoid
instance [has_neg α] : has_neg (matrix m n α) := pi.has_neg
instance [has_sub α] : has_sub (matrix m n α) := pi.has_sub
instance [add_group α] : add_group (matrix m n α) := pi.add_group
instance [add_comm_group α] : add_comm_group (matrix m n α) := pi.add_comm_group
instance [unique α] : unique (matrix m n α) := pi.unique
instance [subsingleton α] : subsingleton (matrix m n α) := pi.subsingleton
instance [nonempty m] [nonempty n] [nontrivial α] : nontrivial (matrix m n α) :=
function.nontrivial
instance [has_smul R α] : has_smul R (matrix m n α) := pi.has_smul
instance [has_smul R α] [has_smul S α] [smul_comm_class R S α] :
smul_comm_class R S (matrix m n α) := pi.smul_comm_class
instance [has_smul R S] [has_smul R α] [has_smul S α] [is_scalar_tower R S α] :
is_scalar_tower R S (matrix m n α) := pi.is_scalar_tower
instance [has_smul R α] [has_smul Rᵐᵒᵖ α] [is_central_scalar R α] :
is_central_scalar R (matrix m n α) := pi.is_central_scalar
instance [monoid R] [mul_action R α] :
mul_action R (matrix m n α) := pi.mul_action _
instance [monoid R] [add_monoid α] [distrib_mul_action R α] :
distrib_mul_action R (matrix m n α) := pi.distrib_mul_action _
instance [semiring R] [add_comm_monoid α] [module R α] :
module R (matrix m n α) := pi.module _ _ _
/-! simp-normal form pulls `of` to the outside. -/
@[simp] lemma of_zero [has_zero α] : of (0 : m → n → α) = 0 := rfl
@[simp] lemma of_add_of [has_add α] (f g : m → n → α) : of f + of g = of (f + g) := rfl
@[simp] lemma of_sub_of [has_sub α] (f g : m → n → α) : of f - of g = of (f - g) := rfl
@[simp] lemma neg_of [has_neg α] (f : m → n → α) : -of f = of (-f) := rfl
@[simp] lemma smul_of [has_smul R α] (r : R) (f : m → n → α) : r • of f = of (r • f) := rfl
@[simp] protected lemma map_zero [has_zero α] [has_zero β] (f : α → β) (h : f 0 = 0) :
(0 : matrix m n α).map f = 0 :=
by { ext, simp [h], }
protected lemma map_add [has_add α] [has_add β] (f : α → β)
(hf : ∀ a₁ a₂, f (a₁ + a₂) = f a₁ + f a₂)
(M N : matrix m n α) : (M + N).map f = M.map f + N.map f :=
ext $ λ _ _, hf _ _
protected lemma map_sub [has_sub α] [has_sub β] (f : α → β)
(hf : ∀ a₁ a₂, f (a₁ - a₂) = f a₁ - f a₂)
(M N : matrix m n α) : (M - N).map f = M.map f - N.map f :=
ext $ λ _ _, hf _ _
lemma map_smul [has_smul R α] [has_smul R β] (f : α → β) (r : R)
(hf : ∀ a, f (r • a) = r • f a) (M : matrix m n α) : (r • M).map f = r • (M.map f) :=
ext $ λ _ _, hf _
/-- The scalar action via `has_mul.to_has_smul` is transformed by the same map as the elements
of the matrix, when `f` preserves multiplication. -/
lemma map_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α)
(hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) :
(r • A).map f = f r • A.map f :=
ext $ λ _ _, hf _ _
/-- The scalar action via `has_mul.to_has_opposite_smul` is transformed by the same map as the
elements of the matrix, when `f` preserves multiplication. -/
lemma map_op_smul' [has_mul α] [has_mul β] (f : α → β) (r : α) (A : matrix n n α)
(hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) :
(mul_opposite.op r • A).map f = mul_opposite.op (f r) • A.map f :=
ext $ λ _ _, hf _ _
lemma _root_.is_smul_regular.matrix [has_smul R S] {k : R} (hk : is_smul_regular S k) :
is_smul_regular (matrix m n S) k :=
is_smul_regular.pi $ λ _, is_smul_regular.pi $ λ _, hk
lemma _root_.is_left_regular.matrix [has_mul α] {k : α} (hk : is_left_regular k) :
is_smul_regular (matrix m n α) k :=
hk.is_smul_regular.matrix
instance subsingleton_of_empty_left [is_empty m] : subsingleton (matrix m n α) :=
⟨λ M N, by { ext, exact is_empty_elim i }⟩
instance subsingleton_of_empty_right [is_empty n] : subsingleton (matrix m n α) :=
⟨λ M N, by { ext, exact is_empty_elim j }⟩
end matrix
open_locale matrix
namespace matrix
section diagonal
variables [decidable_eq n]
/-- `diagonal d` is the square matrix such that `(diagonal d) i i = d i` and `(diagonal d) i j = 0`
if `i ≠ j`.
Note that bundled versions exist as:
* `matrix.diagonal_add_monoid_hom`
* `matrix.diagonal_linear_map`
* `matrix.diagonal_ring_hom`
* `matrix.diagonal_alg_hom`
-/
def diagonal [has_zero α] (d : n → α) : matrix n n α :=
of $ λ i j, if i = j then d i else 0
-- TODO: set as an equation lemma for `diagonal`, see mathlib4#3024
lemma diagonal_apply [has_zero α] (d : n → α) (i j) : diagonal d i j = if i = j then d i else 0 :=
rfl
@[simp] theorem diagonal_apply_eq [has_zero α] (d : n → α) (i : n) : (diagonal d) i i = d i :=
by simp [diagonal]
@[simp] theorem diagonal_apply_ne [has_zero α] (d : n → α) {i j : n} (h : i ≠ j) :
(diagonal d) i j = 0 := by simp [diagonal, h]
theorem diagonal_apply_ne' [has_zero α] (d : n → α) {i j : n} (h : j ≠ i) :
(diagonal d) i j = 0 := diagonal_apply_ne d h.symm
@[simp] theorem diagonal_eq_diagonal_iff [has_zero α] {d₁ d₂ : n → α} :
diagonal d₁ = diagonal d₂ ↔ ∀ i, d₁ i = d₂ i :=
⟨λ h i, by simpa using congr_arg (λ m : matrix n n α, m i i) h,
λ h, by rw show d₁ = d₂, from funext h⟩
lemma diagonal_injective [has_zero α] : function.injective (diagonal : (n → α) → matrix n n α) :=
λ d₁ d₂ h, funext $ λ i, by simpa using matrix.ext_iff.mpr h i i
@[simp] theorem diagonal_zero [has_zero α] : (diagonal (λ _, 0) : matrix n n α) = 0 :=
by { ext, simp [diagonal] }
@[simp] lemma diagonal_transpose [has_zero α] (v : n → α) :
(diagonal v)ᵀ = diagonal v :=
begin
ext i j,
by_cases h : i = j,
{ simp [h, transpose] },
{ simp [h, transpose, diagonal_apply_ne' _ h] }
end
@[simp] theorem diagonal_add [add_zero_class α] (d₁ d₂ : n → α) :
diagonal d₁ + diagonal d₂ = diagonal (λ i, d₁ i + d₂ i) :=
by ext i j; by_cases h : i = j; simp [h]
@[simp] theorem diagonal_smul [monoid R] [add_monoid α] [distrib_mul_action R α] (r : R)
(d : n → α) :
diagonal (r • d) = r • diagonal d :=
by ext i j; by_cases h : i = j; simp [h]
variables (n α)
/-- `matrix.diagonal` as an `add_monoid_hom`. -/
@[simps]
def diagonal_add_monoid_hom [add_zero_class α] : (n → α) →+ matrix n n α :=
{ to_fun := diagonal,
map_zero' := diagonal_zero,
map_add' := λ x y, (diagonal_add x y).symm,}
variables (R)
/-- `matrix.diagonal` as a `linear_map`. -/
@[simps]
def diagonal_linear_map [semiring R] [add_comm_monoid α] [module R α] :
(n → α) →ₗ[R] matrix n n α :=
{ map_smul' := diagonal_smul,
.. diagonal_add_monoid_hom n α,}
variables {n α R}
@[simp] lemma diagonal_map [has_zero α] [has_zero β] {f : α → β} (h : f 0 = 0) {d : n → α} :
(diagonal d).map f = diagonal (λ m, f (d m)) :=
by { ext, simp only [diagonal_apply, map_apply], split_ifs; simp [h], }
@[simp] lemma diagonal_conj_transpose [add_monoid α] [star_add_monoid α] (v : n → α) :
(diagonal v)ᴴ = diagonal (star v) :=
begin
rw [conj_transpose, diagonal_transpose, diagonal_map (star_zero _)],
refl,
end
section one
variables [has_zero α] [has_one α]
instance : has_one (matrix n n α) := ⟨diagonal (λ _, 1)⟩
@[simp] theorem diagonal_one : (diagonal (λ _, 1) : matrix n n α) = 1 := rfl
theorem one_apply {i j} : (1 : matrix n n α) i j = if i = j then 1 else 0 := rfl
@[simp] theorem one_apply_eq (i) : (1 : matrix n n α) i i = 1 := diagonal_apply_eq _ i
@[simp] theorem one_apply_ne {i j} : i ≠ j → (1 : matrix n n α) i j = 0 :=
diagonal_apply_ne _
theorem one_apply_ne' {i j} : j ≠ i → (1 : matrix n n α) i j = 0 :=
diagonal_apply_ne' _
@[simp] lemma map_one [has_zero β] [has_one β]
(f : α → β) (h₀ : f 0 = 0) (h₁ : f 1 = 1) :
(1 : matrix n n α).map f = (1 : matrix n n β) :=
by { ext, simp only [one_apply, map_apply], split_ifs; simp [h₀, h₁], }
lemma one_eq_pi_single {i j} : (1 : matrix n n α) i j = pi.single i 1 j :=
by simp only [one_apply, pi.single_apply, eq_comm]; congr -- deal with decidable_eq
end one
section numeral
@[simp] lemma bit0_apply [has_add α] (M : matrix m m α) (i : m) (j : m) :
(bit0 M) i j = bit0 (M i j) := rfl
variables [add_zero_class α] [has_one α]
lemma bit1_apply (M : matrix n n α) (i : n) (j : n) :
(bit1 M) i j = if i = j then bit1 (M i j) else bit0 (M i j) :=
by dsimp [bit1]; by_cases h : i = j; simp [h]
@[simp]
lemma bit1_apply_eq (M : matrix n n α) (i : n) :
(bit1 M) i i = bit1 (M i i) :=
by simp [bit1_apply]
@[simp]
lemma bit1_apply_ne (M : matrix n n α) {i j : n} (h : i ≠ j) :
(bit1 M) i j = bit0 (M i j) :=
by simp [bit1_apply, h]
end numeral
end diagonal
section diag
/-- The diagonal of a square matrix. -/
@[simp] def diag (A : matrix n n α) (i : n) : α := A i i
@[simp] lemma diag_diagonal [decidable_eq n] [has_zero α] (a : n → α) : diag (diagonal a) = a :=
funext $ @diagonal_apply_eq _ _ _ _ a
@[simp] lemma diag_transpose (A : matrix n n α) : diag Aᵀ = diag A := rfl
@[simp] theorem diag_zero [has_zero α] : diag (0 : matrix n n α) = 0 := rfl
@[simp] theorem diag_add [has_add α] (A B : matrix n n α) : diag (A + B) = diag A + diag B := rfl
@[simp] theorem diag_sub [has_sub α] (A B : matrix n n α) : diag (A - B) = diag A - diag B := rfl
@[simp] theorem diag_neg [has_neg α] (A : matrix n n α) : diag (-A) = -diag A := rfl
@[simp] theorem diag_smul [has_smul R α] (r : R) (A : matrix n n α) : diag (r • A) = r • diag A :=
rfl
@[simp] theorem diag_one [decidable_eq n] [has_zero α] [has_one α] : diag (1 : matrix n n α) = 1 :=
diag_diagonal _
variables (n α)
/-- `matrix.diag` as an `add_monoid_hom`. -/
@[simps]
def diag_add_monoid_hom [add_zero_class α] : matrix n n α →+ (n → α) :=
{ to_fun := diag,
map_zero' := diag_zero,
map_add' := diag_add,}
variables (R)
/-- `matrix.diag` as a `linear_map`. -/
@[simps]
def diag_linear_map [semiring R] [add_comm_monoid α] [module R α] : matrix n n α →ₗ[R] (n → α) :=
{ map_smul' := diag_smul,
.. diag_add_monoid_hom n α,}
variables {n α R}
lemma diag_map {f : α → β} {A : matrix n n α} : diag (A.map f) = f ∘ diag A := rfl
@[simp] lemma diag_conj_transpose [add_monoid α] [star_add_monoid α] (A : matrix n n α) :
diag Aᴴ = star (diag A) := rfl
@[simp] lemma diag_list_sum [add_monoid α] (l : list (matrix n n α)) :
diag l.sum = (l.map diag).sum :=
map_list_sum (diag_add_monoid_hom n α) l
@[simp] lemma diag_multiset_sum [add_comm_monoid α] (s : multiset (matrix n n α)) :
diag s.sum = (s.map diag).sum :=
map_multiset_sum (diag_add_monoid_hom n α) s
@[simp] lemma diag_sum {ι} [add_comm_monoid α] (s : finset ι) (f : ι → matrix n n α) :
diag (∑ i in s, f i) = ∑ i in s, diag (f i) :=
map_sum (diag_add_monoid_hom n α) f s
end diag
section dot_product
variables [fintype m] [fintype n]
/-- `dot_product v w` is the sum of the entrywise products `v i * w i` -/
def dot_product [has_mul α] [add_comm_monoid α] (v w : m → α) : α :=
∑ i, v i * w i
/- The precedence of 72 comes immediately after ` • ` for `has_smul.smul`,
so that `r₁ • a ⬝ᵥ r₂ • b` is parsed as `(r₁ • a) ⬝ᵥ (r₂ • b)` here. -/
localized "infix (name := matrix.dot_product) ` ⬝ᵥ `:72 := matrix.dot_product" in matrix
lemma dot_product_assoc [non_unital_semiring α] (u : m → α) (w : n → α)
(v : matrix m n α) :
(λ j, u ⬝ᵥ (λ i, v i j)) ⬝ᵥ w = u ⬝ᵥ (λ i, (v i) ⬝ᵥ w) :=
by simpa [dot_product, finset.mul_sum, finset.sum_mul, mul_assoc] using finset.sum_comm
lemma dot_product_comm [add_comm_monoid α] [comm_semigroup α] (v w : m → α) :
v ⬝ᵥ w = w ⬝ᵥ v :=
by simp_rw [dot_product, mul_comm]
@[simp] lemma dot_product_punit [add_comm_monoid α] [has_mul α] (v w : punit → α) :
v ⬝ᵥ w = v ⟨⟩ * w ⟨⟩ :=
by simp [dot_product]
section mul_one_class
variables [mul_one_class α] [add_comm_monoid α]
lemma dot_product_one (v : n → α) : v ⬝ᵥ 1 = ∑ i, v i := by simp [(⬝ᵥ)]
lemma one_dot_product (v : n → α) : 1 ⬝ᵥ v = ∑ i, v i := by simp [(⬝ᵥ)]
end mul_one_class
section non_unital_non_assoc_semiring
variables [non_unital_non_assoc_semiring α] (u v w : m → α) (x y : n → α)
@[simp] lemma dot_product_zero : v ⬝ᵥ 0 = 0 := by simp [dot_product]
@[simp] lemma dot_product_zero' : v ⬝ᵥ (λ _, 0) = 0 := dot_product_zero v
@[simp] lemma zero_dot_product : 0 ⬝ᵥ v = 0 := by simp [dot_product]
@[simp] lemma zero_dot_product' : (λ _, (0 : α)) ⬝ᵥ v = 0 := zero_dot_product v
@[simp] lemma add_dot_product : (u + v) ⬝ᵥ w = u ⬝ᵥ w + v ⬝ᵥ w :=
by simp [dot_product, add_mul, finset.sum_add_distrib]
@[simp] lemma dot_product_add : u ⬝ᵥ (v + w) = u ⬝ᵥ v + u ⬝ᵥ w :=
by simp [dot_product, mul_add, finset.sum_add_distrib]
@[simp] lemma sum_elim_dot_product_sum_elim :
(sum.elim u x) ⬝ᵥ (sum.elim v y) = u ⬝ᵥ v + x ⬝ᵥ y :=
by simp [dot_product]
/-- Permuting a vector on the left of a dot product can be transferred to the right. -/
@[simp] lemma comp_equiv_symm_dot_product (e : m ≃ n) : (u ∘ e.symm) ⬝ᵥ x = u ⬝ᵥ (x ∘ e) :=
(e.sum_comp _).symm.trans $ finset.sum_congr rfl $ λ _ _,
by simp only [function.comp, equiv.symm_apply_apply]
/-- Permuting a vector on the right of a dot product can be transferred to the left. -/
@[simp] lemma dot_product_comp_equiv_symm (e : n ≃ m) : u ⬝ᵥ (x ∘ e.symm) = (u ∘ e) ⬝ᵥ x :=
by simpa only [equiv.symm_symm] using (comp_equiv_symm_dot_product u x e.symm).symm
/-- Permuting vectors on both sides of a dot product is a no-op. -/
@[simp] lemma comp_equiv_dot_product_comp_equiv (e : m ≃ n) : (x ∘ e) ⬝ᵥ (y ∘ e) = x ⬝ᵥ y :=
by simp only [←dot_product_comp_equiv_symm, function.comp, equiv.apply_symm_apply]
end non_unital_non_assoc_semiring
section non_unital_non_assoc_semiring_decidable
variables [decidable_eq m] [non_unital_non_assoc_semiring α] (u v w : m → α)
@[simp] lemma diagonal_dot_product (i : m) : diagonal v i ⬝ᵥ w = v i * w i :=
have ∀ j ≠ i, diagonal v i j * w j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij],
by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp
@[simp] lemma dot_product_diagonal (i : m) : v ⬝ᵥ diagonal w i = v i * w i :=
have ∀ j ≠ i, v j * diagonal w i j = 0 := λ j hij, by simp [diagonal_apply_ne' _ hij],
by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp
@[simp] lemma dot_product_diagonal' (i : m) : v ⬝ᵥ (λ j, diagonal w j i) = v i * w i :=
have ∀ j ≠ i, v j * diagonal w j i = 0 := λ j hij, by simp [diagonal_apply_ne _ hij],
by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp
@[simp] lemma single_dot_product (x : α) (i : m) : pi.single i x ⬝ᵥ v = x * v i :=
have ∀ j ≠ i, pi.single i x j * v j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij],
by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp
@[simp] lemma dot_product_single (x : α) (i : m) : v ⬝ᵥ pi.single i x = v i * x :=
have ∀ j ≠ i, v j * pi.single i x j = 0 := λ j hij, by simp [pi.single_eq_of_ne hij],
by convert finset.sum_eq_single i (λ j _, this j) _ using 1; simp
end non_unital_non_assoc_semiring_decidable
section non_assoc_semiring
variables [non_assoc_semiring α]
@[simp] lemma one_dot_product_one : (1 : n → α) ⬝ᵥ 1 = fintype.card n :=
by simp [dot_product, fintype.card]
end non_assoc_semiring
section non_unital_non_assoc_ring
variables [non_unital_non_assoc_ring α] (u v w : m → α)
@[simp] lemma neg_dot_product : -v ⬝ᵥ w = - (v ⬝ᵥ w) := by simp [dot_product]
@[simp] lemma dot_product_neg : v ⬝ᵥ -w = - (v ⬝ᵥ w) := by simp [dot_product]
@[simp] lemma sub_dot_product : (u - v) ⬝ᵥ w = u ⬝ᵥ w - v ⬝ᵥ w :=
by simp [sub_eq_add_neg]
@[simp] lemma dot_product_sub : u ⬝ᵥ (v - w) = u ⬝ᵥ v - u ⬝ᵥ w :=
by simp [sub_eq_add_neg]
end non_unital_non_assoc_ring
section distrib_mul_action
variables [monoid R] [has_mul α] [add_comm_monoid α] [distrib_mul_action R α]
@[simp] lemma smul_dot_product [is_scalar_tower R α α] (x : R) (v w : m → α) :
(x • v) ⬝ᵥ w = x • (v ⬝ᵥ w) :=
by simp [dot_product, finset.smul_sum, smul_mul_assoc]
@[simp] lemma dot_product_smul [smul_comm_class R α α] (x : R) (v w : m → α) :
v ⬝ᵥ (x • w) = x • (v ⬝ᵥ w) :=
by simp [dot_product, finset.smul_sum, mul_smul_comm]
end distrib_mul_action
section star_ring
variables [non_unital_semiring α] [star_ring α] (v w : m → α)
lemma star_dot_product_star : star v ⬝ᵥ star w = star (w ⬝ᵥ v) :=
by simp [dot_product]
lemma star_dot_product : star v ⬝ᵥ w = star (star w ⬝ᵥ v) :=
by simp [dot_product]
lemma dot_product_star : v ⬝ᵥ star w = star (w ⬝ᵥ star v) :=
by simp [dot_product]
end star_ring
end dot_product
open_locale matrix
/-- `M ⬝ N` is the usual product of matrices `M` and `N`, i.e. we have that
`(M ⬝ N) i k` is the dot product of the `i`-th row of `M` by the `k`-th column of `N`.
This is currently only defined when `m` is finite. -/
protected def mul [fintype m] [has_mul α] [add_comm_monoid α]
(M : matrix l m α) (N : matrix m n α) : matrix l n α :=
λ i k, (λ j, M i j) ⬝ᵥ (λ j, N j k)
localized "infixl (name := matrix.mul) ` ⬝ `:75 := matrix.mul" in matrix
theorem mul_apply [fintype m] [has_mul α] [add_comm_monoid α]
{M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = ∑ j, M i j * N j k := rfl
instance [fintype n] [has_mul α] [add_comm_monoid α] : has_mul (matrix n n α) := ⟨matrix.mul⟩
@[simp] theorem mul_eq_mul [fintype n] [has_mul α] [add_comm_monoid α] (M N : matrix n n α) :
M * N = M ⬝ N := rfl
theorem mul_apply' [fintype m] [has_mul α] [add_comm_monoid α]
{M : matrix l m α} {N : matrix m n α} {i k} : (M ⬝ N) i k = (λ j, M i j) ⬝ᵥ (λ j, N j k)
:= rfl
@[simp] theorem diagonal_neg [decidable_eq n] [add_group α] (d : n → α) :
-diagonal d = diagonal (λ i, -d i) :=
((diagonal_add_monoid_hom n α).map_neg d).symm
lemma sum_apply [add_comm_monoid α] (i : m) (j : n)
(s : finset β) (g : β → matrix m n α) :
(∑ c in s, g c) i j = ∑ c in s, g c i j :=
(congr_fun (s.sum_apply i g) j).trans (s.sum_apply j _)
lemma two_mul_expl {R : Type*} [comm_ring R] (A B : matrix (fin 2) (fin 2) R) :
(A * B) 0 0 = A 0 0 * B 0 0 + A 0 1 * B 1 0 ∧
(A * B) 0 1 = A 0 0 * B 0 1 + A 0 1 * B 1 1 ∧
(A * B) 1 0 = A 1 0 * B 0 0 + A 1 1 * B 1 0 ∧
(A * B) 1 1 = A 1 0 * B 0 1 + A 1 1 * B 1 1 :=
begin
split, work_on_goal 2 {split}, work_on_goal 3 {split},
all_goals {simp only [matrix.mul_eq_mul],
rw [matrix.mul_apply, finset.sum_fin_eq_sum_range, finset.sum_range_succ, finset.sum_range_succ],
simp},
end
section add_comm_monoid
variables [add_comm_monoid α] [has_mul α]
@[simp] lemma smul_mul [fintype n] [monoid R] [distrib_mul_action R α] [is_scalar_tower R α α]
(a : R) (M : matrix m n α) (N : matrix n l α) :
(a • M) ⬝ N = a • M ⬝ N :=
by { ext, apply smul_dot_product }
@[simp] lemma mul_smul [fintype n] [monoid R] [distrib_mul_action R α] [smul_comm_class R α α]
(M : matrix m n α) (a : R) (N : matrix n l α) : M ⬝ (a • N) = a • M ⬝ N :=
by { ext, apply dot_product_smul }
end add_comm_monoid
section non_unital_non_assoc_semiring
variables [non_unital_non_assoc_semiring α]
@[simp] protected theorem mul_zero [fintype n] (M : matrix m n α) : M ⬝ (0 : matrix n o α) = 0 :=
by { ext i j, apply dot_product_zero }
@[simp] protected theorem zero_mul [fintype m] (M : matrix m n α) : (0 : matrix l m α) ⬝ M = 0 :=
by { ext i j, apply zero_dot_product }
protected theorem mul_add [fintype n] (L : matrix m n α) (M N : matrix n o α) :
L ⬝ (M + N) = L ⬝ M + L ⬝ N := by { ext i j, apply dot_product_add }
protected theorem add_mul [fintype m] (L M : matrix l m α) (N : matrix m n α) :
(L + M) ⬝ N = L ⬝ N + M ⬝ N := by { ext i j, apply add_dot_product }
instance [fintype n] : non_unital_non_assoc_semiring (matrix n n α) :=
{ mul := (*),
add := (+),
zero := 0,
mul_zero := matrix.mul_zero,
zero_mul := matrix.zero_mul,
left_distrib := matrix.mul_add,
right_distrib := matrix.add_mul,
.. matrix.add_comm_monoid}
@[simp] theorem diagonal_mul [fintype m] [decidable_eq m]
(d : m → α) (M : matrix m n α) (i j) : (diagonal d).mul M i j = d i * M i j :=
diagonal_dot_product _ _ _
@[simp] theorem mul_diagonal [fintype n] [decidable_eq n]
(d : n → α) (M : matrix m n α) (i j) : (M ⬝ diagonal d) i j = M i j * d j :=
by { rw ← diagonal_transpose, apply dot_product_diagonal }
@[simp] theorem diagonal_mul_diagonal [fintype n] [decidable_eq n] (d₁ d₂ : n → α) :
(diagonal d₁) ⬝ (diagonal d₂) = diagonal (λ i, d₁ i * d₂ i) :=
by ext i j; by_cases i = j; simp [h]
theorem diagonal_mul_diagonal' [fintype n] [decidable_eq n] (d₁ d₂ : n → α) :
diagonal d₁ * diagonal d₂ = diagonal (λ i, d₁ i * d₂ i) :=
diagonal_mul_diagonal _ _
lemma smul_eq_diagonal_mul [fintype m] [decidable_eq m] (M : matrix m n α) (a : α) :
a • M = diagonal (λ _, a) ⬝ M :=
by { ext, simp }
@[simp] lemma diag_col_mul_row (a b : n → α) : diag (col a ⬝ row b) = a * b :=
by { ext, simp [matrix.mul_apply, col, row] }
/-- Left multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/
@[simps] def add_monoid_hom_mul_left [fintype m] (M : matrix l m α) :
matrix m n α →+ matrix l n α :=
{ to_fun := λ x, M ⬝ x,
map_zero' := matrix.mul_zero _,
map_add' := matrix.mul_add _ }
/-- Right multiplication by a matrix, as an `add_monoid_hom` from matrices to matrices. -/
@[simps] def add_monoid_hom_mul_right [fintype m] (M : matrix m n α) :
matrix l m α →+ matrix l n α :=
{ to_fun := λ x, x ⬝ M,
map_zero' := matrix.zero_mul _,
map_add' := λ _ _, matrix.add_mul _ _ _ }
protected lemma sum_mul [fintype m] (s : finset β) (f : β → matrix l m α)
(M : matrix m n α) : (∑ a in s, f a) ⬝ M = ∑ a in s, f a ⬝ M :=
(add_monoid_hom_mul_right M : matrix l m α →+ _).map_sum f s
protected lemma mul_sum [fintype m] (s : finset β) (f : β → matrix m n α)
(M : matrix l m α) : M ⬝ ∑ a in s, f a = ∑ a in s, M ⬝ f a :=
(add_monoid_hom_mul_left M : matrix m n α →+ _).map_sum f s
/-- This instance enables use with `smul_mul_assoc`. -/
instance semiring.is_scalar_tower [fintype n] [monoid R] [distrib_mul_action R α]
[is_scalar_tower R α α] : is_scalar_tower R (matrix n n α) (matrix n n α) :=
⟨λ r m n, matrix.smul_mul r m n⟩
/-- This instance enables use with `mul_smul_comm`. -/
instance semiring.smul_comm_class [fintype n] [monoid R] [distrib_mul_action R α]
[smul_comm_class R α α] : smul_comm_class R (matrix n n α) (matrix n n α) :=
⟨λ r m n, (matrix.mul_smul m r n).symm⟩
end non_unital_non_assoc_semiring
section non_assoc_semiring
variables [non_assoc_semiring α]
@[simp] protected theorem one_mul [fintype m] [decidable_eq m] (M : matrix m n α) :
(1 : matrix m m α) ⬝ M = M :=
by ext i j; rw [← diagonal_one, diagonal_mul, one_mul]
@[simp] protected theorem mul_one [fintype n] [decidable_eq n] (M : matrix m n α) :
M ⬝ (1 : matrix n n α) = M :=
by ext i j; rw [← diagonal_one, mul_diagonal, mul_one]
instance [fintype n] [decidable_eq n] : non_assoc_semiring (matrix n n α) :=
{ one := 1,
one_mul := matrix.one_mul,
mul_one := matrix.mul_one,
nat_cast := λ n, diagonal (λ _, n),
nat_cast_zero := by ext; simp [nat.cast],
nat_cast_succ := λ n, by ext; by_cases i = j; simp [nat.cast, *],
.. matrix.non_unital_non_assoc_semiring }
@[simp]
lemma map_mul [fintype n] {L : matrix m n α} {M : matrix n o α} [non_assoc_semiring β]
{f : α →+* β} : (L ⬝ M).map f = L.map f ⬝ M.map f :=
by { ext, simp [mul_apply, ring_hom.map_sum], }
variables (α n)
/-- `matrix.diagonal` as a `ring_hom`. -/
@[simps]
def diagonal_ring_hom [fintype n] [decidable_eq n] : (n → α) →+* matrix n n α :=
{ to_fun := diagonal,
map_one' := diagonal_one,
map_mul' := λ _ _, (diagonal_mul_diagonal' _ _).symm,
.. diagonal_add_monoid_hom n α }
end non_assoc_semiring
section non_unital_semiring
variables [non_unital_semiring α] [fintype m] [fintype n]
protected theorem mul_assoc (L : matrix l m α) (M : matrix m n α) (N : matrix n o α) :
(L ⬝ M) ⬝ N = L ⬝ (M ⬝ N) :=
by { ext, apply dot_product_assoc }
instance : non_unital_semiring (matrix n n α) :=
{ mul_assoc := matrix.mul_assoc, ..matrix.non_unital_non_assoc_semiring }
end non_unital_semiring
section semiring
variables [semiring α]
instance [fintype n] [decidable_eq n] : semiring (matrix n n α) :=
{ ..matrix.non_unital_semiring, ..matrix.non_assoc_semiring }
end semiring
section non_unital_non_assoc_ring
variables [non_unital_non_assoc_ring α] [fintype n]
@[simp] protected theorem neg_mul (M : matrix m n α) (N : matrix n o α) :
(-M) ⬝ N = -(M ⬝ N) :=
by { ext, apply neg_dot_product }
@[simp] protected theorem mul_neg (M : matrix m n α) (N : matrix n o α) :
M ⬝ (-N) = -(M ⬝ N) :=
by { ext, apply dot_product_neg }
protected theorem sub_mul (M M' : matrix m n α) (N : matrix n o α) :
(M - M') ⬝ N = M ⬝ N - M' ⬝ N :=
by rw [sub_eq_add_neg, matrix.add_mul, matrix.neg_mul, sub_eq_add_neg]
protected theorem mul_sub (M : matrix m n α) (N N' : matrix n o α) :
M ⬝ (N - N') = M ⬝ N - M ⬝ N' :=
by rw [sub_eq_add_neg, matrix.mul_add, matrix.mul_neg, sub_eq_add_neg]
instance : non_unital_non_assoc_ring (matrix n n α) :=
{ ..matrix.non_unital_non_assoc_semiring, ..matrix.add_comm_group }
end non_unital_non_assoc_ring
instance [fintype n] [non_unital_ring α] : non_unital_ring (matrix n n α) :=
{ ..matrix.non_unital_semiring, ..matrix.add_comm_group }
instance [fintype n] [decidable_eq n] [non_assoc_ring α] : non_assoc_ring (matrix n n α) :=
{ ..matrix.non_assoc_semiring, ..matrix.add_comm_group }
instance [fintype n] [decidable_eq n] [ring α] : ring (matrix n n α) :=
{ ..matrix.semiring, ..matrix.add_comm_group }
section semiring
variables [semiring α]
lemma diagonal_pow [fintype n] [decidable_eq n] (v : n → α) (k : ℕ) :
diagonal v ^ k = diagonal (v ^ k) :=
(map_pow (diagonal_ring_hom n α) v k).symm
@[simp] lemma mul_mul_left [fintype n] (M : matrix m n α) (N : matrix n o α) (a : α) :
of (λ i j, a * M i j) ⬝ N = a • (M ⬝ N) :=
smul_mul a M N
/--
The ring homomorphism `α →+* matrix n n α`
sending `a` to the diagonal matrix with `a` on the diagonal.
-/
def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α :=
{ to_fun := λ a, a • 1,
map_one' := by simp,
map_mul' := by { intros, ext, simp [mul_assoc], },
.. (smul_add_hom α _).flip (1 : matrix n n α) }
section scalar
variables [decidable_eq n] [fintype n]
@[simp] lemma coe_scalar : (scalar n : α → matrix n n α) = λ a, a • 1 := rfl
lemma scalar_apply_eq (a : α) (i : n) :
scalar n a i i = a :=
by simp only [coe_scalar, smul_eq_mul, mul_one, one_apply_eq, pi.smul_apply]
lemma scalar_apply_ne (a : α) (i j : n) (h : i ≠ j) :
scalar n a i j = 0 :=
by simp only [h, coe_scalar, one_apply_ne, ne.def, not_false_iff, pi.smul_apply, smul_zero]
lemma scalar_inj [nonempty n] {r s : α} : scalar n r = scalar n s ↔ r = s :=
begin
split,
{ intro h,
inhabit n,
rw [← scalar_apply_eq r (arbitrary n), ← scalar_apply_eq s (arbitrary n), h] },
{ rintro rfl, refl }
end
end scalar
end semiring
section comm_semiring
variables [comm_semiring α] [fintype n]
lemma smul_eq_mul_diagonal [decidable_eq n] (M : matrix m n α) (a : α) :
a • M = M ⬝ diagonal (λ _, a) :=
by { ext, simp [mul_comm] }
@[simp] lemma mul_mul_right (M : matrix m n α) (N : matrix n o α) (a : α) :
M ⬝ of (λ i j, a * N i j) = a • (M ⬝ N) :=
mul_smul M a N
lemma scalar.commute [decidable_eq n] (r : α) (M : matrix n n α) : commute (scalar n r) M :=
by simp [commute, semiconj_by]
end comm_semiring
section algebra
variables [fintype n] [decidable_eq n]
variables [comm_semiring R] [semiring α] [semiring β] [algebra R α] [algebra R β]
instance : algebra R (matrix n n α) :=
{ commutes' := λ r x, begin
ext, simp [matrix.scalar, matrix.mul_apply, matrix.one_apply, algebra.commutes, smul_ite], end,
smul_def' := λ r x, begin ext, simp [matrix.scalar, algebra.smul_def r], end,
..((matrix.scalar n).comp (algebra_map R α)) }
lemma algebra_map_matrix_apply {r : R} {i j : n} :
algebra_map R (matrix n n α) r i j = if i = j then algebra_map R α r else 0 :=
begin
dsimp [algebra_map, algebra.to_ring_hom, matrix.scalar],
split_ifs with h; simp [h, matrix.one_apply_ne],
end
lemma algebra_map_eq_diagonal (r : R) :
algebra_map R (matrix n n α) r = diagonal (algebra_map R (n → α) r) :=
matrix.ext $ λ i j, algebra_map_matrix_apply
@[simp] lemma algebra_map_eq_smul (r : R) :
algebra_map R (matrix n n R) r = r • (1 : matrix n n R) := rfl
lemma algebra_map_eq_diagonal_ring_hom :
algebra_map R (matrix n n α) = (diagonal_ring_hom n α).comp (algebra_map R _) :=
ring_hom.ext algebra_map_eq_diagonal
@[simp] lemma map_algebra_map (r : R) (f : α → β) (hf : f 0 = 0)
(hf₂ : f (algebra_map R α r) = algebra_map R β r) :
(algebra_map R (matrix n n α) r).map f = algebra_map R (matrix n n β) r :=
begin
rw [algebra_map_eq_diagonal, algebra_map_eq_diagonal, diagonal_map hf],
congr' 1 with x,
simp only [hf₂, pi.algebra_map_apply]
end
variables (R)
/-- `matrix.diagonal` as an `alg_hom`. -/
@[simps]
def diagonal_alg_hom : (n → α) →ₐ[R] matrix n n α :=
{ to_fun := diagonal,
commutes' := λ r, (algebra_map_eq_diagonal r).symm,
.. diagonal_ring_hom n α }
end algebra
end matrix
/-!
### Bundled versions of `matrix.map`
-/
namespace equiv
/-- The `equiv` between spaces of matrices induced by an `equiv` between their
coefficients. This is `matrix.map` as an `equiv`. -/
@[simps apply]
def map_matrix (f : α ≃ β) : matrix m n α ≃ matrix m n β :=
{ to_fun := λ M, M.map f,
inv_fun := λ M, M.map f.symm,
left_inv := λ M, matrix.ext $ λ _ _, f.symm_apply_apply _,
right_inv := λ M, matrix.ext $ λ _ _, f.apply_symm_apply _, }
@[simp] lemma map_matrix_refl : (equiv.refl α).map_matrix = equiv.refl (matrix m n α) :=
rfl
@[simp] lemma map_matrix_symm (f : α ≃ β) :
f.map_matrix.symm = (f.symm.map_matrix : matrix m n β ≃ _) :=
rfl
@[simp] lemma map_matrix_trans (f : α ≃ β) (g : β ≃ γ) :
f.map_matrix.trans g.map_matrix = ((f.trans g).map_matrix : matrix m n α ≃ _) :=
rfl
end equiv
namespace add_monoid_hom
variables [add_zero_class α] [add_zero_class β] [add_zero_class γ]
/-- The `add_monoid_hom` between spaces of matrices induced by an `add_monoid_hom` between their
coefficients. This is `matrix.map` as an `add_monoid_hom`. -/
@[simps]
def map_matrix (f : α →+ β) : matrix m n α →+ matrix m n β :=
{ to_fun := λ M, M.map f,
map_zero' := matrix.map_zero f f.map_zero,
map_add' := matrix.map_add f f.map_add }
@[simp] lemma map_matrix_id : (add_monoid_hom.id α).map_matrix = add_monoid_hom.id (matrix m n α) :=
rfl
@[simp] lemma map_matrix_comp (f : β →+ γ) (g : α →+ β) :
f.map_matrix.comp g.map_matrix = ((f.comp g).map_matrix : matrix m n α →+ _) :=
rfl
end add_monoid_hom
namespace add_equiv
variables [has_add α] [has_add β] [has_add γ]
/-- The `add_equiv` between spaces of matrices induced by an `add_equiv` between their
coefficients. This is `matrix.map` as an `add_equiv`. -/
@[simps apply]
def map_matrix (f : α ≃+ β) : matrix m n α ≃+ matrix m n β :=
{ to_fun := λ M, M.map f,
inv_fun := λ M, M.map f.symm,
map_add' := matrix.map_add f f.map_add,
.. f.to_equiv.map_matrix }