File size: 21,027 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
459
460
461
462
import Mathlib

/-!
The faithful complex representation needed for a signed Moore determinant.
The representation is algebraic in arbitrary matrix dimension. The intended
determinant is the Pfaffian of the associated complex skew-symmetric matrix,
not a nonnegative root of its ordinary determinant.
-/

namespace OAI.LinearAlgebra.Moore

open Matrix

/-- Standard faithful complex representation of the real quaternions. -/
def quaternionMatrix (q : Quaternion ℝ) : Matrix (Fin 2) (Fin 2) β„‚ :=
  !![⟨q.re, q.imI⟩, ⟨q.imJ, q.imK⟩; ⟨-q.imJ, q.imK⟩, ⟨q.re, -q.imI⟩]

theorem quaternionMatrix_add (p q : Quaternion ℝ) :
    quaternionMatrix (p + q) = quaternionMatrix p + quaternionMatrix q := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;> simp [quaternionMatrix] <;> ring

theorem quaternionMatrix_zero : quaternionMatrix 0 = 0 := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;> simp [quaternionMatrix]

theorem quaternionMatrix_one : quaternionMatrix 1 = 1 := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;> simp [quaternionMatrix]

theorem quaternionMatrix_mul (p q : Quaternion ℝ) :
    quaternionMatrix (p * q) = quaternionMatrix p * quaternionMatrix q := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;>
    simp [quaternionMatrix, Matrix.mul_apply, Fin.sum_univ_two] <;> ring

/-- The representation respects the full noncommutative quaternion algebra. -/
def quaternionMatrixHom : Quaternion ℝ β†’+* Matrix (Fin 2) (Fin 2) β„‚ where
  toFun := quaternionMatrix
  map_zero' := quaternionMatrix_zero
  map_one' := quaternionMatrix_one
  map_add' := quaternionMatrix_add
  map_mul' := quaternionMatrix_mul

theorem quaternionMatrix_star (q : Quaternion ℝ) :
    quaternionMatrix (star q) = (quaternionMatrix q)α΄΄ := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;> simp [quaternionMatrix]

theorem quaternionMatrix_injective : Function.Injective quaternionMatrix := by
  intro p q h
  have h00 := congrArg (fun M : Matrix (Fin 2) (Fin 2) β„‚ => M 0 0) h
  have h01 := congrArg (fun M : Matrix (Fin 2) (Fin 2) β„‚ => M 0 1) h
  have hr := congrArg Complex.re h00
  have hi := congrArg Complex.im h00
  have hj := congrArg Complex.re h01
  have hk := congrArg Complex.im h01
  exact Quaternion.ext p q hr hi hj hk

/-- Complexification of a rectangular quaternionic matrix in interleaved order. -/
def complexify {m n : Type*} (A : Matrix m n (Quaternion ℝ)) :
    Matrix (m Γ— Fin 2) (n Γ— Fin 2) β„‚ :=
  fun i j => quaternionMatrix (A i.1 j.1) i.2 j.2

theorem complexify_add {m n : Type*} (A B : Matrix m n (Quaternion ℝ)) :
    complexify (A + B) = complexify A + complexify B := by
  ext ⟨i, a⟩ ⟨j, b⟩
  exact congrArg (fun M : Matrix (Fin 2) (Fin 2) β„‚ => M a b)
    (quaternionMatrix_add (A i j) (B i j))

theorem complexify_star {m n : Type*} (A : Matrix m n (Quaternion ℝ)) :
    complexify Aα΄΄ = (complexify A)α΄΄ := by
  ext ⟨i, a⟩ ⟨j, b⟩
  exact congrArg (fun M : Matrix (Fin 2) (Fin 2) β„‚ => M a b)
    (quaternionMatrix_star (A j i))

theorem complexify_injective {m n : Type*} :
    Function.Injective (complexify : Matrix m n (Quaternion ℝ) β†’ _) := by
  intro A B h
  funext i j
  apply quaternionMatrix_injective
  ext a b
  exact congrArg (fun M : Matrix (m Γ— Fin 2) (n Γ— Fin 2) β„‚ => M (i, a) (j, b)) h

