mathematics_in_lean

My solutions for this book

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic
import Mathlib.LinearAlgebra.Eigenspace.Minpoly
import Mathlib.LinearAlgebra.Charpoly.Basic

import MIL.Common



variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V]


example (a : K) (u v : V) : a  (u + v) = a  u + a  v :=
  smul_add a u v

example (a b : K) (u : V) : (a + b)  u = a  u + b  u :=
  add_smul a b u

example (a b : K) (u : V) : a  b  u = b  a  u :=
  smul_comm a b u

def mkVectorSpace {K X : Type*} [Field K] [AddCommGroup X] (ρ : K +* AddMonoid.End X) : Module K X where
  smul a x := ρ a x
  one_smul x := show ρ 1 x = x by simp
  mul_smul a b x := show ρ (a * b) _ = _ by rw [map_mul]; rfl
  smul_zero x := map_zero ..
  smul_add a x y := map_add ..
  add_smul a b x := show ρ (a + b) _ = _ by rw [map_add]; rfl
  zero_smul x := show ρ 0 _ = _ by simp

example {R M : Type*} [CommSemiring R] [AddCommMonoid M] [Module R M] :
    Module (Ideal R) (Submodule R M) :=
  inferInstance



variable {W : Type*} [AddCommGroup W] [Module K W]

variable (φ : V [K] W)

example (a : K) (v : V) : φ (a  v) = a  φ v :=
  map_smul φ a v

example (v w : V) : φ (v + w) = φ v + φ w :=
  map_add φ v w


variable (ψ : V [K] W)

#check (2  φ + ψ : V [K] W)


variable (θ : W [K] V)

#check (φ.comp θ : W [K] W)
#check (φ  θ : W [K] W)


example : V [K] V where
  toFun v := 3  v
  map_add' _ _ := smul_add ..
  map_smul' _ _ := smul_comm ..



#check (φ.map_add' :  x y : V, φ.toFun (x + y) = φ.toFun x + φ.toFun y)
#check (φ.map_add :  x y : V, φ (x + y) = φ x + φ y)
#check (map_add φ :  x y : V, φ (x + y) = φ x + φ y)



#check (LinearMap.lsmul K V 3 : V [K] V)
#check (LinearMap.lsmul K V : K [K] V [K] V)


example (f : V [K] W) : f  f.symm = LinearEquiv.refl K V :=
  f.self_trans_symm

noncomputable example (f : V [K] W) (h : Function.Bijective f) : V [K] W :=
  .ofBijective f h


section binary_product

variable {W : Type*} [AddCommGroup W] [Module K W]
variable {U : Type*} [AddCommGroup U] [Module K U]
variable {T : Type*} [AddCommGroup T] [Module K T]

-- First projection map
example : V × W [K] V := LinearMap.fst K V W

-- Second projection map
example : V × W [K] W := LinearMap.snd K V W

-- Universal property of the product
example (φ : U [K] V) (ψ : U [K] W) : U [K]  V × W := LinearMap.prod φ ψ

-- The product map does the expected thing, first component
example (φ : U [K] V) (ψ : U [K] W) : LinearMap.fst K V W  LinearMap.prod φ ψ = φ := rfl

-- The product map does the expected thing, second component
example (φ : U [K] V) (ψ : U [K] W) : LinearMap.snd K V W  LinearMap.prod φ ψ = ψ := rfl

-- We can also combine maps in parallel
example (φ : V [K] U) (ψ : W [K] T) : (V × W) [K] (U × T) := φ.prodMap ψ

-- This is simply done by combining the projections with the universal property
example (φ : V [K] U) (ψ : W [K] T) :
  φ.prodMap ψ = (φ  .fst K V W).prod (ψ  .snd K V W) := rfl

-- First inclusion map
example : V [K] V × W := LinearMap.inl K V W

-- Second inclusion map
example : W [K] V × W := LinearMap.inr K V W

-- Universal property of the sum (aka coproduct)
example (φ : V [K] U) (ψ : W [K] U) : V × W [K] U := φ.coprod ψ

-- The coproduct map does the expected thing, first component
example (φ : V [K] U) (ψ : W [K] U) : φ.coprod ψ  LinearMap.inl K V W = φ :=
  LinearMap.coprod_inl φ ψ

-- The coproduct map does the expected thing, second component
example (φ : V [K] U) (ψ : W [K] U) : φ.coprod ψ  LinearMap.inr K V W = ψ :=
  LinearMap.coprod_inr φ ψ

-- The coproduct map is defined in the expected way
example (φ : V [K] U) (ψ : W [K] U) (v : V) (w : W) :
    φ.coprod ψ (v, w) = φ v + ψ w :=
  rfl

end binary_product


section families
open DirectSum

variable {ι : Type*} [DecidableEq ι]
         (V : ι  Type*) [ i, AddCommGroup (V i)] [ i, Module K (V i)]

-- The universal property of the direct sum assembles maps from the summands to build
-- a map from the direct sum
example (φ : Π i, (V i [K] W)) : ( i, V i) [K] W :=
  DirectSum.toModule K ι W φ

-- The universal property of the direct product assembles maps into the factors
-- to build a map into the direct product
example (φ : Π i, (W [K] V i)) : W [K] (Π i, V i) :=
  LinearMap.pi φ

-- The projection maps from the product
example (i : ι) : (Π j, V j) [K] V i := LinearMap.proj i

-- The inclusion maps into the sum
example (i : ι) : V i [K] ( i, V i) := DirectSum.lof K ι V i

-- The inclusion maps into the product
example (i : ι) : V i [K] (Π i, V i) := LinearMap.single K V i

-- In case `ι` is a finite type, there is an isomorphism between the sum and product.
example [Fintype ι] : ( i, V i) [K] (Π i, V i) :=
  linearEquivFunOnFintype K ι V

end families