File size: 26,892 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Geometry.ComplexBall.PrescribedSignsWeak
import OAI.AlgebraicGeometry.ComplexMultiplication.CM.TraceForm
import Mathlib.LinearAlgebra.TensorProduct.Pi
import Mathlib.LinearAlgebra.TensorProduct.Prod

/-! # Prescribed Signs Period Transport -/

namespace OAI.Geometry.ComplexBall.PrescribedSignsPeriodTransport.Intrinsic

noncomputable section

section

open scoped Classical TensorProduct
open OAI.AlgebraicGeometry.ComplexMultiplication.CM.TraceForm

variable {E Place : Type*} [Field E] [Algebra β„š E] [NumberField E]

abbrev V (E : Type*) := Fin 3 β†’ E
abbrev LineTensor (E : Type*) [Field E] := V E βŠ—[E] E
abbrev SumTensor (E : Type*) [Field E] := V E βŠ—[E] (E Γ— E)

def regroup (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1)) :
    (Fin 3 β†’ (E →ₐ[β„š] β„‚) β†’ β„‚) ≃ₗ[β„‚] PrescribedSignsB.Ambient Place := by
  let e : Place Γ— Bool ≃ (E →ₐ[β„š] β„‚) := Equiv.ofBijective _ hΟƒ
  refine {
    toFun := fun x Ο„ => (fun j => x j (Οƒ Ο„), fun j => x j (AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ Ο„)))
    invFun := fun x j ρ => if (e.symm ρ).2 then (x (e.symm ρ).1).2 j else (x (e.symm ρ).1).1 j
    left_inv := ?_
    right_inv := ?_
    map_add' := fun _ _ => rfl
    map_smul' := fun _ _ => rfl }
  Β· intro x
    funext j ρ
    have he : (if (e.symm ρ).2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ (e.symm ρ).1) else Οƒ (e.symm ρ).1) = ρ := e.apply_symm_apply ρ
    dsimp only
    split_ifs at he ⊒ with hb
    Β· exact congrArg (x j) he
    Β· exact congrArg (x j) he
  Β· intro x
    funext Ο„
    have hfalse : e.symm (Οƒ Ο„) = (Ο„, false) := e.symm_apply_apply (Ο„, false)
    have htrue : e.symm (AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ Ο„)) = (Ο„, true) := e.symm_apply_apply (Ο„, true)
    apply Prod.ext <;> funext j <;> simp [hfalse, htrue]

def vectorCoordinates (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1)) :
    AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (V E) ≃ₗ[β„‚] PrescribedSignsB.Ambient Place :=
  (TensorProduct.piRight β„š β„‚ β„‚ (fun _ : Fin 3 => E)) β‰ͺ≫ₗ
    (LinearEquiv.piCongrRight (fun _ => AlgebraicGeometry.ComplexMultiplication.EmbeddingDecomposition.CM.fieldComplexificationEquiv E)) β‰ͺ≫ₗ regroup Οƒ hΟƒ

lemma vectorCoordinates_tmul (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (z : β„‚) (x : V E) :
    vectorCoordinates Οƒ hΟƒ (z βŠ—β‚œ[β„š] x) = z β€’ PrescribedSignsB.embeddingCoordinates Οƒ x := by
  ext Ο„ j <;> exact AlgebraicGeometry.ComplexMultiplication.EmbeddingDecomposition.CM.fieldComplexificationEquiv_tmul _ _ _ _

lemma vectorCoordinates_conjugation (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (x : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (V E)) :
    vectorCoordinates σ hσ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation (V E) x) =
      PrescribedSignsB.realConj (vectorCoordinates σ hσ x) := by
  induction x using TensorProduct.inductionOn with
  | add x y hx hy =>
      simp only [map_add, hx, hy]
      exact (PrescribedSignsB.realConjLinearMap Place).map_add _ _ |>.symm
  | tmul z x =>
      rw [AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_tmul, vectorCoordinates_tmul, vectorCoordinates_tmul]
      ext Ο„ j <;> simp [PrescribedSignsB.realConj, PrescribedSignsB.embeddingCoordinates, AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding_apply]

def lineReduction : LineTensor E ≃ₗ[E] V E := TensorProduct.rid E (V E)

def lineCoordinates (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1)) :
    AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (LineTensor E) ≃ₗ[β„‚] PrescribedSignsB.Ambient Place :=
  LinearEquiv.baseChange β„š β„‚ _ _ ((lineReduction (E := E)).restrictScalars β„š) β‰ͺ≫ₗ vectorCoordinates Οƒ hΟƒ