theorem complexify_mul {m n p : Type*} [Fintype n]
    (A : Matrix m n (Quaternion ℝ)) (B : Matrix n p (Quaternion ℝ)) :
    complexify (A * B) = complexify A * complexify B := by
  ext ⟨i, a⟩ ⟨j, b⟩
  change quaternionMatrixHom (βˆ‘ k, A i k * B k j) a b =
    βˆ‘ k : n Γ— Fin 2, quaternionMatrix (A i k.1) a k.2 * quaternionMatrix (B k.1 j) k.2 b
  simp only [map_sum, map_mul, Matrix.sum_apply, Matrix.mul_apply, Fintype.sum_prod_type]
  rfl

/-- Quaternionic Hermitian matrices become complex Hermitian matrices. -/
theorem complexify_isHermitian {n : Type*} {A : Matrix n n (Quaternion ℝ)}
    (hA : A.IsHermitian) : (complexify A).IsHermitian := by
  rw [Matrix.IsHermitian, ← complexify_star, hA]

/-- The standard complex symplectic block, with Pfaffian normalized to one. -/
def symplecticUnit : Matrix (Fin 2) (Fin 2) β„‚ := !![0, 1; -1, 0]

def skewQuaternion (q : Quaternion ℝ) : Matrix (Fin 2) (Fin 2) β„‚ :=
  quaternionMatrix q * symplecticUnit

theorem skewQuaternion_star (q : Quaternion ℝ) :
    skewQuaternion (star q) = -(skewQuaternion q)α΅€ := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;>
    simp [skewQuaternion, quaternionMatrix, symplecticUnit, Matrix.mul_apply,
      Fin.sum_univ_two]

/-- Block form of `Ο‡(A) J`, in interleaved order. -/
def skewComplexify {n : Type*} (A : Matrix n n (Quaternion ℝ)) :
    Matrix (n Γ— Fin 2) (n Γ— Fin 2) β„‚ :=
  fun i j => skewQuaternion (A i.1 j.1) i.2 j.2

/-- Hermitian quaternionic matrices produce skew-symmetric complex matrices. -/
theorem skewComplexify_transpose {n : Type*} {A : Matrix n n (Quaternion ℝ)}
    (hA : A.IsHermitian) : (skewComplexify A)α΅€ = -skewComplexify A := by
  ext ⟨i, a⟩ ⟨j, b⟩
  have hij : A j i = star (A i j) := by
    simpa only [Matrix.conjTranspose_apply] using congrArg (fun M => M j i) hA.symm
  change skewQuaternion (A j i) b a = -(skewQuaternion (A i j) a b)
  rw [hij, skewQuaternion_star]
  rfl

/-- Explicit Pfaffian polynomial on paired indices. The normalization divides
out the `2^n n!` choices of orientation and ordering of the matching pairs. -/
noncomputable def pfaffian (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) : β„‚ :=
  (2 ^ n * (n.factorial : β„‚))⁻¹ *
    βˆ‘ Οƒ : Equiv.Perm (Fin n Γ— Fin 2),
      ((Equiv.Perm.sign Οƒ : β„€) : β„‚) * ∏ i : Fin n, M (Οƒ (i, 0)) (Οƒ (i, 1))

/-- Signed Moore determinant candidate, defined by the explicit Pfaffian
polynomial. Proving its full determinant identities remains separate work. -/
noncomputable def mooreDet (n : β„•) (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) : ℝ :=
  (pfaffian n (skewComplexify A)).re

/-- The Pfaffian is homogeneous of degree `n`, including on indefinite data. -/
theorem pfaffian_smul (n : β„•) (c : β„‚)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    pfaffian n (c β€’ M) = c ^ n * pfaffian n M := by
  unfold pfaffian
  simp only [Matrix.smul_apply, smul_eq_mul, Finset.prod_mul_distrib,
    Finset.prod_const, Finset.card_univ, Fintype.card_fin]
  simp_rw [mul_left_comm _ (c ^ n)]
  rw [← Finset.mul_sum]
  ring

