/
Basic.lean
2771 lines (2165 loc) · 99.5 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 Mathlib.Algebra.Algebra.Opposite
import Mathlib.Algebra.Algebra.Pi
import Mathlib.Algebra.BigOperators.Pi
import Mathlib.Algebra.BigOperators.Ring
import Mathlib.Algebra.BigOperators.RingEquiv
import Mathlib.Algebra.Module.LinearMap.Basic
import Mathlib.Algebra.Module.Pi
import Mathlib.Algebra.Star.BigOperators
import Mathlib.Algebra.Star.Module
import Mathlib.Algebra.Star.Pi
import Mathlib.Data.Fintype.BigOperators
import Mathlib.GroupTheory.GroupAction.BigOperators
#align_import data.matrix.basic from "leanprover-community/mathlib"@"eba5bb3155cab51d80af00e8d7d69fa271b1302b"
/-!
# Matrices
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.dotProduct`
* `*ᵥ` for `Matrix.mulVec`
* `ᵥ*` for `Matrix.vecMul`
* `ᵀ` for `Matrix.transpose`
* `ᴴ` for `Matrix.conjTranspose`
## 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 `fun i j ↦ _` or even `(fun 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.
-/
universe u u' v w
open BigOperators
/-- `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 → α
#align matrix Matrix
variable {l m n o : Type*} {m' : o → Type*} {n' : o → Type*}
variable {R : Type*} {S : Type*} {α : Type v} {β : Type w} {γ : Type*}
namespace Matrix
section Ext
variable {M N : Matrix m n α}
theorem ext_iff : (∀ i j, M i j = N i j) ↔ M = N :=
⟨fun h => funext fun i => funext <| h i, fun h => by simp [h]⟩
#align matrix.ext_iff Matrix.ext_iff
@[ext]
theorem ext : (∀ i j, M i j = N i j) → M = N :=
ext_iff.mp
#align matrix.ext Matrix.ext
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.mul`,
which performs elementwise multiplication, vs `Matrix.mul`).
If you are defining a matrix, in terms of its entries, use `of (fun i j ↦ _)`. The
purpose of this approach is to ensure that terms of the form `(fun i j ↦ _) * (fun 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 _
#align matrix.of Matrix.of
@[simp]
theorem of_apply (f : m → n → α) (i j) : of f i j = f i j :=
rfl
#align matrix.of_apply Matrix.of_apply
@[simp]
theorem of_symm_apply (f : Matrix m n α) (i j) : of.symm f i j = f i j :=
rfl
#align matrix.of_symm_apply Matrix.of_symm_apply
/-- `M.map f` is the matrix obtained by applying `f` to each entry of the matrix `M`.
This is available in bundled forms as:
* `AddMonoidHom.mapMatrix`
* `LinearMap.mapMatrix`
* `RingHom.mapMatrix`
* `AlgHom.mapMatrix`
* `Equiv.mapMatrix`
* `AddEquiv.mapMatrix`
* `LinearEquiv.mapMatrix`
* `RingEquiv.mapMatrix`
* `AlgEquiv.mapMatrix`
-/
def map (M : Matrix m n α) (f : α → β) : Matrix m n β :=
of fun i j => f (M i j)
#align matrix.map Matrix.map
@[simp]
theorem map_apply {M : Matrix m n α} {f : α → β} {i : m} {j : n} : M.map f i j = f (M i j) :=
rfl
#align matrix.map_apply Matrix.map_apply
@[simp]
theorem map_id (M : Matrix m n α) : M.map id = M := by
ext
rfl
#align matrix.map_id Matrix.map_id
@[simp]
theorem map_id' (M : Matrix m n α) : M.map (·) = M := map_id M
@[simp]
theorem map_map {M : Matrix m n α} {β γ : Type*} {f : α → β} {g : β → γ} :
(M.map f).map g = M.map (g ∘ f) := by
ext
rfl
#align matrix.map_map Matrix.map_map
theorem map_injective {f : α → β} (hf : Function.Injective f) :
Function.Injective fun M : Matrix m n α => M.map f := fun _ _ h =>
ext fun i j => hf <| ext_iff.mpr h i j
#align matrix.map_injective Matrix.map_injective
/-- The transpose of a matrix. -/
def transpose (M : Matrix m n α) : Matrix n m α :=
of fun x y => M y x
#align matrix.transpose Matrix.transpose
-- TODO: set as an equation lemma for `transpose`, see mathlib4#3024
@[simp]
theorem transpose_apply (M : Matrix m n α) (i j) : transpose M i j = M j i :=
rfl
#align matrix.transpose_apply Matrix.transpose_apply
@[inherit_doc]
scoped postfix:1024 "ᵀ" => Matrix.transpose
/-- The conjugate transpose of a matrix defined in term of `star`. -/
def conjTranspose [Star α] (M : Matrix m n α) : Matrix n m α :=
M.transpose.map star
#align matrix.conj_transpose Matrix.conjTranspose
@[inherit_doc]
scoped postfix:1024 "ᴴ" => Matrix.conjTranspose
instance inhabited [Inhabited α] : Inhabited (Matrix m n α) :=
inferInstanceAs <| Inhabited <| m → n → α
-- Porting note: new, Lean3 found this automatically
instance decidableEq [DecidableEq α] [Fintype m] [Fintype n] : DecidableEq (Matrix m n α) :=
Fintype.decidablePiFintype
instance {n m} [Fintype m] [DecidableEq m] [Fintype n] [DecidableEq n] (α) [Fintype α] :
Fintype (Matrix m n α) := inferInstanceAs (Fintype (m → n → α))
instance {n m} [Finite m] [Finite n] (α) [Finite α] :
Finite (Matrix m n α) := inferInstanceAs (Finite (m → n → α))
instance add [Add α] : Add (Matrix m n α) :=
Pi.instAdd
instance addSemigroup [AddSemigroup α] : AddSemigroup (Matrix m n α) :=
Pi.addSemigroup
instance addCommSemigroup [AddCommSemigroup α] : AddCommSemigroup (Matrix m n α) :=
Pi.addCommSemigroup
instance zero [Zero α] : Zero (Matrix m n α) :=
Pi.instZero
instance addZeroClass [AddZeroClass α] : AddZeroClass (Matrix m n α) :=
Pi.addZeroClass
instance addMonoid [AddMonoid α] : AddMonoid (Matrix m n α) :=
Pi.addMonoid
instance addCommMonoid [AddCommMonoid α] : AddCommMonoid (Matrix m n α) :=
Pi.addCommMonoid
instance neg [Neg α] : Neg (Matrix m n α) :=
Pi.instNeg
instance sub [Sub α] : Sub (Matrix m n α) :=
Pi.instSub
instance addGroup [AddGroup α] : AddGroup (Matrix m n α) :=
Pi.addGroup
instance addCommGroup [AddCommGroup α] : AddCommGroup (Matrix m n α) :=
Pi.addCommGroup
instance unique [Unique α] : Unique (Matrix m n α) :=
Pi.unique
instance subsingleton [Subsingleton α] : Subsingleton (Matrix m n α) :=
inferInstanceAs <| Subsingleton <| m → n → α
instance nonempty [Nonempty m] [Nonempty n] [Nontrivial α] : Nontrivial (Matrix m n α) :=
Function.nontrivial
instance smul [SMul R α] : SMul R (Matrix m n α) :=
Pi.instSMul
instance smulCommClass [SMul R α] [SMul S α] [SMulCommClass R S α] :
SMulCommClass R S (Matrix m n α) :=
Pi.smulCommClass
instance isScalarTower [SMul R S] [SMul R α] [SMul S α] [IsScalarTower R S α] :
IsScalarTower R S (Matrix m n α) :=
Pi.isScalarTower
instance isCentralScalar [SMul R α] [SMul Rᵐᵒᵖ α] [IsCentralScalar R α] :
IsCentralScalar R (Matrix m n α) :=
Pi.isCentralScalar
instance mulAction [Monoid R] [MulAction R α] : MulAction R (Matrix m n α) :=
Pi.mulAction _
instance distribMulAction [Monoid R] [AddMonoid α] [DistribMulAction R α] :
DistribMulAction R (Matrix m n α) :=
Pi.distribMulAction _
instance module [Semiring R] [AddCommMonoid α] [Module R α] : Module R (Matrix m n α) :=
Pi.module _ _ _
-- Porting note (#10756): added the following section with simp lemmas because `simp` fails
-- to apply the corresponding lemmas in the namespace `Pi`.
-- (e.g. `Pi.zero_apply` used on `OfNat.ofNat 0 i j`)
section
@[simp]
theorem zero_apply [Zero α] (i : m) (j : n) : (0 : Matrix m n α) i j = 0 := rfl
@[simp]
theorem add_apply [Add α] (A B : Matrix m n α) (i : m) (j : n) :
(A + B) i j = (A i j) + (B i j) := rfl
@[simp]
theorem smul_apply [SMul β α] (r : β) (A : Matrix m n α) (i : m) (j : n) :
(r • A) i j = r • (A i j) := rfl
@[simp]
theorem sub_apply [Sub α] (A B : Matrix m n α) (i : m) (j : n) :
(A - B) i j = (A i j) - (B i j) := rfl
@[simp]
theorem neg_apply [Neg α] (A : Matrix m n α) (i : m) (j : n) :
(-A) i j = -(A i j) := rfl
end
/-! simp-normal form pulls `of` to the outside. -/
@[simp]
theorem of_zero [Zero α] : of (0 : m → n → α) = 0 :=
rfl
#align matrix.of_zero Matrix.of_zero
@[simp]
theorem of_add_of [Add α] (f g : m → n → α) : of f + of g = of (f + g) :=
rfl
#align matrix.of_add_of Matrix.of_add_of
@[simp]
theorem of_sub_of [Sub α] (f g : m → n → α) : of f - of g = of (f - g) :=
rfl
#align matrix.of_sub_of Matrix.of_sub_of
@[simp]
theorem neg_of [Neg α] (f : m → n → α) : -of f = of (-f) :=
rfl
#align matrix.neg_of Matrix.neg_of
@[simp]
theorem smul_of [SMul R α] (r : R) (f : m → n → α) : r • of f = of (r • f) :=
rfl
#align matrix.smul_of Matrix.smul_of
@[simp]
protected theorem map_zero [Zero α] [Zero β] (f : α → β) (h : f 0 = 0) :
(0 : Matrix m n α).map f = 0 := by
ext
simp [h]
#align matrix.map_zero Matrix.map_zero
protected theorem map_add [Add α] [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 fun _ _ => hf _ _
#align matrix.map_add Matrix.map_add
protected theorem map_sub [Sub α] [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 fun _ _ => hf _ _
#align matrix.map_sub Matrix.map_sub
theorem map_smul [SMul R α] [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 fun _ _ => hf _
#align matrix.map_smul Matrix.map_smul
/-- The scalar action via `Mul.toSMul` is transformed by the same map as the elements
of the matrix, when `f` preserves multiplication. -/
theorem map_smul' [Mul α] [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 fun _ _ => hf _ _
#align matrix.map_smul' Matrix.map_smul'
/-- The scalar action via `mul.toOppositeSMul` is transformed by the same map as the
elements of the matrix, when `f` preserves multiplication. -/
theorem map_op_smul' [Mul α] [Mul β] (f : α → β) (r : α) (A : Matrix n n α)
(hf : ∀ a₁ a₂, f (a₁ * a₂) = f a₁ * f a₂) :
(MulOpposite.op r • A).map f = MulOpposite.op (f r) • A.map f :=
ext fun _ _ => hf _ _
#align matrix.map_op_smul' Matrix.map_op_smul'
theorem _root_.IsSMulRegular.matrix [SMul R S] {k : R} (hk : IsSMulRegular S k) :
IsSMulRegular (Matrix m n S) k :=
IsSMulRegular.pi fun _ => IsSMulRegular.pi fun _ => hk
#align is_smul_regular.matrix IsSMulRegular.matrix
theorem _root_.IsLeftRegular.matrix [Mul α] {k : α} (hk : IsLeftRegular k) :
IsSMulRegular (Matrix m n α) k :=
hk.isSMulRegular.matrix
#align is_left_regular.matrix IsLeftRegular.matrix
instance subsingleton_of_empty_left [IsEmpty m] : Subsingleton (Matrix m n α) :=
⟨fun M N => by
ext i
exact isEmptyElim i⟩
#align matrix.subsingleton_of_empty_left Matrix.subsingleton_of_empty_left
instance subsingleton_of_empty_right [IsEmpty n] : Subsingleton (Matrix m n α) :=
⟨fun M N => by
ext i j
exact isEmptyElim j⟩
#align matrix.subsingleton_of_empty_right Matrix.subsingleton_of_empty_right
end Matrix
open Matrix
namespace Matrix
section Diagonal
variable [DecidableEq 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.diagonalAddMonoidHom`
* `Matrix.diagonalLinearMap`
* `Matrix.diagonalRingHom`
* `Matrix.diagonalAlgHom`
-/
def diagonal [Zero α] (d : n → α) : Matrix n n α :=
of fun i j => if i = j then d i else 0
#align matrix.diagonal Matrix.diagonal
-- TODO: set as an equation lemma for `diagonal`, see mathlib4#3024
theorem diagonal_apply [Zero α] (d : n → α) (i j) : diagonal d i j = if i = j then d i else 0 :=
rfl
#align matrix.diagonal_apply Matrix.diagonal_apply
@[simp]
theorem diagonal_apply_eq [Zero α] (d : n → α) (i : n) : (diagonal d) i i = d i := by
simp [diagonal]
#align matrix.diagonal_apply_eq Matrix.diagonal_apply_eq
@[simp]
theorem diagonal_apply_ne [Zero α] (d : n → α) {i j : n} (h : i ≠ j) : (diagonal d) i j = 0 := by
simp [diagonal, h]
#align matrix.diagonal_apply_ne Matrix.diagonal_apply_ne
theorem diagonal_apply_ne' [Zero α] (d : n → α) {i j : n} (h : j ≠ i) : (diagonal d) i j = 0 :=
diagonal_apply_ne d h.symm
#align matrix.diagonal_apply_ne' Matrix.diagonal_apply_ne'
@[simp]
theorem diagonal_eq_diagonal_iff [Zero α] {d₁ d₂ : n → α} :
diagonal d₁ = diagonal d₂ ↔ ∀ i, d₁ i = d₂ i :=
⟨fun h i => by simpa using congr_arg (fun m : Matrix n n α => m i i) h, fun h => by
rw [show d₁ = d₂ from funext h]⟩
#align matrix.diagonal_eq_diagonal_iff Matrix.diagonal_eq_diagonal_iff
theorem diagonal_injective [Zero α] : Function.Injective (diagonal : (n → α) → Matrix n n α) :=
fun d₁ d₂ h => funext fun i => by simpa using Matrix.ext_iff.mpr h i i
#align matrix.diagonal_injective Matrix.diagonal_injective
@[simp]
theorem diagonal_zero [Zero α] : (diagonal fun _ => 0 : Matrix n n α) = 0 := by
ext
simp [diagonal]
#align matrix.diagonal_zero Matrix.diagonal_zero
@[simp]
theorem diagonal_transpose [Zero α] (v : n → α) : (diagonal v)ᵀ = diagonal v := by
ext i j
by_cases h : i = j
· simp [h, transpose]
· simp [h, transpose, diagonal_apply_ne' _ h]
#align matrix.diagonal_transpose Matrix.diagonal_transpose
@[simp]
theorem diagonal_add [AddZeroClass α] (d₁ d₂ : n → α) :
diagonal d₁ + diagonal d₂ = diagonal fun i => d₁ i + d₂ i := by
ext i j
by_cases h : i = j <;>
simp [h]
#align matrix.diagonal_add Matrix.diagonal_add
@[simp]
theorem diagonal_smul [Zero α] [SMulZeroClass R α] (r : R) (d : n → α) :
diagonal (r • d) = r • diagonal d := by
ext i j
by_cases h : i = j <;> simp [h]
#align matrix.diagonal_smul Matrix.diagonal_smul
@[simp]
theorem diagonal_neg [NegZeroClass α] (d : n → α) :
-diagonal d = diagonal fun i => -d i := by
ext i j
by_cases h : i = j <;>
simp [h]
#align matrix.diagonal_neg Matrix.diagonal_neg
@[simp]
theorem diagonal_sub [SubNegZeroMonoid α] (d₁ d₂ : n → α) :
diagonal d₁ - diagonal d₂ = diagonal fun i => d₁ i - d₂ i := by
ext i j
by_cases h : i = j <;>
simp [h]
instance [Zero α] [NatCast α] : NatCast (Matrix n n α) where
natCast m := diagonal fun _ => m
@[norm_cast]
theorem diagonal_natCast [Zero α] [NatCast α] (m : ℕ) : diagonal (fun _ : n => (m : α)) = m := rfl
@[norm_cast]
theorem diagonal_natCast' [Zero α] [NatCast α] (m : ℕ) : diagonal ((m : n → α)) = m := rfl
-- See note [no_index around OfNat.ofNat]
theorem diagonal_ofNat [Zero α] [NatCast α] (m : ℕ) [m.AtLeastTwo] :
diagonal (fun _ : n => no_index (OfNat.ofNat m : α)) = OfNat.ofNat m := rfl
-- See note [no_index around OfNat.ofNat]
theorem diagonal_ofNat' [Zero α] [NatCast α] (m : ℕ) [m.AtLeastTwo] :
diagonal (no_index (OfNat.ofNat m : n → α)) = OfNat.ofNat m := rfl
instance [Zero α] [IntCast α] : IntCast (Matrix n n α) where
intCast m := diagonal fun _ => m
@[norm_cast]
theorem diagonal_intCast [Zero α] [IntCast α] (m : ℤ) : diagonal (fun _ : n => (m : α)) = m := rfl
@[norm_cast]
theorem diagonal_intCast' [Zero α] [IntCast α] (m : ℤ) : diagonal ((m : n → α)) = m := rfl
variable (n α)
/-- `Matrix.diagonal` as an `AddMonoidHom`. -/
@[simps]
def diagonalAddMonoidHom [AddZeroClass α] : (n → α) →+ Matrix n n α where
toFun := diagonal
map_zero' := diagonal_zero
map_add' x y := (diagonal_add x y).symm
#align matrix.diagonal_add_monoid_hom Matrix.diagonalAddMonoidHom
variable (R)
/-- `Matrix.diagonal` as a `LinearMap`. -/
@[simps]
def diagonalLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : (n → α) →ₗ[R] Matrix n n α :=
{ diagonalAddMonoidHom n α with map_smul' := diagonal_smul }
#align matrix.diagonal_linear_map Matrix.diagonalLinearMap
variable {n α R}
@[simp]
theorem diagonal_map [Zero α] [Zero β] {f : α → β} (h : f 0 = 0) {d : n → α} :
(diagonal d).map f = diagonal fun m => f (d m) := by
ext
simp only [diagonal_apply, map_apply]
split_ifs <;> simp [h]
#align matrix.diagonal_map Matrix.diagonal_map
@[simp]
theorem diagonal_conjTranspose [AddMonoid α] [StarAddMonoid α] (v : n → α) :
(diagonal v)ᴴ = diagonal (star v) := by
rw [conjTranspose, diagonal_transpose, diagonal_map (star_zero _)]
rfl
#align matrix.diagonal_conj_transpose Matrix.diagonal_conjTranspose
section One
variable [Zero α] [One α]
instance one : One (Matrix n n α) :=
⟨diagonal fun _ => 1⟩
@[simp]
theorem diagonal_one : (diagonal fun _ => 1 : Matrix n n α) = 1 :=
rfl
#align matrix.diagonal_one Matrix.diagonal_one
theorem one_apply {i j} : (1 : Matrix n n α) i j = if i = j then 1 else 0 :=
rfl
#align matrix.one_apply Matrix.one_apply
@[simp]
theorem one_apply_eq (i) : (1 : Matrix n n α) i i = 1 :=
diagonal_apply_eq _ i
#align matrix.one_apply_eq Matrix.one_apply_eq
@[simp]
theorem one_apply_ne {i j} : i ≠ j → (1 : Matrix n n α) i j = 0 :=
diagonal_apply_ne _
#align matrix.one_apply_ne Matrix.one_apply_ne
theorem one_apply_ne' {i j} : j ≠ i → (1 : Matrix n n α) i j = 0 :=
diagonal_apply_ne' _
#align matrix.one_apply_ne' Matrix.one_apply_ne'
@[simp]
theorem map_one [Zero β] [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₁]
#align matrix.map_one Matrix.map_one
-- Porting note: added implicit argument `(f := fun_ => α)`, why is that needed?
theorem one_eq_pi_single {i j} : (1 : Matrix n n α) i j = Pi.single (f := fun _ => α) i 1 j := by
simp only [one_apply, Pi.single_apply, eq_comm]
#align matrix.one_eq_pi_single Matrix.one_eq_pi_single
end One
instance instAddMonoidWithOne [AddMonoidWithOne α] : AddMonoidWithOne (Matrix n n α) where
natCast_zero := show diagonal _ = _ by
rw [Nat.cast_zero, diagonal_zero]
natCast_succ n := show diagonal _ = diagonal _ + _ by
rw [Nat.cast_succ, ← diagonal_add, diagonal_one]
instance instAddGroupWithOne [AddGroupWithOne α] : AddGroupWithOne (Matrix n n α) where
intCast_ofNat n := show diagonal _ = diagonal _ by
rw [Int.cast_natCast]
intCast_negSucc n := show diagonal _ = -(diagonal _) by
rw [Int.cast_negSucc, diagonal_neg]
__ := addGroup
__ := instAddMonoidWithOne
instance instAddCommMonoidWithOne [AddCommMonoidWithOne α] :
AddCommMonoidWithOne (Matrix n n α) where
__ := addCommMonoid
__ := instAddMonoidWithOne
instance instAddCommGroupWithOne [AddCommGroupWithOne α] :
AddCommGroupWithOne (Matrix n n α) where
__ := addCommGroup
__ := instAddGroupWithOne
section Numeral
set_option linter.deprecated false
@[deprecated, simp]
theorem bit0_apply [Add α] (M : Matrix m m α) (i : m) (j : m) : (bit0 M) i j = bit0 (M i j) :=
rfl
#align matrix.bit0_apply Matrix.bit0_apply
variable [AddZeroClass α] [One α]
@[deprecated]
theorem 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]
#align matrix.bit1_apply Matrix.bit1_apply
@[deprecated, simp]
theorem bit1_apply_eq (M : Matrix n n α) (i : n) : (bit1 M) i i = bit1 (M i i) := by
simp [bit1_apply]
#align matrix.bit1_apply_eq Matrix.bit1_apply_eq
@[deprecated, simp]
theorem 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]
#align matrix.bit1_apply_ne Matrix.bit1_apply_ne
end Numeral
end Diagonal
section Diag
/-- The diagonal of a square matrix. -/
-- @[simp] -- Porting note: simpNF does not like this.
def diag (A : Matrix n n α) (i : n) : α :=
A i i
#align matrix.diag Matrix.diag
-- Porting note: new, because of removed `simp` above.
-- TODO: set as an equation lemma for `diag`, see mathlib4#3024
@[simp]
theorem diag_apply (A : Matrix n n α) (i) : diag A i = A i i :=
rfl
@[simp]
theorem diag_diagonal [DecidableEq n] [Zero α] (a : n → α) : diag (diagonal a) = a :=
funext <| @diagonal_apply_eq _ _ _ _ a
#align matrix.diag_diagonal Matrix.diag_diagonal
@[simp]
theorem diag_transpose (A : Matrix n n α) : diag Aᵀ = diag A :=
rfl
#align matrix.diag_transpose Matrix.diag_transpose
@[simp]
theorem diag_zero [Zero α] : diag (0 : Matrix n n α) = 0 :=
rfl
#align matrix.diag_zero Matrix.diag_zero
@[simp]
theorem diag_add [Add α] (A B : Matrix n n α) : diag (A + B) = diag A + diag B :=
rfl
#align matrix.diag_add Matrix.diag_add
@[simp]
theorem diag_sub [Sub α] (A B : Matrix n n α) : diag (A - B) = diag A - diag B :=
rfl
#align matrix.diag_sub Matrix.diag_sub
@[simp]
theorem diag_neg [Neg α] (A : Matrix n n α) : diag (-A) = -diag A :=
rfl
#align matrix.diag_neg Matrix.diag_neg
@[simp]
theorem diag_smul [SMul R α] (r : R) (A : Matrix n n α) : diag (r • A) = r • diag A :=
rfl
#align matrix.diag_smul Matrix.diag_smul
@[simp]
theorem diag_one [DecidableEq n] [Zero α] [One α] : diag (1 : Matrix n n α) = 1 :=
diag_diagonal _
#align matrix.diag_one Matrix.diag_one
variable (n α)
/-- `Matrix.diag` as an `AddMonoidHom`. -/
@[simps]
def diagAddMonoidHom [AddZeroClass α] : Matrix n n α →+ n → α where
toFun := diag
map_zero' := diag_zero
map_add' := diag_add
#align matrix.diag_add_monoid_hom Matrix.diagAddMonoidHom
variable (R)
/-- `Matrix.diag` as a `LinearMap`. -/
@[simps]
def diagLinearMap [Semiring R] [AddCommMonoid α] [Module R α] : Matrix n n α →ₗ[R] n → α :=
{ diagAddMonoidHom n α with map_smul' := diag_smul }
#align matrix.diag_linear_map Matrix.diagLinearMap
variable {n α R}
theorem diag_map {f : α → β} {A : Matrix n n α} : diag (A.map f) = f ∘ diag A :=
rfl
#align matrix.diag_map Matrix.diag_map
@[simp]
theorem diag_conjTranspose [AddMonoid α] [StarAddMonoid α] (A : Matrix n n α) :
diag Aᴴ = star (diag A) :=
rfl
#align matrix.diag_conj_transpose Matrix.diag_conjTranspose
@[simp]
theorem diag_list_sum [AddMonoid α] (l : List (Matrix n n α)) : diag l.sum = (l.map diag).sum :=
map_list_sum (diagAddMonoidHom n α) l
#align matrix.diag_list_sum Matrix.diag_list_sum
@[simp]
theorem diag_multiset_sum [AddCommMonoid α] (s : Multiset (Matrix n n α)) :
diag s.sum = (s.map diag).sum :=
map_multiset_sum (diagAddMonoidHom n α) s
#align matrix.diag_multiset_sum Matrix.diag_multiset_sum
@[simp]
theorem diag_sum {ι} [AddCommMonoid α] (s : Finset ι) (f : ι → Matrix n n α) :
diag (∑ i in s, f i) = ∑ i in s, diag (f i) :=
map_sum (diagAddMonoidHom n α) f s
#align matrix.diag_sum Matrix.diag_sum
end Diag
section DotProduct
variable [Fintype m] [Fintype n]
/-- `dotProduct v w` is the sum of the entrywise products `v i * w i` -/
def dotProduct [Mul α] [AddCommMonoid α] (v w : m → α) : α :=
∑ i, v i * w i
#align matrix.dot_product Matrix.dotProduct
/- The precedence of 72 comes immediately after ` • ` for `SMul.smul`,
so that `r₁ • a ⬝ᵥ r₂ • b` is parsed as `(r₁ • a) ⬝ᵥ (r₂ • b)` here. -/
@[inherit_doc]
scoped infixl:72 " ⬝ᵥ " => Matrix.dotProduct
theorem dotProduct_assoc [NonUnitalSemiring α] (u : m → α) (w : n → α) (v : Matrix m n α) :
(fun j => u ⬝ᵥ fun i => v i j) ⬝ᵥ w = u ⬝ᵥ fun i => v i ⬝ᵥ w := by
simpa [dotProduct, Finset.mul_sum, Finset.sum_mul, mul_assoc] using Finset.sum_comm
#align matrix.dot_product_assoc Matrix.dotProduct_assoc
theorem dotProduct_comm [AddCommMonoid α] [CommSemigroup α] (v w : m → α) : v ⬝ᵥ w = w ⬝ᵥ v := by
simp_rw [dotProduct, mul_comm]
#align matrix.dot_product_comm Matrix.dotProduct_comm
@[simp]
theorem dotProduct_pUnit [AddCommMonoid α] [Mul α] (v w : PUnit → α) : v ⬝ᵥ w = v ⟨⟩ * w ⟨⟩ := by
simp [dotProduct]
#align matrix.dot_product_punit Matrix.dotProduct_pUnit
section MulOneClass
variable [MulOneClass α] [AddCommMonoid α]
theorem dotProduct_one (v : n → α) : v ⬝ᵥ 1 = ∑ i, v i := by simp [(· ⬝ᵥ ·)]
#align matrix.dot_product_one Matrix.dotProduct_one
theorem one_dotProduct (v : n → α) : 1 ⬝ᵥ v = ∑ i, v i := by simp [(· ⬝ᵥ ·)]
#align matrix.one_dot_product Matrix.one_dotProduct
end MulOneClass
section NonUnitalNonAssocSemiring
variable [NonUnitalNonAssocSemiring α] (u v w : m → α) (x y : n → α)
@[simp]
theorem dotProduct_zero : v ⬝ᵥ 0 = 0 := by simp [dotProduct]
#align matrix.dot_product_zero Matrix.dotProduct_zero
@[simp]
theorem dotProduct_zero' : (v ⬝ᵥ fun _ => 0) = 0 :=
dotProduct_zero v
#align matrix.dot_product_zero' Matrix.dotProduct_zero'
@[simp]
theorem zero_dotProduct : 0 ⬝ᵥ v = 0 := by simp [dotProduct]
#align matrix.zero_dot_product Matrix.zero_dotProduct
@[simp]
theorem zero_dotProduct' : (fun _ => (0 : α)) ⬝ᵥ v = 0 :=
zero_dotProduct v
#align matrix.zero_dot_product' Matrix.zero_dotProduct'
@[simp]
theorem add_dotProduct : (u + v) ⬝ᵥ w = u ⬝ᵥ w + v ⬝ᵥ w := by
simp [dotProduct, add_mul, Finset.sum_add_distrib]
#align matrix.add_dot_product Matrix.add_dotProduct
@[simp]
theorem dotProduct_add : u ⬝ᵥ (v + w) = u ⬝ᵥ v + u ⬝ᵥ w := by
simp [dotProduct, mul_add, Finset.sum_add_distrib]
#align matrix.dot_product_add Matrix.dotProduct_add
@[simp]
theorem sum_elim_dotProduct_sum_elim : Sum.elim u x ⬝ᵥ Sum.elim v y = u ⬝ᵥ v + x ⬝ᵥ y := by
simp [dotProduct]
#align matrix.sum_elim_dot_product_sum_elim Matrix.sum_elim_dotProduct_sum_elim
/-- Permuting a vector on the left of a dot product can be transferred to the right. -/
@[simp]
theorem comp_equiv_symm_dotProduct (e : m ≃ n) : u ∘ e.symm ⬝ᵥ x = u ⬝ᵥ x ∘ e :=
(e.sum_comp _).symm.trans <|
Finset.sum_congr rfl fun _ _ => by simp only [Function.comp, Equiv.symm_apply_apply]
#align matrix.comp_equiv_symm_dot_product Matrix.comp_equiv_symm_dotProduct
/-- Permuting a vector on the right of a dot product can be transferred to the left. -/
@[simp]
theorem dotProduct_comp_equiv_symm (e : n ≃ m) : u ⬝ᵥ x ∘ e.symm = u ∘ e ⬝ᵥ x := by
simpa only [Equiv.symm_symm] using (comp_equiv_symm_dotProduct u x e.symm).symm
#align matrix.dot_product_comp_equiv_symm Matrix.dotProduct_comp_equiv_symm
/-- Permuting vectors on both sides of a dot product is a no-op. -/
@[simp]
theorem comp_equiv_dotProduct_comp_equiv (e : m ≃ n) : x ∘ e ⬝ᵥ y ∘ e = x ⬝ᵥ y := by
-- Porting note: was `simp only` with all three lemmas
rw [← dotProduct_comp_equiv_symm]; simp only [Function.comp, Equiv.apply_symm_apply]
#align matrix.comp_equiv_dot_product_comp_equiv Matrix.comp_equiv_dotProduct_comp_equiv
end NonUnitalNonAssocSemiring
section NonUnitalNonAssocSemiringDecidable
variable [DecidableEq m] [NonUnitalNonAssocSemiring α] (u v w : m → α)
@[simp]
theorem diagonal_dotProduct (i : m) : diagonal v i ⬝ᵥ w = v i * w i := by
have : ∀ j ≠ i, diagonal v i j * w j = 0 := fun j hij => by
simp [diagonal_apply_ne' _ hij]
convert Finset.sum_eq_single i (fun j _ => this j) _ using 1 <;> simp
#align matrix.diagonal_dot_product Matrix.diagonal_dotProduct
@[simp]
theorem dotProduct_diagonal (i : m) : v ⬝ᵥ diagonal w i = v i * w i := by
have : ∀ j ≠ i, v j * diagonal w i j = 0 := fun j hij => by
simp [diagonal_apply_ne' _ hij]
convert Finset.sum_eq_single i (fun j _ => this j) _ using 1 <;> simp
#align matrix.dot_product_diagonal Matrix.dotProduct_diagonal
@[simp]
theorem dotProduct_diagonal' (i : m) : (v ⬝ᵥ fun j => diagonal w j i) = v i * w i := by
have : ∀ j ≠ i, v j * diagonal w j i = 0 := fun j hij => by
simp [diagonal_apply_ne _ hij]
convert Finset.sum_eq_single i (fun j _ => this j) _ using 1 <;> simp
#align matrix.dot_product_diagonal' Matrix.dotProduct_diagonal'
@[simp]
theorem single_dotProduct (x : α) (i : m) : Pi.single i x ⬝ᵥ v = x * v i := by
-- Porting note: (implicit arg) added `(f := fun _ => α)`
have : ∀ j ≠ i, Pi.single (f := fun _ => α) i x j * v j = 0 := fun j hij => by
simp [Pi.single_eq_of_ne hij]
convert Finset.sum_eq_single i (fun j _ => this j) _ using 1 <;> simp
#align matrix.single_dot_product Matrix.single_dotProduct
@[simp]
theorem dotProduct_single (x : α) (i : m) : v ⬝ᵥ Pi.single i x = v i * x := by
-- Porting note: (implicit arg) added `(f := fun _ => α)`
have : ∀ j ≠ i, v j * Pi.single (f := fun _ => α) i x j = 0 := fun j hij => by
simp [Pi.single_eq_of_ne hij]
convert Finset.sum_eq_single i (fun j _ => this j) _ using 1 <;> simp
#align matrix.dot_product_single Matrix.dotProduct_single
end NonUnitalNonAssocSemiringDecidable
section NonAssocSemiring
variable [NonAssocSemiring α]
@[simp]
theorem one_dotProduct_one : (1 : n → α) ⬝ᵥ 1 = Fintype.card n := by
simp [dotProduct]
#align matrix.one_dot_product_one Matrix.one_dotProduct_one
end NonAssocSemiring
section NonUnitalNonAssocRing
variable [NonUnitalNonAssocRing α] (u v w : m → α)
@[simp]
theorem neg_dotProduct : -v ⬝ᵥ w = -(v ⬝ᵥ w) := by simp [dotProduct]
#align matrix.neg_dot_product Matrix.neg_dotProduct
@[simp]
theorem dotProduct_neg : v ⬝ᵥ -w = -(v ⬝ᵥ w) := by simp [dotProduct]
#align matrix.dot_product_neg Matrix.dotProduct_neg
@[simp]
theorem sub_dotProduct : (u - v) ⬝ᵥ w = u ⬝ᵥ w - v ⬝ᵥ w := by simp [sub_eq_add_neg]
#align matrix.sub_dot_product Matrix.sub_dotProduct
@[simp]
theorem dotProduct_sub : u ⬝ᵥ (v - w) = u ⬝ᵥ v - u ⬝ᵥ w := by simp [sub_eq_add_neg]
#align matrix.dot_product_sub Matrix.dotProduct_sub
end NonUnitalNonAssocRing
section DistribMulAction
variable [Monoid R] [Mul α] [AddCommMonoid α] [DistribMulAction R α]
@[simp]
theorem smul_dotProduct [IsScalarTower R α α] (x : R) (v w : m → α) : x • v ⬝ᵥ w = x • (v ⬝ᵥ w) :=
by simp [dotProduct, Finset.smul_sum, smul_mul_assoc]
#align matrix.smul_dot_product Matrix.smul_dotProduct
@[simp]
theorem dotProduct_smul [SMulCommClass R α α] (x : R) (v w : m → α) : v ⬝ᵥ x • w = x • (v ⬝ᵥ w) :=
by simp [dotProduct, Finset.smul_sum, mul_smul_comm]
#align matrix.dot_product_smul Matrix.dotProduct_smul
end DistribMulAction
section StarRing
variable [NonUnitalSemiring α] [StarRing α] (v w : m → α)
theorem star_dotProduct_star : star v ⬝ᵥ star w = star (w ⬝ᵥ v) := by simp [dotProduct]
#align matrix.star_dot_product_star Matrix.star_dotProduct_star
theorem star_dotProduct : star v ⬝ᵥ w = star (star w ⬝ᵥ v) := by simp [dotProduct]
#align matrix.star_dot_product Matrix.star_dotProduct
theorem dotProduct_star : v ⬝ᵥ star w = star (w ⬝ᵥ star v) := by simp [dotProduct]
#align matrix.dot_product_star Matrix.dotProduct_star
end StarRing
end DotProduct
open 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. -/
-- We want to be lower priority than `instHMul`, but without this we can't have operands with
-- implicit dimensions.
@[default_instance 100]
instance [Fintype m] [Mul α] [AddCommMonoid α] :
HMul (Matrix l m α) (Matrix m n α) (Matrix l n α) where
hMul M N := fun i k => (fun j => M i j) ⬝ᵥ fun j => N j k
#align matrix.mul HMul.hMul
theorem mul_apply [Fintype m] [Mul α] [AddCommMonoid α] {M : Matrix l m α} {N : Matrix m n α}
{i k} : (M * N) i k = ∑ j, M i j * N j k :=
rfl
#align matrix.mul_apply Matrix.mul_apply
instance [Fintype n] [Mul α] [AddCommMonoid α] : Mul (Matrix n n α) where mul M N := M * N
#noalign matrix.mul_eq_mul
theorem mul_apply' [Fintype m] [Mul α] [AddCommMonoid α] {M : Matrix l m α} {N : Matrix m n α}
{i k} : (M * N) i k = (fun j => M i j) ⬝ᵥ fun j => N j k :=
rfl
#align matrix.mul_apply' Matrix.mul_apply'
theorem sum_apply [AddCommMonoid α] (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 _)
#align matrix.sum_apply Matrix.sum_apply
theorem two_mul_expl {R : Type*} [CommRing 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 := by
refine ⟨?_, ?_, ?_, ?_⟩ <;>
· rw [Matrix.mul_apply, Finset.sum_fin_eq_sum_range, Finset.sum_range_succ, Finset.sum_range_succ]
simp
#align matrix.two_mul_expl Matrix.two_mul_expl
section AddCommMonoid
variable [AddCommMonoid α] [Mul α]
@[simp]
theorem smul_mul [Fintype n] [Monoid R] [DistribMulAction R α] [IsScalarTower R α α] (a : R)
(M : Matrix m n α) (N : Matrix n l α) : (a • M) * N = a • (M * N) := by
ext
apply smul_dotProduct a
#align matrix.smul_mul Matrix.smul_mul
@[simp]
theorem mul_smul [Fintype n] [Monoid R] [DistribMulAction R α] [SMulCommClass R α α]
(M : Matrix m n α) (a : R) (N : Matrix n l α) : M * (a • N) = a • (M * N) := by
ext
apply dotProduct_smul
#align matrix.mul_smul Matrix.mul_smul
end AddCommMonoid