lemma lineCoordinates_tmul (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (z : β„‚) (v : V E) (w : E) :
    lineCoordinates Οƒ hΟƒ (z βŠ—β‚œ[β„š] (v βŠ—β‚œ[E] w)) =
      z β€’ PrescribedSignsB.embeddingCoordinates Οƒ (w β€’ v) := by
  simp [lineCoordinates, lineReduction, vectorCoordinates_tmul]

lemma lineCoordinates_conjugation (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (x : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (LineTensor E)) :
    lineCoordinates σ hσ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation (LineTensor E) x) =
      PrescribedSignsB.realConj (lineCoordinates σ hσ x) := by
  exact congrArg (vectorCoordinates σ hσ)
    (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.baseChange_coefficientConjugation ((lineReduction (E := E)).restrictScalars β„š).toLinearMap x)
      |>.trans (vectorCoordinates_conjugation σ hσ _)

def sumReduction : SumTensor E ≃ₗ[E] LineTensor E Γ— LineTensor E :=
  TensorProduct.prodRight E E (V E) E E

def sumCoordinates (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1)) :
    AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (SumTensor E) ≃ₗ[β„‚] PrescribedSignsB.Ambient Place Γ— PrescribedSignsB.Ambient Place :=
  LinearEquiv.baseChange β„š β„‚ _ _ ((sumReduction (E := E)).restrictScalars β„š) β‰ͺ≫ₗ
    TensorProduct.prodRight β„š β„‚ β„‚ (LineTensor E) (LineTensor E) β‰ͺ≫ₗ
      (lineCoordinates σ hσ).prodCongr (lineCoordinates σ hσ)