theorem pfaffian_zero (n : β„•) : pfaffian (n + 1) 0 = 0 := by
  simp [pfaffian]

/-- Simultaneous permutation of rows and columns changes the Pfaffian by the
permutation sign. This is the elementary congruence law for the signed polynomial. -/
theorem pfaffian_reindex (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚)
    (Ο„ : Equiv.Perm (Fin n Γ— Fin 2)) :
    pfaffian n (M.submatrix Ο„ Ο„) =
      ((Equiv.Perm.sign Ο„ : β„€) : β„‚) * pfaffian n M := by
  have hs : ((Equiv.Perm.sign Ο„ : β„€) : β„‚) * ((Equiv.Perm.sign Ο„ : β„€) : β„‚) = 1 := by
    have hz : (Equiv.Perm.sign Ο„ : β„€) * (Equiv.Perm.sign Ο„ : β„€) = 1 := by
      simp
    exact_mod_cast hz
  unfold pfaffian
  have he :
      (βˆ‘ Οƒ : Equiv.Perm (Fin n Γ— Fin 2),
        ((Equiv.Perm.sign Οƒ : β„€) : β„‚) * ∏ i : Fin n, M (Ο„ (Οƒ (i, 0))) (Ο„ (Οƒ (i, 1)))) =
      ((Equiv.Perm.sign Ο„ : β„€) : β„‚) *
        βˆ‘ Οƒ : Equiv.Perm (Fin n Γ— Fin 2),
          ((Equiv.Perm.sign Οƒ : β„€) : β„‚) * ∏ i : Fin n, M (Οƒ (i, 0)) (Οƒ (i, 1)) := by
    calc
      _ = ((Equiv.Perm.sign Ο„ : β„€) : β„‚) *
          βˆ‘ Οƒ : Equiv.Perm (Fin n Γ— Fin 2),
            ((Equiv.Perm.sign (Ο„ * Οƒ) : β„€) : β„‚) *
              ∏ i : Fin n, M ((Ο„ * Οƒ) (i, 0)) ((Ο„ * Οƒ) (i, 1)) := by
        rw [Finset.mul_sum]
        apply Finset.sum_congr rfl
        intro Οƒ _
        simp only [Equiv.Perm.sign_mul, Units.val_mul, Int.cast_mul, Equiv.Perm.mul_apply]
        symm
        calc
          _ = (((Equiv.Perm.sign Ο„ : β„€) : β„‚) * ((Equiv.Perm.sign Ο„ : β„€) : β„‚)) *
              (((Equiv.Perm.sign Οƒ : β„€) : β„‚) *
                ∏ i : Fin n, M (Ο„ (Οƒ (i, 0))) (Ο„ (Οƒ (i, 1)))) := by ring
          _ = _ := by rw [hs, one_mul]
      _ = _ := by
        congr 1
        exact Equiv.sum_comp (Equiv.mulLeft Ο„) (fun Οƒ =>
          ((Equiv.Perm.sign Οƒ : β„€) : β„‚) * ∏ i : Fin n, M (Οƒ (i, 0)) (Οƒ (i, 1)))
  simp only [Matrix.submatrix_apply]
  rw [he]
  ring

