File size: 67,413 Bytes
9b6e014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
import Mathlib

namespace OAI

/-! Signature. -/

noncomputable section

open scoped BigOperators
open scoped BigOperators
open QuadraticMap

namespace Contingency

 

section Signature
variable {V : Type*} [AddCommGroup V] [Module ℝ V] [FiniteDimensional ℝ V]

 

theorem sigPos_le_one_of_kernel (Q : QuadraticForm ℝ V) (l : V →ₗ[ℝ] ℝ)
    (h : ∀ x, l x = 0 → Q x ≤ 0) : sigPos Q ≤ 1 := by
  have hs := QuadraticForm.sigPos_add_finrank_le_of_nonpos
    (Q := Q) (V := LinearMap.ker l) (fun x hx => h x hx)
  have hd := l.finrank_range_add_finrank_ker
  have hr := (LinearMap.range l).finrank_le
  simp only [Module.finrank_self] at hr
  omega

 
omit [FiniteDimensional ℝ V] in
theorem quadratic_add (Q : QuadraticForm ℝ V) (x y : V) :
    Q (x + y) = Q x + Q y + 2 * Q.associated x y := by
  have h := Q.associated_apply ℝ x y
  norm_num at h
  linarith

 

theorem sigPos_two_of_orthogonal_positive (Q : QuadraticForm ℝ V) (e v : V)
    (he : 0 < Q e) (hv : 0 < Q v) (hev : Q.associated e v = 0) :
    2 ≤ sigPos Q := by
  let f : (ℝ × ℝ) →ₗ[ℝ] V :=
    (LinearMap.fst ℝ ℝ ℝ).smulRight e + (LinearMap.snd ℝ ℝ ℝ).smulRight v
  have hf (p : ℝ × ℝ) : Q (f p) = p.1 ^ 2 * Q e + p.2 ^ 2 * Q v := by
    change Q (p.1 • e + p.2 • v) = _
    rw [quadratic_add]
    simp [QuadraticMap.map_smul, map_smul, hev, pow_two]
  have hpos (p : ℝ × ℝ) (hp : p ≠ 0) : 0 < Q (f p) := by
    rw [hf]
    have hp' : p.1 ≠ 0 ∨ p.2 ≠ 0 := by
      by_contra h
      push Not at h
      apply hp
      exact Prod.ext h.1 h.2
    rcases hp' with hp' | hp'
    · exact add_pos_of_pos_of_nonneg (mul_pos (sq_pos_of_ne_zero hp') he)
        (mul_nonneg (sq_nonneg _) hv.le)
    · exact add_pos_of_nonneg_of_pos (mul_nonneg (sq_nonneg _) he.le)
        (mul_pos (sq_pos_of_ne_zero hp') hv)
  have hinj : Function.Injective f := by
    apply (LinearMap.ker_eq_bot).mp
    apply LinearMap.ker_eq_bot'.mpr
    intro p hp
    by_contra hp'
    have := hpos p hp'
    rw [hp, QuadraticMap.map_zero] at this
    exact lt_irrefl _ this
  have hdef : (Q.restrict (LinearMap.range f)).PosDef := by
    rintro ⟨x, hx⟩ hne
    obtain ⟨p, rfl⟩ := hx
    apply hpos
    intro hp
    apply hne
    apply Subtype.ext
    simp [hp]
  have hs := le_sigPos_of_posDef Q hdef
  have hr := LinearEquiv.finrank_eq (LinearEquiv.ofInjective f hinj)
  have htwo : Module.finrank ℝ (LinearMap.range f) = 2 := by
    rw [← hr]
    simp [Module.finrank_prod]
  rwa [htwo] at hs

 
theorem positive_projection (Q : QuadraticForm ℝ V) (hs : sigPos Q ≤ 1)
    (e v : V) (he : 0 < Q e) :
    Q v ≤ (Q.associated v e) ^ 2 / Q e := by
  let a := Q.associated v e / Q e
  let w := v - a • e
  have hee : Q.associated e e = Q e := Q.associated_eq_self_apply ℝ e
  have hew : Q.associated e w = 0 := by
    simp only [w, map_sub, map_smul]
    rw [QuadraticMap.associated_isSymm ℝ Q e v, hee]
    simp [a, div_mul_cancel₀ _ he.ne']
  have hw : Q w ≤ 0 := by
    by_contra h
    have := sigPos_two_of_orthogonal_positive Q e w he (lt_of_not_ge h) hew
    omega
  have hexp : Q w = Q v - 2 * a * Q.associated v e + a ^ 2 * Q e := by
    rw [show w = v + (-a) • e by simp [w, sub_eq_add_neg], quadratic_add]
    simp only [QuadraticMap.map_smul, map_smul, smul_eq_mul]
    ring
  rw [hexp] at hw
  dsimp [a] at hw
  field_simp at hw ⊢
  nlinarith [sq_pos_of_pos he]

 
noncomputable def pairForm (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) : QuadraticForm ℝ (V × ℝ × ℝ) :=
  let old := LinearMap.fst ℝ V (ℝ × ℝ)
  let x := (LinearMap.fst ℝ ℝ ℝ).comp (LinearMap.snd ℝ V (ℝ × ℝ))
  let y := (LinearMap.snd ℝ ℝ ℝ).comp (LinearMap.snd ℝ V (ℝ × ℝ))
  Q.comp old + 2 • linMulLin (Sa.comp old) x + 2 • linMulLin (Sb.comp old) y +
    a • linMulLin x x + (2 * b) • linMulLin x y + c • linMulLin y y

omit [FiniteDimensional ℝ V] in
theorem pairForm_apply (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) (h : V) (x y : ℝ) :
    pairForm Q Sa Sb a b c (h, x, y) =
      Q h + 2 * Sa h * x + 2 * Sb h * y + a * x ^ 2 + 2 * b * x * y + c * y ^ 2 := by
  simp [pairForm, linMulLin_apply, pow_two, mul_assoc]

omit [FiniteDimensional ℝ V] in
theorem pairForm_first (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) : pairForm Q Sa Sb a b c (0, 1, 0) = a := by
  simp [pairForm_apply]

omit [FiniteDimensional ℝ V] in
theorem pairForm_second (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) : pairForm Q Sa Sb a b c (0, 0, 1) = c := by
  simp [pairForm_apply]

omit [FiniteDimensional ℝ V] in
theorem pairForm_both (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) : pairForm Q Sa Sb a b c (0, 1, 1) = a + 2 * b + c := by
  simp [pairForm_apply]

omit [FiniteDimensional ℝ V] in
theorem pairForm_associated_first (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) (h : V) (x y : ℝ) :
    (pairForm Q Sa Sb a b c).associated (h, x, y) (0, 1, 0) =
      Sa h + a * x + b * y := by
  rw [QuadraticMap.associated_apply]
  simp [pairForm_apply]
  ring

omit [FiniteDimensional ℝ V] in
theorem pairForm_associated_second (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) (h : V) (x y : ℝ) :
    (pairForm Q Sa Sb a b c).associated (h, x, y) (0, 0, 1) =
      Sb h + b * x + c * y := by
  rw [QuadraticMap.associated_apply]
  simp [pairForm_apply]
  ring

 

theorem pair_one (Q : QuadraticForm ℝ V) (Sa Sb : V →ₗ[ℝ] ℝ)
    (a b c : ℝ) (ha : 0 ≤ a) (hc : 0 ≤ c)
    (hpositive : 0 < a ∨ 0 < c ∨ 0 < b)
    (hs : sigPos (pairForm Q Sa Sb a b c) ≤ 1)
    (h : V) (gp g gn : ℝ)
    (hSa : Sa h = b * g - a * gp) (hSb : Sb h = c * gn - b * g) :
    2 * b * g ^ 2 - a * gp ^ 2 - c * gn ^ 2 ≤ -Q h := by
  let M := pairForm Q Sa Sb a b c
  have hfirst : M.associated (h, g, -g) (0, 1, 0) = a * (g - gp) := by
    rw [pairForm_associated_first, hSa]
    ring
  have hsecond : M.associated (h, g, -g) (0, 0, 1) = c * (gn - g) := by
    rw [pairForm_associated_second, hSb]
    ring
  have hbound : M (h, g, -g) ≤ a * (g - gp) ^ 2 + c * (g - gn) ^ 2 := by
    by_cases ha' : 0 < a
    · have ht := positive_projection M hs (0, 1, 0) (h, g, -g)
        (by simpa [M, pairForm_first] using ha')
      rw [hfirst, pairForm_first] at ht
      have heq : (a * (g - gp)) ^ 2 / a = a * (g - gp) ^ 2 := by
        field_simp
      rw [heq] at ht
      exact ht.trans (le_add_of_nonneg_right (mul_nonneg hc (sq_nonneg _)))
    · by_cases hc' : 0 < c
      · have ht := positive_projection M hs (0, 0, 1) (h, g, -g)
          (by simpa [M, pairForm_second] using hc')
        rw [hsecond, pairForm_second] at ht
        have heq : (c * (gn - g)) ^ 2 / c = c * (g - gn) ^ 2 := by
          field_simp
          ring
        rw [heq] at ht
        exact ht.trans (le_add_of_nonneg_left (mul_nonneg ha (sq_nonneg _)))
      · have hae : a = 0 := le_antisymm (le_of_not_gt ha') ha
        have hce : c = 0 := le_antisymm (le_of_not_gt hc') hc
        have hb : 0 < b := hpositive.resolve_left ha' |>.resolve_left hc'
        have ht := positive_projection M hs (0, 1, 1) (h, g, -g)
          (by rw [pairForm_both, hae, hce]; linarith)
        have heq : M.associated (h, g, -g) (0, 1, 1) = 0 := by
          have hsum : ((0 : V), (1 : ℝ), (1 : ℝ)) = (0, 1, 0) + (0, 0, 1) := by
            ext <;> simp
          rw [hsum, map_add, hfirst, hsecond, hae, hce]
          ring
        simpa [heq, hae, hce] using ht
  rw [pairForm_apply, hSa, hSb] at hbound
  nlinarith

 
noncomputable def pairCumulative (D : ℕ) (u : ℕ → ℝ) (S : ℕ → V →ₗ[ℝ] ℝ)
    (h : V) (j : ℕ) : ℝ :=
  if 0 < j ∧ j < D then (∑ l ∈ Finset.range j, S l h) / u j else 0

omit [FiniteDimensional ℝ V] in
theorem pairCumulative_mul (D : ℕ) (u : ℕ → ℝ) (S : ℕ → V →ₗ[ℝ] ℝ)
    (hu : ∀ j, 0 < j → j < D → 0 < u j) (h : V)
    (hbal : ∑ l ∈ Finset.range D, S l h = 0) (j : ℕ) (hj : j ≤ D) :
    u j * pairCumulative D u S h j = ∑ l ∈ Finset.range j, S l h := by
  by_cases h0 : j = 0
  · simp [h0, pairCumulative]
  by_cases hD : j = D
  · simp [hD, pairCumulative, hbal]
  have hj' : 0 < j ∧ j < D := by omega
  simp only [pairCumulative, ite_eq_left hj']
  exact mul_div_cancel₀ _ (hu j hj'.1 hj'.2).ne'

 
omit [FiniteDimensional ℝ V] in
theorem pairCumulative_difference (D : ℕ) (u : ℕ → ℝ) (S : ℕ → V →ₗ[ℝ] ℝ)
    (hu : ∀ j, 0 < j → j < D → 0 < u j) (h : V)
    (hbal : ∑ l ∈ Finset.range D, S l h = 0) (j : ℕ) (hj : j < D) :
    S j h = u (j+1) * pairCumulative D u S h (j+1) -
      u j * pairCumulative D u S h j := by
  rw [pairCumulative_mul D u S hu h hbal _ (by omega),
    pairCumulative_mul D u S hu h hbal _ (by omega), Finset.sum_range_succ]
  ring

 
noncomputable def pairPrevious (S : ℕ → V →ₗ[ℝ] ℝ) (j : ℕ) : V →ₗ[ℝ] ℝ :=
  if j = 0 then 0 else S (j-1)

 

theorem pair_sequence_one (D : ℕ) (hD : 2 ≤ D) (u : ℕ → ℝ)
    (S : ℕ → V →ₗ[ℝ] ℝ) (Q : ℕ → QuadraticForm ℝ V)
    (hu : ∀ j, 0 < j → j < D → 0 < u j)
    (uzero : ∀ j, j = 0 ∨ D ≤ j → u j = 0) (sD : S D = 0)
    (h : V) (hbal : ∑ l ∈ Finset.range D, S l h = 0)
    (j : ℕ) (hj : j ≤ D)
    (hs : sigPos (pairForm (Q j) (pairPrevious S j) (S j)
      (u (j-1)) (u j) (u (j+1))) ≤ 1) :
    2 * u j * pairCumulative D u S h j ^ 2 -
        u (j-1) * pairCumulative D u S h (j-1) ^ 2 -
        u (j+1) * pairCumulative D u S h (j+1) ^ 2 ≤ -(Q j) h := by
  have hunonneg (i : ℕ) : 0 ≤ u i := by
    by_cases hi : i = 0 ∨ D ≤ i
    · rw [uzero i hi]
    · push Not at hi
      exact (hu i (by omega) (by omega)).le
  have hpos : 0 < u (j-1) ∨ 0 < u (j+1) ∨ 0 < u j := by
    by_cases h0 : j = 0
    · subst j
      exact Or.inr (Or.inl (hu 1 (by omega) (by omega)))
    by_cases hjD : j = D
    · exact Or.inl (hu (j-1) (by omega) (by omega))
    · exact Or.inr (Or.inr (hu j (by omega) (by omega)))
  have ha : (pairPrevious S j) h =
      u j * pairCumulative D u S h j - u (j-1) * pairCumulative D u S h (j-1) := by
    by_cases hj0 : j = 0
    · simp [hj0, pairPrevious, pairCumulative]
    · rw [pairPrevious, ite_eq_right hj0]
      have ht := pairCumulative_difference D u S hu h hbal (j-1) (by omega)
      simpa [Nat.sub_add_cancel (by omega : 1 ≤ j)] using ht
  have hb : S j h = u (j+1) * pairCumulative D u S h (j+1) -
      u j * pairCumulative D u S h j := by
    by_cases hjD : j = D
    · simp [hjD, sD, pairCumulative]
    · exact pairCumulative_difference D u S hu h hbal j (by omega)
  exact pair_one (Q j) (pairPrevious S j) (S j) _ _ _
    (hunonneg _) (hunonneg _) hpos hs h _ _ _ ha hb

 
theorem discrete_laplacian_sum (a : ℕ → ℝ) (D : ℕ)
    (h0 : a 0 = 0) (hD : a D = 0) (hnext : a (D+1) = 0) :
    ∑ j ∈ Finset.range (D+1), (2 * a j - a (j-1) - a (j+1)) = 0 := by
  have hp : ∑ j ∈ Finset.range (D+1), a (j-1) = ∑ j ∈ Finset.range (D+1), a j := by
    rw [Finset.sum_range_succ', Finset.sum_range_succ]
    simp [h0, hD]
  have hn : ∑ j ∈ Finset.range (D+1), a (j+1) = ∑ j ∈ Finset.range (D+1), a j := by
    have ht := Finset.sum_range_succ' a (D+1)
    rw [Finset.sum_range_succ, hnext, h0, add_zero, add_zero] at ht
    exact ht.symm
  simp only [Finset.sum_sub_distrib, ← Finset.mul_sum, hp, hn]
  ring

 

theorem pair_summation_signature (D : ℕ) (hD : 2 ≤ D) (u : ℕ → ℝ)
    (S : ℕ → V →ₗ[ℝ] ℝ) (Q : ℕ → QuadraticForm ℝ V)
    (hu : ∀ j, 0 < j → j < D → 0 < u j)
    (uzero : ∀ j, j = 0 ∨ D ≤ j → u j = 0) (sD : S D = 0)
    (hs : ∀ j, j ≤ D → sigPos (pairForm (Q j) (pairPrevious S j) (S j)
      (u (j-1)) (u j) (u (j+1))) ≤ 1) :
    sigPos (∑ j ∈ Finset.range (D+1), Q j) ≤ 1 := by
  apply sigPos_le_one_of_kernel _ (∑ l ∈ Finset.range D, S l)
  intro h hbal
  have hb : ∑ l ∈ Finset.range D, S l h = 0 := by simpa using hbal
  let a := fun j => u j * pairCumulative D u S h j ^ 2
  have ht : ∑ j ∈ Finset.range (D+1), (2 * a j - a (j-1) - a (j+1)) = 0 := by
    apply discrete_laplacian_sum
    all_goals simp [a, pairCumulative]
  have hsum : ∑ j ∈ Finset.range (D+1), (2 * a j - a (j-1) - a (j+1)) ≤
      ∑ j ∈ Finset.range (D+1), -(Q j) h := by
    apply Finset.sum_le_sum
    intro j hj
    have hj' : j ≤ D := by simpa using Finset.mem_range.mp hj
    simpa only [a, mul_assoc] using
      pair_sequence_one D hD u S Q hu uzero sD h hb j hj' (hs j hj')
  rw [ht, Finset.sum_neg_distrib] at hsum
  simpa using neg_nonneg.mp hsum

 

theorem discrete_summation_by_parts (a w : ℕ → ℝ) (D : ℕ)
    (h0 : a 0 = 0) (hD : a D = 0) (hnext : a (D+1) = 0) :
    (∑ j ∈ Finset.range (D+1), w j * (2 * a j - a (j-1) - a (j+1))) =
      ∑ j ∈ Finset.range (D+1), (2 * w j - w (j-1) - w (j+1)) * a j := by
  have hp : ∑ j ∈ Finset.range (D+1), w j * a (j-1) =
      ∑ j ∈ Finset.range (D+1), w (j+1) * a j := by
    rw [Finset.sum_range_succ', Finset.sum_range_succ]
    simp [h0, hD]
  have hn : ∑ j ∈ Finset.range (D+1), w j * a (j+1) =
      ∑ j ∈ Finset.range (D+1), w (j-1) * a j := by
    have ht := Finset.sum_range_succ' (fun j => w (j-1) * a j) (D+1)
    rw [Finset.sum_range_succ] at ht
    simpa [h0, hnext] using ht.symm
  simp only [mul_sub, sub_mul, Finset.sum_sub_distrib]
  rw [hp, hn]
  have ht : ∑ j ∈ Finset.range (D+1), w j * (2 * a j) =
      ∑ j ∈ Finset.range (D+1), 2 * w j * a j := by
    apply Finset.sum_congr rfl
    intros
    ring
  rw [ht]
  ring

theorem discrete_quadratic_weight (a : ℕ → ℝ) (D : ℕ)
    (h0 : a 0 = 0) (hD : a D = 0) (hnext : a (D+1) = 0) :
    (∑ j ∈ Finset.range (D+1), (j : ℝ) * (D - j) *
      (2 * a j - a (j-1) - a (j+1))) = 2 * ∑ j ∈ Finset.range (D+1), a j := by
  rw [discrete_summation_by_parts a (fun j => (j : ℝ) * (D-j)) D h0 hD hnext,
    Finset.mul_sum]
  apply Finset.sum_congr rfl
  intro j hj
  by_cases hj0 : j = 0
  · simp [hj0, h0]
  · rw [Nat.cast_sub (by omega : 1 ≤ j), Nat.cast_one, Nat.cast_add, Nat.cast_one]
    ring

 

theorem pair_summation_weighted (D : ℕ) (hD : 2 ≤ D) (u : ℕ → ℝ)
    (S : ℕ → V →ₗ[ℝ] ℝ) (Q : ℕ → QuadraticForm ℝ V)
    (hu : ∀ j, 0 < j → j < D → 0 < u j)
    (uzero : ∀ j, j = 0 ∨ D ≤ j → u j = 0) (sD : S D = 0)
    (hs : ∀ j, 0 < j → j < D → sigPos (pairForm (Q j) (pairPrevious S j) (S j)
      (u (j-1)) (u j) (u (j+1))) ≤ 1)
    (h : V) (hbal : ∑ l ∈ Finset.range D, S l h = 0) :
    2 * (∑ j ∈ Finset.range (D+1), u j * pairCumulative D u S h j ^ 2) ≤
      -(∑ j ∈ Finset.range (D+1), ((j : ℝ) * (D-j)) • Q j) h := by
  let a := fun j => u j * pairCumulative D u S h j ^ 2
  have ha0 : a 0 = 0 := by simp [a, pairCumulative]
  have haD : a D = 0 := by simp [a, pairCumulative]
  have haN : a (D+1) = 0 := by simp [a, pairCumulative]
  have ht := discrete_quadratic_weight a D ha0 haD haN
  have hsum :
      (∑ j ∈ Finset.range (D+1), (j : ℝ) * (D-j) * (2*a j-a (j-1)-a (j+1))) ≤
      ∑ j ∈ Finset.range (D+1), -((j : ℝ) * (D-j)) * (Q j) h := by
    apply Finset.sum_le_sum
    intro j hj
    have hj' : j ≤ D := by simpa using Finset.mem_range.mp hj
    by_cases hj0 : j = 0
    · simp [hj0]
    by_cases hjD : j = D
    · simp [hjD]
    have h := pair_sequence_one D hD u S Q hu uzero sD h hbal j hj'
      (hs j (by omega) (by omega))
    have hw : 0 ≤ (j : ℝ) * (D-j) :=
      mul_nonneg (Nat.cast_nonneg _) (sub_nonneg.mpr (Nat.cast_le.mpr hj'))
    simpa only [a, mul_assoc, mul_neg, neg_mul] using mul_le_mul_of_nonneg_left h hw
  rw [ht] at hsum
  simpa only [a, sum_apply, smul_apply, smul_eq_mul,
    neg_mul, Finset.sum_neg_distrib] using hsum

section Laminar
variable {ι : Type*} [Fintype ι] [DecidableEq ι]

 
omit [Fintype ι] in
theorem chain_product_telescope (s : Finset (Finset ι)) (g : Finset ι → ℝ)
    (hc : ∀ G ∈ s, ∀ H ∈ s, G ⊆ H ∨ H ⊆ G) :
    ∏ G ∈ s, g G = 1 - ∑ G ∈ s, (1-g G) * ∏ H ∈ s.filter (G ⊂ ·), g H := by
  classical
  induction s using Finset.strongInductionOn with
  | _ s ih =>
    by_cases he : s = ∅
    · simp [he]
    obtain ⟨G, hG, hmin⟩ := s.exists_min_image Finset.card (Finset.nonempty_iff_ne_empty.mpr he)
    have hsmall (H) (hH : H ∈ s.erase G) : G ⊂ H := by
      obtain ⟨hHG, hHs⟩ := Finset.mem_erase.mp hH
      rcases hc G hG H hHs with h | h
      · exact Finset.ssubset_iff_subset_ne.mpr ⟨h, Ne.symm hHG⟩
      · have heq := Finset.eq_of_subset_of_card_le h (hmin H hHs)
        exact (hHG heq).elim
    have hfilter (H) (hH : H ∈ s.erase G) :
        s.filter (H ⊂ ·) = (s.erase G).filter (H ⊂ ·) := by
      ext J
      simp only [Finset.mem_filter, Finset.mem_erase]
      constructor
      · rintro ⟨hJ, hHJ⟩
        refine ⟨⟨?_, hJ⟩, hHJ⟩
        intro heq
        subst J
        have hGH := Finset.ssubset_iff_subset_ne.mp (hsmall H hH)
        exact hGH.2 (Finset.Subset.antisymm hGH.1 (Finset.ssubset_iff_subset_ne.mp hHJ).1)
      · tauto
    have hGeq : s.filter (G ⊂ ·) = s.erase G := by
      ext H
      simp only [Finset.mem_filter, Finset.mem_erase]
      constructor
      · rintro ⟨hH, hGH⟩
        exact ⟨(Finset.ssubset_iff_subset_ne.mp hGH).2.symm, hH⟩
      · intro hH
        exact ⟨hH.2, hsmall H (Finset.mem_erase.mpr hH)⟩
    have ht := ih (s.erase G) (Finset.erase_ssubset hG)
      (fun H hH J hJ => hc H (Finset.mem_of_mem_erase hH) J (Finset.mem_of_mem_erase hJ))
    have hp : ∏ H ∈ s, g H = g G * ∏ H ∈ s.erase G, g H :=
      (Finset.mul_prod_erase s g hG).symm
    have hs : ∑ H ∈ s, (1-g H) * ∏ J ∈ s.filter (H ⊂ ·), g J =
        (1-g G) * (∏ H ∈ s.erase G, g H) +
          ∑ H ∈ s.erase G, (1-g H) * ∏ J ∈ (s.erase G).filter (H ⊂ ·), g J := by
      rw [← Finset.add_sum_erase s _ hG, hGeq]
      congr 1
      apply Finset.sum_congr rfl
      intro H hH
      rw [hfilter H hH]
    rw [hp, hs]
    linarith

 
def IsLaminar (s : Finset (Finset ι)) : Prop :=
  ∀ G ∈ s, ∀ H ∈ s, G ⊆ H ∨ H ⊆ G ∨ Disjoint G H

noncomputable def laminarCoefficient (s : Finset (Finset ι))
    (g : Finset ι → ℝ) (G : Finset ι) : ℝ :=
  (1-g G) * ∏ H ∈ s.filter (G ⊂ ·), g H

 
omit [Fintype ι] in
theorem laminar_blocks (s : Finset (Finset ι)) (g : Finset ι → ℝ)
    (hl : IsLaminar s) (i j : ι) :
    (∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), g G) =
      1 - ∑ G ∈ s, if i ∈ G ∧ j ∈ G then laminarCoefficient s g G else 0 := by
  classical
  let t := s.filter (fun G => i ∈ G ∧ j ∈ G)
  have hc : ∀ G ∈ t, ∀ H ∈ t, G ⊆ H ∨ H ⊆ G := by
    intro G hG H hH
    obtain ⟨hGs, hiG, _⟩ := Finset.mem_filter.mp hG
    obtain ⟨hHs, hiH, _⟩ := Finset.mem_filter.mp hH
    rcases hl G hGs H hHs with h | h | h
    · exact Or.inl h
    · exact Or.inr h
    · exact (Finset.disjoint_left.mp h hiG hiH).elim
  have hf (G) (hG : G ∈ t) : t.filter (G ⊂ ·) = s.filter (G ⊂ ·) := by
    obtain ⟨hGs, hiG, hjG⟩ := Finset.mem_filter.mp hG
    ext H
    simp only [t, Finset.mem_filter]
    constructor
    · tauto
    · rintro ⟨hH, hGH⟩
      exact ⟨⟨hH, (Finset.ssubset_iff_subset_ne.mp hGH).1 hiG,
        (Finset.ssubset_iff_subset_ne.mp hGH).1 hjG⟩, hGH⟩
  rw [show (∑ G ∈ s, if i ∈ G ∧ j ∈ G then laminarCoefficient s g G else 0) =
      ∑ G ∈ t, laminarCoefficient s g G by simp [t, Finset.sum_filter]]
  rw [chain_product_telescope t g hc]
  congr 1
  apply Finset.sum_congr rfl
  intro G hG
  rw [hf G hG]
  rfl

 

noncomputable def matrixForm (M : ι → ι → ℝ) : QuadraticForm ℝ (ι → ℝ) :=
  ∑ i, ∑ j, M i j • linMulLin (LinearMap.proj i) (LinearMap.proj j)

omit [DecidableEq ι] in
theorem matrixForm_apply (M : ι → ι → ℝ) (x : ι → ℝ) :
    matrixForm M x = ∑ i, ∑ j, M i j * x i * x j := by
  simp [matrixForm, linMulLin_apply, mul_assoc]

 
theorem laminar_quadratic (s : Finset (Finset ι)) (g : Finset ι → ℝ)
    (hl : IsLaminar s) (z : ι → ℝ) :
    (∑ i, ∑ j, (∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), g G) * z i * z j) =
      (∑ i, z i)^2 - ∑ G ∈ s, laminarCoefficient s g G * (∑ i ∈ G, z i)^2 := by
  classical
  simp_rw [laminar_blocks s g hl, sub_mul, one_mul]
  simp_rw [Finset.sum_sub_distrib]
  have hfirst : (∑ i, ∑ j, z i * z j) = (∑ i, z i)^2 := by
    rw [pow_two, Finset.sum_mul]
    apply Finset.sum_congr rfl
    intro i hi
    rw [Finset.mul_sum]
  rw [hfirst]
  congr 1
  simp_rw [Finset.sum_mul]
  conv_lhs =>
    arg 2
    ext i
    rw [Finset.sum_comm]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl
  intro G hG
  have he (i j : ι) :
      (if i ∈ G ∧ j ∈ G then laminarCoefficient s g G else 0) * z i * z j =
      laminarCoefficient s g G * (if i ∈ G then z i else 0) *
        (if j ∈ G then z j else 0) := by
    by_cases hi : i ∈ G <;> by_cases hj : j ∈ G <;> simp [hi, hj]
  simp_rw [he, ← Finset.mul_sum, ← Finset.sum_mul]
  simp [pow_two, ← Finset.mul_sum, mul_assoc]

 
omit [Fintype ι] in
theorem laminarCoefficient_nonneg (s : Finset (Finset ι))
    (g : Finset ι → ℝ) (hg : ∀ G ∈ s, 0 ≤ g G ∧ g G ≤ 1)
    (G : Finset ι) (hG : G ∈ s) : 0 ≤ laminarCoefficient s g G := by
  apply mul_nonneg (sub_nonneg.mpr (hg G hG).2)
  exact Finset.prod_nonneg fun H hH => (hg H (Finset.mem_filter.mp hH).1).1

 

theorem laminar_matrix_signature (s : Finset (Finset ι)) (g : Finset ι → ℝ)
    (hl : IsLaminar s) (hg : ∀ G ∈ s, 0 ≤ g G ∧ g G ≤ 1)
    (W : ℝ) (hW : 0 ≤ W) (α e : ι → ℝ) (he : ∀ i, 0 ≤ e i) :
    sigPos (matrixForm (fun i j =>
      W * α i * α j * (∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), g G) -
        if i = j then e i else 0)) ≤ 1 := by
  classical
  apply sigPos_le_one_of_kernel _ (∑ i, α i • LinearMap.proj i)
  intro x hx
  have hx' : ∑ i, α i * x i = 0 := by simpa using hx
  rw [matrixForm_apply]
  have hexp :
      (∑ i, ∑ j, (W * α i * α j *
        (∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), g G) -
        if i = j then e i else 0) * x i * x j) =
      W * ((∑ i, α i * x i)^2 -
        ∑ G ∈ s, laminarCoefficient s g G * (∑ i ∈ G, α i * x i)^2) -
        ∑ i, e i * x i^2 := by
    simp_rw [sub_mul, Finset.sum_sub_distrib]
    congr 1
    · rw [← laminar_quadratic s g hl (fun i => α i * x i), Finset.mul_sum]
      apply Finset.sum_congr rfl
      intro i hi
      rw [Finset.mul_sum]
      apply Finset.sum_congr rfl
      intro j hj
      ring
    · apply Finset.sum_congr rfl
      intro i hi
      simp [ite_mul, pow_two, mul_assoc]
  rw [hexp, hx', zero_pow (by decide : 2 ≠ 0), zero_sub]
  have ha : 0 ≤ ∑ G ∈ s, laminarCoefficient s g G * (∑ i ∈ G, α i * x i)^2 :=
    Finset.sum_nonneg fun G hG => mul_nonneg (laminarCoefficient_nonneg s g hg G hG) (sq_nonneg _)
  have hd : 0 ≤ ∑ i, e i * x i^2 :=
    Finset.sum_nonneg fun i _ => mul_nonneg (he i) (sq_nonneg _)
  nlinarith

 
noncomputable def factorRemoval (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ) (i j : ι) : ℝ :=
  ∏ G ∈ s, ψ G (t G - (if i ∈ G then 1 else 0) - (if j ∈ G then 1 else 0))

noncomputable def factorRatio (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ) (i : ι) : ℝ :=
  ∏ G ∈ s.filter (i ∈ ·), ψ G (t G-1) / ψ G (t G)

noncomputable def factorGamma (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ)
    (G : Finset ι) : ℝ := ψ G (t G) * ψ G (t G-2) / ψ G (t G-1)^2

 

omit [Fintype ι] in
theorem factorRemoval_eq (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ)
    (hψ : ∀ G ∈ s, ∀ z, 0 < ψ G z) (i j : ι) :
    factorRemoval s ψ t i j = (∏ G ∈ s, ψ G (t G)) *
      factorRatio s ψ t i * factorRatio s ψ t j *
      ∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), factorGamma ψ t G := by
  classical
  simp only [factorRemoval, factorRatio, Finset.prod_filter]
  rw [← Finset.prod_mul_distrib, ← Finset.prod_mul_distrib, ← Finset.prod_mul_distrib]
  apply Finset.prod_congr rfl
  intro G hG
  have ha : ψ G (t G) ≠ 0 := (hψ G hG _).ne'
  have hb : ψ G (t G-1) ≠ 0 := (hψ G hG _).ne'
  by_cases hi : i ∈ G <;> by_cases hj : j ∈ G
  · simp only [hi, hj, and_self, ↓reduceIte, factorGamma]
    rw [show t G-1-1 = t G-2 by omega]
    field_simp
  · simp only [hi, hj, and_false, ↓reduceIte, sub_zero, mul_one, sub_zero]
    field_simp
  · simp only [hi, hj, false_and, ↓reduceIte, sub_zero, mul_one, sub_zero]
    field_simp
  · simp [hi, hj]

 

noncomputable def truncatedFactorMatrix (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ) (p : ι → ℤ) (i j : ι) : ℝ :=
  if p i = 0 ∨ p j = 0 ∨ (i = j ∧ p i = 1) then 0 else factorRemoval s ψ t i j

 

theorem truncated_laminar_signature (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (t : Finset ι → ℤ) (p : ι → ℤ)
    (hl : IsLaminar s) (hψ : ∀ G ∈ s, ∀ z, 0 < ψ G z)
    (hc : ∀ G ∈ s, ψ G (t G) * ψ G (t G-2) ≤ ψ G (t G-1)^2) :
    sigPos (matrixForm (truncatedFactorMatrix s ψ t p)) ≤ 1 := by
  classical
  let g := factorGamma ψ t
  let W := ∏ G ∈ s, ψ G (t G)
  let α := fun i => if p i = 0 then 0 else factorRatio s ψ t i
  let e := fun i => if p i = 1 then
    W * α i * α i * (∏ G ∈ s.filter (fun G => i ∈ G ∧ i ∈ G), g G) else 0
  have hg : ∀ G ∈ s, 0 ≤ g G ∧ g G ≤ 1 := by
    intro G hG
    dsimp [g, factorGamma]
    exact ⟨div_nonneg (mul_pos (hψ G hG _) (hψ G hG _)).le (sq_nonneg _),
      (div_le_one (sq_pos_of_pos (hψ G hG _))).mpr (hc G hG)⟩
  have hW : 0 ≤ W := (Finset.prod_pos fun G hG => hψ G hG _).le
  have he : ∀ i, 0 ≤ e i := by
    intro i
    dsimp [e]
    split_ifs
    · rw [mul_assoc W, ← pow_two]
      apply mul_nonneg (mul_nonneg hW (sq_nonneg _))
      exact Finset.prod_nonneg fun G hG => (hg G (Finset.mem_filter.mp hG).1).1
    · exact le_rfl
  have hM : truncatedFactorMatrix s ψ t p = fun i j =>
      W * α i * α j * (∏ G ∈ s.filter (fun G => i ∈ G ∧ j ∈ G), g G) -
        if i = j then e i else 0 := by
    funext i j
    rw [truncatedFactorMatrix]
    by_cases hi : p i = 0
    · simp [hi, α, e]
    by_cases hj : p j = 0
    · by_cases hij : i = j
      · subst j; contradiction
      · simp [hj, hij, α, e]
    by_cases hij : i = j
    · subst j
      by_cases hi1 : p i = 1
      · simp [hi1, e]
      · simp [hi, hi1, α, e, factorRemoval_eq s ψ t hψ, W, g]
    · simp [hi, hj, hij, α, factorRemoval_eq s ψ t hψ, W, g]
  rw [hM]
  exact laminar_matrix_signature s g hl hg W hW α e he

 

def removePair (p : ι → ℤ) (i j : ι) : ι → ℤ :=
  fun k => p k - (if k = i then 1 else 0) - (if k = j then 1 else 0)

def InBox (B p : ι → ℤ) : Prop := ∀ i, 0 ≤ p i ∧ p i ≤ B i

noncomputable def twoRemoval (f : (ι → ℤ) → ℝ) (p : ι → ℤ) : ι → ι → ℝ :=
  fun i j => f (removePair p i j)

 
def HasSignature (f : (ι → ℤ) → ℝ) (B : ι → ℤ) (T : ℤ) : Prop :=
  ∀ p, InBox B p → (∑ i, p i) = T+2 → sigPos (matrixForm (twoRemoval f p)) ≤ 1

 
def PositiveSlice (f : (ι → ℤ) → ℝ) (B : ι → ℤ) (T : ℤ) : Prop :=
  (∀ p, InBox B p → (∑ i, p i) = T → 0 < f p) ∧
  (∀ p, ¬(InBox B p ∧ (∑ i, p i) = T) → f p = 0)

omit [Fintype ι] in
theorem sum_removePair (G : Finset ι) (p : ι → ℤ) (i j : ι) :
    (∑ k ∈ G, removePair p i j k) =
      (∑ k ∈ G, p k) - (if i ∈ G then 1 else 0) - (if j ∈ G then 1 else 0) := by
  simp [removePair, Finset.sum_sub_distrib]

omit [Fintype ι] in
theorem removePair_comm (p : ι → ℤ) (i j : ι) : removePair p i j = removePair p j i := by
  funext k
  simp only [removePair]
  omega

omit [Fintype ι] in
theorem removePair_inBox_iff (B p : ι → ℤ) (hp : InBox B p) (i j : ι) :
    InBox B (removePair p i j) ↔ ¬(p i = 0 ∨ p j = 0 ∨ (i = j ∧ p i = 1)) := by
  constructor
  · intro h hbad
    obtain h1 := (h i).1
    obtain h2 := (h j).1
    simp only [removePair, ↓reduceIte] at h1 h2
    rcases hbad with hi | hj | ⟨rfl, hi⟩
    · split_ifs at h1 <;> omega
    · split_ifs at h2 <;> omega
    · simp [hi] at h1
  · intro h k
    obtain ⟨hk0, hkB⟩ := hp k
    dsimp [removePair]
    by_cases hki : k = i <;> by_cases hkj : k = j
    · subst i; subst j
      simp only [not_or, not_and, true_implies] at h
      simp only [↓reduceIte]
      omega
    · subst i
      simp only [↓reduceIte, hkj, sub_zero]
      have : p k ≠ 0 := fun hh => h (Or.inl hh)
      omega
    · subst j
      simp only [hki, ↓reduceIte, sub_zero]
      have : p k ≠ 0 := fun hh => h (Or.inr (Or.inl hh))
      omega
    · simpa [hki, hkj] using hp k

noncomputable def factorSlice (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (B : ι → ℤ) (T : ℤ) (p : ι → ℤ) : ℝ := by
  classical
  exact if InBox B p ∧ (∑ i, p i) = T then ∏ G ∈ s, ψ G (∑ i ∈ G, p i) else 0

omit [DecidableEq ι] in
theorem factorSlice_positive (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (B : ι → ℤ) (T : ℤ)
    (hψ : ∀ G ∈ s, ∀ z, 0 < ψ G z) : PositiveSlice (factorSlice s ψ B T) B T := by
  constructor
  · intro p hb ht
    simp only [factorSlice, hb, ht, and_self, ↓reduceIte]
    exact Finset.prod_pos fun G hG => hψ G hG _
  · intro p h
    simp [factorSlice, h]

 

theorem factorSlice_signature (s : Finset (Finset ι))
    (ψ : Finset ι → ℤ → ℝ) (B : ι → ℤ) (T : ℤ)
    (hl : IsLaminar s) (hψ : ∀ G ∈ s, ∀ z, 0 < ψ G z)
    (hc : ∀ G ∈ s, ∀ z, ψ G z * ψ G (z-2) ≤ ψ G (z-1)^2) :
    HasSignature (factorSlice s ψ B T) B T := by
  intro p hp ht
  have hM : twoRemoval (factorSlice s ψ B T) p =
      truncatedFactorMatrix s ψ (fun G => ∑ i ∈ G, p i) p := by
    funext i j
    have htotal : (∑ k, removePair p i j k) = T := by
      rw [sum_removePair]
      simpa [ht] using (show T+2-1-1 = T by omega)
    simp only [twoRemoval, factorSlice, truncatedFactorMatrix, htotal, and_true,
      removePair_inBox_iff B p hp i j]
    split_ifs <;> simp_all [factorRemoval, sum_removePair]
  rw [hM]
  exact truncated_laminar_signature s ψ _ p hl hψ (fun G hG => hc G hG _)

end Laminar

section ScalarFactors

 
def PosLogConcave (ψ : ℤ → ℝ) : Prop :=
  (∀ z, 0 < ψ z) ∧ (∀ z, ψ z * ψ (z-2) ≤ ψ (z-1)^2)

theorem posLogConcave_of_convex_exponent (η : ℝ) (hη : 0 < η) (hη1 : η < 1)
    (D : ℤ → ℕ) (hD : ∀ z, 2 * D (z-1) ≤ D z + D (z-2)) :
    PosLogConcave (fun z => η ^ D z) := by
  refine ⟨fun z => pow_pos hη _, ?_⟩
  intro z
  rw [← pow_add, ← pow_mul]
  apply pow_le_pow_of_le_one hη.le hη1.le
  simpa [Nat.mul_comm] using hD z

theorem posLogConcave_abs (η : ℝ) (hη : 0 < η) (hη1 : η < 1) :
    PosLogConcave (fun z : ℤ => η ^ z.natAbs) := by
  apply posLogConcave_of_convex_exponent η hη hη1
  intro z
  have ht := Int.natAbs_add_le z (z-2)
  rw [show z+(z-2) = 2*(z-1) by ring, Int.natAbs_mul] at ht
  simpa using ht

theorem posLogConcave_positive_part (η : ℝ) (hη : 0 < η) (hη1 : η < 1) :
    PosLogConcave (fun z : ℤ => η ^ z.toNat) := by
  apply posLogConcave_of_convex_exponent η hη hη1
  intro z
  omega

theorem PosLogConcave.translate {ψ : ℤ → ℝ} (h : PosLogConcave ψ) (a : ℤ) :
    PosLogConcave (fun z => ψ (z+a)) := by
  refine ⟨fun z => h.1 _, ?_⟩
  intro z
  have ht := h.2 (z+a)
  convert ht using 1 <;> congr 2 <;> ring_nf

theorem PosLogConcave.reflect {ψ : ℤ → ℝ} (h : PosLogConcave ψ) :
    PosLogConcave (fun z => ψ (-z)) := by
  refine ⟨fun z => h.1 _, ?_⟩
  intro z
  have ht := h.2 (2-z)
  rw [show 2-z-2 = -z by ring, show 2-z-1 = -(z-1) by ring] at ht
  rw [show 2-z = -(z-2) by ring, mul_comm] at ht
  exact ht

theorem posLogConcave_prod {α : Type*} (s : Finset α) (ψ : α → ℤ → ℝ)
    (hψ : ∀ a ∈ s, PosLogConcave (ψ a)) :
    PosLogConcave (fun z => ∏ a ∈ s, ψ a z) := by
  refine ⟨fun z => Finset.prod_pos fun a ha => (hψ a ha).1 z, ?_⟩
  intro z
  rw [← Finset.prod_mul_distrib, ← Finset.prod_pow]
  exact Finset.prod_le_prod₀
    (fun a ha => mul_nonneg ((hψ a ha).1 _).le ((hψ a ha).1 _).le)
    (fun a ha => (hψ a ha).2 z)

end ScalarFactors

section IndexedFactors
variable {ι α : Type*} [Fintype ι] [DecidableEq ι] [Fintype α] [DecidableEq α]

noncomputable def groupedSequence (G : α → Finset ι) (ψ : α → ℤ → ℝ)
    (H : Finset ι) (z : ℤ) : ℝ := ∏ a ∈ Finset.univ.filter (fun a => G a = H), ψ a z

noncomputable def indexedFactorSlice (G : α → Finset ι) (ψ : α → ℤ → ℝ)
    (B : ι → ℤ) (T : ℤ) (p : ι → ℤ) : ℝ := by
  classical
  exact if InBox B p ∧ (∑ i, p i) = T then ∏ a, ψ a (∑ i ∈ G a, p i) else 0

 

omit [DecidableEq α] in
theorem indexedFactorSlice_eq (G : α → Finset ι) (ψ : α → ℤ → ℝ)
    (B : ι → ℤ) (T : ℤ) : indexedFactorSlice G ψ B T =
      factorSlice (Finset.univ.image G) (groupedSequence G ψ) B T := by
  classical
  funext p
  dsimp [indexedFactorSlice, factorSlice]
  split_ifs
  · symm
    calc
      (∏ H ∈ Finset.univ.image G, groupedSequence G ψ H (∑ i ∈ H, p i)) =
          ∏ H ∈ Finset.univ.image G, ∏ a ∈ Finset.univ.filter (fun a => G a = H),
            ψ a (∑ i ∈ G a, p i) := by
        apply Finset.prod_congr rfl
        intro H hH
        apply Finset.prod_congr rfl
        intro a ha
        rw [(Finset.mem_filter.mp ha).2]
      _ = ∏ a, ψ a (∑ i ∈ G a, p i) :=
        Finset.prod_fiberwise_of_maps_to (fun a _ => Finset.mem_image.mpr ⟨a, Finset.mem_univ _, rfl⟩) _
  · rfl

omit [DecidableEq α] in
theorem indexedFactorSlice_signature (G : α → Finset ι) (ψ : α → ℤ → ℝ)
    (B : ι → ℤ) (T : ℤ)
    (hl : ∀ a b, G a ⊆ G b ∨ G b ⊆ G a ∨ Disjoint (G a) (G b))
    (hψ : ∀ a, PosLogConcave (ψ a)) : HasSignature (indexedFactorSlice G ψ B T) B T := by
  rw [indexedFactorSlice_eq]
  have hp (H : Finset ι) : PosLogConcave (groupedSequence G ψ H) :=
    posLogConcave_prod _ ψ (fun a _ => hψ a)
  apply factorSlice_signature
  · rintro H hH J hJ
    obtain ⟨a, _, rfl⟩ := Finset.mem_image.mp hH
    obtain ⟨b, _, rfl⟩ := Finset.mem_image.mp hJ
    exact hl a b
  · exact fun H _ => (hp H).1
  · exact fun H _ => (hp H).2

end IndexedFactors

section SoftWeight
variable {m n : ℕ}

abbrev Cell (m n : ℕ) := Fin m × Fin n
abbrev Coordinate (m n : ℕ) := Bool × Cell m n
abbrev FactorLabel (m n : ℕ) := (Fin m ⊕ Fin n) × Bool

 

def coordinateLine (a : Coordinate m n) : Fin m ⊕ Fin n :=
  if a.1 then Sum.inl a.2.1 else Sum.inr a.2.2

def softFactorSet (small : Finset (Cell m n)) (a : FactorLabel m n) :
    Finset (Coordinate m n) :=
  Finset.univ.filter fun v => coordinateLine v = a.1 ∧ (a.2 = true → v.2 ∈ small)

theorem softFactorSet_laminar (small : Finset (Cell m n)) :
    ∀ a b, softFactorSet small a ⊆ softFactorSet small b ∨
      softFactorSet small b ⊆ softFactorSet small a ∨
      Disjoint (softFactorSet small a) (softFactorSet small b) := by
  rintro ⟨l, a⟩ ⟨k, b⟩
  by_cases h : l = k
  · subst k
    cases a <;> cases b
    · exact Or.inl (Finset.Subset.refl _)
    · right; left
      intro v hv
      simp only [softFactorSet, Finset.mem_filter, Finset.mem_univ, true_and,
        Bool.false_eq_true, false_implies, and_true] at *
      exact hv.1
    · left
      intro v hv
      simp only [softFactorSet, Finset.mem_filter, Finset.mem_univ, true_and,
        Bool.false_eq_true, false_implies, and_true] at *
      exact hv.1
    · exact Or.inl (Finset.Subset.refl _)
  · right; right
    apply Finset.disjoint_left.mpr
    intro v hv hk
    have hv' := (Finset.mem_filter.mp hv).2.1
    have hk' := (Finset.mem_filter.mp hk).2.1
    exact h (hv'.symm.trans hk')

 

def softFactorExponent (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n))
    (a : FactorLabel m n) (z : ℤ) : ℕ :=
  match a with
  | (Sum.inl i, false) => (z - rb i).natAbs
  | (Sum.inl i, true) => (z - r i).toNat
  | (Sum.inr j, false) => ((∑ i, B (i,j)) - z - cb j).natAbs
  | (Sum.inr j, true) => ((∑ i, if (i,j) ∈ small then B (i,j) else 0) - z - c j).toNat

noncomputable def softFactor (η : ℝ) (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (a : FactorLabel m n) : ℤ → ℝ :=
  fun z => η ^ softFactorExponent r rb c cb B small a z

theorem softFactor_logconcave (η : ℝ) (hη : 0 < η) (hη1 : η < 1)
    (r rb : Fin m → ℤ) (c cb : Fin n → ℤ) (B : Cell m n → ℤ)
    (small : Finset (Cell m n)) (a : FactorLabel m n) :
    PosLogConcave (softFactor η r rb c cb B small a) := by
  obtain ⟨l, b⟩ := a
  cases l with
  | inl i =>
    cases b
    · change PosLogConcave (fun z => η ^ (z - rb i).natAbs)
      simpa only [sub_eq_add_neg] using
        (posLogConcave_abs η hη hη1).translate (-rb i)
    · change PosLogConcave (fun z => η ^ (z - r i).toNat)
      simpa only [sub_eq_add_neg] using
        (posLogConcave_positive_part η hη hη1).translate (-r i)
  | inr j =>
    cases b
    · have h := ((posLogConcave_abs η hη hη1).translate
        ((∑ i, B (i,j)) - cb j)).reflect
      convert h using 1
      funext z
      dsimp [softFactor, softFactorExponent]
      congr 2
      ring
    · have h := ((posLogConcave_positive_part η hη hη1).translate
        ((∑ i, if (i,j) ∈ small then B (i,j) else 0) - c j)).reflect
      convert h using 1
      funext z
      dsimp [softFactor, softFactorExponent]
      congr 2
      ring

 

def softPenalty (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (p : Coordinate m n → ℤ) : ℕ :=
  ∑ a : FactorLabel m n, softFactorExponent r rb c cb B small a
    (∑ v ∈ softFactorSet small a, p v)

noncomputable def softWeight (η : ℝ) (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (p : Coordinate m n → ℤ) : ℝ := by
  classical
  exact if InBox (fun v => B v.2) p ∧ (∑ v, p v) = ∑ s, B s
    then η ^ softPenalty r rb c cb B small p else 0

theorem softWeight_eq_factorSlice (η : ℝ) (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) :
    softWeight η r rb c cb B small = indexedFactorSlice (softFactorSet small)
      (softFactor η r rb c cb B small) (fun v => B v.2) (∑ s, B s) := by
  classical
  funext p
  simp only [softWeight, indexedFactorSlice]
  split_ifs
  · simp [softPenalty, softFactor, Finset.prod_pow_eq_pow_sum]
  · rfl

theorem softWeight_signature (η : ℝ) (hη : 0 < η) (hη1 : η < 1)
    (r rb : Fin m → ℤ) (c cb : Fin n → ℤ) (B : Cell m n → ℤ)
    (small : Finset (Cell m n)) :
    HasSignature (softWeight η r rb c cb B small) (fun v => B v.2) (∑ s, B s) := by
  rw [softWeight_eq_factorSlice]
  apply indexedFactorSlice_signature
  · exact softFactorSet_laminar small
  · exact softFactor_logconcave η hη hη1 r rb c cb B small

theorem softFactorSet_sum_row (small : Finset (Cell m n)) (p : Coordinate m n → ℤ)
    (i : Fin m) (b : Bool) :
    (∑ v ∈ softFactorSet small (Sum.inl i,b), p v) =
      ∑ j, if b = true → (i,j) ∈ small then p (true,(i,j)) else 0 := by
  cases b <;>
    simp [softFactorSet, coordinateLine, Finset.sum_filter, Fintype.sum_prod_type,
      ite_and, Finset.sum_ite_irrel]

theorem softFactorSet_sum_col (small : Finset (Cell m n)) (p : Coordinate m n → ℤ)
    (j : Fin n) (b : Bool) :
    (∑ v ∈ softFactorSet small (Sum.inr j,b), p v) =
      ∑ i, if b = true → (i,j) ∈ small then p (false,(i,j)) else 0 := by
  cases b <;>
    simp [softFactorSet, coordinateLine, Finset.sum_filter, Fintype.sum_prod_type,
      ite_and]

 

theorem softPenalty_expansion (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (p : Coordinate m n → ℤ) :
    softPenalty r rb c cb B small p =
      (∑ i, ((∑ j, p (true,(i,j))) - rb i).natAbs) +
      (∑ j, ((∑ i, (B (i,j) - p (false,(i,j)))) - cb j).natAbs) +
      (∑ i, ((∑ j, if (i,j) ∈ small then p (true,(i,j)) else 0) - r i).toNat) +
      (∑ j, ((∑ i, if (i,j) ∈ small then B (i,j) - p (false,(i,j)) else 0) - c j).toNat) := by
  simp only [softPenalty, Fintype.sum_prod_type, Fintype.sum_sum_type, Fintype.sum_bool]
  simp only [softFactorSet_sum_row, softFactorSet_sum_col, Bool.false_eq_true,
    false_implies, ite_true, true_implies, softFactorExponent]
  simp only [Finset.sum_add_distrib, Finset.sum_sub_distrib]
  have hsplit (j : Fin n) :
      (∑ i, if (i,j) ∈ small then B (i,j) - p (false,(i,j)) else 0) =
      (∑ i, if (i,j) ∈ small then B (i,j) else 0) -
      (∑ i, if (i,j) ∈ small then p (false,(i,j)) else 0) := by
    rw [← Finset.sum_sub_distrib]
    apply Finset.sum_congr rfl
    intro i _
    split_ifs <;> ring
  have hcol (j : Fin n) :
      (∑ i, (B (i,j) - p (false,(i,j)))) = (∑ i, B (i,j)) - (∑ i, p (false,(i,j))) :=
    Finset.sum_sub_distrib (s := Finset.univ) (fun i : Fin m => B (i,j))
      (fun i => p (false,(i,j)))
  simp only [hsplit]
  omega

end SoftWeight

section BalancedClosure
variable {ι : Type*} [Fintype ι] [DecidableEq ι]

 
def pairAssembly {R : Type*} (p : ι → R) (a b : R) : ι ⊕ Bool → R :=
  Sum.elim p (fun t => if t then a else b)

omit [Fintype ι] [DecidableEq ι] in
@[simp] theorem pairAssembly_old {R : Type*} (p : ι → R) (a b : R) (i : ι) :
    pairAssembly p a b (Sum.inl i) = p i := rfl
omit [Fintype ι] [DecidableEq ι] in
@[simp] theorem pairAssembly_first {R : Type*} (p : ι → R) (a b : R) :
    pairAssembly p a b (Sum.inr true) = a := rfl
omit [Fintype ι] [DecidableEq ι] in
@[simp] theorem pairAssembly_second {R : Type*} (p : ι → R) (a b : R) :
    pairAssembly p a b (Sum.inr false) = b := rfl

noncomputable def pairAssemblyLinear : ((ι → ℝ) × ℝ × ℝ) →ₗ[ℝ] (ι ⊕ Bool → ℝ) where
  toFun v := pairAssembly v.1 v.2.1 v.2.2
  map_add' := by intros; funext i; cases i with
    | inl i => rfl
    | inr b => cases b <;> rfl
  map_smul' := by intros; funext i; cases i with
    | inl i => rfl
    | inr b => cases b <;> rfl

 

theorem signature_comp {W : Type*} [AddCommGroup W] [Module ℝ W]
    [FiniteDimensional ℝ W] (Q : QuadraticForm ℝ V) (hs : sigPos Q ≤ 1)
    (L : W →ₗ[ℝ] V) : sigPos (Q.comp L) ≤ 1 := by
  by_cases h : ∃ e, 0 < Q e
  · obtain ⟨e, he⟩ := h
    apply sigPos_le_one_of_kernel _ ((Q.associated e).comp L)
    intro x hx
    have hp := positive_projection Q hs e (L x) he
    have hc : Q.associated (L x) e = 0 := by
      rw [QuadraticMap.associated_isSymm ℝ Q]
      exact hx
    simpa [hc] using hp
  · push Not at h
    apply sigPos_le_one_of_kernel _ 0
    intro x _
    exact h (L x)

noncomputable def columnLinear (s : ι → ℝ) : (ι → ℝ) →ₗ[ℝ] ℝ :=
  ∑ i, (s i) • LinearMap.proj i

omit [DecidableEq ι] in
@[simp] theorem columnLinear_apply (s h : ι → ℝ) :
    columnLinear s h = ∑ i, s i * h i := by simp [columnLinear]

 
omit [DecidableEq ι] in
theorem matrixForm_pairAssembly (M : (ι ⊕ Bool) → (ι ⊕ Bool) → ℝ)
    (hsym : ∀ i j, M i j = M j i) :
    (matrixForm M).comp pairAssemblyLinear =
      pairForm (matrixForm (fun i j => M (Sum.inl i) (Sum.inl j)))
        (columnLinear fun i => M (Sum.inl i) (Sum.inr true))
        (columnLinear fun i => M (Sum.inl i) (Sum.inr false))
        (M (Sum.inr true) (Sum.inr true)) (M (Sum.inr true) (Sum.inr false))
        (M (Sum.inr false) (Sum.inr false)) := by
  ext v
  obtain ⟨h,a,b⟩ := v
  simp only [QuadraticMap.comp_apply, pairAssemblyLinear, LinearMap.coe_mk, AddHom.coe_mk,
    matrixForm_apply, Fintype.sum_sum_type, Fintype.sum_bool, pairAssembly_old,
    pairAssembly_first, pairAssembly_second, pairForm_apply, columnLinear_apply]
  simp_rw [hsym (Sum.inr true) (Sum.inl _), hsym (Sum.inr false) (Sum.inl _),
    hsym (Sum.inr false) (Sum.inr true)]
  have h1 : (∑ i, M (Sum.inl i) (Sum.inr true) * a * h i) =
      (∑ i, M (Sum.inl i) (Sum.inr true) * h i) * a := by
    rw [Finset.sum_mul]
    apply Finset.sum_congr rfl
    intro i _
    ring
  have h2 : (∑ i, M (Sum.inl i) (Sum.inr false) * b * h i) =
      (∑ i, M (Sum.inl i) (Sum.inr false) * h i) * b := by
    rw [Finset.sum_mul]
    apply Finset.sum_congr rfl
    intro i _
    ring
  rw [h1, h2]
  simp only [Finset.sum_add_distrib, ← Finset.sum_mul]
  ring

def removeOne (p : ι → ℤ) (i : ι) : ι → ℤ :=
  fun k => p k - if k = i then 1 else 0

omit [DecidableEq ι] in
theorem pairAssembly_sum (p : ι → ℤ) (a b : ℤ) :
    (∑ i, pairAssembly p a b i) = (∑ i, p i) + a + b := by
  simp [Fintype.sum_sum_type, pairAssembly, add_assoc]

omit [Fintype ι] [DecidableEq ι] in
theorem pairAssembly_inBox (B p : ι → ℤ) (C a b : ℤ) :
    InBox (pairAssembly B C C) (pairAssembly p a b) ↔
      InBox B p ∧ (0 ≤ a ∧ a ≤ C) ∧ (0 ≤ b ∧ b ≤ C) := by
  constructor
  · intro h
    exact ⟨fun i => h (Sum.inl i), h (Sum.inr true), h (Sum.inr false)⟩
  · rintro ⟨hp, ha, hb⟩ i
    cases i with
    | inl i => exact hp i
    | inr b => cases b
               · exact hb
               · exact ha

theorem PositiveSlice.zero_of_negative {κ : Type*} [Fintype κ]
    {f : (κ → ℤ) → ℝ} {B : κ → ℤ} {T : ℤ}
    (hf : PositiveSlice f B T) (p : κ → ℤ) (i : κ) (hi : p i < 0) : f p = 0 := by
  apply hf.2
  rintro ⟨h, _⟩
  exact (not_lt_of_ge (h i).1) hi

omit [Fintype ι] in
theorem removePair_pair_old (p : ι → ℤ) (a b : ℤ) (i j : ι) :
    removePair (pairAssembly p a b) (Sum.inl i) (Sum.inl j) =
      pairAssembly (removePair p i j) a b := by
  funext k
  cases k with
  | inl k => simp [removePair, pairAssembly]
  | inr k => cases k <;> simp [removePair, pairAssembly]

omit [Fintype ι] in
theorem removePair_pair_first (p : ι → ℤ) (a b : ℤ) (i : ι) :
    removePair (pairAssembly p a b) (Sum.inl i) (Sum.inr true) =
      pairAssembly (removeOne p i) (a-1) b := by
  funext k
  cases k with
  | inl k => simp [removePair, pairAssembly, removeOne]
  | inr k => cases k <;> simp [removePair, pairAssembly]

omit [Fintype ι] in
theorem removePair_pair_second (p : ι → ℤ) (a b : ℤ) (i : ι) :
    removePair (pairAssembly p a b) (Sum.inl i) (Sum.inr false) =
      pairAssembly (removeOne p i) a (b-1) := by
  funext k
  cases k with
  | inl k => simp [removePair, pairAssembly, removeOne]
  | inr k => cases k <;> simp [removePair, pairAssembly]

omit [Fintype ι] in
theorem removePair_pair_fresh (p : ι → ℤ) (a b : ℤ) (s t : Bool) :
    removePair (pairAssembly p a b) (Sum.inr s) (Sum.inr t) =
      pairAssembly p (a - (if s then 1 else 0) - (if t then 1 else 0))
        (b - (if s then 0 else 1) - (if t then 0 else 1)) := by
  funext k
  cases k with
  | inl k => simp [removePair, pairAssembly]
  | inr k => cases k <;> cases s <;> cases t <;> simp [removePair, pairAssembly]

noncomputable def pairQ (f : (ι ⊕ Bool → ℤ) → ℝ) (p : ι → ℤ) (D j : ℕ) :
    QuadraticForm ℝ (ι → ℝ) :=
  matrixForm (twoRemoval (fun q => f (pairAssembly q (j:ℤ) ((D:ℤ)-j))) p)

noncomputable def pairS (f : (ι ⊕ Bool → ℤ) → ℝ) (p : ι → ℤ) (D j : ℕ) :
    (ι → ℝ) →ₗ[ℝ] ℝ :=
  columnLinear (fun i => f (pairAssembly (removeOne p i) (j:ℤ) ((D:ℤ)-1-j)))

noncomputable def pairU (f : (ι ⊕ Bool → ℤ) → ℝ) (p : ι → ℤ) (D j : ℕ) : ℝ :=
  f (pairAssembly p ((j:ℤ)-1) ((D:ℤ)-1-j))

 

theorem pair_block_identity (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ) (C T : ℤ)
    (hf : PositiveSlice f (pairAssembly B C C) T) (p : ι → ℤ) (D j : ℕ) :
    (matrixForm (twoRemoval f (pairAssembly p (j:ℤ) ((D:ℤ)-j)))).comp pairAssemblyLinear =
      pairForm (pairQ f p D j) (pairPrevious (pairS f p D) j) (pairS f p D j)
        (pairU f p D (j-1)) (pairU f p D j) (pairU f p D (j+1)) := by
  rw [matrixForm_pairAssembly (twoRemoval f (pairAssembly p (j:ℤ) ((D:ℤ)-j)))
    (fun i k => congrArg f (removePair_comm _ i k))]
  simp only [twoRemoval, removePair_pair_old, removePair_pair_first,
    removePair_pair_second, removePair_pair_fresh, ↓reduceIte, sub_zero]
  have hSa : (columnLinear fun i => f (pairAssembly (removeOne p i) ((j:ℤ)-1) ((D:ℤ)-j))) =
      pairPrevious (pairS f p D) j := by
    by_cases hj : j = 0
    · subst j
      apply LinearMap.ext
      intro h
      simp only [pairPrevious, ↓reduceIte, LinearMap.zero_apply, columnLinear_apply]
      apply Finset.sum_eq_zero
      intro i _
      have hz := hf.zero_of_negative (pairAssembly (removeOne p i) ((0:ℤ)-1) ((D:ℤ)-0))
        (Sum.inr true) (by simp)
      calc
        _ = 0 * h i := congrArg (fun z => z * h i) hz
        _ = 0 := zero_mul _
    · simp only [pairPrevious, ite_eq_right hj, pairS]
      congr 1
      funext i
      congr 2
      · simp [Nat.cast_sub (by omega : 1 ≤ j)]
      · simp [Nat.cast_sub (by omega : 1 ≤ j)]
  have haa : f (pairAssembly p ((j:ℤ)-1-1) ((D:ℤ)-j)) = pairU f p D (j-1) := by
    by_cases hj : j = 0
    · subst j
      dsimp [pairU]
      rw [hf.zero_of_negative _ (Sum.inr true) (by simp),
        hf.zero_of_negative _ (Sum.inr true) (by simp)]
    · simp only [pairU, Nat.cast_sub (by omega : 1 ≤ j), Nat.cast_one]
      congr 2
      ring
  rw [hSa, haa]
  congr 1
  · dsimp [pairS]
    congr 1
    funext i
    congr 2
    ring
  · dsimp [pairU]
    congr 2 <;> ring
  · dsimp [pairU]
    congr 2 <;> ring

omit [DecidableEq ι] in
theorem pairU_positive (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ) (C : ℕ) (T : ℤ)
    (hf : PositiveSlice f (pairAssembly B (C:ℤ) C) T) (p : ι → ℤ)
    (hp : InBox B p) (D : ℕ) (hDC : D ≤ C+1)
    (ht : (∑ i, p i) + (D:ℤ) = T+2) (j : ℕ) (hj : 0 < j) (hjD : j < D) :
    0 < pairU f p D j := by
  apply hf.1
  · rw [pairAssembly_inBox]
    exact ⟨hp, by omega, by omega⟩
  · rw [pairAssembly_sum]
    omega

omit [DecidableEq ι] in
theorem pairU_zero (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ) (C T : ℤ)
    (hf : PositiveSlice f (pairAssembly B C C) T) (p : ι → ℤ) (D j : ℕ)
    (hj : j = 0 ∨ D ≤ j) : pairU f p D j = 0 := by
  rcases hj with rfl | hj
  · exact hf.zero_of_negative _ (Sum.inr true) (by simp [pairAssembly])
  · exact hf.zero_of_negative _ (Sum.inr false) (by dsimp [pairAssembly]; omega)

theorem pairS_top (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ) (C T : ℤ)
    (hf : PositiveSlice f (pairAssembly B C C) T) (p : ι → ℤ) (D : ℕ) :
    pairS f p D D = 0 := by
  apply LinearMap.ext
  intro h
  simp only [pairS, columnLinear_apply, LinearMap.zero_apply]
  apply Finset.sum_eq_zero
  intro i _
  rw [hf.zero_of_negative _ (Sum.inr false) (by dsimp [pairAssembly]; omega), zero_mul]

theorem pair_block_signature (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ) (C : ℕ) (T : ℤ)
    (hf : PositiveSlice f (pairAssembly B (C:ℤ) C) T)
    (hs : HasSignature f (pairAssembly B (C:ℤ) C) T)
    (p : ι → ℤ) (hp : InBox B p) (D : ℕ)
    (ht : (∑ i, p i) + (D:ℤ) = T+2)
    (j : ℕ) (hj : j ≤ D) (hjC : j ≤ C) (hDj : D ≤ C+j) :
    sigPos (pairForm (pairQ f p D j) (pairPrevious (pairS f p D) j) (pairS f p D j)
      (pairU f p D (j-1)) (pairU f p D j) (pairU f p D (j+1))) ≤ 1 := by
  rw [← pair_block_identity f B (C:ℤ) T hf p D j]
  apply signature_comp
  apply hs
  · rw [pairAssembly_inBox]
    exact ⟨hp, by omega, by omega⟩
  · rw [pairAssembly_sum]
    omega

noncomputable def balancedMarginal (f : (ι ⊕ Bool → ℤ) → ℝ) (C : ℕ)
    (p : ι → ℤ) : ℝ := ∑ j ∈ Finset.range (C+1),
      f (pairAssembly p (j:ℤ) ((C:ℤ)-j))

omit [DecidableEq ι] in
theorem matrixForm_sum {α : Type*} (s : Finset α) (M : α → ι → ι → ℝ) :
    matrixForm (fun i k => ∑ a ∈ s, M a i k) = ∑ a ∈ s, matrixForm (M a) := by
  ext h
  simp only [matrixForm_apply, sum_apply, Finset.sum_mul]
  calc
    _ = ∑ i, ∑ a ∈ s, ∑ k, M a i k * h i * h k := by
      apply Finset.sum_congr rfl
      intro i _
      exact Finset.sum_comm
    _ = _ := Finset.sum_comm

theorem balancedMarginal_signature (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ)
    (C : ℕ) (hC : 2 ≤ C) (T : ℤ)
    (hf : PositiveSlice f (pairAssembly B (C:ℤ) C) T)
    (hs : HasSignature f (pairAssembly B (C:ℤ) C) T) :
    HasSignature (balancedMarginal f C) B (T-C) := by
  intro p hp ht
  have htotal : (∑ i, p i) + (C:ℤ) = T+2 := by omega
  have heq : matrixForm (twoRemoval (balancedMarginal f C) p) =
      ∑ j ∈ Finset.range (C+1), pairQ f p C j := by
    exact matrixForm_sum (Finset.range (C+1))
      (fun j i k => f (pairAssembly (removePair p i k) (j:ℤ) ((C:ℤ)-j)))
  rw [heq]
  apply pair_summation_signature C hC (pairU f p C) (pairS f p C) (pairQ f p C)
  · exact pairU_positive f B C T hf p hp C (by omega) htotal
  · exact pairU_zero f B (C:ℤ) T hf p C
  · exact pairS_top f B (C:ℤ) T hf p C
  · intro j hj
    exact pair_block_signature f B C T hf hs p hp C htotal j hj hj (by omega)

omit [DecidableEq ι] in
theorem balancedMarginal_positive (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ)
    (C : ℕ) (T : ℤ) (hf : PositiveSlice f (pairAssembly B (C:ℤ) C) T) :
    PositiveSlice (balancedMarginal f C) B (T-C) := by
  constructor
  · intro p hp ht
    apply Finset.sum_pos
    · intro j hj
      have hjC : j ≤ C := by simpa using Finset.mem_range.mp hj
      apply hf.1
      · rw [pairAssembly_inBox]
        exact ⟨hp, by omega, by omega⟩
      · rw [pairAssembly_sum]
        omega
    · exact ⟨0, by simp⟩
  · intro p hp
    apply Finset.sum_eq_zero
    intro j _
    apply hf.2
    rintro ⟨hb, ht⟩
    apply hp
    rw [pairAssembly_inBox] at hb
    rw [pairAssembly_sum] at ht
    exact ⟨hb.1, by omega⟩

 

theorem actual_pair_weighted (f : (ι ⊕ Bool → ℤ) → ℝ) (B : ι → ℤ)
    (C : ℕ) (hC : 2 ≤ C) (T : ℤ)
    (hf : PositiveSlice f (pairAssembly B (C:ℤ) C) T)
    (hs : HasSignature f (pairAssembly B (C:ℤ) C) T)
    (p : ι → ℤ) (hp : InBox B p) (ht : (∑ i, p i) + (C+1:ℕ) = T+2)
    (h : ι → ℝ) (hbal : ∑ j ∈ Finset.range (C+1), pairS f p (C+1) j h = 0) :
    2 * (∑ j ∈ Finset.range (C+2), pairU f p (C+1) j *
      pairCumulative (C+1) (pairU f p (C+1)) (pairS f p (C+1)) h j ^ 2) ≤
    -(∑ j ∈ Finset.range (C+2), ((j:ℝ) * (C+1-j)) • pairQ f p (C+1) j) h := by
  have hw := pair_summation_weighted (C+1) (by omega)
    (pairU f p (C+1)) (pairS f p (C+1)) (pairQ f p (C+1))
    (pairU_positive f B C T hf p hp (C+1) (by omega) ht)
    (pairU_zero f B (C:ℤ) T hf p (C+1))
    (pairS_top f B (C:ℤ) T hf p (C+1))
    (fun j hj hjC => pair_block_signature f B C T hf hs p hp (C+1) ht j
      (by omega) (by omega) (by omega)) h hbal
  simpa only [Nat.cast_add, Nat.cast_one] using hw

end BalancedClosure

section ReindexAndContext
variable {ι κ : Type*} [Fintype ι] [DecidableEq ι] [Fintype κ] [DecidableEq κ]

noncomputable def coordinatePullback (e : ι ≃ κ) : (κ → ℝ) →ₗ[ℝ] (ι → ℝ) where
  toFun p := p ∘ e
  map_add' := by intros; rfl
  map_smul' := by intros; rfl

omit [Fintype ι] [Fintype κ] in
theorem removePair_reindex (e : ι ≃ κ) (p : κ → ℤ) (i j : ι) :
    removePair (p ∘ e) i j = (removePair p (e i) (e j)) ∘ e := by
  funext k
  simp [removePair, e.injective.eq_iff]

omit [DecidableEq ι] [DecidableEq κ] in
theorem matrixForm_reindex (e : ι ≃ κ) (M : κ → κ → ℝ) :
    (matrixForm fun i j => M (e i) (e j)) = (matrixForm M).comp (coordinatePullback e.symm) := by
  ext h
  simp only [matrixForm_apply, QuadraticMap.comp_apply, coordinatePullback,
    LinearMap.coe_mk, AddHom.coe_mk, Function.comp_apply]
  rw [← Equiv.sum_comp e]
  apply Finset.sum_congr rfl
  intro i _
  rw [← Equiv.sum_comp e]
  simp

 

theorem HasSignature.reindex {f : (ι → ℤ) → ℝ} {B : ι → ℤ} {T : ℤ}
    (hf : HasSignature f B T) (e : ι ≃ κ) :
    HasSignature (fun p => f (p ∘ e)) (B ∘ e.symm) T := by
  intro p hp ht
  have hb : InBox B (p ∘ e) := by
    intro i
    simpa [Function.comp_def] using hp (e i)
  have htotal : (∑ i, (p ∘ e) i) = T+2 := by simpa [Function.comp_def, Equiv.sum_comp e] using ht
  have hs := hf (p ∘ e) hb htotal
  have hM : twoRemoval (fun p => f (p ∘ e)) p =
      fun i j => twoRemoval f (p ∘ e) (e.symm i) (e.symm j) := by
    funext i j
    simp [twoRemoval, removePair_reindex]
  rw [hM, matrixForm_reindex e.symm]
  exact signature_comp _ hs _

omit [DecidableEq ι] [DecidableEq κ] in
theorem PositiveSlice.reindex {f : (ι → ℤ) → ℝ} {B : ι → ℤ} {T : ℤ}
    (hf : PositiveSlice f B T) (e : ι ≃ κ) :
    PositiveSlice (fun p => f (p ∘ e)) (B ∘ e.symm) T := by
  have hbox (p : κ → ℤ) : InBox (B ∘ e.symm) p ↔ InBox B (p ∘ e) := by
    constructor
    · intro hp i
      simpa [Function.comp_def] using hp (e i)
    · intro hp i
      simpa [Function.comp_def] using hp (e.symm i)
  have hsum (p : κ → ℤ) : (∑ i, (p ∘ e) i) = ∑ i, p i := Equiv.sum_comp e p
  constructor
  · intro p hp ht
    apply hf.1 _ ((hbox p).mp hp)
    rw [hsum, ht]
  · intro p hp
    apply hf.2
    simpa only [← hbox, hsum] using hp

noncomputable def rightCoordinates : (κ → ℝ) →ₗ[ℝ] (ι ⊕ κ → ℝ) where
  toFun p := Sum.elim 0 p
  map_add' := by intros; funext i; cases i <;> simp
  map_smul' := by intros; funext i; cases i <;> simp

omit [DecidableEq ι] [DecidableEq κ] in
theorem matrixForm_right (M : (ι ⊕ κ) → (ι ⊕ κ) → ℝ) :
    matrixForm (fun i j => M (Sum.inr i) (Sum.inr j)) =
      (matrixForm M).comp rightCoordinates := by
  ext h
  simp [matrixForm_apply, rightCoordinates, Fintype.sum_sum_type]

omit [Fintype ι] [Fintype κ] in
theorem removePair_fixed (σ : ι → ℤ) (p : κ → ℤ) (i j : κ) :
    removePair (Sum.elim σ p) (Sum.inr i) (Sum.inr j) = Sum.elim σ (removePair p i j) := by
  funext k
  cases k <;> simp [removePair]

theorem HasSignature.fixedContext {f : (ι ⊕ κ → ℤ) → ℝ} {A : ι → ℤ} {B : κ → ℤ} {T : ℤ}
    (hf : HasSignature f (Sum.elim A B) T) (σ : ι → ℤ) (hσ : InBox A σ) :
    HasSignature (fun p => f (Sum.elim σ p)) B (T - ∑ i, σ i) := by
  intro p hp ht
  have hb : InBox (Sum.elim A B) (Sum.elim σ p) := by
    intro i
    cases i with
    | inl i => exact hσ i
    | inr i => exact hp i
  have htotal : (∑ i, Sum.elim σ p i) = T+2 := by
    simp only [Fintype.sum_sum_type, Sum.elim_inl, Sum.elim_inr]
    omega
  have hs := hf (Sum.elim σ p) hb htotal
  have hM : twoRemoval (fun p => f (Sum.elim σ p)) p =
      fun i j => twoRemoval f (Sum.elim σ p) (Sum.inr i) (Sum.inr j) := by
    funext i j
    simp [twoRemoval, removePair_fixed]
  rw [hM, matrixForm_right]
  exact signature_comp _ hs _

omit [DecidableEq ι] [DecidableEq κ] in
theorem PositiveSlice.fixedContext {f : (ι ⊕ κ → ℤ) → ℝ} {A : ι → ℤ} {B : κ → ℤ} {T : ℤ}
    (hf : PositiveSlice f (Sum.elim A B) T) (σ : ι → ℤ) (hσ : InBox A σ) :
    PositiveSlice (fun p => f (Sum.elim σ p)) B (T - ∑ i, σ i) := by
  constructor
  · intro p hp ht
    apply hf.1
    · intro i
      cases i with
      | inl i => exact hσ i
      | inr i => exact hp i
    · simp only [Fintype.sum_sum_type, Sum.elim_inl, Sum.elim_inr]
      omega
  · intro p hp
    apply hf.2
    rintro ⟨hb, ht⟩
    apply hp
    refine ⟨fun i => hb (Sum.inr i), ?_⟩
    simp only [Fintype.sum_sum_type, Sum.elim_inl, Sum.elim_inr] at ht
    omega

end ReindexAndContext

section ManyBalanced
variable {ι : Type*} [Fintype ι] [DecidableEq ι]

noncomputable def balanceBox {k : ℕ} (A : ι → ℤ) (C : Fin k → ℕ) :
    ι ⊕ (Fin k × Bool) → ℤ := Sum.elim A (fun sb => (C sb.1 : ℤ))

def balanceProfile {k : ℕ} (C : Fin k → ℕ) (a : ∀ s, Fin (C s+1)) :
    Fin k × Bool → ℤ := fun sb => if sb.2 then (a sb.1:ℕ) else (C sb.1:ℤ) - (a sb.1:ℕ)

noncomputable def balancedSlots {k : ℕ} (f : (ι ⊕ (Fin k × Bool) → ℤ) → ℝ)
    (C : Fin k → ℕ) (p : ι → ℤ) : ℝ :=
  ∑ a : ∀ s, Fin (C s+1), f (Sum.elim p (balanceProfile C a))

def splitSlot (k : ℕ) : (ι ⊕ (Fin (k+1) × Bool)) ≃ ((ι ⊕ (Fin k × Bool)) ⊕ Bool) where
  toFun := Sum.elim (fun i => Sum.inl (Sum.inl i))
    (fun sb => Fin.cases (Sum.inr sb.2) (fun s => Sum.inl (Sum.inr (s,sb.2))) sb.1)
  invFun := Sum.elim (Sum.elim Sum.inl (fun sb => Sum.inr (sb.1.succ, sb.2)))
    (fun b => Sum.inr (0,b))
  left_inv := by
    intro i
    rcases i with i | ⟨s,b⟩
    · rfl
    · refine Fin.cases ?_ (fun j => ?_) s <;> rfl
  right_inv := by
    intro i
    rcases i with (i | ⟨s,b⟩) | b <;> rfl

def piHeadEquiv {k : ℕ} (D : Fin (k+1) → Type*) :
    (∀ s, D s) ≃ D 0 × (∀ s : Fin k, D s.succ) where
  toFun a := (a 0, fun s => a s.succ)
  invFun a := Fin.cons a.1 a.2
  left_inv := by
    intro a
    funext s
    refine Fin.cases ?_ (fun j => ?_) s <;> rfl
  right_inv := by intro a; rfl

omit [Fintype ι] [DecidableEq ι] in
theorem balanceBox_split {k : ℕ} (A : ι → ℤ) (C : Fin (k+1) → ℕ) :
    balanceBox A C ∘ (splitSlot k).symm =
      pairAssembly (balanceBox A (fun s => C s.succ)) (C 0) (C 0) := by
  funext i
  rcases i with (i | ⟨s,b⟩) | b
  · rfl
  · rfl
  · cases b <;> rfl

omit [Fintype ι] [DecidableEq ι] in
theorem balancedSlots_succ {k : ℕ}
    (f : (ι ⊕ (Fin (k+1) × Bool) → ℤ) → ℝ) (C : Fin (k+1) → ℕ) :
    balancedSlots f C = balancedSlots
      (balancedMarginal (fun q => f (q ∘ splitSlot k)) (C 0)) (fun s => C s.succ) := by
  funext p
  unfold balancedSlots
  rw [← Equiv.sum_comp (piHeadEquiv (fun s => Fin (C s+1))).symm]
  rw [Fintype.sum_prod_type, Finset.sum_comm]
  apply Finset.sum_congr rfl
  intro a _
  rw [balancedMarginal, ← Fin.sum_univ_eq_sum_range]
  apply Finset.sum_congr rfl
  intro j _
  congr 1
  funext i
  rcases i with i | ⟨s,b⟩
  · rfl
  · refine Fin.cases ?_ (fun t => ?_) s <;> cases b <;> rfl

 

theorem balancedSlots_closure (k : ℕ) (f : (ι ⊕ (Fin k × Bool) → ℤ) → ℝ)
    (A : ι → ℤ) (C : Fin k → ℕ) (hC : ∀ s, 2 ≤ C s) (T : ℤ)
    (hp : PositiveSlice f (balanceBox A C) T)
    (hs : HasSignature f (balanceBox A C) T) :
    PositiveSlice (balancedSlots f C) A (T - ∑ s, (C s:ℤ)) ∧
    HasSignature (balancedSlots f C) A (T - ∑ s, (C s:ℤ)) := by
  induction k generalizing T with
  | zero =>
    let e : (ι ⊕ (Fin 0 × Bool)) ≃ ι := Equiv.sumEmpty _ _
    have hbox : balanceBox A C ∘ e.symm = A := by funext i; rfl
    have hf : balancedSlots f C = (fun p => f (p ∘ e)) := by
      funext p
      simp only [balancedSlots, Finset.univ_unique, Finset.sum_singleton]
      congr 1
      funext i
      rcases i with i | ⟨s,b⟩
      · rfl
      · exact Fin.elim0 s
    have hsum : (∑ s, (C s:ℤ)) = 0 := by simp
    rw [hsum, sub_zero, hf]
    simpa only [hbox] using And.intro (hp.reindex e) (hs.reindex e)
  | succ k ih =>
    rw [balancedSlots_succ]
    have hp' := hp.reindex (splitSlot k)
    have hs' := hs.reindex (splitSlot k)
    rw [balanceBox_split] at hp' hs'
    have hp'' := balancedMarginal_positive _ _ (C 0) T hp'
    have hs'' := balancedMarginal_signature _ _ (C 0) (hC 0) T hp' hs'
    have h := ih (balancedMarginal (fun q => f (q ∘ splitSlot k)) (C 0))
      (fun s => C s.succ) (fun s => hC s.succ) (T - (C 0:ℤ)) hp'' hs''
    rw [Fin.sum_univ_succ]
    convert h using 1 <;> ring_nf

end ManyBalanced

section SoftClosure
attribute [local instance] Classical.propDecidable
variable {m n k : ℕ} {ι κ : Type*}
  [Fintype ι] [DecidableEq ι] [Fintype κ] [DecidableEq κ]

theorem softWeight_positive (η : ℝ) (hη : 0 < η)
    (r rb : Fin m → ℤ) (c cb : Fin n → ℤ) (B : Cell m n → ℤ)
    (small : Finset (Cell m n)) :
    PositiveSlice (softWeight η r rb c cb B small) (fun v => B v.2) (∑ s, B s) := by
  classical
  constructor
  · intro p hp ht
    simp only [softWeight]
    rw [ite_eq_left ⟨hp,ht⟩]
    exact pow_pos hη _
  · intro p hp
    simp only [softWeight, ite_eq_right hp]

 

theorem softWeight_context_balanced_signature
    (η : ℝ) (hη : 0 < η) (hη1 : η < 1)
    (r rb : Fin m → ℤ) (c cb : Fin n → ℤ) (B : Cell m n → ℤ)
    (small : Finset (Cell m n))
    (e : Coordinate m n ≃ (κ ⊕ (ι ⊕ (Fin k × Bool))))
    (A : κ → ℤ) (D : ι → ℤ) (C : Fin k → ℕ) (hC : ∀ s, 2 ≤ C s)
    (he : (fun v => B v.2) ∘ e.symm = Sum.elim A (balanceBox D C))
    (σ : κ → ℤ) (hσ : InBox A σ) :
    let Φ := balancedSlots (fun p => softWeight η r rb c cb B small ((Sum.elim σ p) ∘ e)) C
    PositiveSlice Φ D ((∑ s, B s) - (∑ i, σ i) - ∑ s, (C s:ℤ)) ∧
    HasSignature Φ D ((∑ s, B s) - (∑ i, σ i) - ∑ s, (C s:ℤ)) := by
  have hp := (softWeight_positive η hη r rb c cb B small).reindex e
  have hs := (softWeight_signature η hη hη1 r rb c cb B small).reindex e
  rw [he] at hp hs
  exact balancedSlots_closure k _ D C hC _ (hp.fixedContext σ hσ) (hs.fixedContext σ hσ)

theorem softWeight_continuous (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (p : Coordinate m n → ℤ) :
    Continuous (fun η : ℝ => softWeight η r rb c cb B small p) := by
  classical
  unfold softWeight
  split_ifs
  · fun_prop
  · fun_prop

 

theorem softWeight_zero (r rb : Fin m → ℤ) (c cb : Fin n → ℤ)
    (B : Cell m n → ℤ) (small : Finset (Cell m n)) (p : Coordinate m n → ℤ) :
    softWeight 0 r rb c cb B small p =
      if InBox (fun v => B v.2) p ∧ (∑ v, p v) = ∑ s, B s ∧
        softPenalty r rb c cb B small p = 0 then 1 else 0 := by
  classical
  unfold softWeight
  by_cases hb : InBox (fun v => B v.2) p ∧ (∑ v, p v) = ∑ s, B s
  · by_cases hd : softPenalty r rb c cb B small p = 0
    · simp [hb.1, hb.2, hd]
    · simp [hb.1, hb.2, hd, zero_pow hd]
  · simp only [ite_eq_right hb]
    symm
    apply ite_eq_right
    exact fun h => hb ⟨h.1,h.2.1⟩

omit [Fintype ι] [DecidableEq ι] [Fintype κ] [DecidableEq κ] in
theorem balancedSoft_continuous
    (r rb : Fin m → ℤ) (c cb : Fin n → ℤ) (B : Cell m n → ℤ)
    (small : Finset (Cell m n))
    (e : Coordinate m n ≃ (κ ⊕ (ι ⊕ (Fin k × Bool))))
    (σ : κ → ℤ) (C : Fin k → ℕ) (p : ι → ℤ) :
    Continuous (fun η : ℝ => balancedSlots
      (fun q => softWeight η r rb c cb B small ((Sum.elim σ q) ∘ e)) C p) := by
  unfold balancedSlots
  apply continuous_finsetSum
  intro a _
  exact softWeight_continuous r rb c cb B small _

end SoftClosure

end Signature
end Contingency

end

end OAI