lemma sumCoordinates_tmul (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (z : β„‚) (v : V E) (w : E Γ— E) :
    sumCoordinates Οƒ hΟƒ (z βŠ—β‚œ[β„š] (v βŠ—β‚œ[E] w)) =
      (z β€’ PrescribedSignsB.embeddingCoordinates Οƒ (w.1 β€’ v), z β€’ PrescribedSignsB.embeddingCoordinates Οƒ (w.2 β€’ v)) := by
  simp [sumCoordinates, sumReduction, lineCoordinates_tmul]

lemma sumCoordinates_conjugation (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (x : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (SumTensor E)) :
    sumCoordinates σ hσ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation (SumTensor E) x) =
      (PrescribedSignsB.realConj (sumCoordinates σ hσ x).1, PrescribedSignsB.realConj (sumCoordinates σ hσ x).2) := by
  induction x using TensorProduct.inductionOn with
  | add x y hx hy =>
      simp only [map_add, hx, hy, Prod.fst_add, Prod.snd_add]
      exact Prod.ext ((PrescribedSignsB.realConjLinearMap Place).map_add _ _).symm
        ((PrescribedSignsB.realConjLinearMap Place).map_add _ _).symm
  | tmul z v =>
      simp only [AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_tmul, sumCoordinates,
        LinearEquiv.trans_apply, LinearEquiv.baseChange_tmul,
        TensorProduct.prodRight_tmul, LinearEquiv.prodCongr_apply]
      apply Prod.ext
      Β· simpa only [AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_tmul] using
          (lineCoordinates_conjugation Οƒ hΟƒ (z βŠ—β‚œ[β„š] (((sumReduction (E := E)).restrictScalars β„š) v).1))
      Β· simpa only [AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_tmul] using
          (lineCoordinates_conjugation Οƒ hΟƒ (z βŠ—β‚œ[β„š] (((sumReduction (E := E)).restrictScalars β„š) v).2))

end

section

open scoped Classical TensorProduct
open OAI.AlgebraicGeometry.ComplexMultiplication.CM.TraceForm

variable {E Place : Type*} [Field E] [Algebra β„š E] [NumberField E]
  [NumberField.IsCMField E] [Fintype Place]

def rationalVectorForm (Ξ΄ a : E) (H : Matrix (Fin 3) (Fin 3) E) :
    LinearMap.BilinForm β„š (V E) :=
  βˆ‘ j, βˆ‘ k, (AlgebraicGeometry.ComplexMultiplication.CM.TraceForm.cmTracePairing E (Ξ΄ * a * H j k)).flip.compl₁₂
    (LinearMap.proj j) (LinearMap.proj k)

lemma rationalVectorForm_apply (Ξ΄ a : E) (H : Matrix (Fin 3) (Fin 3) E) (x y : V E) :
    rationalVectorForm Ξ΄ a H x y = Algebra.trace β„š E
      (Ξ΄ * a * (βˆ‘ j, βˆ‘ k, x j * H j k * NumberField.IsCMField.complexConj E (y k))) := by
  simp only [rationalVectorForm, LinearMap.sum_apply, LinearMap.compl₁₂_apply,
    LinearMap.proj_apply, Finset.mul_sum, map_sum]
  apply Finset.sum_congr rfl
  intro j _
  apply Finset.sum_congr rfl
  intro k _
  change Algebra.trace β„š E (Ξ΄ * a * H j k * NumberField.IsCMField.complexConj E (y k) * x j) = _
  congr 1
  ring

def coordinateForm (B : Place β†’ PrescribedSignsB.Bilin) (t : Place β†’ β„‚) :
    LinearMap.BilinForm β„‚ (PrescribedSignsB.Ambient Place) :=
  βˆ‘ Ο„, t Ο„ β€’
    ((B Ο„).compl₁₂
      ((LinearMap.fst β„‚ PrescribedSignsB.Vec PrescribedSignsB.Vec).comp (LinearMap.proj Ο„))
      ((LinearMap.snd β„‚ PrescribedSignsB.Vec PrescribedSignsB.Vec).comp (LinearMap.proj Ο„)) -
    (B Ο„).flip.compl₁₂
      ((LinearMap.snd β„‚ PrescribedSignsB.Vec PrescribedSignsB.Vec).comp (LinearMap.proj Ο„))
      ((LinearMap.fst β„‚ PrescribedSignsB.Vec PrescribedSignsB.Vec).comp (LinearMap.proj Ο„)))

lemma coordinateForm_apply (B : Place β†’ PrescribedSignsB.Bilin) (t : Place β†’ β„‚) (x y : PrescribedSignsB.Ambient Place) :
    coordinateForm B t x y = PrescribedSignsB.tracePairing (fun Ο„ u v => B Ο„ u v) t x y := by
  simp [coordinateForm, PrescribedSignsB.tracePairing]

lemma vectorForm_transport (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (Ξ΄ a : E) (hΞ΄ : NumberField.IsCMField.complexConj E Ξ΄ = -Ξ΄)
    (ha : NumberField.IsCMField.complexConj E a = a)
    (H : Matrix (Fin 3) (Fin 3) E)
    (hH : βˆ€ j k, H j k = NumberField.IsCMField.complexConj E (H k j))
    (x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (V E)) :
    (rationalVectorForm Ξ΄ a H).baseChange β„‚ x y =
      coordinateForm (PrescribedSignsB.matrixB Οƒ H) (fun Ο„ => Οƒ Ο„ (Ξ΄ * a))
        (vectorCoordinates σ hσ x) (vectorCoordinates σ hσ y) := by
  induction x using TensorProduct.inductionOn with
  | add x x' hx hx' => simp only [map_add, LinearMap.add_apply, hx, hx']
  | tmul z v =>
    induction y using TensorProduct.inductionOn with
    | add y y' hy hy' => simp only [map_add, hy, hy']
    | tmul w v' =>
      rw [LinearMap.BilinForm.baseChange_tmul, vectorCoordinates_tmul, vectorCoordinates_tmul]
      simp only [map_smul, LinearMap.smul_apply, smul_eq_mul]
      rw [coordinateForm_apply]
      have he : (fun Ο„ u v => PrescribedSignsB.matrixB Οƒ H Ο„ u v) = PrescribedSignsB.localB Οƒ H := by
        funext Ο„ u v
        exact PrescribedSignsB.matrixB_apply Οƒ H Ο„ u v
      rw [he, PrescribedSignsB.tracePairing_rational σ hσ δ a hδ ha H hH, rationalVectorForm_apply]
      simp only [Algebra.smul_def]
      change (↑(Algebra.trace β„š E (Ξ΄ * a * (βˆ‘ j, βˆ‘ k, v j * H j k * NumberField.IsCMField.complexConj E (v' k)))) : β„‚) * (z * w) = _
      ring

def rationalLineForm (Ξ΄ a : E) (H : Matrix (Fin 3) (Fin 3) E) :
    LinearMap.BilinForm β„š (LineTensor E) :=
  (rationalVectorForm Ξ΄ a H).compl₁₂
    ((lineReduction (E := E)).restrictScalars β„š).toLinearMap
    ((lineReduction (E := E)).restrictScalars β„š).toLinearMap

lemma baseChange_compl {X Y : Type*} [AddCommGroup X] [Module β„š X]
    [AddCommGroup Y] [Module β„š Y] (B : LinearMap.BilinForm β„š Y) (f : X β†’β‚—[β„š] Y)
    (x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) :
    LinearMap.BilinForm.baseChange β„‚ (B.compl₁₂ f f) x y =
      B.baseChange β„‚ (f.baseChange β„‚ x) (f.baseChange β„‚ y) := by
  induction x using TensorProduct.inductionOn with
  | add x x' hx hx' => simp only [map_add, LinearMap.add_apply, hx, hx']
  | tmul z v =>
    induction y using TensorProduct.inductionOn with
    | add y y' hy hy' => simp only [map_add, hy, hy']
    | tmul w v' => simp

lemma lineForm_transport (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (Ξ΄ a : E) (hΞ΄ : NumberField.IsCMField.complexConj E Ξ΄ = -Ξ΄)
    (ha : NumberField.IsCMField.complexConj E a = a)
    (H : Matrix (Fin 3) (Fin 3) E)
    (hH : βˆ€ j k, H j k = NumberField.IsCMField.complexConj E (H k j))
    (x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (LineTensor E)) :
    (rationalLineForm Ξ΄ a H).baseChange β„‚ x y =
      coordinateForm (PrescribedSignsB.matrixB Οƒ H) (fun Ο„ => Οƒ Ο„ (Ξ΄ * a))
        (lineCoordinates σ hσ x) (lineCoordinates σ hσ y) := by
  rw [rationalLineForm, baseChange_compl]
  exact vectorForm_transport σ hσ δ a hδ ha H hH _ _

lemma rationalLineForm_tmul (Ξ΄ a : E) (H : Matrix (Fin 3) (Fin 3) E)
    (v v' : V E) (w w' : E) :
    rationalLineForm Ξ΄ a H (v βŠ—β‚œ[E] w) (v' βŠ—β‚œ[E] w') =
      Algebra.trace β„š E (Ξ΄ *
        (βˆ‘ j, βˆ‘ k, v j * H j k * NumberField.IsCMField.complexConj E (v' k)) *
        (a * w * NumberField.IsCMField.complexConj E w')) := by
  rw [rationalLineForm, LinearMap.compl₁₂_apply, rationalVectorForm_apply]
  change Algebra.trace β„š E (Ξ΄ * a * (βˆ‘ j, βˆ‘ k, (w * v j) * H j k * NumberField.IsCMField.complexConj E (w' * v' k))) = _
  congr 1
  simp only [map_mul]
  simp only [Finset.mul_sum, Finset.sum_mul]
  apply Finset.sum_congr rfl
  intro j _
  apply Finset.sum_congr rfl
  intro k _
  ring

end

section

open scoped Classical TensorProduct
open OAI.AlgebraicGeometry.ComplexMultiplication.CM.TraceForm

lemma positivePeriod_transport {X Y : Type*}
    [AddCommGroup X] [Module β„‚ X] [AddCommGroup Y] [Module β„‚ Y]
    (e : X ≃ₗ[β„‚] Y) (r : β„•) (ψ : Y β†’ Y β†’ β„‚) (ΞΊ : Y β†’ Y)
    (ΞΊX : X β†’ X) (hc : βˆ€ x, e (ΞΊX x) = ΞΊ (e x))
    (P : Submodule β„‚ Y) (h : PrescribedSignsB.PositivePeriod r ψ ΞΊ P) :
    PrescribedSignsB.PositivePeriod r (fun x y => ψ (e x) (e y)) κX (P.comap e.toLinearMap) := by
  have hm : (P.comap e.toLinearMap).map e.toLinearMap = P := by
    exact Submodule.map_comap_eq_of_surjective e.surjective P
  refine ⟨?_, ?_, ?_, ?_, ?_⟩
  Β· exact (e.ofSubmodule' P).finrank_eq.trans h.1
  Β· exact (Submodule.Quotient.equiv _ _ e hm).finrank_eq.trans h.2.1
  Β· intro u hu v hv
    exact h.2.2.1 (e u) hu (e v) hv
  Β· intro u hu hne
    dsimp only
    rw [hc]
    exact h.2.2.2.1 (e u) hu (fun he => hne (e.injective (he.trans e.map_zero.symm)))
  Β· intro x
    obtain ⟨uv, huv, huniq⟩ := h.2.2.2.2 (e x)
    refine ⟨(e.symm uv.1, e.symm uv.2), ⟨?_, ?_, ?_⟩, ?_⟩
    Β· simpa only [Submodule.mem_comap, LinearEquiv.coe_coe, e.apply_symm_apply] using huv.1
    Β· simpa only [Submodule.mem_comap, LinearEquiv.coe_coe, e.apply_symm_apply] using huv.2.1
    Β· apply e.injective
      simpa only [map_add, hc, e.apply_symm_apply] using huv.2.2
    Β· intro ab hab
      have he : (e ab.1, e ab.2) = uv := huniq _ ⟨hab.1, hab.2.1, by
        rw [hab.2.2, map_add, hc]⟩
      exact Prod.ext (e.eq_symm_apply.mpr (congrArg Prod.fst he))
        (e.eq_symm_apply.mpr (congrArg Prod.snd he))

lemma linePositivePeriod_transport
    {E Place : Type*} [Field E] [Algebra β„š E] [NumberField E]
    [NumberField.IsCMField E] [Fintype Place]
    (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (Ξ΄ a : E) (hΞ΄ : NumberField.IsCMField.complexConj E Ξ΄ = -Ξ΄)
    (ha : NumberField.IsCMField.complexConj E a = a)
    (H : Matrix (Fin 3) (Fin 3) E)
    (hH : βˆ€ j k, H j k = NumberField.IsCMField.complexConj E (H k j))
    (r : β„•) (P : Submodule β„‚ (PrescribedSignsB.Ambient Place))
    (hp : PrescribedSignsB.PositivePeriod r
      (fun x y => coordinateForm (PrescribedSignsB.matrixB Οƒ H) (fun Ο„ => Οƒ Ο„ (Ξ΄ * a)) x y)
      PrescribedSignsB.realConj P) :
    PrescribedSignsB.PositivePeriod r (fun x y => (rationalLineForm Ξ΄ a H).baseChange β„‚ x y)
      (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation (LineTensor E))
      (P.comap (lineCoordinates σ hσ).toLinearMap) := by
  have ht := positivePeriod_transport (lineCoordinates σ hσ) r _ PrescribedSignsB.realConj
    (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation (LineTensor E)) (lineCoordinates_conjugation σ hσ) P hp
  have he : (fun x y => (rationalLineForm Ξ΄ a H).baseChange β„‚ x y) =
      (fun x y => coordinateForm (PrescribedSignsB.matrixB Οƒ H) (fun Ο„ => Οƒ Ο„ (Ξ΄ * a))
        (lineCoordinates σ hσ x) (lineCoordinates σ hσ y)) := by
    funext x y
    exact lineForm_transport σ hσ δ a hδ ha H hH x y
  rw [he]
  exact ht

end

section

open scoped Classical

/-- The homology complex structure acts by `-i` on the filtration and by
`i` on its conjugate. Its Riemann form therefore has the opposite sign to
our positive-period trace form. -/
lemma riemannForm_sign {Y : Type*} [AddCommGroup Y] [Module β„‚ Y]
    (ψ : LinearMap.BilinForm β„‚ Y) (u v : Y)
    (hu : ψ u u = 0) (hv : ψ v v = 0) (hskew : ψ v u = -ψ u v)
    (hpos : 0 < (ψ u v / Complex.I).re) :
    0 < (-ψ (u + v) ((-Complex.I) β€’ u + Complex.I β€’ v)).re := by
  have he : -ψ (u + v) ((-Complex.I) β€’ u + Complex.I β€’ v) =
      2 * (ψ u v / Complex.I) := by
    simp only [map_add, LinearMap.add_apply, map_smul, hu, hv, hskew, smul_eq_mul]
    simp only [div_eq_mul_inv, Complex.inv_I]
    ring
  rw [he]
  simpa using mul_pos (by norm_num : (0 : ℝ) < 2) hpos

lemma homologyComplexStructure_quotient {Y : Type*}
    [AddCommGroup Y] [Module β„‚ Y] (P : Submodule β„‚ Y)
    (u v : Y) (hu : u ∈ P) :
    P.mkQ ((-Complex.I) β€’ u + Complex.I β€’ v) = Complex.I β€’ P.mkQ (u + v) := by
  have hz : P.mkQ u = 0 := (Submodule.Quotient.mk_eq_zero P).mpr hu
  simp only [map_add, map_smul, hz, smul_zero, zero_add]

end

open scoped Classical TensorProduct
open OAI.AlgebraicGeometry.ComplexMultiplication.CM.TraceForm

lemma baseChange_conjugation {X : Type*} [AddCommGroup X] [Module β„š X]
    (B : LinearMap.BilinForm β„š X) (x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) :
    B.baseChange β„‚ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X x) (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X y) =
      star (B.baseChange β„‚ x y) := by
  induction x using TensorProduct.inductionOn with
  | add x x' hx hx' => simp only [map_add, LinearMap.add_apply, hx, hx', star_add]
  | tmul z v =>
    induction y using TensorProduct.inductionOn with
    | add y y' hy hy' => simp only [map_add, hy, hy', star_add]
    | tmul w v' => simp [Algebra.smul_def]

lemma coordinateForm_skew {Place : Type*} [Fintype Place]
    (B : Place β†’ PrescribedSignsB.Bilin) (t : Place β†’ β„‚) (x y : PrescribedSignsB.Ambient Place) :
    coordinateForm B t y x = -coordinateForm B t x y := by
  simp only [coordinateForm_apply, PrescribedSignsB.tracePairing, ← Finset.sum_neg_distrib]
  apply Finset.sum_congr rfl
  intro Ο„ _
  ring

/-- For a real vector written as `u + conjugate u`, the quotient complex
structure has the sign prescribed by `homologyComplexStructure_quotient`.
The negative rational trace form is strictly positive in that direction. -/
lemma rational_riemann_positive {X : Type*} [AddCommGroup X] [Module β„š X]
    (B : LinearMap.BilinForm β„š X)
    (hskew : βˆ€ x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X, B.baseChange β„‚ y x = -B.baseChange β„‚ x y)
    (r : β„•) (P : Submodule β„‚ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X))
    (hp : PrescribedSignsB.PositivePeriod r (fun x y => B.baseChange β„‚ x y) (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X) P)
    (u : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) (hu : u ∈ P) (hne : u β‰  0) :
    let v := AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u
    0 < (-B.baseChange β„‚ (u + v) ((-Complex.I) β€’ u + Complex.I β€’ v)).re := by
  apply riemannForm_sign (B.baseChange β„‚) u (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u)
  Β· exact hp.2.2.1 u hu u hu
  Β· have huu : B.baseChange β„‚ u u = 0 := hp.2.2.1 u hu u hu
    rw [baseChange_conjugation, huu, star_zero]
  Β· exact hskew _ _
  Β· exact (hp.2.2.2.1 u hu hne).2

lemma realVector_period_split {X : Type*} [AddCommGroup X] [Module β„š X]
    (ψ : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X β†’ AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X β†’ β„‚)
    (r : β„•) (P : Submodule β„‚ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X))
    (hp : PrescribedSignsB.PositivePeriod r ψ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X) P)
    (x : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) (hx : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X x = x) (hne : x β‰  0) :
    βˆƒ u ∈ P, u β‰  0 ∧ x = u + AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u := by
  obtain ⟨⟨u,v⟩, ⟨hu,hv,he⟩, hun⟩ := hp.2.2.2.2 x
  have hs : x = v + AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u := by
    calc
      x = AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X x := hx.symm
      _ = AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X (u + AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X v) := congrArg _ he
      _ = v + AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u := by
        rw [map_add, AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_involutive, add_comm]
  have heq : (v,u) = (u,v) := hun _ ⟨hv, hu, hs⟩
  have hvu : v = u := congrArg Prod.fst heq
  subst v
  refine ⟨u, hu, ?_, he⟩
  intro hz
  apply hne
  simpa only [hz, map_zero, add_zero] using he

lemma homologyJ_real {X : Type*} [AddCommGroup X] [Module β„š X]
    (u : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) :
    AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X ((-Complex.I) β€’ u + Complex.I β€’ AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u) =
      (-Complex.I) β€’ u + Complex.I β€’ AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u := by
  simp only [map_add, AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.coefficientConjugation_smul, star_neg, Complex.star_def,
    Complex.conj_I, neg_neg, AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation_involutive]
  exact add_comm _ _

lemma rationalRiemann_on_real {X : Type*} [AddCommGroup X] [Module β„š X]
    (B : LinearMap.BilinForm β„š X)
    (hskew : βˆ€ x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X, B.baseChange β„‚ y x = -B.baseChange β„‚ x y)
    (r : β„•) (P : Submodule β„‚ (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X))
    (hp : PrescribedSignsB.PositivePeriod r (fun x y => B.baseChange β„‚ x y) (AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X) P)
    (x : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X) (hx : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X x = x) (hne : x β‰  0) :
    βˆƒ j : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification X, AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X j = j ∧
      P.mkQ j = Complex.I β€’ P.mkQ x ∧ 0 < (-B.baseChange β„‚ x j).re := by
  obtain ⟨u, hu, hune, rfl⟩ := realVector_period_split _ r P hp x hx hne
  exact ⟨(-Complex.I) β€’ u + Complex.I β€’ AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.coefficientConjugation X u,
    homologyJ_real u, homologyComplexStructure_quotient P u _ hu,
    rational_riemann_positive B hskew r P hp u hu hune⟩

lemma lineForm_skew
    {E Place : Type*} [Field E] [Algebra β„š E] [NumberField E]
    [NumberField.IsCMField E] [Fintype Place]
    (Οƒ : Place β†’ E →ₐ[β„š] β„‚)
    (hσ : Function.Bijective (fun p : Place × Bool =>
      if p.2 then AlgebraicGeometry.ComplexMultiplication.EmbeddingHodgeType.CM.conjugateEmbedding (Οƒ p.1) else Οƒ p.1))
    (Ξ΄ a : E) (hΞ΄ : NumberField.IsCMField.complexConj E Ξ΄ = -Ξ΄)
    (ha : NumberField.IsCMField.complexConj E a = a)
    (H : Matrix (Fin 3) (Fin 3) E)
    (hH : βˆ€ j k, H j k = NumberField.IsCMField.complexConj E (H k j))
    (x y : AlgebraicGeometry.ComplexMultiplication.WeightOne.CM.Complexification (LineTensor E)) :
    (rationalLineForm Ξ΄ a H).baseChange β„‚ y x =
      -(rationalLineForm Ξ΄ a H).baseChange β„‚ x y := by
  rw [lineForm_transport σ hσ δ a hδ ha H hH y x,
    lineForm_transport σ hσ δ a hδ ha H hH x y]
  exact coordinateForm_skew _ _ _ _

end

end OAI.Geometry.ComplexBall.PrescribedSignsPeriodTransport.Intrinsic