/-- Scaling the row and matching column by each `c i` scales the Pfaffian
by the product of these factors. -/
theorem pfaffian_diagonal_congruence (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚)
    (c : Fin n Γ— Fin 2 β†’ β„‚) :
    pfaffian n (of fun i j => c i * M i j * c j) =
      (∏ i, c i) * pfaffian n M := by
  have hp (Οƒ : Equiv.Perm (Fin n Γ— Fin 2)) :
      (∏ i : Fin n, c (Οƒ (i, 0))) * (∏ i : Fin n, c (Οƒ (i, 1))) = ∏ i, c i := by
    rw [← Finset.prod_mul_distrib]
    simpa only [Fintype.prod_prod_type, Fin.prod_univ_two] using Equiv.prod_comp Οƒ c
  unfold pfaffian
  simp only [of_apply, Finset.prod_mul_distrib]
  have ht (Οƒ : Equiv.Perm (Fin n Γ— Fin 2)) :
      ((Equiv.Perm.sign Οƒ : β„€) : β„‚) *
          ((∏ i : Fin n, c (Οƒ (i, 0))) * (∏ i : Fin n, M (Οƒ (i, 0)) (Οƒ (i, 1))) *
            ∏ i : Fin n, c (Οƒ (i, 1))) =
      (∏ i, c i) * (((Equiv.Perm.sign Οƒ : β„€) : β„‚) *
          ∏ i : Fin n, M (Οƒ (i, 0)) (Οƒ (i, 1))) := by
    rw [← hp Οƒ]
    ring
  simp_rw [ht]
  rw [← Finset.mul_sum]
  ring

/-- The one-pair Pfaffian has the expected signed normalization. -/
theorem pfaffian_one
    (M : Matrix (Fin 1 Γ— Fin 2) (Fin 1 Γ— Fin 2) β„‚) :
    pfaffian 1 M = (M (0, 0) (0, 1) - M (0, 1) (0, 0)) / 2 := by
  have hab : ((0, 0) : Fin 1 Γ— Fin 2) β‰  (0, 1) := by decide
  have hp : (1 : Equiv.Perm (Fin 1 Γ— Fin 2)) β‰  Equiv.swap (0, 0) (0, 1) := by
    intro h
    have hx := congrArg (fun e : Equiv.Perm (Fin 1 Γ— Fin 2) => e (0, 0)) h
    simp at hx
  have hu : ({1, Equiv.swap ((0, 0) : Fin 1 Γ— Fin 2) (0, 1)} :
      Finset (Equiv.Perm (Fin 1 Γ— Fin 2))) = Finset.univ := by
    apply Finset.eq_univ_of_card
    simp [hp, Fintype.card_perm]
  unfold pfaffian
  rw [← hu]
  simp [hp, Equiv.Perm.sign_swap hab]
  ring

/-- For a one-by-one quaternionic matrix, the construction gives the real
part of its sole entry. In particular, negative real entries stay negative. -/
theorem mooreDet_one (A : Matrix (Fin 1) (Fin 1) (Quaternion ℝ)) :
    mooreDet 1 A = (A 0 0).re := by
  rw [mooreDet, pfaffian_one]
  simp [skewComplexify, skewQuaternion, quaternionMatrix, symplecticUnit,
    Matrix.mul_apply, Fin.sum_univ_two]

theorem skewQuaternion_smul (c : ℝ) (q : Quaternion ℝ) :
    skewQuaternion (c β€’ q) = (c : β„‚) β€’ skewQuaternion q := by
  ext i j
  fin_cases i <;> fin_cases j <;> apply Complex.ext <;>
    simp [skewQuaternion, quaternionMatrix, symplecticUnit, Matrix.mul_apply,
      Fin.sum_univ_two]

theorem mooreDet_smul (n : β„•) (c : ℝ)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    mooreDet n (c β€’ A) = c ^ n * mooreDet n A := by
  have h : skewComplexify (c β€’ A) = (c : β„‚) β€’ skewComplexify A := by
    ext ⟨i, a⟩ ⟨j, b⟩
    exact congrArg (fun M : Matrix (Fin 2) (Fin 2) β„‚ => M a b)
      (skewQuaternion_smul c (A i j))
  unfold mooreDet
  rw [h, pfaffian_smul, ← Complex.ofReal_pow]
  simp only [Complex.mul_re, Complex.ofReal_re, Complex.ofReal_im, zero_mul, sub_zero]

/-- A complex bilinear form viewed as a two-variable multilinear map. -/
def pairingMultilinear {I : Type*} [Fintype I] (M : Matrix I I β„‚) :
    MultilinearMap β„‚ (fun _ : Fin 2 => I β†’ β„‚) β„‚ :=
  MultilinearMap.mk' (fun v => v 0 ⬝α΅₯ (M *α΅₯ v 1))
    (by
      intro v i x y
      fin_cases i <;>
        simp [Function.update, add_dotProduct, dotProduct_add, Matrix.mulVec_add])
    (by
      intro v i c x
      fin_cases i <;>
        simp [Function.update, smul_dotProduct, dotProduct_smul, Matrix.mulVec_smul])

/-- Product of the `n` paired bilinear evaluations, before antisymmetrization. -/
def pairProduct (n : β„•) (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    MultilinearMap β„‚ (fun _ : Fin n Γ— Fin 2 => (Fin n Γ— Fin 2) β†’ β„‚) β„‚ :=
  (((MultilinearMap.mkPiAlgebra β„‚ (Fin n) β„‚).compMultilinearMap
    (fun _ => pairingMultilinear M)).domDomCongr (Equiv.sigmaEquivProd (Fin n) (Fin 2)))

theorem pairProduct_apply (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚)
    (v : (Fin n Γ— Fin 2) β†’ (Fin n Γ— Fin 2) β†’ β„‚) :
    pairProduct n M v = ∏ i : Fin n, v (i, 0) ⬝α΅₯ (M *α΅₯ v (i, 1)) := by
  rfl

/-- The normalized alternating form whose evaluation on the standard basis
is the Pfaffian. This supplies the route to general congruence invariance. -/
noncomputable def pfaffianAlternating (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    AlternatingMap β„‚ ((Fin n Γ— Fin 2) β†’ β„‚) β„‚ (Fin n Γ— Fin 2) :=
  (2 ^ n * (n.factorial : β„‚))⁻¹ β€’ (pairProduct n M).alternatization

theorem pfaffianAlternating_apply (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚)
    (v : (Fin n Γ— Fin 2) β†’ (Fin n Γ— Fin 2) β†’ β„‚) :
    pfaffianAlternating n M v =
      pfaffian n (of fun i j => v i ⬝α΅₯ (M *α΅₯ v j)) := by
  simp only [pfaffianAlternating, AlternatingMap.smul_apply,
    MultilinearMap.alternatization_apply, MultilinearMap.domDomCongr_apply,
    pairProduct_apply, pfaffian, of_apply, smul_eq_mul,
    Units.smul_def, zsmul_eq_mul]

theorem pfaffianAlternating_basis (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    pfaffianAlternating n M (Pi.basisFun β„‚ (Fin n Γ— Fin 2)) = pfaffian n M := by
  rw [pfaffianAlternating_apply]
  congr 1
  ext i j
  simp [Pi.basisFun_apply, dotProduct, Matrix.mulVec, Pi.single_apply]

/-- General congruence formula for the Pfaffian. It follows from the universal
determinant characterization of alternating top-degree multilinear forms. -/
theorem pfaffian_congruence (n : β„•)
    (M B : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    pfaffian n (B * M * Bα΅€) = B.det * pfaffian n M := by
  have hb : (of fun i j => B i ⬝α΅₯ (M *α΅₯ B j)) = B * M * Bα΅€ := by
    ext i j
    simp only [of_apply, Matrix.mul_apply, transpose_apply, dotProduct, mulVec,
      Finset.mul_sum, Finset.sum_mul]
    rw [Finset.sum_comm]
    apply Finset.sum_congr rfl
    intro k _
    apply Finset.sum_congr rfl
    intro l _
    ring
  have ht := (pfaffianAlternating n M).eq_smul_basis_det
    (Pi.basisFun β„‚ (Fin n Γ— Fin 2))
  have he := congrArg (fun f : AlternatingMap β„‚ ((Fin n Γ— Fin 2) β†’ β„‚) β„‚ (Fin n Γ— Fin 2) =>
    f (fun i j => B i j)) ht
  rw [AlternatingMap.smul_apply, pfaffianAlternating_basis, Pi.basisFun_det_apply,
    pfaffianAlternating_apply, hb] at he
  change pfaffian n (B * M * Bα΅€) = pfaffian n M * B.det at he
  exact he.trans (mul_comm _ _)

/-- The block diagonal symplectic matrix in interleaved coordinates. -/
def symplecticMatrix (n : β„•) : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚ :=
  of fun i j => if i.1 = j.1 then symplecticUnit i.2 j.2 else 0

theorem complexify_mul_symplectic (n : β„•)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    complexify A * symplecticMatrix n = skewComplexify A := by
  ext ⟨i, a⟩ ⟨j, b⟩
  simp [Matrix.mul_apply, Fintype.sum_prod_type, complexify, symplecticMatrix,
    skewComplexify, skewQuaternion, mul_ite]

theorem symplectic_mul_transpose (n : β„•)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    symplecticMatrix n * (complexify A)α΅€ = complexify Aα΄΄ * symplecticMatrix n := by
  rw [complexify_mul_symplectic]
  ext ⟨i, a⟩ ⟨j, b⟩
  simp only [Matrix.mul_apply, Fintype.sum_prod_type, symplecticMatrix, of_apply,
    Matrix.transpose_apply, complexify, skewComplexify, Matrix.conjTranspose_apply]
  simp only [ite_mul, zero_mul, Finset.sum_ite_irrel, Finset.sum_const_zero,
    Finset.sum_ite_eq, Finset.mem_univ, ite_true]
  change (symplecticUnit * (quaternionMatrix (A j i))α΅€) a b =
    skewQuaternion (star (A j i)) a b
  rw [skewQuaternion_star, skewQuaternion, Matrix.transpose_mul]
  have hj : symplecticUnitα΅€ = -symplecticUnit := by
    ext i j
    fin_cases i <;> fin_cases j <;> simp [symplecticUnit]
  rw [hj, Matrix.neg_mul, neg_neg]

/-- Quaternionic Hermitian congruence is carried to ordinary complex
skew-symmetric congruence by the faithful representation. -/
theorem skewComplexify_congruence (n : β„•)
    (A B : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    skewComplexify (B * A * Bα΄΄) =
      complexify B * skewComplexify A * (complexify B)α΅€ := by
  rw [← complexify_mul_symplectic, complexify_mul, complexify_mul]
  rw [Matrix.mul_assoc, Matrix.mul_assoc, ← symplectic_mul_transpose]
  rw [← complexify_mul_symplectic n A]
  simp only [Matrix.mul_assoc]

/-- Full quaternionic congruence law for the signed complex Pfaffian polynomial. -/
theorem quaternionic_pfaffian_congruence (n : β„•)
    (A B : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    pfaffian n (skewComplexify (B * A * Bα΄΄)) =
      (complexify B).det * pfaffian n (skewComplexify A) := by
  rw [skewComplexify_congruence, pfaffian_congruence]

theorem det_symplecticMatrix (n : β„•) : (symplecticMatrix n).det = 1 := by
  have hk : symplecticMatrix n =
      Matrix.kronecker (1 : Matrix (Fin n) (Fin n) β„‚) symplecticUnit := by
    ext ⟨i, a⟩ ⟨j, b⟩
    simp [symplecticMatrix, Matrix.one_apply, ite_mul]
  rw [hk]
  change (Matrix.kroneckerMap (fun x y : β„‚ => x * y)
    (1 : Matrix (Fin n) (Fin n) β„‚) symplecticUnit).det = 1
  have hd := Matrix.det_kronecker (1 : Matrix (Fin n) (Fin n) β„‚) symplecticUnit
  have hj : symplecticUnit.det = 1 := by norm_num [symplecticUnit, Matrix.det_fin_two]
  simpa only [Matrix.det_one, hj, one_pow, mul_one] using hd

/-- The determinant of the faithful complex representation is real. -/
theorem complexify_det_real (n : β„•)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    (complexify A).det.im = 0 := by
  have h := congrArg Matrix.det (symplectic_mul_transpose n A)
  rw [Matrix.det_mul, Matrix.det_mul, complexify_star, Matrix.det_conjTranspose,
    Matrix.det_transpose, det_symplecticMatrix, one_mul, mul_one] at h
  have hi := congrArg Complex.im h
  simp only [Complex.star_def, Complex.conj_im] at hi
  linarith

/-- The real signed Moore polynomial obeys the expected congruence law. -/
theorem mooreDet_congruence (n : β„•)
    (A B : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    mooreDet n (B * A * Bα΄΄) = (complexify B).det.re * mooreDet n A := by
  unfold mooreDet
  rw [quaternionic_pfaffian_congruence, Complex.mul_re, complexify_det_real,
    zero_mul, sub_zero]

theorem pfaffian_map_star (n : β„•)
    (M : Matrix (Fin n Γ— Fin 2) (Fin n Γ— Fin 2) β„‚) :
    pfaffian n (M.map star) = star (pfaffian n M) := by
  change pfaffian n (M.map (starRingEnd β„‚)) = (starRingEnd β„‚) (pfaffian n M)
  simp only [pfaffian, map_mul, map_invβ‚€, map_pow, map_ofNat, map_natCast,
    map_sum, map_prod, map_intCast, Matrix.map_apply]

theorem symplecticMatrix_mul_transpose (n : β„•) :
    symplecticMatrix n * (symplecticMatrix n)α΅€ = 1 := by
  ext ⟨i, a⟩ ⟨j, b⟩
  by_cases hij : i = j
  Β· subst j
    fin_cases a <;> fin_cases b <;>
      simp [symplecticMatrix, symplecticUnit, Matrix.mul_apply,
        Fintype.sum_prod_type, Fin.sum_univ_two]
  Β· fin_cases a <;> fin_cases b <;>
      simp [symplecticMatrix, symplecticUnit, Matrix.mul_apply,
        Fintype.sum_prod_type, Fin.sum_univ_two, hij]

theorem skewComplexify_map_star (n : β„•)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    (skewComplexify A).map star = symplecticMatrix n * complexify A := by
  ext ⟨i, a⟩ ⟨j, b⟩
  simp only [Matrix.map_apply, Matrix.mul_apply, Fintype.sum_prod_type,
    symplecticMatrix, of_apply, complexify]
  simp only [ite_mul, zero_mul, Finset.sum_ite_irrel, Finset.sum_const_zero,
    Finset.sum_ite_eq, Finset.mem_univ, ite_true]
  fin_cases a <;> fin_cases b <;> apply Complex.ext <;>
    simp [skewComplexify, skewQuaternion, quaternionMatrix, symplecticUnit,
      Matrix.mul_apply, Fin.sum_univ_two]

/-- The Pfaffian polynomial defining the Moore determinant is real-valued.
Taking its real part in `mooreDet` therefore loses no information. -/
theorem quaternionic_pfaffian_real (n : β„•)
    (A : Matrix (Fin n) (Fin n) (Quaternion ℝ)) :
    (pfaffian n (skewComplexify A)).im = 0 := by
  have hm : (skewComplexify A).map star =
      symplecticMatrix n * skewComplexify A * (symplecticMatrix n)α΅€ := by
    rw [skewComplexify_map_star, ← complexify_mul_symplectic]
    simp only [Matrix.mul_assoc, symplecticMatrix_mul_transpose, Matrix.mul_one]
  have h := pfaffian_congruence n (skewComplexify A) (symplecticMatrix n)
  rw [← hm, pfaffian_map_star, det_symplecticMatrix, one_mul] at h
  have hi := congrArg Complex.im h
  simp only [Complex.star_def, Complex.conj_im] at hi
  linarith

end OAI.LinearAlgebra.Moore