File size: 148,227 Bytes
f61e547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
import Mathlib

namespace OAI

 

 
section

 

noncomputable section

open Set Filter MeasureTheory ProbabilityTheory
open scoped Topology ENNReal NNReal

namespace BelowOne

abbrev Time := Set.Icc (0 : ℝ) 1
abbrev Curve := C(Time, ℂ)
abbrev GridPoint := ℤ × ℤ

instance : MeasurableSpace Curve := borel Curve
instance : BorelSpace Curve := ⟨rfl⟩

 
structure IncreasingReparam where
  homeomorph : Time ≃ₜ Time
  increasing : StrictMono homeomorph

instance : Inhabited IncreasingReparam :=
  ⟨⟨Homeomorph.refl _, fun _ _ h => h⟩⟩

 
def curveDistance (α β : Curve) : ℝ :=
  sInf {r : ℝ | ∃ φ ψ : IncreasingReparam,
    r = sSup (Set.range fun t : Time => dist (α (φ.homeomorph t)) (β (ψ.homeomorph t)))}

 

def IsCurveTest (f : Curve → ℝ) : Prop :=
  (∃ C : ℝ, ∀ α, |f α| ≤ C) ∧
  ∀ α, ∀ ε > 0, ∃ r > 0, ∀ β, curveDistance α β < r → |f α - f β| < ε

def gridEmbed (δ : ℝ) (v : GridPoint) : ℂ :=
  ⟨δ * v.1, δ * v.2⟩

def gridNeighbor (v w : GridPoint) : Prop :=
  (v.1 = w.1 ∧ |v.2 - w.2| = 1) ∨
  (v.2 = w.2 ∧ |v.1 - w.1| = 1)

 
def interpolate (points : List ℂ) (t : Time) : ℂ :=
  if (t : ℝ) = 1 then points.getLast?.getD 0 else
    let s := (t : ℝ) * (points.length - 1)
    let j := Nat.floor s
    let θ : ℂ := ((s - j : ℝ) : ℂ)
    (1 - θ) * points[j]?.getD 0 + θ * points[j + 1]?.getD 0

def cycleNext {n : ℕ} (hn : 0 < n) (i : Fin n) : Fin n :=
  ⟨(i.val + 1) % n, Nat.mod_lt _ hn⟩

def cyclePrev {n : ℕ} (hn : 0 < n) (i : Fin n) : Fin n :=
  ⟨(i.val + n - 1) % n, Nat.mod_lt _ hn⟩

 

structure SmoothMarkedJordan where
  domain : Set ℂ
  isOpen : IsOpen domain
  isConnected : IsConnected domain
  bounded : Bornology.IsBounded domain
  boundary : ℝ → ℂ
  smooth : ContDiff ℝ (↑(⊤ : ℕ∞)) boundary
  periodic : Function.Periodic boundary 1
  simple : Set.InjOn boundary (Set.Ico 0 1)
  regular : ∀ t, deriv boundary t ≠ 0
  frontier : frontier domain = boundary '' Set.Icc 0 1
  positive_orientation :
    0 < ∫ t in (0 : ℝ)..1,
      (boundary t).re * (deriv boundary t).im -
        (boundary t).im * (deriv boundary t).re
  secondMark : ℝ
  secondMark_pos : 0 < secondMark
  secondMark_lt : secondMark < 1

def SmoothMarkedJordan.a (D : SmoothMarkedJordan) := D.boundary 0
def SmoothMarkedJordan.b (D : SmoothMarkedJordan) := D.boundary D.secondMark

 

structure LatticePolygon (δ : ℝ) where
  domain : Set ℂ
  isOpen : IsOpen domain
  isConnected : IsConnected domain
  bounded : Bornology.IsBounded domain
  nVertices : ℕ
  vertices : Fin nVertices → GridPoint
  vertices_injective : Function.Injective vertices
  all_vertices : ∀ v : GridPoint,
    (∃ i, vertices i = v) ↔ gridEmbed δ v ∈ closure domain
  nBoundary : ℕ
  boundary_pos : 0 < nBoundary
  boundary_three : 3 ≤ nBoundary
  boundaryVertex : Fin nBoundary → Fin nVertices
  boundary_injective : Function.Injective boundaryVertex
  boundary_neighbor : ∀ i,
    gridNeighbor (vertices (boundaryVertex i))
      (vertices (boundaryVertex (cycleNext boundary_pos i)))
  boundary_segment : ∀ i,
    segment ℝ (gridEmbed δ (vertices (boundaryVertex i)))
      (gridEmbed δ (vertices (boundaryVertex (cycleNext boundary_pos i)))) ⊆
      closure domain
  boundaryCurve : Curve
  boundary_interpolation : ∀ t,
    boundaryCurve t = interpolate
      ((List.ofFn fun i => gridEmbed δ (vertices (boundaryVertex i))) ++
        [gridEmbed δ (vertices (boundaryVertex ⟨0, boundary_pos⟩))]) t
  boundary_simple : ∀ s t : Time,
    (s : ℝ) < 1 → (t : ℝ) < 1 → boundaryCurve s = boundaryCurve t → s = t
  frontier : frontier domain = Set.range boundaryCurve
  positive_orientation :
    0 < ∑ i : Fin nBoundary,
      ((gridEmbed δ (vertices (boundaryVertex i))).re *
        (gridEmbed δ (vertices (boundaryVertex (cycleNext boundary_pos i)))).im -
      (gridEmbed δ (vertices (boundaryVertex i))).im *
        (gridEmbed δ (vertices (boundaryVertex (cycleNext boundary_pos i)))).re)
  secondMark : Fin nBoundary
  secondMark_ne : secondMark.val ≠ 0
  wiredForward : Bool

namespace LatticePolygon

variable {δ : ℝ} (P : LatticePolygon δ)

abbrev Vertex := Fin P.nVertices

def position (v : P.Vertex) : ℂ := gridEmbed δ (P.vertices v)

def graph : SimpleGraph P.Vertex :=
  SimpleGraph.fromRel fun v w =>
    gridNeighbor (P.vertices v) (P.vertices w) ∧
      segment ℝ (P.position v) (P.position w) ⊆ closure P.domain

def edges : Finset (Sym2 P.Vertex) := P.graph.edgeSet.toFinite.toFinset

def first : Fin P.nBoundary := ⟨0, P.boundary_pos⟩

def aVertex : P.Vertex := P.boundaryVertex P.first
def bVertex : P.Vertex := P.boundaryVertex P.secondMark

def wiredVertices : Finset P.Vertex :=
  Finset.univ.filter fun v => ∃ i : Fin P.nBoundary,
    P.boundaryVertex i = v ∧
      if P.wiredForward then i.val ≤ P.secondMark.val
      else P.secondMark.val ≤ i.val ∨ i.val = 0

 

def wiredEdges : Finset (Sym2 P.Vertex) :=
  ((Finset.univ : Finset (Fin P.nBoundary)).filter fun i =>
    if P.wiredForward then i.val < P.secondMark.val
    else P.secondMark.val ≤ i.val).image fun i =>
      s(P.boundaryVertex i, P.boundaryVertex (cycleNext P.boundary_pos i))

 

def wiredConfiguration (A : Finset (Sym2 P.Vertex)) : SimpleGraph P.Vertex :=
  SimpleGraph.fromRel fun v w =>
    (s(v,w) ∈ A) ∨ (v ∈ P.wiredVertices ∧ w ∈ P.wiredVertices)

def componentCount (A : Finset (Sym2 P.Vertex)) : ℕ :=
  Nat.card (P.wiredConfiguration A).ConnectedComponent

 
def configurationWeight (q : ℝ) (A : Finset (Sym2 P.Vertex)) : ℝ :=
  let p := Real.sqrt q / (1 + Real.sqrt q)
  p ^ A.card * (1 - p) ^ (P.edges.card - A.card) * q ^ P.componentCount A

def partitionFunction (q : ℝ) : ℝ :=
  ∑ A ∈ P.edges.powerset, P.configurationWeight q A

 
structure Dart where
  source : P.Vertex
  target : P.Vertex
  adjacent : P.graph.Adj source target

instance : Finite P.Dart :=
  Finite.of_injective (fun e : P.Dart => (e.source, e.target)) (by
    rintro ⟨s, t, h⟩ ⟨s', t', h'⟩ he
    obtain ⟨rfl, rfl⟩ := Prod.mk.inj he
    rfl)

instance : Fintype P.Dart := Fintype.ofFinite _
instance : DecidableEq P.Dart := Classical.decEq _

def Dart.reverse (e : P.Dart) : P.Dart :=
  ⟨e.target, e.source, P.graph.adj_symm e.adjacent⟩

def dartPosition (e : P.Dart) : ℂ :=
  (P.position e.source + P.position e.target) / 2

 
def dartDirection (e : P.Dart) : ℕ :=
  if (P.vertices e.source).1 < (P.vertices e.target).1 then 0 else
  if (P.vertices e.source).2 < (P.vertices e.target).2 then 1 else
  if (P.vertices e.target).1 < (P.vertices e.source).1 then 2 else 3

def angularSteps (clockwise : Bool) (e f : P.Dart) : ℕ :=
  let k := if clockwise then
    (P.dartDirection e + 4 - P.dartDirection f) % 4 else
    (P.dartDirection f + 4 - P.dartDirection e) % 4
  if k = 0 then 4 else k

 
def IsNextDart (clockwise : Bool) (e f : P.Dart) : Prop :=
  e.source = f.source ∧ ∀ g : P.Dart, e.source = g.source →
    P.angularSteps clockwise e f ≤ P.angularSteps clockwise e g

 

def IsSwitched (A : Finset (Sym2 P.Vertex)) (e f : P.Dart) : Prop :=
  if s(e.source, e.target) ∈ A ∪ P.wiredEdges then f = e.reverse P else f = e

def IsContourSuccessor (A : Finset (Sym2 P.Vertex)) (e f : P.Dart) : Prop :=
  if P.wiredForward then ∃ h, P.IsNextDart true e h ∧ P.IsSwitched A h f
  else ∃ h, P.IsSwitched A e h ∧ P.IsNextDart false h f

 
def IsInitialDart (A : Finset (Sym2 P.Vertex)) (e : P.Dart) : Prop :=
  if P.wiredForward then ∃ h : P.Dart,
    h.source = P.aVertex ∧
    h.target = P.boundaryVertex (cyclePrev P.boundary_pos P.first) ∧
    P.IsSwitched A h e
  else e.source = P.aVertex ∧
    e.target = P.boundaryVertex (cycleNext P.boundary_pos P.first)

def IsFinalDart (A : Finset (Sym2 P.Vertex)) (e : P.Dart) : Prop :=
  if P.wiredForward then e.source = P.bVertex ∧
    e.target = P.boundaryVertex (cycleNext P.boundary_pos P.secondMark)
  else ∃ h : P.Dart,
    h.source = P.bVertex ∧
    h.target = P.boundaryVertex (cyclePrev P.boundary_pos P.secondMark) ∧
    P.IsSwitched A h e

 

def IsInterface (A : Finset (Sym2 P.Vertex)) (η : Curve) : Prop :=
  ∃ walk : List P.Dart,
    walk ≠ [] ∧ walk.Nodup ∧
    (∃ e, walk.head? = some e ∧ P.IsInitialDart A e) ∧
    (∃ e, walk.getLast? = some e ∧ P.IsFinalDart A e) ∧
    walk.IsChain (P.IsContourSuccessor A) ∧
    ∀ t, η t = interpolate
      (P.position P.aVertex :: walk.map P.dartPosition ++ [P.position P.bVertex]) t

end LatticePolygon

 

structure MarkedApproximation (D : SmoothMarkedJordan) where
  mesh : ℕ → ℝ
  mesh_pos : ∀ n, 0 < mesh n
  mesh_zero : Tendsto mesh atTop (𝓝 0)
  polygon : ∀ n, LatticePolygon (mesh n)
  boundaryReparam : ℕ → IncreasingReparam
  boundary_uniform :
    Tendsto (fun n => sSup (Set.range fun t : Time =>
      dist ((polygon n).boundaryCurve ((boundaryReparam n).homeomorph t))
        (D.boundary t))) atTop (𝓝 0)
  marked_uniform :
    Tendsto (fun n =>
      (((boundaryReparam n).homeomorph.symm
        ⟨((polygon n).secondMark.val : ℝ) / (polygon n).nBoundary,
          by
            have hn : (0 : ℝ) < (polygon n).nBoundary := by
              exact_mod_cast (polygon n).boundary_pos
            constructor
            · exact div_nonneg (Nat.cast_nonneg _) hn.le
            · exact (div_le_one hn).2 (by exact_mod_cast (polygon n).secondMark.isLt.le)⟩ : Time) : ℝ))
      atTop (𝓝 D.secondMark)
  interface : ∀ n, Finset (Sym2 (polygon n).Vertex) → Curve
  interface_drawing : ∀ n A, A ⊆ (polygon n).edges →
    (polygon n).IsInterface A (interface n A)

 
def interfaceExpectation {D : SmoothMarkedJordan} (M : MarkedApproximation D)
    (q : ℝ) (n : ℕ) (f : Curve → ℝ) : ℝ :=
  (∑ A ∈ (M.polygon n).edges.powerset,
    (M.polygon n).configurationWeight q A * f (M.interface n A)) /
      (M.polygon n).partitionFunction q

 
def kappa (q : ℝ) : ℝ := 4 * Real.pi / Real.arccos (-Real.sqrt q / 2)

def upperHalfPlane : Set ℂ := {z | 0 < z.im}

def pastTrace (γ : ℝ≥0 → ℂ) (t : ℝ≥0) : Set ℂ := γ '' Set.Iic t

 
def loewnerDomain (γ : ℝ≥0 → ℂ) (t : ℝ≥0) : Set ℂ :=
  {z ∈ upperHalfPlane \ pastTrace γ t |
    ¬ Bornology.IsBounded
      (connectedComponentIn (upperHalfPlane \ pastTrace γ t) z)}

 
def inverseOn (g : ℂ → ℂ) (U : Set ℂ) (w : ℂ) : ℂ := by
  classical
  exact if h : ∃ z ∈ U, g z = w then h.choose else 0

 

def IsLoewnerTrace (W : ℝ≥0 → ℝ) (γ : ℝ≥0 → ℂ)
    (g : ℝ≥0 → ℂ → ℂ) : Prop :=
  Continuous γ ∧ γ 0 = 0 ∧
  (∀ t, 0 ≤ (γ t).im) ∧
  (∀ R : ℝ, ∃ T : ℝ≥0, ∀ t ≥ T, R ≤ ‖γ t‖) ∧
  (∀ z ∈ upperHalfPlane, g 0 z = z) ∧
  (∀ t, DifferentiableOn ℂ (g t) (loewnerDomain γ t)) ∧
  (∀ t, Set.BijOn (g t) (loewnerDomain γ t) upperHalfPlane) ∧
  (∀ t,
    Tendsto (fun z => g t z - z)
      (Filter.cocompact ℂ ⊓ Filter.principal (loewnerDomain γ t)) (𝓝 0)) ∧
  (∀ t,
    Tendsto (fun z => z * (g t z - z))
      (Filter.cocompact ℂ ⊓ Filter.principal (loewnerDomain γ t)) (𝓝 (2 * (t : ℝ) : ℂ))) ∧
  (∀ t z, z ∈ loewnerDomain γ t →
    HasDerivWithinAt (fun s : ℝ => g (Real.toNNReal s) z)
      (2 / (g t z - (W t : ℂ))) (Set.Ici 0) (t : ℝ)) ∧
  (∀ t,
    Tendsto (fun y : ℝ => inverseOn (g t) (loewnerDomain γ t)
      ((W t : ℂ) + Complex.I * y)) (𝓝[>] 0) (𝓝 (γ t)))

def cayley (z : ℂ) : ℂ := (z - Complex.I) / (z + Complex.I)

 

def IsDomainCoordinate (D : SmoothMarkedJordan) (φ : ℂ → ℂ) : Prop :=
  DifferentiableOn ℂ φ (Metric.ball 0 1) ∧
  Set.BijOn φ (Metric.ball 0 1) D.domain ∧
  ContinuousOn φ (Metric.closedBall 0 1) ∧
  Set.BijOn φ (Metric.closedBall 0 1) (closure D.domain) ∧
  φ (-1) = D.a ∧ φ 1 = D.b

 
abbrev BrownianSample := C(ℝ≥0, ℝ)

instance : MeasurableSpace BrownianSample := borel BrownianSample
instance : BorelSpace BrownianSample := ⟨rfl⟩

 

def IsChordalSLE (κ : ℝ) (D : SmoothMarkedJordan) (μ : Measure Curve) : Prop :=
  IsProbabilityMeasure μ ∧
  ∃ (P : Measure BrownianSample) (φ : ℂ → ℂ)
      (γ : BrownianSample → ℝ≥0 → ℂ)
      (g : BrownianSample → ℝ≥0 → ℂ → ℂ)
      (η : BrownianSample → Curve),
    IsProbabilityMeasure P ∧
    IsBrownianReal (fun t ω => ω t) P ∧
    IsDomainCoordinate D φ ∧
    Measurable η ∧ μ = Measure.map η P ∧
    (∀ᵐ ω ∂P,
      IsLoewnerTrace (fun t => Real.sqrt κ * ω t) (γ ω) (g ω) ∧
      (∀ u : Time, (u : ℝ) < 1 →
        η ω u = φ (cayley (γ ω (Real.toNNReal ((u : ℝ) / (1 - (u : ℝ))))))) ∧
      η ω ⟨1, by simp⟩ = D.b)

end BelowOne

 

noncomputable section
open SimpleGraph Function
namespace BelowOne.FKComponents

variable {V : Type*} {G : SimpleGraph V}

lemma eq_of_walk {β : Sort*} {f : V → β}
    (hf : ∀ ⦃v w⦄, G.Adj v w → f v = f w) {v w : V} (p : G.Walk v w) :
    f v = f w := by
  induction p with
  | nil => rfl
  | cons h p ih => exact (hf h).trans ih

lemma component_card_sup_edge_of_reachable [Finite V] {a b : V}
    (hab : G.Reachable a b) :
    Nat.card (G ⊔ edge a b).ConnectedComponent = Nat.card G.ConnectedComponent := by
  classical
  let H := G ⊔ edge a b
  have hconst : ∀ ⦃v w⦄, H.Adj v w → G.connectedComponentMk v = G.connectedComponentMk w := by
    intro v w hvw
    rcases hvw with hG | he
    · exact ConnectedComponent.sound hG.reachable
    · rcases (edge_adj _ _ _ _).mp he with ⟨⟨rfl,rfl⟩ | ⟨rfl,rfl⟩,_⟩
      · exact ConnectedComponent.sound hab
      · exact ConnectedComponent.sound hab.symm
  let f : H.ConnectedComponent → G.ConnectedComponent :=
    ConnectedComponent.lift G.connectedComponentMk (fun _ _ p _ => eq_of_walk hconst p)
  let g : G.ConnectedComponent → H.ConnectedComponent :=
    ConnectedComponent.map (Hom.ofLE le_sup_left)
  let e : H.ConnectedComponent ≃ G.ConnectedComponent :=
    { toFun := f
      invFun := g
      left_inv := fun c => c.ind fun v => rfl
      right_inv := fun c => c.ind fun v => rfl }
  exact Nat.card_congr e

lemma component_card_sup_edge_of_not_reachable [Finite V] {a b : V}
    (hab : ¬ G.Reachable a b) :
    Nat.card (G ⊔ edge a b).ConnectedComponent + 1 = Nat.card G.ConnectedComponent := by
  classical
  let H := G ⊔ edge a b
  let A := G.connectedComponentMk a
  let B := G.connectedComponentMk b
  have hAB : A ≠ B := fun h => hab (ConnectedComponent.exact h)
  let collapse : G.ConnectedComponent → {c : G.ConnectedComponent // c ≠ B} :=
    fun c => ⟨if c = B then A else c, by split_ifs with h; exact hAB; exact h⟩
  have hcollapse : collapse A = collapse B := by
    apply Subtype.ext
    simp [collapse,hAB]
  have hconst : ∀ ⦃v w⦄, H.Adj v w →
      collapse (G.connectedComponentMk v) = collapse (G.connectedComponentMk w) := by
    intro v w hvw
    rcases hvw with hG | he
    · exact congrArg collapse (ConnectedComponent.sound hG.reachable)
    · rcases (edge_adj _ _ _ _).mp he with ⟨⟨rfl,rfl⟩ | ⟨rfl,rfl⟩,_⟩
      · exact hcollapse
      · exact hcollapse.symm
  let f : H.ConnectedComponent → {c : G.ConnectedComponent // c ≠ B} :=
    ConnectedComponent.lift (fun v => collapse (G.connectedComponentMk v))
      (fun _ _ p _ => eq_of_walk hconst p)
  let g : G.ConnectedComponent → H.ConnectedComponent :=
    ConnectedComponent.map (Hom.ofLE le_sup_left)
  have hgab : g A = g B := by
    apply ConnectedComponent.sound
    apply SimpleGraph.Adj.reachable
    exact Or.inr ((edge_adj _ _ _ _).mpr ⟨Or.inl ⟨rfl,rfl⟩,fun h => hAB (congrArg G.connectedComponentMk h)⟩)
  have hfg (c : G.ConnectedComponent) : f (g c) = collapse c := by
    exact c.ind fun v => rfl
  let e : H.ConnectedComponent ≃ {c : G.ConnectedComponent // c ≠ B} :=
    { toFun := f
      invFun := fun c => g c.val
      left_inv := by
        intro c
        induction c using ConnectedComponent.ind with
        | h v =>
          change g (if G.connectedComponentMk v = B then A else G.connectedComponentMk v) = g (G.connectedComponentMk v)
          split_ifs with hv
          · rw [hv]; exact hgab
          · rfl
      right_inv := by
        intro c
        rw [hfg]
        apply Subtype.ext
        simp [collapse,c.property] }
  have hcard := Nat.card_congr e
  let := Fintype.ofFinite G.ConnectedComponent
  let := Fintype.ofFinite H.ConnectedComponent
  simp only [Nat.card_eq_fintype_card] at hcard ⊢
  rw [hcard]
  have hsub := Fintype.card_subtype_compl (fun c : G.ConnectedComponent => c = B)
  have hpos : 0 < Fintype.card G.ConnectedComponent := Fintype.card_pos_iff.mpr ⟨A⟩
  simp only [Fintype.card_unique] at hsub
  change Fintype.card {c : G.ConnectedComponent // c ≠ B} =
    Fintype.card G.ConnectedComponent - 1 at hsub
  omega

lemma component_card_sup_edge [Finite V] (a b : V) :
    Nat.card (G ⊔ edge a b).ConnectedComponent = Nat.card G.ConnectedComponent ∨
      Nat.card (G ⊔ edge a b).ConnectedComponent + 1 = Nat.card G.ConnectedComponent := by
  by_cases h : G.Reachable a b
  · exact Or.inl (component_card_sup_edge_of_reachable h)
  · exact Or.inr (component_card_sup_edge_of_not_reachable h)

end BelowOne.FKComponents

namespace BelowOne.FKFinite
open Finset SimpleGraph
attribute [local instance] Classical.propDecidable
variable {V : Type*}

 
def configuration (G : SimpleGraph V) (A : Finset (Sym2 V)) : SimpleGraph V :=
  G ⊔ fromEdgeSet (A : Set (Sym2 V))

def count (G : SimpleGraph V) (A : Finset (Sym2 V)) : ℕ :=
  Nat.card (configuration G A).ConnectedComponent

def weight (G : SimpleGraph V) (q v : ℝ) (A : Finset (Sym2 V)) : ℝ :=
  v ^ A.card * q ^ count G A

def partition (G : SimpleGraph V) (q v : ℝ) (E : Finset (Sym2 V)) : ℝ :=
  ∑ A ∈ E.powerset, weight G q v A

lemma weight_pos (G : SimpleGraph V) {q v : ℝ} (hq : 0 < q) (hv : 0 < v)
    (A : Finset (Sym2 V)) : 0 < weight G q v A := by
  exact mul_pos (pow_pos hv _) (pow_pos hq _)

lemma partition_pos (G : SimpleGraph V) {q v : ℝ} (hq : 0 < q) (hv : 0 < v)
    (E : Finset (Sym2 V)) : 0 < partition G q v E := by
  apply sum_pos
  · intro A hA
    exact weight_pos G hq hv A
  · exact ⟨∅,by simp⟩

lemma configuration_insert (G : SimpleGraph V) (A : Finset (Sym2 V)) (a b : V) :
    configuration G (insert s(a,b) A) = configuration G A ⊔ edge a b := by
  classical
  ext u w
  simp only [configuration, sup_adj, fromEdgeSet_adj, mem_coe, Finset.mem_insert,
    edge, Set.mem_singleton_iff]
  tauto

lemma configuration_union (G : SimpleGraph V) (A F : Finset (Sym2 V)) :
    configuration G (A ∪ F) = configuration (configuration G F) A := by
  classical
  ext u w
  simp only [configuration,sup_adj,fromEdgeSet_adj,mem_coe,Finset.mem_union]
  tauto

lemma weight_union (G : SimpleGraph V) (q v : ℝ) {A F : Finset (Sym2 V)}
    (hAF : Disjoint A F) :
    weight G q v (A ∪ F) = v ^ F.card * weight (configuration G F) q v A := by
  classical
  simp only [weight,count,configuration_union,card_union_of_disjoint hAF,pow_add]
  ring

 

theorem domain_markov (G : SimpleGraph V) {q v : ℝ} (hv : 0 < v)
    {S F : Finset (Sym2 V)} (hSF : Disjoint S F) (f : Finset (Sym2 V) → ℝ) :
    (∑ A ∈ S.powerset, weight G q v (A ∪ F) * f A) /
        (∑ A ∈ S.powerset, weight G q v (A ∪ F)) =
      (∑ A ∈ S.powerset, weight (configuration G F) q v A * f A) /
        partition (configuration G F) q v S := by
  classical
  have hx : ∀ A ∈ S.powerset, weight G q v (A ∪ F) =
      v ^ F.card * weight (configuration G F) q v A := by
    intro A hA
    exact weight_union G q v (hSF.mono_left (mem_powerset.mp hA))
  simp_rw [sum_congr rfl hx]
  have hn : (∑ A ∈ S.powerset, weight G q v (A ∪ F) * f A) =
      v ^ F.card * (∑ A ∈ S.powerset, weight (configuration G F) q v A * f A) := by
    rw [mul_sum]
    apply sum_congr rfl
    intro A hA
    rw [hx A hA]
    ring
  rw [hn,← mul_sum]
  change _ / (v ^ F.card * partition (configuration G F) q v S) = _
  exact mul_div_mul_left _ _ (ne_of_gt (pow_pos hv _))

variable [Finite V]

lemma weight_insert_connected (G : SimpleGraph V) (q v : ℝ) (A : Finset (Sym2 V))
    {a b : V} (he : s(a,b) ∉ A) (hab : (configuration G A).Reachable a b) :
    weight G q v (insert s(a,b) A) = v * weight G q v A := by
  classical
  have hc := FKComponents.component_card_sup_edge_of_reachable hab
  simp only [weight,count,configuration_insert,card_insert_of_notMem he,pow_succ]
  rw [hc]
  ring

lemma weight_insert_separated (G : SimpleGraph V) (q v : ℝ) (A : Finset (Sym2 V))
    {a b : V} (he : s(a,b) ∉ A) (hab : ¬(configuration G A).Reachable a b) :
    q * weight G q v (insert s(a,b) A) = v * weight G q v A := by
  classical
  have hc := FKComponents.component_card_sup_edge_of_not_reachable hab
  unfold weight count
  rw [configuration_insert,card_insert_of_notMem he,← hc,pow_succ,pow_succ]
  ring

 

theorem edge_conditional_probability (G : SimpleGraph V) {q v : ℝ}
    (hq : 0 < q) (hv : 0 < v) (A : Finset (Sym2 V)) {a b : V}
    (he : s(a,b) ∉ A) :
    weight G q v (insert s(a,b) A) /
        (weight G q v A + weight G q v (insert s(a,b) A)) =
      if (configuration G A).Reachable a b then v/(1+v) else v/(q+v) := by
  classical
  have hw := weight_pos G hq hv A
  split_ifs with hab
  · rw [weight_insert_connected G q v A he hab]
    apply (div_eq_div_iff (ne_of_gt (add_pos hw (mul_pos hv hw)))
      (ne_of_gt (add_pos zero_lt_one hv))).mpr
    ring
  · have hc := weight_insert_separated G q v A he hab
    have hi := weight_pos G hq hv (insert s(a,b) A)
    apply (div_eq_div_iff (ne_of_gt (add_pos hw hi)) (ne_of_gt (add_pos hq hv))).mpr
    nlinarith

end BelowOne.FKFinite

namespace BelowOne.LatticePolygon
open Finset SimpleGraph
variable {δ : ℝ} (P : LatticePolygon δ)

def wireGraph : SimpleGraph P.Vertex :=
  fromRel fun v w => v ∈ P.wiredVertices ∧ w ∈ P.wiredVertices

lemma wiredConfiguration_eq (A : Finset (Sym2 P.Vertex)) :
    P.wiredConfiguration A = FKFinite.configuration P.wireGraph A := by
  classical
  ext v w
  simp only [wiredConfiguration,wireGraph,FKFinite.configuration,fromRel_adj,
    sup_adj,fromEdgeSet_adj,mem_coe,Sym2.eq_swap]
  tauto

lemma componentCount_eq (A : Finset (Sym2 P.Vertex)) :
    P.componentCount A = FKFinite.count P.wireGraph A := by
  unfold componentCount FKFinite.count
  rw [P.wiredConfiguration_eq]

lemma configurationWeight_odds {q : ℝ} (hq : 0 < q) (A : Finset (Sym2 P.Vertex))
    (hA : A ⊆ P.edges) :
    P.configurationWeight q A =
      (1 / (1 + Real.sqrt q)) ^ P.edges.card *
        FKFinite.weight P.wireGraph q (Real.sqrt q) A := by
  have hs : 0 < Real.sqrt q := Real.sqrt_pos.mpr hq
  have hd : 1 + Real.sqrt q ≠ 0 := ne_of_gt (by linarith)
  have hp : 1 - Real.sqrt q / (1 + Real.sqrt q) = 1/(1+Real.sqrt q) := by
    field_simp
    ring
  have hc : A.card + (P.edges.card - A.card) = P.edges.card :=
    Nat.add_sub_of_le (card_le_card hA)
  unfold configurationWeight FKFinite.weight
  dsimp only
  rw [P.componentCount_eq,hp]
  rw [div_eq_mul_one_div, mul_pow, ← hc, pow_add, Nat.add_sub_cancel_left]
  ring

lemma partitionFunction_odds {q : ℝ} (hq : 0 < q) :
    P.partitionFunction q = (1 / (1 + Real.sqrt q)) ^ P.edges.card *
      FKFinite.partition P.wireGraph q (Real.sqrt q) P.edges := by
  unfold partitionFunction FKFinite.partition
  rw [mul_sum]
  apply sum_congr rfl
  intro A hA
  exact P.configurationWeight_odds hq A (mem_powerset.mp hA)

 
theorem normalized_configurationWeight_odds {q : ℝ} (hq : 0 < q)
    (A : Finset (Sym2 P.Vertex)) (hA : A ⊆ P.edges) :
    P.configurationWeight q A / P.partitionFunction q =
      FKFinite.weight P.wireGraph q (Real.sqrt q) A /
        FKFinite.partition P.wireGraph q (Real.sqrt q) P.edges := by
  rw [P.configurationWeight_odds hq A hA,P.partitionFunction_odds hq]
  exact mul_div_mul_left _ _ (ne_of_gt (pow_pos (one_div_pos.mpr
    (by have := Real.sqrt_nonneg q; linarith)) _))

end BelowOne.LatticePolygon

end
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
attribute [local instance] Classical.propDecidable
namespace BelowOne.FKPressure
variable {V I : Type*} [Finite V]

 
def graph (G : SimpleGraph V) (ends : I → V × V) (A : Finset I) : SimpleGraph V :=
  G ⊔ A.sup (fun e => edge (ends e).1 (ends e).2)

def components (G : SimpleGraph V) (ends : I → V × V) (A : Finset I) : ℕ :=
  Nat.card (graph G ends A).ConnectedComponent

 
def partition (G : SimpleGraph V) (ends : I → V × V) (s : ℝ) (w : I → ℝ)
    (E : Finset I) : ℝ :=
  ∑ A ∈ E.powerset, (∏ e ∈ A, w e) * s ^ components G ends A

omit [Finite V] in
@[simp] lemma graph_empty (G : SimpleGraph V) (ends : I → V × V) :
    graph G ends ∅ = G := by simp [graph]

omit [Finite V] in
lemma graph_mono (G : SimpleGraph V) (ends : I → V × V) {A B : Finset I}
    (h : A ⊆ B) : graph G ends A ≤ graph G ends B := by
  exact sup_le_sup_left (Finset.sup_mono h) _

omit [Finite V] in
lemma graph_insert (G : SimpleGraph V) (ends : I → V × V) (e : I) (A : Finset I) :
    graph G ends (insert e A) = graph G ends A ⊔ edge (ends e).1 (ends e).2 := by
  classical
  simp [graph, sup_left_comm, sup_comm]

omit [Finite V] in
lemma graph_contracted (G : SimpleGraph V) (ends : I → V × V) (e : I) (A : Finset I) :
    graph (G ⊔ edge (ends e).1 (ends e).2) ends A =
      graph G ends A ⊔ edge (ends e).1 (ends e).2 := by
  simp [graph, sup_assoc, sup_left_comm, sup_comm]

omit [Finite V] in
@[simp] lemma partition_empty (G : SimpleGraph V) (ends : I → V × V)
    (s : ℝ) (w : I → ℝ) : partition G ends s w ∅ = s ^ Nat.card G.ConnectedComponent := by
  simp [partition, components]

 
omit [Finite V] in
lemma deletion_contraction (G : SimpleGraph V) (ends : I → V × V)
    (s : ℝ) (w : I → ℝ) {e : I} {E : Finset I} (he : e ∉ E) :
    partition G ends s w (insert e E) = partition G ends s w E +
      w e * partition (G ⊔ edge (ends e).1 (ends e).2) ends s w E := by
  classical
  unfold partition
  rw [sum_powerset_insert he, mul_sum]
  congr 1
  apply sum_congr rfl
  intro A hA
  have heA : e ∉ A := fun h => he (mem_powerset.mp hA h)
  rw [prod_insert heA]
  simp only [components,graph_insert,graph_contracted]
  ring

 
lemma separating_edge_count (G : SimpleGraph V) (ends : I → V × V)
    {e : I} {E A : Finset I} (h : ¬(graph G ends E).Reachable (ends e).1 (ends e).2)
    (hA : A ⊆ E) :
    components (G ⊔ edge (ends e).1 (ends e).2) ends A + 1 = components G ends A := by
  unfold components
  rw [graph_contracted]
  apply FKComponents.component_card_sup_edge_of_not_reachable
  exact fun hr => h (hr.mono (graph_mono G ends hA))

 
lemma separating_edge_partition (G : SimpleGraph V) (ends : I → V × V)
    (s : ℝ) (w : I → ℝ) {e : I} {E : Finset I}
    (h : ¬(graph G ends E).Reachable (ends e).1 (ends e).2) :
    partition G ends s w E =
      s * partition (G ⊔ edge (ends e).1 (ends e).2) ends s w E := by
  unfold partition
  rw [mul_sum]
  apply sum_congr rfl
  intro A hA
  rw [← separating_edge_count G ends h (mem_powerset.mp hA),pow_succ]
  ring

 

theorem negative_partition (G : SimpleGraph V) (ends : I → V × V)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (w : I → ℝ) (E : Finset I)
    (hw : ∀ e ∈ E, 1 ≤ w e) (hc : components G ends E = 1) :
    partition G ends (-u) w E < 0 := by
  classical
  induction E using Finset.induction_on generalizing G with
  | empty =>
    simp only [components,graph_empty] at hc
    simpa [hc] using neg_neg_of_pos hu
  | @insert e E he ih =>
    have hwE : ∀ f ∈ E, 1 ≤ w f := fun f hf => hw f (mem_insert_of_mem hf)
    have hwe : 1 ≤ w e := hw e (mem_insert_self _ _)
    let G' := G ⊔ edge (ends e).1 (ends e).2
    have hc' : components G' ends E = 1 := by
      simpa only [components,G',graph_contracted,graph_insert] using hc
    have hp' : partition G' ends (-u) w E < 0 := ih G' hwE hc'
    rw [deletion_contraction G ends (-u) w he]
    by_cases hE : components G ends E = 1
    · exact add_neg (ih G hwE hE) (mul_neg_of_pos_of_neg (by linarith) hp')
    · have hsep : ¬(graph G ends E).Reachable (ends e).1 (ends e).2 := by
        intro hr
        have hcards := FKComponents.component_card_sup_edge_of_reachable hr
        apply hE
        apply hcards.symm.trans
        simpa only [components,graph_insert] using hc
      rw [separating_edge_partition G ends (-u) w hsep]
      change (-u) * partition G' ends (-u) w E + w e * partition G' ends (-u) w E < 0
      rw [← add_mul]
      exact mul_neg_of_pos_of_neg (by linarith) hp'

 

theorem signed_partition_pos (G : SimpleGraph V) (ends : I → V × V)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (w : I → ℝ) (E : Finset I)
    (hw : ∀ e ∈ E, 1 ≤ w e) :
    0 < (-1 : ℝ) ^ components G ends E * partition G ends (-u) w E := by
  classical
  induction E using Finset.induction_on generalizing G with
  | empty =>
    simp only [components, graph_empty, partition_empty, ← mul_pow, neg_mul_neg,
      one_mul]
    exact pow_pos hu _
  | @insert e E he ih =>
    have hwE : ∀ f ∈ E, 1 ≤ w f := fun f hf => hw f (mem_insert_of_mem hf)
    have hwe : 1 ≤ w e := hw e (mem_insert_self _ _)
    let G' := G ⊔ edge (ends e).1 (ends e).2
    have hc' : components G ends (insert e E) = components G' ends E := by
      simp only [components,G',graph_contracted,graph_insert]
    have hp' := ih G' hwE
    rw [hc', deletion_contraction G ends (-u) w he, mul_add]
    by_cases hr : (graph G ends E).Reachable (ends e).1 (ends e).2
    · have hc : components G' ends E = components G ends E := by
        simp only [components,G',graph_contracted]
        exact FKComponents.component_card_sup_edge_of_reachable hr
      have hp := ih G hwE
      rw [← hc] at hp
      have hpw := mul_pos (lt_of_lt_of_le zero_lt_one hwe) hp'
      nlinarith
    · rw [separating_edge_partition G ends (-u) w hr]
      change 0 < (-1 : ℝ) ^ components G' ends E *
        ((-u) * partition G' ends (-u) w E) +
        (-1 : ℝ) ^ components G' ends E * (w e * partition G' ends (-u) w E)
      have hpw := mul_pos (show 0 < w e - u by linarith) hp'
      nlinarith

 
def hardSum (G : SimpleGraph V) (ends : I → V × V) (E : Finset I) : ℝ :=
  ∑ A ∈ E.powerset, if components G ends A = 1 then (-1 : ℝ)^A.card else 0

lemma components_contracted_of_reachable (G : SimpleGraph V) (ends : I → V × V)
    {e : I} (hr : G.Reachable (ends e).1 (ends e).2) (A : Finset I) :
    components (G ⊔ edge (ends e).1 (ends e).2) ends A = components G ends A := by
  unfold components
  rw [graph_contracted]
  exact FKComponents.component_card_sup_edge_of_reachable (hr.mono le_sup_left)

omit [Finite V] in
lemma hardSum_deletion_contraction (G : SimpleGraph V) (ends : I → V × V)
    {e : I} {E : Finset I} (he : e ∉ E) :
    hardSum G ends (insert e E) = hardSum G ends E -
      hardSum (G ⊔ edge (ends e).1 (ends e).2) ends E := by
  unfold hardSum
  rw [sum_powerset_insert he,sub_eq_add_neg,← sum_neg_distrib]
  congr 1
  apply sum_congr rfl
  intro A hA
  have heA : e ∉ A := fun h => he (mem_powerset.mp hA h)
  simp only [components,graph_insert,graph_contracted,card_insert_of_notMem heA,pow_succ]
  split_ifs <;> ring

 
lemma hardSum_eq_zero_of_reachable (G : SimpleGraph V) (ends : I → V × V)
    {e : I} {E : Finset I} (he : e ∉ E) (hr : G.Reachable (ends e).1 (ends e).2) :
    hardSum G ends (insert e E) = 0 := by
  rw [hardSum_deletion_contraction G ends he]
  have h : hardSum (G ⊔ edge (ends e).1 (ends e).2) ends E = hardSum G ends E := by
    unfold hardSum
    congr 1
    funext A
    rw [components_contracted_of_reachable G ends hr]
  rw [h,sub_self]

 

theorem hardSum_sign (G : SimpleGraph V) (ends : I → V × V) (E : Finset I) :
    (-1 : ℝ) ^ Nat.card G.ConnectedComponent * hardSum G ends E ≤ 0 := by
  induction E using Finset.induction_on generalizing G with
  | empty =>
    simp only [hardSum,powerset_empty,sum_singleton,components,graph_empty,card_empty,pow_zero]
    split_ifs with h
    · simp [h]
    · simp
  | @insert e E he ih =>
    by_cases hr : G.Reachable (ends e).1 (ends e).2
    · rw [hardSum_eq_zero_of_reachable G ends he hr,mul_zero]
    · have hc := FKComponents.component_card_sup_edge_of_not_reachable hr
      have h0 := ih G
      have h1 := ih (G ⊔ edge (ends e).1 (ends e).2)
      rw [hardSum_deletion_contraction G ends he,mul_sub]
      have hsign : (-1 : ℝ) ^ Nat.card G.ConnectedComponent =
          - (-1 : ℝ) ^ Nat.card (G ⊔ edge (ends e).1 (ends e).2).ConnectedComponent := by
        rw [← hc,pow_succ]
        ring
      rw [hsign] at h0 ⊢
      nlinarith

 

lemma union_constrained_sum (v : I → ℝ) {A D : Finset I} (hA : A ⊆ D) :
    (∑ B ∈ D.powerset with A ∪ B = D, ∏ e ∈ B, v e) =
      (∏ e ∈ D \ A, v e) * ∏ e ∈ A, (1 + v e) := by
  classical
  rw [prod_one_add, mul_sum]
  apply sum_bij' (fun B _ => B ∩ A) (fun C _ => (D \ A) ∪ C)
  · intro B hB
    exact mem_powerset.mpr inter_subset_right
  · intro C hC
    have hc := mem_powerset.mp hC
    apply mem_filter.mpr
    constructor
    · exact mem_powerset.mpr (union_subset sdiff_subset (hc.trans hA))
    · ext e
      simp only [mem_union, mem_sdiff]
      constructor
      · rintro (he | ⟨he, _⟩ | he)
        · exact hA he
        · exact he
        · exact hA (hc he)
      · intro he
        by_cases ha : e ∈ A
        · exact Or.inl ha
        · exact Or.inr (Or.inl ⟨he, ha⟩)
  · intro B hB
    obtain ⟨hb, hab⟩ := mem_filter.mp hB
    have hbD := mem_powerset.mp hb
    ext e
    simp only [mem_union, mem_sdiff, mem_inter]
    constructor
    · rintro (⟨hd, ha⟩ | ⟨hb, _⟩)
      · have he : e ∈ A ∪ B := by rw [hab]; exact hd
        exact (mem_union.mp he).resolve_left ha
      · exact hb
    · intro hb
      by_cases ha : e ∈ A
      · exact Or.inr ⟨hb, ha⟩
      · exact Or.inl ⟨hbD hb, ha⟩
  · intro C hC
    have hc := mem_powerset.mp hC
    ext e
    simp only [mem_inter, mem_union, mem_sdiff]
    constructor
    · rintro ⟨(⟨_, hn⟩ | he), ha⟩
      · exact (hn ha).elim
      · exact he
    · intro he
      exact ⟨Or.inr he, hc he⟩
  · intro B hB
    obtain ⟨hb, hab⟩ := mem_filter.mp hB
    have hbD := mem_powerset.mp hb
    have heq : B = (D \ A) ∪ (B ∩ A) := by
      ext e
      simp only [mem_union,mem_sdiff,mem_inter]
      constructor
      · intro he
        by_cases ha : e ∈ A
        · exact Or.inr ⟨he,ha⟩
        · exact Or.inl ⟨hbD he,ha⟩
      · rintro (⟨hd,hn⟩ | ⟨he,_⟩)
        · have he : e ∈ A ∪ B := by rw [hab]; exact hd
          exact (mem_union.mp he).resolve_left hn
        · exact he
    conv_lhs => rw [heq,prod_union (show Disjoint (D \ A) (B ∩ A) from
      disjoint_left.mpr fun e he he' => (mem_sdiff.mp he).2 (mem_inter.mp he').2)]

omit [Finite V] in
lemma fixed_union (G : SimpleGraph V) (ends : I → V × V) (v : I → ℝ)
    (s : ℝ) (D : Finset I) :
    (∑ A ∈ D.powerset, (∏ e ∈ A, v e) * s ^ components G ends A *
      ∑ B ∈ D.powerset with A ∪ B = D, ∏ e ∈ B, v e) =
      (∏ e ∈ D, v e) * partition G ends s (fun e => 1 + v e) D := by
  unfold partition
  rw [mul_sum]
  apply sum_congr rfl
  intro A hA
  have ha := mem_powerset.mp hA
  rw [union_constrained_sum v ha]
  have hp := prod_sdiff (f := v) ha
  calc
    _ = ((∏ e ∈ D \ A, v e) * ∏ e ∈ A, v e) *
      ((∏ e ∈ A, (1 + v e)) * s ^ components G ends A) := by ring
    _ = _ := by rw [hp]

 

theorem fixed_union_hard_nonpos {J : Type*} (G : SimpleGraph V)
    (ends : I → V × V) (hardEnds : J → V × V) (v : I → ℝ)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (D : Finset I) (H : Finset J)
    (hv : ∀ e ∈ D, 0 ≤ v e) :
    (∏ e ∈ D, v e) * partition G ends (-u) (fun e => 1 + v e) D *
      hardSum (graph G ends D) hardEnds H ≤ 0 := by
  have hp := signed_partition_pos G ends hu hu1 (fun e => 1 + v e) D
    (fun e he => by linarith [hv e he])
  have hh := hardSum_sign (graph G ends D) hardEnds H
  change (-1 : ℝ) ^ components G ends D * hardSum (graph G ends D) hardEnds H ≤ 0 at hh
  have hs : ((-1 : ℝ) ^ components G ends D) ^ 2 = 1 := by
    rw [← pow_mul, Nat.mul_comm, pow_mul]
    simp
  have hph := mul_nonpos_of_nonneg_of_nonpos (le_of_lt hp) hh
  have hprod : partition G ends (-u) (fun e => 1 + v e) D *
      hardSum (graph G ends D) hardEnds H ≤ 0 := by
    nlinarith [sq_nonneg ((-1 : ℝ) ^ components G ends D)]
  exact (by simpa only [mul_assoc] using
    mul_nonpos_of_nonneg_of_nonpos (prod_nonneg hv) hprod)

end BelowOne.FKPressure
end
end

 
section

 
noncomputable section
open Finset SimpleGraph
attribute [local instance] Classical.propDecidable
namespace BelowOne.FKPressure
variable {V I J : Type*} [Finite V]

lemma regroup_union (E : Finset I) (f : Finset I → Finset I → ℝ) :
    (∑ A ∈ E.powerset, ∑ B ∈ E.powerset, f A B) =
      ∑ D ∈ E.powerset, ∑ A ∈ D.powerset,
        ∑ B ∈ D.powerset with A ∪ B = D, f A B := by
  classical
  rw [← sum_product']
  have hmap : ∀ p ∈ E.powerset ×ˢ E.powerset, p.1 ∪ p.2 ∈ E.powerset := by
    intro p hp
    obtain ⟨ha,hb⟩ := mem_product.mp hp
    exact mem_powerset.mpr (union_subset (mem_powerset.mp ha) (mem_powerset.mp hb))
  rw [← sum_fiberwise_of_maps_to hmap]
  apply sum_congr rfl
  intro D hD
  have hd := mem_powerset.mp hD
  apply sum_finset_product'
  intro p
  simp only [mem_filter,mem_product,mem_powerset]
  constructor
  · rintro ⟨⟨ha,hb⟩, hab⟩
    refine ⟨?_, ?_, hab⟩
    · rw [← hab]
      exact subset_union_left
    · rw [← hab]
      exact subset_union_right
  · rintro ⟨ha,hb, hab⟩
    exact ⟨⟨ha.trans hd, hb.trans hd⟩,hab⟩

 

def connectedSum (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) : ℝ :=
  ∑ A ∈ E.powerset, ∑ B ∈ E.powerset,
    (∏ e ∈ A, v e) * (∏ e ∈ B, v e) * s ^ components G ends A *
      hardSum (graph G ends (A ∪ B)) hardEnds H

omit [Finite V] in
lemma connectedSum_grouped (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) :
    connectedSum G ends hardEnds E H s v =
      ∑ D ∈ E.powerset, (∏ e ∈ D, v e) *
        partition G ends s (fun e => 1 + v e) D *
          hardSum (graph G ends D) hardEnds H := by
  unfold connectedSum
  rw [regroup_union]
  apply sum_congr rfl
  intro D hD
  rw [← fixed_union, sum_mul]
  apply sum_congr rfl
  intro A hA
  rw [mul_sum, sum_mul]
  apply sum_congr rfl
  intro B hB
  have hab := (mem_filter.mp hB).2
  rw [hab]
  ring

 

theorem connectedSum_nonpos (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (v : I → ℝ)
    (hv : ∀ e ∈ E, 0 ≤ v e) :
    connectedSum G ends hardEnds E H (-u) v ≤ 0 := by
  rw [connectedSum_grouped]
  apply sum_nonpos
  intro D hD
  exact fixed_union_hard_nonpos G ends hardEnds v hu hu1 D H
    (fun e he => hv e (mem_powerset.mp hD he))

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset
attribute [local instance] Classical.propDecidable
namespace BelowOne.PartitionCumulant

instance finiteSetoid (α : Type*) [Finite α] : Finite (Setoid α) :=
  Finite.of_injective (fun r : Setoid α => r.r) (fun _ _ h => Setoid.ext (by simp [h]))

instance fintypeSetoid (α : Type*) [Finite α] : Fintype (Setoid α) := Fintype.ofFinite _

variable {α : Type*}

def extendMap (r : Setoid α) (o : Option (Quotient r)) : Option α → Option (Quotient r)
  | none => o
  | some a => some (Quotient.mk r a)

def extend (r : Setoid α) (o : Option (Quotient r)) : Setoid (Option α) :=
  Setoid.ker (extendMap r o)

@[simp] lemma extend_some (r : Setoid α) (o : Option (Quotient r)) (a b : α) :
    extend r o (some a) (some b) ↔ r a b := by
  change some (Quotient.mk r a) = some (Quotient.mk r b) ↔ r a b
  rw [Option.some.injEq, Quotient.eq]

@[simp] lemma extend_none_some (r : Setoid α) (o : Option (Quotient r)) (a : α) :
    extend r o none (some a) ↔ o = some (Quotient.mk r a) := Iff.rfl

@[simp] lemma restrict_extend (r : Setoid α) (o : Option (Quotient r)) :
    (extend r o).comap some = r := by
  ext a b
  exact extend_some r o a b

lemma extend_injective (r : Setoid α) : Function.Injective (extend r) := by
  intro o p h
  have hh (a : α) : (o = some (Quotient.mk r a)) ↔ (p = some (Quotient.mk r a)) := by
    simpa only [extend_none_some] using Setoid.ext_iff.mp h none (some a)
  cases o with
  | none =>
    cases p with
    | none => rfl
    | some q =>
      obtain ⟨a,rfl⟩ := q.exists_rep
      simpa using (hh a).mpr rfl
  | some q =>
    obtain ⟨a,rfl⟩ := q.exists_rep
    exact ((hh a).mp rfl).symm

lemma extend_bijective : Function.Bijective
    (fun p : Σ r : Setoid α, Option (Quotient r) => extend p.1 p.2) := by
  constructor
  · rintro ⟨r,o⟩ ⟨s,p⟩ h
    have hrs : r = s := by
      simpa using congrArg (Setoid.comap (some : α → Option α)) h
    subst s
    have hop : o = p := extend_injective r h
    subst p
    rfl
  · intro s
    let r := s.comap (some : α → Option α)
    by_cases h : ∃ a, s none (some a)
    · obtain ⟨a,ha⟩ := h
      refine ⟨⟨r,some (Quotient.mk r a)⟩, ?_⟩
      apply Setoid.ext
      intro x y
      cases x with
      | none =>
        cases y with
        | none => simp
        | some b =>
          simp only [extend_none_some, Option.some.injEq, Quotient.eq]
          change s (some a) (some b) ↔ s none (some b)
          exact ⟨fun h => s.trans ha h, fun h => s.trans (s.symm ha) h⟩
      | some a' =>
        cases y with
        | none =>
          change some (Quotient.mk r a') = some (Quotient.mk r a) ↔ s (some a') none
          rw [Option.some.injEq, Quotient.eq]
          change s (some a') (some a) ↔ s (some a') none
          exact ⟨fun h => s.trans h (s.symm ha), fun h => s.trans h ha⟩
        | some b => exact extend_some r _ _ _
    · refine ⟨⟨r,none⟩, ?_⟩
      apply Setoid.ext
      intro x y
      cases x with
      | none =>
        cases y with
        | none => simp
        | some b =>
          rw [extend_none_some]
          exact iff_of_false (by simp) (fun hb => h ⟨b,hb⟩)
      | some a =>
        cases y with
        | none =>
          change some (Quotient.mk r a) = none ↔ s (some a) none
          exact iff_of_false (by simp) (fun ha => h ⟨a,s.symm ha⟩)
        | some b => exact extend_some r _ _ _

def extensionEquiv : (Σ r : Setoid α, Option (Quotient r)) ≃ Setoid (Option α) :=
  Equiv.ofBijective _ extend_bijective

lemma card_extend_none [Finite α] (r : Setoid α) :
    Nat.card (Quotient (extend r none)) = Nat.card (Quotient r) + 1 := by
  have hs : Function.Surjective (extendMap r none) := by
    intro x
    cases x with
    | none => exact ⟨none,rfl⟩
    | some q =>
      obtain ⟨a,rfl⟩ := q.exists_rep
      exact ⟨some a,rfl⟩
  change Nat.card (Quotient (Setoid.ker (extendMap r none))) = _
  rw [Nat.card_congr (Setoid.quotientKerEquivOfSurjective (extendMap r none) hs)]
  let := Fintype.ofFinite (Quotient r)
  simp only [Nat.card_eq_fintype_card, Fintype.card_option]

lemma card_extend_some [Finite α] (r : Setoid α) (q : Quotient r) :
    Nat.card (Quotient (extend r (some q))) = Nat.card (Quotient r) := by
  let f : Option α → Quotient r := Option.elim' q (Quotient.mk r)
  have he : extend r (some q) = Setoid.ker f := by
    apply Setoid.ext
    intro x y
    change extendMap r (some q) x = extendMap r (some q) y ↔ f x = f y
    cases x <;> cases y <;> simp [extendMap, f]
  have hs : Function.Surjective f := by
    intro p
    obtain ⟨a,rfl⟩ := p.exists_rep
    exact ⟨some a,rfl⟩
  rw [he]
  exact Nat.card_congr (Setoid.quotientKerEquivOfSurjective f hs)

 

theorem sum_option [Finite α] (f : ℕ → ℝ) :
    (∑ s : Setoid (Option α), f (Nat.card (Quotient s))) =
      ∑ r : Setoid α, (f (Nat.card (Quotient r) + 1) +
        (Nat.card (Quotient r) : ℝ) * f (Nat.card (Quotient r))) := by
  classical
  let := Fintype.ofFinite α
  let (r : Setoid α) := Fintype.ofFinite (Quotient r)
  rw [← extensionEquiv.sum_comp, Fintype.sum_sigma]
  apply sum_congr rfl
  intro r _
  rw [Fintype.sum_option]
  change f (Nat.card (Quotient (extend r none))) +
    (∑ x : Quotient r, f (Nat.card (Quotient (extend r (some x))))) = _
  simp only [card_extend_none, card_extend_some]
  simp only [sum_const, card_univ, nsmul_eq_mul, Nat.card_eq_fintype_card]

 
def coefficient (n : ℕ) : ℝ := (-1) ^ (n - 1) * (n - 1).factorial

lemma coefficient_recurrence {n : ℕ} (hn : 0 < n) :
    coefficient (n+1) + (n : ℝ) * coefficient n = 0 := by
  have hn' : n = (n-1)+1 := by omega
  simp only [coefficient, Nat.add_sub_cancel]
  conv_lhs => arg 1; arg 1; rw [hn',pow_succ]
  have hf : n.factorial = n * (n-1).factorial := by
    conv_lhs => rw [hn', Nat.factorial_succ]
    rw [← hn']
  rw [hf]
  push_cast
  ring

 

theorem sum_coefficient_option [Finite α] [Nonempty α] :
    (∑ r : Setoid (Option α), coefficient (Nat.card (Quotient r))) = 0 := by
  rw [sum_option]
  apply sum_eq_zero
  intro r _
  let : Nonempty (Quotient r) := ⟨Quotient.mk r (Classical.arbitrary α)⟩
  exact coefficient_recurrence Nat.card_pos

 
def setoidEquiv {β : Type*} (e : α ≃ β) : Setoid α ≃ Setoid β where
  toFun r := r.comap e.symm
  invFun s := s.comap e
  left_inv r := by
    apply Setoid.ext
    intro a b
    change r (e.symm (e a)) (e.symm (e b)) ↔ r a b
    simp only [e.symm_apply_apply]
  right_inv s := by
    apply Setoid.ext
    intro a b
    change s (e (e.symm a)) (e (e.symm b)) ↔ s a b
    simp only [e.apply_symm_apply]

lemma card_relabel {β : Type*} (e : α ≃ β) (r : Setoid α) :
    Nat.card (Quotient (setoidEquiv e r)) = Nat.card (Quotient r) := by
  apply Nat.card_congr
  exact Quotient.congr e.symm (fun _ _ => Iff.rfl)

lemma sum_relabel {β : Type*} [Finite α] [Finite β] (e : α ≃ β) (f : ℕ → ℝ) :
    (∑ r : Setoid α, f (Nat.card (Quotient r))) =
      ∑ s : Setoid β, f (Nat.card (Quotient s)) := by
  exact Fintype.sum_equiv (setoidEquiv e) _ _ (fun r => by rw [card_relabel])

lemma sum_coefficient_nontrivial [Finite α] [Nontrivial α] :
    (∑ r : Setoid α, coefficient (Nat.card (Quotient r))) = 0 := by
  classical
  obtain ⟨a,b,hab⟩ := exists_pair_ne α
  let : Nonempty {x : α // x ≠ a} := ⟨⟨b,hab.symm⟩⟩
  rw [← sum_relabel (Equiv.optionSubtypeNe a)]
  exact sum_coefficient_option

lemma sum_coefficient_subsingleton [Finite α] [Subsingleton α] [Nonempty α] :
    (∑ r : Setoid α, coefficient (Nat.card (Quotient r))) = 1 := by
  let : Unique (Setoid α) :=
    { default := ⊤
      uniq := fun r => Setoid.ext (fun a b => by
        rw [Subsingleton.elim b a]
        simp) }
  rw [Fintype.sum_unique]
  have hcard : Nat.card (Quotient (default : Setoid α)) = 1 := by
    apply Nat.card_eq_one_iff_unique.mpr
    refine ⟨?_, ⟨Quotient.mk _ (Classical.arbitrary α)⟩⟩
    refine ⟨fun x y => ?_⟩
    induction x using Quotient.inductionOn with | h a =>
      induction y using Quotient.inductionOn with | h b =>
        rw [Subsingleton.elim b a]
  rw [hcard]
  norm_num [coefficient]

lemma sum_coefficient [Finite α] [Nonempty α] :
    (∑ r : Setoid α, coefficient (Nat.card (Quotient r))) =
      if Nat.card α = 1 then 1 else 0 := by
  classical
  cases subsingleton_or_nontrivial α with
  | inl h =>
    let := h
    rw [sum_coefficient_subsingleton, ite_eq_left (Nat.card_eq_one_iff_unique.mpr ⟨h,inferInstance⟩)]
  | inr h =>
    let := h
    rw [sum_coefficient_nontrivial, ite_eq_right]
    exact fun he => not_subsingleton α (Nat.card_eq_one_iff_unique.mp he).1

 

def coarseningQuotientEquiv (r s : Setoid α) (h : r ≤ s) :
    Quotient (Setoid.correspondence r ⟨s,h⟩) ≃ Quotient s := by
  apply Equiv.trans _ (Setoid.quotientQuotientEquivQuotient r s h)
  apply Quotient.congrRight
  intro a b
  induction a using Quotient.inductionOn with | h x =>
    induction b using Quotient.inductionOn with | h y =>
      change s x y ↔ Quotient.mk s x = Quotient.mk s y
      exact Quotient.eq.symm

 

theorem sum_coarsenings [Finite α] [Nonempty α] (r : Setoid α) :
    (∑ s : {s : Setoid α // r ≤ s}, coefficient (Nat.card (Quotient s.1))) =
      if Nat.card (Quotient r) = 1 then 1 else 0 := by
  classical
  let := Fintype.ofFinite (Quotient r)
  let : Nonempty (Quotient r) := ⟨Quotient.mk r (Classical.arbitrary α)⟩
  rw [← sum_coefficient (α := Quotient r)]
  apply Fintype.sum_equiv (Setoid.correspondence r).toEquiv
  intro s
  exact congrArg coefficient (Nat.card_congr (coarseningQuotientEquiv r s.1 s.2)).symm

 

theorem connected_cancellation [Finite α] [Nonempty α]
    {Ω : Type*} [Fintype Ω] (component : Ω → Setoid α) (weight : Ω → ℝ) :
    (∑ r : Setoid α, coefficient (Nat.card (Quotient r)) *
      ∑ ω : Ω, if component ω ≤ r then weight ω else 0) =
      ∑ ω : Ω, if Nat.card (Quotient (component ω)) = 1 then weight ω else 0 := by
  classical
  simp only [mul_sum]
  rw [sum_comm]
  apply sum_congr rfl
  intro ω _
  have he : (∑ r : Setoid α, coefficient (Nat.card (Quotient r)) *
      (if component ω ≤ r then weight ω else 0)) =
      (∑ r : {r : Setoid α // component ω ≤ r},
        coefficient (Nat.card (Quotient r.1))) * weight ω := by
    rw [sum_mul, ← Finset.sum_subtype (univ.filter (fun r => component ω ≤ r))
      (by simp) (fun r => coefficient (Nat.card (Quotient r)) * weight ω), sum_filter]
    apply sum_congr rfl
    intro r _
    split_ifs <;> simp_all
  rw [he, sum_coarsenings]
  split_ifs <;> simp

end BelowOne.PartitionCumulant
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
attribute [local instance] Classical.propDecidable
namespace BelowOne.FKPressure
variable {V I J : Type*} [Finite V]

 

def blockStructureSum (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) (r : Setoid V) : ℝ :=
  ∑ A ∈ E.powerset, ∑ B ∈ E.powerset, ∑ L ∈ H.powerset,
    if (graph (graph G ends (A ∪ B)) hardEnds L).reachableSetoid ≤ r then
      (∏ e ∈ A, v e) * (∏ e ∈ B, v e) * s ^ components G ends A * (-1 : ℝ)^L.card
    else 0

 

theorem exponential_cancellation [Nonempty V] (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) :
    (∑ r : Setoid V, PartitionCumulant.coefficient (Nat.card (Quotient r)) *
      blockStructureSum G ends hardEnds E H s v r) =
      connectedSum G ends hardEnds E H s v := by
  classical
  let Ω := (E.powerset ×ˢ E.powerset) ×ˢ H.powerset
  let component (ω : ((Finset I × Finset I) × Finset J)) : Setoid V :=
    (graph (graph G ends (ω.1.1 ∪ ω.1.2)) hardEnds ω.2).reachableSetoid
  let weight (ω : ((Finset I × Finset I) × Finset J)) : ℝ :=
    (∏ e ∈ ω.1.1, v e) * (∏ e ∈ ω.1.2, v e) *
      s ^ components G ends ω.1.1 * (-1 : ℝ)^ω.2.card
  have hsum (f : ((Finset I × Finset I) × Finset J) → ℝ) :
      (∑ ω : Ω, f ω.1) = ∑ A ∈ E.powerset, ∑ B ∈ E.powerset,
        ∑ L ∈ H.powerset, f ((A,B),L) := by
    rw [Finset.sum_coe_sort Ω f]
    simp only [Ω, sum_product]
  have h := PartitionCumulant.connected_cancellation
    (fun ω : Ω => component ω.1) (fun ω : Ω => weight ω.1)
  have hs (r : Setoid V) :
      (∑ ω : Ω, if component ω.1 ≤ r then weight ω.1 else 0) =
        blockStructureSum G ends hardEnds E H s v r := by
    exact hsum (fun ω => if component ω ≤ r then weight ω else 0)
  have hc : (∑ ω : Ω, if Nat.card (Quotient (component ω.1)) = 1 then weight ω.1 else 0) =
      connectedSum G ends hardEnds E H s v := by
    rw [hsum (fun ω => if Nat.card (Quotient (component ω)) = 1 then weight ω else 0)]
    unfold connectedSum hardSum
    apply sum_congr rfl
    intro A hA
    apply sum_congr rfl
    intro B hB
    rw [mul_sum]
    apply sum_congr rfl
    intro L hL
    change (if components (graph G ends (A ∪ B)) hardEnds L = 1 then _ else _) = _
    split_ifs <;> simp [weight]
  simpa only [hs, hc] using h

 

theorem logarithmic_structure_nonpos [Nonempty V]
    (G : SimpleGraph V) (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) {u : ℝ} (hu : 0 < u) (hu1 : u < 1)
    (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    (∑ r : Setoid V, PartitionCumulant.coefficient (Nat.card (Quotient r)) *
      blockStructureSum G ends hardEnds E H (-u) v r) ≤ 0 := by
  rw [exponential_cancellation]
  exact connectedSum_nonpos G ends hardEnds E H hu hu1 v hv

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset SimpleGraph Set
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V : Type*} [Fintype V]

lemma component_card_block_sum (G : SimpleGraph V) (r : Setoid V)
    (h : G.reachableSetoid ≤ r) :
    Nat.card G.ConnectedComponent =
      ∑ q : Quotient r, Nat.card
        (Quotient (G.reachableSetoid.comap
          (Subtype.val : Quotient.mk r ⁻¹' {q} → V))) := by
  classical
  change Nat.card (Quotient G.reachableSetoid) = _
  rw [← Nat.card_congr (Setoid.sigmaQuotientEquivOfLe h)]
  exact Nat.card_sigma

omit [Fintype V] in
lemma reachable_induce_block (G : SimpleGraph V) (r : Setoid V)
    (h : G.reachableSetoid ≤ r) (q : Quotient r)
    (x y : Quotient.mk r ⁻¹' {q}) :
    (G.induce (Quotient.mk r ⁻¹' {q})).Reachable x y ↔ G.Reachable x.1 y.1 := by
  constructor
  · intro hxy
    exact hxy.map (SimpleGraph.Embedding.induce _).toHom
  · rintro ⟨p⟩
    have hp : ∀ z ∈ p.support, z ∈ Quotient.mk r ⁻¹' {q} := by
      intro z hz
      have hr : r x.1 z := h ⟨p.takeUntil z hz⟩
      change Quotient.mk r z = q
      exact (Quotient.sound hr).symm.trans x.property
    exact ⟨p.induce _ hp⟩

lemma components_induce_block_sum (G : SimpleGraph V) (r : Setoid V)
    (h : G.reachableSetoid ≤ r) :
    Nat.card G.ConnectedComponent =
      ∑ q : Quotient r, Nat.card (G.induce (Quotient.mk r ⁻¹' {q})).ConnectedComponent := by
  classical
  rw [component_card_block_sum G r h]
  apply sum_congr rfl
  intro q hq
  have he : G.reachableSetoid.comap (Subtype.val : Quotient.mk r ⁻¹' {q} → V) =
      (G.induce (Quotient.mk r ⁻¹' {q})).reachableSetoid := by
    apply Setoid.ext
    intro x y
    exact (reachable_induce_block G r h q x y).symm
  rw [he]
  rfl

lemma component_weight_block_prod (G : SimpleGraph V) (r : Setoid V)
    (h : G.reachableSetoid ≤ r) (s : ℝ) :
    s ^ Nat.card G.ConnectedComponent =
      ∏ q : Quotient r, s ^ Nat.card (G.induce (Quotient.mk r ⁻¹' {q})).ConnectedComponent := by
  classical
  rw [components_induce_block_sum G r h,← Finset.prod_pow_eq_pow_sum]

variable {I : Type*}

omit [Fintype V] in
lemma graph_adj_iff (G : SimpleGraph V) (ends : I → V × V) (A : Finset I) (x y : V) :
    (graph G ends A).Adj x y ↔ G.Adj x y ∨ ∃ e ∈ A, (edge (ends e).1 (ends e).2).Adj x y := by
  classical
  induction A using Finset.induction_on with
  | empty => simp
  | @insert e A he ih =>
    simp only [graph_insert,SimpleGraph.sup_adj,ih]
    aesop

omit [Fintype V] in
lemma endpoints_reachable (G : SimpleGraph V) (ends : I → V × V) {A : Finset I} {e : I}
    (he : e ∈ A) : (graph G ends A).Reachable (ends e).1 (ends e).2 := by
  by_cases h : (ends e).1 = (ends e).2
  · rw [h]
  · apply SimpleGraph.Adj.reachable
    apply (graph_adj_iff G ends A _ _).mpr
    exact Or.inr ⟨e,he,(edge_adj _ _ _ _).mpr ⟨Or.inl ⟨rfl,rfl⟩,h⟩⟩

omit [Fintype V] in
lemma graph_reachable_le_iff (G : SimpleGraph V) (ends : I → V × V) (A : Finset I)
    (r : Setoid V) : (graph G ends A).reachableSetoid ≤ r ↔
      G.reachableSetoid ≤ r ∧ ∀ e ∈ A, r (ends e).1 (ends e).2 := by
  constructor
  · intro h
    exact ⟨fun x y hxy => h (hxy.mono le_sup_left),fun e he => h (endpoints_reachable G ends he)⟩
  · rintro ⟨hG,hA⟩ x y ⟨p⟩
    apply Quotient.exact
    apply FKComponents.eq_of_walk (f := Quotient.mk r) (p := p)
    intro x y hxy
    apply Quotient.sound
    rcases (graph_adj_iff G ends A x y).mp hxy with hxy | ⟨e,he,hexy⟩
    · exact hG hxy.reachable
    · rcases (edge_adj _ _ _ _).mp hexy with ⟨⟨rfl,rfl⟩ | ⟨rfl,rfl⟩,_⟩
      · exact hA e he
      · exact r.symm (hA e he)

def edgeBlock (r : Setoid V) (ends : I → V × V) (e : I) : Quotient r := Quotient.mk r (ends e).1

omit [Fintype V] in
lemma graph_induce_filter (G : SimpleGraph V) (r : Setoid V) (ends : I → V × V)
    (A : Finset I) (q : Quotient r) :
    (graph G ends (A.filter (edgeBlock r ends · = q))).induce (Quotient.mk r ⁻¹' {q}) =
      (graph G ends A).induce (Quotient.mk r ⁻¹' {q}) := by
  classical
  ext x y
  simp only [SimpleGraph.induce_adj,graph_adj_iff,mem_filter]
  constructor
  · rintro (h | ⟨e,⟨he,_⟩,hxy⟩)
    · exact Or.inl h
    · exact Or.inr ⟨e,he,hxy⟩
  · rintro (h | ⟨e,he,hxy⟩)
    · exact Or.inl h
    · refine Or.inr ⟨e,⟨he,?_⟩,hxy⟩
      rcases (edge_adj _ _ _ _).mp hxy with ⟨h | h,_⟩
      · change Quotient.mk r (ends e).1 = q
        rw [← h.1]
        exact x.2
      · change Quotient.mk r (ends e).1 = q
        rw [← h.2]
        exact y.2

end BelowOne.FKPressure
end
end

 
section

 
noncomputable section
open Finset
namespace BelowOne.SubsetSplitting
variable {I Q : Type*} [Fintype Q]
attribute [local instance] Classical.propDecidable

abbrev Selection (E : Finset I) := ↥E.powerset

def fiber (E : Finset I) (f : I → Q) (q : Q) : Finset I := E.filter (f · = q)

def split (E : Finset I) (f : I → Q) (A : Selection E) (q : Q) : Selection (fiber E f q) :=
  ⟨A.1.filter (f · = q), mem_powerset.mpr (by
    intro e he
    have h := mem_filter.mp he
    exact mem_filter.mpr ⟨mem_powerset.mp A.2 h.1,h.2⟩)⟩

def join (E : Finset I) (f : I → Q) (B : ∀ q, Selection (fiber E f q)) : Selection E :=
  ⟨univ.biUnion (fun q => (B q).1),mem_powerset.mpr (by
    intro e he
    obtain ⟨q,_,hq⟩ := mem_biUnion.mp he
    exact mem_filter.mp (mem_powerset.mp (B q).2 hq) |>.1)⟩

lemma join_split (E : Finset I) (f : I → Q) (A : Selection E) :
    join E f (split E f A) = A := by
  classical
  apply Subtype.ext
  ext e
  simp only [join,split,mem_biUnion,mem_univ,true_and,mem_filter]
  constructor
  · rintro ⟨q,he,_⟩
    exact he
  · intro he
    exact ⟨f e,he,rfl⟩

lemma split_join (E : Finset I) (f : I → Q) (B : ∀ q, Selection (fiber E f q)) :
    split E f (join E f B) = B := by
  classical
  funext q
  apply Subtype.ext
  ext e
  simp only [split,join,mem_filter,mem_biUnion,mem_univ,true_and]
  constructor
  · rintro ⟨⟨r,he⟩,hq⟩
    have hr := mem_filter.mp (mem_powerset.mp (B r).2 he) |>.2
    have hqr : q = r := hq.symm.trans hr
    cases hqr
    exact he
  · intro he
    exact ⟨⟨q,he⟩,mem_filter.mp (mem_powerset.mp (B q).2 he) |>.2⟩

def selectionEquiv (E : Finset I) (f : I → Q) : Selection E ≃ (∀ q, Selection (fiber E f q)) where
  toFun := split E f
  invFun := join E f
  left_inv := join_split E f
  right_inv := split_join E f

lemma sum_factorization (E : Finset I) (f : I → Q) (W : Finset I → ℝ)
    (w : Q → Finset I → ℝ)
    (hW : ∀ A ⊆ E, W A = ∏ q, w q (A.filter (f · = q))) :
    (∑ A ∈ E.powerset, W A) = ∏ q, ∑ A ∈ (fiber E f q).powerset, w q A := by
  classical
  have hs : (∑ A : Selection E, W A.1) =
      ∑ B : (∀ q, Selection (fiber E f q)), ∏ q, w q (B q).1 := by
    apply Fintype.sum_equiv (selectionEquiv E f)
    intro A
    exact hW A.1 (mem_powerset.mp A.2)
  have hp := Fintype.prod_sum (fun q (A : Selection (fiber E f q)) => w q A.1)
  simpa only [Selection,Finset.sum_coe_sort] using hs.trans hp.symm

def piTripleEquiv {Q : Type*} (A B C : Q → Type*) :
    ((∀ q, A q) × (∀ q, B q) × (∀ q, C q)) ≃ (∀ q, A q × B q × C q) where
  toFun p q := (p.1 q,p.2.1 q,p.2.2 q)
  invFun p := (fun q => (p q).1, fun q => (p q).2.1, fun q => (p q).2.2)
  left_inv p := rfl
  right_inv p := by funext q; rfl

lemma sum_triple_coe {I J K : Type*} (E : Finset I) (F : Finset J) (H : Finset K)
    (W : I → J → K → ℝ) :
    (∑ p : E × F × H, W p.1.1 p.2.1.1 p.2.2.1) =
      ∑ a ∈ E, ∑ b ∈ F, ∑ c ∈ H, W a b c := by
  simp only [Fintype.sum_prod_type]
  rw [show (∑ a : E, ∑ b : F, ∑ c : H, W a.1 b.1 c.1) =
      ∑ a ∈ E, ∑ b : F, ∑ c : H, W a b.1 c.1 from Finset.sum_coe_sort E (fun a => ∑ b : F, ∑ c : H, W a b.1 c.1)]
  apply sum_congr rfl
  intro a ha
  rw [show (∑ b : F, ∑ c : H, W a b.1 c.1) =
      ∑ b ∈ F, ∑ c : H, W a b c.1 from Finset.sum_coe_sort F (fun b => ∑ c : H, W a b c.1)]
  simp only [Finset.sum_coe_sort]

lemma sum_factorization_three {J K : Type*}
    (E : Finset I) (F : Finset J) (H : Finset K)
    (f : I → Q) (g : J → Q) (h : K → Q)
    (W : Finset I → Finset J → Finset K → ℝ)
    (w : Q → Finset I → Finset J → Finset K → ℝ)
    (hW : ∀ A ⊆ E, ∀ B ⊆ F, ∀ C ⊆ H,
      W A B C = ∏ q, w q (A.filter (f · = q)) (B.filter (g · = q)) (C.filter (h · = q))) :
    (∑ A ∈ E.powerset, ∑ B ∈ F.powerset, ∑ C ∈ H.powerset, W A B C) =
      ∏ q, ∑ A ∈ (fiber E f q).powerset, ∑ B ∈ (fiber F g q).powerset,
        ∑ C ∈ (fiber H h q).powerset, w q A B C := by
  classical
  let e := (Equiv.prodCongr (selectionEquiv E f)
    (Equiv.prodCongr (selectionEquiv F g) (selectionEquiv H h))).trans
      (piTripleEquiv _ _ _)
  have hs : (∑ p : Selection E × Selection F × Selection H, W p.1.1 p.2.1.1 p.2.2.1) =
      ∑ p : (∀ q, Selection (fiber E f q) × Selection (fiber F g q) × Selection (fiber H h q)),
        ∏ q, w q (p q).1.1 (p q).2.1.1 (p q).2.2.1 := by
    apply Fintype.sum_equiv e
    intro p
    exact hW p.1.1 (mem_powerset.mp p.1.2) p.2.1.1 (mem_powerset.mp p.2.1.2)
      p.2.2.1 (mem_powerset.mp p.2.2.2)
  have hp := Fintype.prod_sum (fun q
    (p : Selection (fiber E f q) × Selection (fiber F g q) × Selection (fiber H h q)) =>
      w q p.1.1 p.2.1.1 p.2.2.1)
  have hh := hs.trans hp.symm
  change (∑ p : E.powerset × F.powerset × H.powerset, _) =
    ∏ q, ∑ p : (fiber E f q).powerset × (fiber F g q).powerset × (fiber H h q).powerset, _ at hh
  rw [sum_triple_coe] at hh
  simpa only [sum_triple_coe] using hh

end BelowOne.SubsetSplitting
end
end

 
section

 
noncomputable section
open Finset SimpleGraph
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V I J : Type*} [Fintype V]

lemma filter_classical (E : Finset I) (p : I → Prop) (d : DecidablePred p) :
    @Finset.filter I p d E = @Finset.filter I p (fun e => Classical.propDecidable (p e)) E := by
  exact @Finset.filter_congr_decidable I E p d (fun e => Classical.propDecidable (p e))

lemma powerset_filter_eq (E : Finset I) (p : I → Prop) :
    (E.filter p).powerset = E.powerset.filter (fun A => ∀ e ∈ A, p e) := by
  classical
  ext A
  simp only [mem_powerset,mem_filter]
  constructor
  · intro h
    exact ⟨fun e he => (mem_filter.mp (h he)).1,fun e he => (mem_filter.mp (h he)).2⟩
  · rintro ⟨h₁,h₂⟩ e he
    exact mem_filter.mpr ⟨h₁ he,h₂ e he⟩

lemma sum_powerset_filter (E : Finset I) (p : I → Prop) (W : Finset I → ℝ) :
    (∑ A ∈ E.powerset, if ∀ e ∈ A, p e then W A else 0) =
      ∑ A ∈ (E.filter p).powerset, W A := by
  rw [powerset_filter_eq,sum_filter]

omit [Fintype V] in
lemma confinement_iff (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (A B : Finset I) (L : Finset J) (r : Setoid V)
    (hG : G.reachableSetoid ≤ r) :
    (graph (graph G ends (A ∪ B)) hardEnds L).reachableSetoid ≤ r ↔
      (∀ e ∈ A, r (ends e).1 (ends e).2) ∧
      (∀ e ∈ B, r (ends e).1 (ends e).2) ∧
      (∀ e ∈ L, r (hardEnds e).1 (hardEnds e).2) := by
  rw [graph_reachable_le_iff,graph_reachable_le_iff]
  simp only [hG,true_and,mem_union,or_imp,forall_and,and_assoc]

omit [Fintype V] in
lemma blockStructureSum_restrict (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) (r : Setoid V) (hG : G.reachableSetoid ≤ r) :
    blockStructureSum G ends hardEnds E H s v r =
      ∑ A ∈ (E.filter (fun e => r (ends e).1 (ends e).2)).powerset,
      ∑ B ∈ (E.filter (fun e => r (ends e).1 (ends e).2)).powerset,
      ∑ L ∈ (H.filter (fun e => r (hardEnds e).1 (hardEnds e).2)).powerset,
        (∏ e ∈ A, v e) * (∏ e ∈ B, v e) * s ^ components G ends A * (-1 : ℝ)^L.card := by
  unfold blockStructureSum
  simp_rw [confinement_iff G ends hardEnds _ _ _ r hG]
  rw [← sum_powerset_filter E (fun e => r (ends e).1 (ends e).2)]
  apply sum_congr rfl
  intro A hA
  by_cases ha : ∀ e ∈ A, r (ends e).1 (ends e).2
  · have eqa : (∀ e ∈ A, r (ends e).1 (ends e).2) = True := propext (iff_true_intro ha)
    simp only [eqa,true_and,ite_true]
    rw [← sum_powerset_filter E (fun e => r (ends e).1 (ends e).2)]
    apply sum_congr rfl
    intro B hB
    by_cases hb : ∀ e ∈ B, r (ends e).1 (ends e).2
    · have eqb : (∀ e ∈ B, r (ends e).1 (ends e).2) = True := propext (iff_true_intro hb)
      simp only [eqb,true_and,ite_true]
      rw [sum_powerset_filter]
    · simp [hb]
  · simp [ha]

 

def inducedBlockMoment (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) (r : Setoid V) (q : Quotient r) : ℝ :=
  ∑ A ∈ (E.filter (fun e => r (ends e).1 (ends e).2 ∧ edgeBlock r ends e = q)).powerset,
  ∑ B ∈ (E.filter (fun e => r (ends e).1 (ends e).2 ∧ edgeBlock r ends e = q)).powerset,
  ∑ L ∈ (H.filter (fun e => r (hardEnds e).1 (hardEnds e).2 ∧ edgeBlock r hardEnds e = q)).powerset,
    (∏ e ∈ A, v e) * (∏ e ∈ B, v e) *
      s ^ Nat.card ((graph G ends A).induce (Quotient.mk r ⁻¹' {q})).ConnectedComponent * (-1 : ℝ)^L.card

lemma structure_weight_factor (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (A B : Finset I) (L : Finset J)
    (s : ℝ) (v : I → ℝ) (r : Setoid V) (hA : (graph G ends A).reachableSetoid ≤ r) :
    (∏ e ∈ A, v e) * (∏ e ∈ B, v e) * s ^ components G ends A * (-1 : ℝ)^L.card =
      ∏ q : Quotient r,
        (∏ e ∈ A.filter (edgeBlock r ends · = q), v e) *
        (∏ e ∈ B.filter (edgeBlock r ends · = q), v e) *
        s ^ Nat.card ((graph G ends (A.filter (edgeBlock r ends · = q))).induce
          (Quotient.mk r ⁻¹' {q})).ConnectedComponent *
        (-1 : ℝ)^(L.filter (edgeBlock r hardEnds · = q)).card := by
  classical
  simp_rw [graph_induce_filter,Finset.prod_mul_distrib]
  rw [prod_fiberwise,prod_fiberwise]
  congr 1
  · congr 1
    exact component_weight_block_prod (graph G ends A) r hA s
  · simpa only [prod_const] using
      (prod_fiberwise L (edgeBlock r hardEnds) (fun _ => (-1:ℝ))).symm

lemma blockStructureSum_factorization (G : SimpleGraph V) (ends : I → V × V)
    (hardEnds : J → V × V) (E : Finset I) (H : Finset J)
    (s : ℝ) (v : I → ℝ) (r : Setoid V) (hG : G.reachableSetoid ≤ r) :
    blockStructureSum G ends hardEnds E H s v r =
      ∏ q : Quotient r, inducedBlockMoment G ends hardEnds E H s v r q := by
  rw [blockStructureSum_restrict G ends hardEnds E H s v r hG]
  have hf := SubsetSplitting.sum_factorization_three
    (E.filter (fun e => r (ends e).1 (ends e).2))
    (E.filter (fun e => r (ends e).1 (ends e).2))
    (H.filter (fun e => r (hardEnds e).1 (hardEnds e).2))
    (edgeBlock r ends) (edgeBlock r ends) (edgeBlock r hardEnds)
    (fun A B L => (∏ e ∈ A, v e) * (∏ e ∈ B, v e) * s ^ components G ends A * (-1 : ℝ)^L.card)
    (fun q A B L => (∏ e ∈ A, v e) * (∏ e ∈ B, v e) *
      s ^ Nat.card ((graph G ends A).induce (Quotient.mk r ⁻¹' {q})).ConnectedComponent * (-1 : ℝ)^L.card)
    (by
      intro A hA B hB L hL
      simpa only [filter_classical] using structure_weight_factor G ends hardEnds A B L s v r
        ((graph_reachable_le_iff G ends A r).mpr ⟨hG,fun e he => (mem_filter.mp (hA he)).2⟩))
  simpa only [SubsetSplitting.fiber,filter_filter,inducedBlockMoment,filter_classical] using hf

end BelowOne.FKPressure
end
end

 
section

 
noncomputable section
open Finset
open scoped Classical
namespace BelowOne.MultiaffineDerivative
variable {V : Type*} [Fintype V]

abbrev coordinate (i : V) : (V → ℝ) := Pi.single i 1

def restrict (T : Finset V) : (V → ℝ) →L[ℝ] (T → ℝ) :=
  ContinuousLinearMap.pi (fun i => ContinuousLinearMap.proj i.1)

omit [Fintype V] in
lemma restrict_apply (T : Finset V) (x : V → ℝ) (i : T) : restrict T x i = x i.1 := rfl

def Matches {n : ℕ} (τ : Fin n → V) {T : Finset V} (e : Fin n ↪ T) : Prop :=
  ∀ t : T, ∃ i, e i = t ∧ τ i = t.1

omit [Fintype V] in
lemma matches_iff {n : ℕ} (τ : Fin n → V) {T : Finset V} (e : Fin n ↪ T) :
    Matches τ e ↔ (∀ i, τ i = (e i).1) ∧ Function.Surjective e := by
  constructor
  · intro h
    constructor
    · intro i
      obtain ⟨j,hj,hτ⟩ := h (e i)
      have hji := e.injective hj
      simpa [hji] using hτ
    · intro t
      obtain ⟨i,hi,_⟩ := h t
      exact ⟨i,hi⟩
  · rintro ⟨hτ,he⟩ t
    obtain ⟨i,rfl⟩ := he t
    exact ⟨i,rfl,hτ i⟩

omit [Fintype V] in
lemma matches_data {n : ℕ} {τ : Fin n → V} {T : Finset V} {e : Fin n ↪ T}
    (h : Matches τ e) : Function.Injective τ ∧ univ.image τ = T := by
  obtain ⟨hτ,he⟩ := (matches_iff τ e).mp h
  constructor
  · intro i j hij
    apply e.injective
    apply Subtype.ext
    rw [←hτ i,←hτ j,hij]
  · ext t
    simp only [mem_image,mem_univ,true_and]
    constructor
    · rintro ⟨i,rfl⟩
      rw [hτ i]
      exact (e i).2
    · intro ht
      obtain ⟨i,hi⟩ := he ⟨t,ht⟩
      exact ⟨i,(hτ i).trans (congrArg Subtype.val hi)⟩

omit [Fintype V] in
lemma matches_unique {n : ℕ} {τ : Fin n → V} {T : Finset V} {e f : Fin n ↪ T}
    (he : Matches τ e) (hf : Matches τ f) : e = f := by
  ext i
  exact ((matches_iff τ e).mp he).1 i |>.symm.trans (((matches_iff τ f).mp hf).1 i)

def matchingEmbedding {n : ℕ} (τ : Fin n → V) (T : Finset V)
    (h : Function.Injective τ ∧ univ.image τ = T) : Fin n ↪ T where
  toFun i := ⟨τ i,by rw [←h.2]; exact mem_image_of_mem τ (mem_univ i)⟩
  inj' := fun i j hij => h.1 (congrArg Subtype.val hij)

omit [Fintype V] in
lemma matchingEmbedding_matches {n : ℕ} (τ : Fin n → V) (T : Finset V)
    (h : Function.Injective τ ∧ univ.image τ = T) : Matches τ (matchingEmbedding τ T h) := by
  intro t
  have ht : t.1 ∈ univ.image τ := by rw [h.2]; exact t.2
  obtain ⟨i,_,hi⟩ := mem_image.mp ht
  refine ⟨i,?_,hi⟩
  exact Subtype.ext hi

omit [Fintype V] in
lemma sum_matches {n : ℕ} (τ : Fin n → V) (T : Finset V) :
    (∑ e : Fin n ↪ T, if Matches τ e then (1:ℝ) else 0) =
      if Function.Injective τ ∧ univ.image τ = T then 1 else 0 := by
  by_cases h : Function.Injective τ ∧ univ.image τ = T
  · rw [ite_eq_left h]
    let e := matchingEmbedding τ T h
    have he := matchingEmbedding_matches τ T h
    rw [Fintype.sum_eq_single e]
    · exact ite_eq_left he
    · intro f hf
      exact ite_eq_right (fun hh => hf (matches_unique hh he))
  · rw [ite_eq_right h]
    apply sum_eq_zero
    intro e he
    exact ite_eq_right (fun hh => h (matches_data hh))

omit [Fintype V] in
lemma factor_indicator {n : ℕ} (τ : Fin n → V) (T : Finset V) (e : Fin n ↪ T) (t : T) :
    (if ht : t ∈ Set.range e then
      coordinate (τ (e.toEquivRange.symm ⟨t,ht⟩)) t.1 else 0) =
      if ∃ i, e i = t ∧ τ i = t.1 then (1:ℝ) else 0 := by
  by_cases ht : t ∈ Set.range e
  · obtain ⟨i,rfl⟩ := ht
    rw [dite_eq_left (Set.mem_range_self i)]
    have hi : e.toEquivRange.symm ⟨e i, Set.mem_range_self i⟩ = i :=
      e.toEquivRange.symm_apply_apply i
    rw [hi]
    simp [coordinate,Pi.single_apply,e.injective.eq_iff,eq_comm]
  · rw [dite_eq_right ht,ite_eq_right]
    rintro ⟨i,hi,_⟩
    exact ht ⟨i,hi⟩

lemma prod_indicator {T : Type*} [Fintype T] (p : T → Prop) :
    (∏ t : T, if p t then (1:ℝ) else 0) = if ∀ t, p t then 1 else 0 := by
  by_cases h : ∀ t, p t
  · simp [h]
  · rw [ite_eq_right h]
    obtain ⟨t,ht⟩ := not_forall.mp h
    exact Finset.prod_eq_zero (mem_univ t) (ite_eq_right ht)

lemma ite_classical {α : Sort*} (p : Prop) (d : Decidable p) (a b : α) :
    @ite α p d a b = @ite α p (Classical.propDecidable p) a b := by
  cases d <;> simp_all

lemma dite_classical {α : Sort*} (p : Prop) (d : Decidable p) (a : p → α) (b : ¬p → α) :
    @dite α p d a b = @dite α p (Classical.propDecidable p) a b := by
  cases d <;> simp_all

lemma range_equiv_classical {α β : Type*} [Fintype α] (d : DecidableEq β) (e : α ↪ β) :
    @Function.Embedding.toEquivRange α β _ d e =
      @Function.Embedding.toEquivRange α β _ (fun a b => Classical.propDecidable (a = b)) e := by
  ext i
  rfl

lemma monomial_derivative {n : ℕ} (τ : Fin n → V) (T : Finset V) :
    iteratedFDeriv ℝ n (fun x : V → ℝ => ∏ i ∈ T, x i) 0 (coordinate ∘ τ) =
      if Function.Injective τ ∧ univ.image τ = T then 1 else 0 := by
  let f := ContinuousMultilinearMap.mkPiAlgebra ℝ T ℝ
  have hf : (fun x : V → ℝ => ∏ i ∈ T, x i) = f ∘ restrict T := by
    funext x
    simp only [Function.comp_apply,f,ContinuousMultilinearMap.mkPiAlgebra_apply,restrict_apply]
    exact (Finset.prod_coe_sort T x).symm
  rw [hf,ContinuousLinearMap.iteratedFDeriv_comp_right _ f.contDiff _ le_rfl]
  rw [f.iteratedFDeriv_eq]
  simp only [ContinuousMultilinearMap.compContinuousLinearMap_apply,
    ContinuousMultilinearMap.iteratedFDeriv,_root_.sum_apply,
    ContinuousMultilinearMap.iteratedFDerivComponent_apply,
    ContinuousLinearMap.map_zero,Pi.zero_apply,
    Function.comp_apply,restrict_apply,f,ContinuousMultilinearMap.mkPiAlgebra_apply]
  trans ∑ e : Fin n ↪ T, if Matches τ e then (1:ℝ) else 0
  · apply sum_congr rfl
    intro e he
    trans ∏ t : T, if ∃ i, e i = t ∧ τ i = t.1 then (1:ℝ) else 0
    · apply prod_congr rfl
      intro t ht
      simpa only [range_equiv_classical,ite_classical,dite_classical] using factor_indicator τ T e t
    · by_cases hm : Matches τ e
      · rw [ite_eq_left hm]
        apply prod_eq_one
        intro t ht
        exact ite_eq_left (hm t)
      · rw [ite_eq_right hm]
        obtain ⟨t,ht⟩ := not_forall.mp hm
        exact prod_eq_zero (mem_univ t) (ite_eq_right ht)
  · exact sum_matches τ T

end BelowOne.MultiaffineDerivative
end
end

 
section

 

noncomputable section
open Finset
open scoped Classical ContDiff
namespace BelowOne.MultiaffineDerivative
variable {V : Type*} [Fintype V]

lemma contDiff_monomial (T : Finset V) (n : ℕ∞ω) :
    ContDiff ℝ n (fun x : V → ℝ => ∏ i ∈ T, x i) := by
  fun_prop

lemma weighted_monomial_derivative {n : ℕ} (τ : Fin n → V) (T : Finset V) (c : ℝ) :
    iteratedFDeriv ℝ n (fun x : V → ℝ => c * ∏ i ∈ T, x i) 0 (coordinate ∘ τ) =
      if Function.Injective τ ∧ univ.image τ = T then c else 0 := by
  change iteratedFDeriv ℝ n (fun x : V → ℝ => c • ∏ i ∈ T, x i) 0 (coordinate ∘ τ) = _
  rw [iteratedFDeriv_const_smul_apply' (contDiff_monomial T n).contDiffAt]
  simp only [_root_.smul_apply,smul_eq_mul,monomial_derivative]
  split_ifs <;> simp

def polynomial (W : Finset V → ℝ) (x : V → ℝ) : ℝ :=
  ∑ T : Finset V, W T * ∏ i ∈ T, x i

lemma contDiff_polynomial (W : Finset V → ℝ) (n : ℕ∞ω) :
    ContDiff ℝ n (polynomial W) := by
  unfold polynomial
  fun_prop

lemma polynomial_derivative (W : Finset V → ℝ) {n : ℕ} (τ : Fin n → V) :
    iteratedFDeriv ℝ n (polynomial W) 0 (coordinate ∘ τ) =
      if Function.Injective τ then W (univ.image τ) else 0 := by
  unfold polynomial
  rw [iteratedFDeriv_fun_sum_apply
    (fun T hT => (contDiff_const.mul (contDiff_monomial T n)).contDiffAt)]
  simp only [_root_.sum_apply,weighted_monomial_derivative]
  by_cases hτ : Function.Injective τ
  · simp only [hτ,true_and,ite_true]
    simp [eq_comm]
  · simp [hτ]

lemma polynomial_zero (W : Finset V → ℝ) : polynomial W 0 = W ∅ := by
  have h := polynomial_derivative W (fun i : Fin 0 => i.elim0)
  simpa only [iteratedFDeriv_zero_apply,Finset.univ_eq_empty,Finset.image_empty,
    Function.injective_of_subsingleton,ite_true] using h

end BelowOne.MultiaffineDerivative
end
end

 
section

 

noncomputable section
open Finset Function Set
namespace BelowOne.OrderedPartitionBridge

variable {n : ℕ}

def partition (c : OrderedFinpartition n) : Setoid (Fin n) := Setoid.ker c.index

lemma index_emb (c : OrderedFinpartition n) (i : Fin c.length) (j : Fin (c.partSize i)) :
    c.index (c.emb i j) = i := by
  exact congrArg Sigma.fst (c.equivSigma.left_inv ⟨i,j⟩)

lemma index_surjective (c : OrderedFinpartition n) : Surjective c.index := by
  intro i
  exact ⟨c.emb i ⟨0,c.partSize_pos i⟩,index_emb c i _⟩

lemma card_partition (c : OrderedFinpartition n) : Nat.card (Quotient (partition c)) = c.length := by
  unfold partition
  rw [Nat.card_congr (Setoid.quotientKerEquivOfSurjective c.index (index_surjective c)),Nat.card_fin]

def maximum (c : OrderedFinpartition n) (i : Fin c.length) : Fin n :=
  c.emb i ⟨c.partSize i - 1, Nat.sub_one_lt_of_lt (c.partSize_pos i)⟩

lemma index_maximum (c : OrderedFinpartition n) (i : Fin c.length) :
    c.index (maximum c i) = i := index_emb c _ _

lemma le_maximum (c : OrderedFinpartition n) (x : Fin n) :
    x ≤ maximum c (c.index x) := by
  nth_rw 1 [← c.emb_invEmbedding x]
  exact (c.emb_strictMono _).monotone (Fin.le_iff_val_le_val.mpr (Nat.le_sub_one_of_lt (c.invEmbedding x).isLt))

lemma relation_iff_range (c : OrderedFinpartition n) (i : Fin c.length) (x : Fin n) :
    c.index x = i ↔ x ∈ Set.range (c.emb i) := by
  constructor
  · intro h
    subst i
    exact ⟨c.invEmbedding x,c.emb_invEmbedding x⟩
  · rintro ⟨j,rfl⟩
    exact index_emb c _ _

lemma maximum_eq_of_partition_eq (c d : OrderedFinpartition n)
    (h : partition c = partition d) (x : Fin n) :
    maximum c (c.index x) = maximum d (d.index x) := by
  apply le_antisymm
  · have hr : d.index (maximum c (c.index x)) = d.index x := by
      apply (Setoid.ext_iff.mp h _ _).mp
      exact index_maximum c _
    simpa only [hr] using le_maximum d (maximum c (c.index x))
  · have hr : c.index (maximum d (d.index x)) = c.index x := by
      apply (Setoid.ext_iff.mp h _ _).mpr
      exact index_maximum d _
    simpa only [hr] using le_maximum c (maximum d (d.index x))

lemma partition_injective : Injective (partition (n := n)) := by
  intro c d h
  have hl : c.length = d.length := (card_partition c).symm.trans
    ((congrArg (fun r : Setoid (Fin n) => Nat.card (Quotient r)) h).trans (card_partition d))
  have hm : Set.range (maximum c) = Set.range (maximum d) := by
    ext x
    constructor
    · rintro ⟨i,rfl⟩
      refine ⟨d.index (maximum c i),?_⟩
      simpa only [index_maximum] using (maximum_eq_of_partition_eq c d h (maximum c i)).symm
    · rintro ⟨i,rfl⟩
      refine ⟨c.index (maximum d i),?_⟩
      simpa only [index_maximum] using maximum_eq_of_partition_eq c d h (maximum d i)
  cases c with | mk cl cs cp ce cm co cd cc =>
    cases d with | mk dl ds dp de dm do' dd dc =>
      dsimp only at hl
      subst dl
      have hmax : (fun i => ce i ⟨cs i - 1,Nat.sub_one_lt_of_lt (cp i)⟩) =
          (fun i => de i ⟨ds i - 1,Nat.sub_one_lt_of_lt (dp i)⟩) :=
        co.range_inj_of_wellFoundedLT do' |>.mp hm
      have hr (i : Fin cl) : Set.range (ce i) = Set.range (de i) := by
        ext x
        let c' : OrderedFinpartition n := ⟨cl,cs,cp,ce,cm,co,cd,cc⟩
        let d' : OrderedFinpartition n := ⟨cl,ds,dp,de,dm,do',dd,dc⟩
        rw [← relation_iff_range c' i x,← relation_iff_range d' i x]
        have hx := Setoid.ext_iff.mp h x (ce i ⟨cs i-1,Nat.sub_one_lt_of_lt (cp i)⟩)
        change (c'.index x = c'.index (ce i ⟨cs i-1,Nat.sub_one_lt_of_lt (cp i)⟩)) ↔
          (d'.index x = d'.index (ce i ⟨cs i-1,Nat.sub_one_lt_of_lt (cp i)⟩)) at hx
        have hci : c'.index (ce i ⟨cs i-1,Nat.sub_one_lt_of_lt (cp i)⟩) = i :=
          index_emb c' _ _
        have hdi : d'.index (ce i ⟨cs i-1,Nat.sub_one_lt_of_lt (cp i)⟩) = i := by
          rw [congrFun hmax i]
          exact index_emb d' _ _
        simpa only [hci,hdi] using hx
      have hs : cs = ds := by
        funext i
        have hh := Nat.card_congr (Set.equivOfEq (hr i))
        simpa only [Nat.card_range_of_injective (cm i).injective,
          Nat.card_range_of_injective (dm i).injective, Nat.card_fin] using hh
      subst ds
      have he : ce = de := by
        funext i
        exact ((cm i).range_inj_of_wellFoundedLT (dm i)).mp (hr i)
      subst de
      rfl

def block (r : Setoid (Fin n)) (x : Fin n) : Finset (Fin n) := by
  classical
  exact univ.filter (r x)

@[simp] lemma mem_block (r : Setoid (Fin n)) (x y : Fin n) : y ∈ block r x ↔ r x y := by
  classical
  simp [block]

lemma block_nonempty (r : Setoid (Fin n)) (x : Fin n) : (block r x).Nonempty :=
  ⟨x,(mem_block r x x).mpr (r.refl x)⟩

lemma block_eq (r : Setoid (Fin n)) {x y : Fin n} (h : r x y) : block r x = block r y := by
  ext z
  simp only [mem_block]
  exact ⟨fun hx => r.trans (r.symm h) hx,fun hy => r.trans h hy⟩

def blockMax (r : Setoid (Fin n)) (x : Fin n) : Fin n :=
  (block r x).max' (block_nonempty r x)

lemma blockMax_related (r : Setoid (Fin n)) (x : Fin n) : r x (blockMax r x) :=
  (mem_block r x _).mp (max'_mem _ _)

lemma blockMax_eq (r : Setoid (Fin n)) {x y : Fin n} (h : r x y) : blockMax r x = blockMax r y := by
  simp only [blockMax,block_eq r h]

def maxima (r : Setoid (Fin n)) : Finset (Fin n) := by
  classical
  exact univ.image (blockMax r)

lemma maximum_fixed (r : Setoid (Fin n)) {x : Fin n} (h : x ∈ maxima r) : blockMax r x = x := by
  classical
  obtain ⟨y,_,rfl⟩ := mem_image.mp h
  exact (blockMax_eq r (blockMax_related r y)).symm

lemma blockMax_mem (r : Setoid (Fin n)) (x : Fin n) : blockMax r x ∈ maxima r := by
  classical
  exact mem_image_of_mem _ (mem_univ x)

def ofSetoid (r : Setoid (Fin n)) : OrderedFinpartition n where
  length := (maxima r).card
  partSize i := (block r ((maxima r).orderEmbOfFin rfl i)).card
  partSize_pos i := card_pos.mpr (block_nonempty r _)
  emb i := (block r ((maxima r).orderEmbOfFin rfl i)).orderEmbOfFin rfl
  emb_strictMono i := OrderEmbedding.strictMono _
  parts_strictMono := by
    have hm (i : Fin (maxima r).card) :
        (block r ((maxima r).orderEmbOfFin rfl i)).orderEmbOfFin rfl
          ⟨(block r ((maxima r).orderEmbOfFin rfl i)).card-1,
            Nat.sub_one_lt_of_lt (card_pos.mpr (block_nonempty r _))⟩ =
            (maxima r).orderEmbOfFin rfl i := by
      rw [orderEmbOfFin_last rfl (card_pos.mpr (block_nonempty r _))]
      exact maximum_fixed r (orderEmbOfFin_mem _ _ _)
    simpa only [hm] using (maxima r).orderEmbOfFin rfl |>.strictMono
  disjoint := by
    intro i _ j _ hij
    dsimp only [Function.onFun]
    rw [range_orderEmbOfFin,range_orderEmbOfFin]
    apply Set.disjoint_left.mpr
    intro x hi hj
    have hr := r.trans (mem_block r _ x |>.mp hi) (r.symm (mem_block r _ x |>.mp hj))
    have he := blockMax_eq r hr
    rw [maximum_fixed r (orderEmbOfFin_mem _ _ _),
      maximum_fixed r (orderEmbOfFin_mem _ _ _)] at he
    exact hij ((maxima r).orderEmbOfFin rfl |>.injective he)
  cover := by
    intro x
    obtain ⟨i,hi⟩ : ∃ i : Fin (maxima r).card,
        (maxima r).orderEmbOfFin rfl i = blockMax r x := by
      apply Set.mem_range.mp
      rw [range_orderEmbOfFin]
      exact blockMax_mem r x
    refine ⟨i,?_⟩
    rw [range_orderEmbOfFin]
    apply (mem_block r _ x).mpr
    rw [hi]
    exact r.symm (blockMax_related r x)

lemma range_ofSetoid (r : Setoid (Fin n)) (i : Fin (ofSetoid r).length) :
    Set.range ((ofSetoid r).emb i) = block r ((maxima r).orderEmbOfFin rfl i) :=
  range_orderEmbOfFin _ _

@[simp] lemma partition_ofSetoid (r : Setoid (Fin n)) : partition (ofSetoid r) = r := by
  apply Setoid.ext
  intro x y
  change (ofSetoid r).index x = (ofSetoid r).index y ↔ r x y
  have hx := (relation_iff_range (ofSetoid r) ((ofSetoid r).index x) x).mp rfl
  rw [range_ofSetoid] at hx
  have hy := (relation_iff_range (ofSetoid r) ((ofSetoid r).index y) y).mp rfl
  rw [range_ofSetoid] at hy
  constructor
  · intro h
    rw [← h] at hy
    exact r.trans (r.symm ((mem_block r _ _).mp hx)) ((mem_block r _ _).mp hy)
  · intro h
    apply Eq.symm
    apply (relation_iff_range (ofSetoid r) ((ofSetoid r).index x) y).mpr
    rw [range_ofSetoid]
    exact (mem_block r _ _).mpr (r.trans ((mem_block r _ _).mp hx) h)

def partitionEquiv (n : ℕ) : OrderedFinpartition n ≃ Setoid (Fin n) :=
  Equiv.ofBijective partition ⟨partition_injective,fun r => ⟨ofSetoid r,partition_ofSetoid r⟩⟩

end BelowOne.OrderedPartitionBridge
end
end

 
section

 
noncomputable section
open Finset Filter
open scoped Topology
namespace BelowOne.PartitionCumulant

lemma iteratedDeriv_succ_log {n : ℕ} {x : ℝ} (hx : x ≠ 0) :
    iteratedDeriv (n+1) Real.log x = (-1 : ℝ)^n * n.factorial * x^(-(n:ℤ)-1) := by
  have heq : deriv Real.log =ᶠ[𝓝 x] Inv.inv := by
    filter_upwards [eventually_ne_nhds hx] with y hy
    simp [Real.deriv_log]
  rw [iteratedDeriv_succ',heq.iteratedDeriv_eq,iteratedDeriv_eq_iterate,iter_deriv_inv]
  grind

lemma iteratedDeriv_log_one {n : ℕ} (hn : 0 < n) :
    iteratedDeriv n Real.log 1 = coefficient n := by
  obtain ⟨k,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_zero_of_lt hn)
  simp [iteratedDeriv_succ_log (by norm_num : (1:ℝ) ≠ 0),coefficient]

variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]

lemma iteratedFDeriv_log_comp {f : E → ℝ} {x : E} {n : ℕ}
    (hf : ContDiffAt ℝ n f x) (hx : f x = 1) (hn : 0 < n) (v : Fin n → E) :
    iteratedFDeriv ℝ n (Real.log ∘ f) x v =
      ∑ c : OrderedFinpartition n, coefficient c.length *
        ∏ i : Fin c.length, iteratedFDeriv ℝ (c.partSize i) f x (v ∘ c.emb i) := by
  have hg : ContDiffAt ℝ n Real.log (f x) := by
    rw [hx]
    exact Real.contDiffAt_log.mpr (by norm_num)
  rw [iteratedFDeriv_comp hg hf (le_refl _)]
  simp only [FormalMultilinearSeries.taylorComp,_root_.sum_apply,
    FormalMultilinearSeries.compAlongOrderedFinpartition_apply,ftaylorSeries]
  apply sum_congr rfl
  intro c hc
  have hcl : 0 < c.length := Nat.pos_of_ne_zero (fun h => by
    have hi := (c.index ⟨0,hn⟩).isLt
    omega)
  rw [hx,iteratedFDeriv_apply_eq_iteratedDeriv_mul_prod,
    iteratedDeriv_log_one hcl]
  simp only [smul_eq_mul,mul_comm]
  rfl

 
def blockDerivative {n : ℕ} (f : E → ℝ) (x : E) (v : Fin n → E)
    (r : Setoid (Fin n)) : ℝ :=
  let c := OrderedPartitionBridge.ofSetoid r
  ∏ i : Fin c.length, iteratedFDeriv ℝ (c.partSize i) f x (v ∘ c.emb i)

lemma logarithmic_derivative_partition_sum {f : E → ℝ} {x : E} {n : ℕ}
    (hf : ContDiffAt ℝ n f x) (hx : f x = 1) (hn : 0 < n) (v : Fin n → E) :
    iteratedFDeriv ℝ n (Real.log ∘ f) x v =
      ∑ r : Setoid (Fin n), coefficient (Nat.card (Quotient r)) * blockDerivative f x v r := by
  rw [iteratedFDeriv_log_comp hf hx hn]
  apply Fintype.sum_equiv (OrderedPartitionBridge.partitionEquiv n)
  intro c
  have hc : OrderedPartitionBridge.ofSetoid (OrderedPartitionBridge.partition c) = c := by
    apply OrderedPartitionBridge.partition_injective
    simp
  change coefficient c.length * _ = coefficient
    (Nat.card (Quotient (OrderedPartitionBridge.partition c))) * _
  rw [OrderedPartitionBridge.card_partition]
  change _ = coefficient c.length * blockDerivative f x v (OrderedPartitionBridge.partition c)
  unfold blockDerivative
  rw [hc]

end BelowOne.PartitionCumulant
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical ContDiff
namespace BelowOne.FKPressure
variable {V I J : Type*} [Fintype V]

 

def siteMoment (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (T : Set V) : ℝ :=
  ∑ A ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset,
  ∑ B ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset,
  ∑ L ∈ (H.filter (fun e => (hardEnds e).1 ∈ T ∧ (hardEnds e).2 ∈ T)).powerset,
    (∏ e ∈ A, v e) * (∏ e ∈ B, v e) *
      s ^ Nat.card ((graph (⊥ : SimpleGraph V) ends A).induce T).ConnectedComponent * (-1 : ℝ)^L.card

omit [Fintype V] in
lemma siteMoment_empty (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) :
    siteMoment ends hardEnds E H s v ∅ = 1 := by
  simp [siteMoment,Nat.card_eq_fintype_card]

omit [Fintype V] in
lemma block_edge_iff (r : Setoid V) (ends : I → V × V) (q : Quotient r) (e : I) :
    r (ends e).1 (ends e).2 ∧ edgeBlock r ends e = q ↔
      (ends e).1 ∈ Quotient.mk r ⁻¹' {q} ∧ (ends e).2 ∈ Quotient.mk r ⁻¹' {q} := by
  change (r (ends e).1 (ends e).2 ∧ Quotient.mk r (ends e).1 = q) ↔
    Quotient.mk r (ends e).1 = q ∧ Quotient.mk r (ends e).2 = q
  constructor
  · rintro ⟨h,e⟩
    exact ⟨e,(Quotient.sound h).symm.trans e⟩
  · rintro ⟨h₁,h₂⟩
    exact ⟨Quotient.exact (h₁.trans h₂.symm),h₁⟩

omit [Fintype V] in
lemma inducedBlockMoment_eq_siteMoment (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (r : Setoid V) (q : Quotient r) :
    inducedBlockMoment ⊥ ends hardEnds E H s v r q =
      siteMoment ends hardEnds E H s v (Quotient.mk r ⁻¹' {q}) := by
  unfold inducedBlockMoment siteMoment
  simp_rw [block_edge_iff]

omit [Fintype V] in
lemma bot_reachable_le (r : Setoid V) : (⊥ : SimpleGraph V).reachableSetoid ≤ r := by
  intro x y h
  have hx := SimpleGraph.reachable_bot.mp h
  exact hx ▸ r.refl x

lemma blockStructureSum_siteMoment (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (r : Setoid V) :
    blockStructureSum ⊥ ends hardEnds E H s v r =
      ∏ q : Quotient r, siteMoment ends hardEnds E H s v (Quotient.mk r ⁻¹' {q}) := by
  rw [blockStructureSum_factorization _ ends hardEnds E H s v r (bot_reachable_le r)]
  simp_rw [inducedBlockMoment_eq_siteMoment]

def sitePolynomial (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) : (V → ℝ) → ℝ :=
  MultiaffineDerivative.polynomial (fun T : Finset V => siteMoment ends hardEnds E H s v T)

lemma sitePolynomial_zero (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) :
    sitePolynomial ends hardEnds E H s v 0 = 1 := by
  rw [sitePolynomial,MultiaffineDerivative.polynomial_zero]
  simpa only [Finset.coe_empty] using siteMoment_empty ends hardEnds E H s v

lemma coe_image_emb {n : ℕ} (c : OrderedFinpartition n) (i : Fin c.length) :
    ((univ.image (c.emb i) : Finset (Fin n)) : Set (Fin n)) = Set.range (c.emb i) := by
  ext x
  simp

lemma product_moments_partition {n : ℕ}
    (W : Set (Fin n) → ℝ) (c : OrderedFinpartition n) :
    (∏ q : Quotient (OrderedPartitionBridge.partition c),
      W (Quotient.mk (OrderedPartitionBridge.partition c) ⁻¹' {q})) =
        ∏ i : Fin c.length, W (Set.range (c.emb i)) := by
  unfold OrderedPartitionBridge.partition
  let e := Setoid.quotientKerEquivOfSurjective c.index
    (OrderedPartitionBridge.index_surjective c)
  apply Fintype.prod_equiv e
  intro q
  congr 1
  ext x
  change Quotient.mk (Setoid.ker c.index) x = q ↔ _
  constructor
  · intro hx
    exact (OrderedPartitionBridge.relation_iff_range c (e q) x).mp (congrArg e hx)
  · intro hx
    apply e.injective
    exact (OrderedPartitionBridge.relation_iff_range c (e q) x).mpr hx

lemma blockDerivative_sitePolynomial {n : ℕ}
    (ends : I → Fin n × Fin n) (hardEnds : J → Fin n × Fin n)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (r : Setoid (Fin n)) :
    PartitionCumulant.blockDerivative (sitePolynomial ends hardEnds E H s v) 0
      MultiaffineDerivative.coordinate r =
        blockStructureSum ⊥ ends hardEnds E H s v r := by
  unfold PartitionCumulant.blockDerivative
  let c := OrderedPartitionBridge.ofSetoid r
  have hp : OrderedPartitionBridge.partition c = r := OrderedPartitionBridge.partition_ofSetoid r
  have heq : (∏ q : Quotient r, siteMoment ends hardEnds E H s v (Quotient.mk r ⁻¹' {q})) =
      ∏ i : Fin c.length, siteMoment ends hardEnds E H s v (Set.range (c.emb i)) := by
    rw [← hp]
    exact product_moments_partition _ c
  rw [blockStructureSum_siteMoment,heq]
  change (∏ i : Fin c.length, _) = _
  apply prod_congr rfl
  intro i hi
  rw [sitePolynomial,MultiaffineDerivative.polynomial_derivative,
    ite_eq_left (c.emb_strictMono i).injective]
  simp only [Finset.coe_image,Finset.coe_univ,Set.image_univ,c]

 

theorem sitePolynomial_log_derivative {n : ℕ} (hn : 0 < n)
    (ends : I → Fin n × Fin n) (hardEnds : J → Fin n × Fin n)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) :
    iteratedFDeriv ℝ n (Real.log ∘ sitePolynomial ends hardEnds E H s v) 0
      MultiaffineDerivative.coordinate = connectedSum ⊥ ends hardEnds E H s v := by
  have : NeZero n := ⟨Nat.ne_zero_of_lt hn⟩
  have hf : ContDiffAt ℝ n (sitePolynomial ends hardEnds E H s v) 0 :=
    (MultiaffineDerivative.contDiff_polynomial _ n).contDiffAt
  rw [PartitionCumulant.logarithmic_derivative_partition_sum hf
    (sitePolynomial_zero ends hardEnds E H s v) hn]
  simp_rw [blockDerivative_sitePolynomial]
  exact exponential_cancellation ⊥ ends hardEnds E H s v

theorem sitePolynomial_log_derivative_nonpos {n : ℕ} (hn : 0 < n)
    (ends : I → Fin n × Fin n) (hardEnds : J → Fin n × Fin n)
    (E : Finset I) (H : Finset J) {u : ℝ} (hu : 0 < u) (hu1 : u < 1)
    (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    iteratedFDeriv ℝ n (Real.log ∘ sitePolynomial ends hardEnds E H (-u) v) 0
      MultiaffineDerivative.coordinate ≤ 0 := by
  rw [sitePolynomial_log_derivative hn]
  exact connectedSum_nonpos ⊥ ends hardEnds E H hu hu1 v hv

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical
namespace BelowOne.FKPressure
variable {V I J : Type*} [Fintype V]

def inducedPartition (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (T : Set V) : ℝ :=
  ∑ A ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset,
    (∏ e ∈ A, v e) *
      s ^ Nat.card ((graph (⊥ : SimpleGraph V) ends A).induce T).ConnectedComponent

lemma alternating_powerset (H : Finset J) :
    (∑ L ∈ H.powerset, (-1 : ℝ)^L.card) = if H = ∅ then 1 else 0 := by
  have hh : (∑ L ∈ H.powerset, (-1 : ℝ)^L.card) = ∏ h ∈ H, (1 + (-1 : ℝ)) := by
    simpa only [prod_const] using (prod_one_add (f := fun _ : J => (-1:ℝ)) H).symm
  rw [hh]
  by_cases h : H = ∅
  · simp [h]
  · rw [ite_eq_right h]
    obtain ⟨e,he⟩ := nonempty_iff_ne_empty.mpr h
    exact prod_eq_zero he (by norm_num)

omit [Fintype V] in
lemma siteMoment_separate (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (T : Set V) :
    siteMoment ends hardEnds E H s v T =
      inducedPartition ends E s v T *
        (∑ B ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset,
          ∏ e ∈ B, v e) *
        (∑ L ∈ (H.filter (fun e => (hardEnds e).1 ∈ T ∧ (hardEnds e).2 ∈ T)).powerset,
          (-1:ℝ)^L.card) := by
  unfold siteMoment inducedPartition
  rw [sum_mul,sum_mul]
  apply sum_congr rfl
  intro A hA
  rw [show (∏ e ∈ A, v e) * s ^ Nat.card (induce T (graph ⊥ ends A)).ConnectedComponent *
      (∑ B ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset, ∏ e ∈ B, v e) =
      ∑ B ∈ (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)).powerset,
        (∏ e ∈ A, v e) * s ^ Nat.card (induce T (graph ⊥ ends A)).ConnectedComponent * (∏ e ∈ B, v e) from mul_sum _ _ _]
  rw [sum_mul]
  apply sum_congr rfl
  intro B hB
  rw [mul_sum]
  apply sum_congr rfl
  intro L hL
  ring

omit [Fintype V] in
lemma siteMoment_squarefree (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (T : Set V) :
    siteMoment ends hardEnds E H s v T =
      if (H.filter (fun e => (hardEnds e).1 ∈ T ∧ (hardEnds e).2 ∈ T)) = ∅ then
        (∏ e ∈ E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T), (1 + v e)) *
          inducedPartition ends E s v T else 0 := by
  rw [siteMoment_separate,alternating_powerset,← prod_one_add]
  split_ifs <;> ring

omit [Fintype V] in
lemma siteMoment_collision_zero (ends : I → V × V) (hardEnds : J → V × V)
    (E : Finset I) (H : Finset J) (s : ℝ) (v : I → ℝ) (T : Set V)
    {e : J} (he : e ∈ H) (h₁ : (hardEnds e).1 ∈ T) (h₂ : (hardEnds e).2 ∈ T) :
    siteMoment ends hardEnds E H s v T = 0 := by
  rw [siteMoment_squarefree,ite_eq_right]
  exact nonempty_iff_ne_empty.mp ⟨e,mem_filter.mpr ⟨he,h₁,h₂⟩⟩

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical
namespace BelowOne.FKPressure
variable {V W I J : Type*} [Fintype V] [Fintype W]

def imageEquiv (f : V → W) (T : Set V) (hf : Set.InjOn f T) : T ≃ f '' T :=
  Equiv.ofBijective (fun x => ⟨f x, Set.mem_image_of_mem f x.property⟩) (by
    constructor
    · intro x y h
      exact Subtype.ext (hf x.property y.property (congrArg Subtype.val h))
    · rintro ⟨y,x,hx,rfl⟩
      exact ⟨⟨x,hx⟩,rfl⟩)

omit [Fintype V] [Fintype W] in
lemma induced_graph_image_iso (f : V → W) (T : Set V) (hf : Set.InjOn f T)
    (ends : I → V × V) (ends' : J → W × W) (g : I → J) (A : Finset I)
    (hT : ∀ e ∈ A, (ends e).1 ∈ T ∧ (ends e).2 ∈ T)
    (he : ∀ e ∈ A, ends' (g e) = (f (ends e).1, f (ends e).2)) :
    Nonempty ((graph ⊥ ends A).induce T ≃g
      (graph ⊥ ends' (A.image g)).induce (f '' T)) := by
  refine ⟨{ toEquiv := imageEquiv f T hf, map_rel_iff' := ?_ }⟩
  intro x y
  change (graph ⊥ ends' (A.image g)).Adj (f x) (f y) ↔ (graph ⊥ ends A).Adj x y
  simp only [graph_adj_iff,SimpleGraph.bot_adj,false_or]
  constructor
  · rintro ⟨j,hj,hxy⟩
    obtain ⟨e,heA,rfl⟩ := mem_image.mp hj
    rw [he e heA] at hxy
    obtain ⟨h,hne⟩ := (edge_adj _ _ _ _).mp hxy
    refine ⟨e,heA,(edge_adj _ _ _ _).mpr ⟨?_,?_⟩⟩
    · rcases h with ⟨hx,hy⟩ | ⟨hx,hy⟩
      · exact Or.inl ⟨hf x.property (hT e heA).1 hx,hf y.property (hT e heA).2 hy⟩
      · exact Or.inr ⟨hf x.property (hT e heA).2 hx,hf y.property (hT e heA).1 hy⟩
    · intro h
      exact hne (congrArg f h)
  · rintro ⟨e,heA,hxy⟩
    refine ⟨g e,mem_image_of_mem g heA,?_⟩
    rw [he e heA]
    obtain ⟨h,hne⟩ := (edge_adj _ _ _ _).mp hxy
    apply (edge_adj _ _ _ _).mpr
    constructor
    · rcases h with ⟨hx,hy⟩ | ⟨hx,hy⟩
      · exact Or.inl ⟨congrArg f hx,congrArg f hy⟩
      · exact Or.inr ⟨congrArg f hx,congrArg f hy⟩
    · exact fun h => hne (hf x.property y.property h)

omit [Fintype V] [Fintype W] in
lemma induced_component_image (f : V → W) (T : Set V) (hf : Set.InjOn f T)
    (ends : I → V × V) (ends' : J → W × W) (g : I → J) (A : Finset I)
    (hT : ∀ e ∈ A, (ends e).1 ∈ T ∧ (ends e).2 ∈ T)
    (he : ∀ e ∈ A, ends' (g e) = (f (ends e).1, f (ends e).2)) :
    Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent =
      Nat.card ((graph ⊥ ends' (A.image g)).induce (f '' T)).ConnectedComponent := by
  obtain ⟨e⟩ := induced_graph_image_iso f T hf ends ends' g A hT he
  exact Nat.card_congr e.connectedComponentEquiv

omit [Fintype V] [Fintype W] in
lemma inducedPartition_reindex (f : V → W) (T : Set V) (hf : Set.InjOn f T)
    (ends : I → V × V) (ends' : J → W × W) (g : I → J) (E : Finset I)
    (s : ℝ) (v : J → ℝ)
    (hT : ∀ e ∈ E, (ends e).1 ∈ T ∧ (ends e).2 ∈ T)
    (he : ∀ e ∈ E, ends' (g e) = (f (ends e).1, f (ends e).2))
    (hg : Set.InjOn g E) :
    inducedPartition ends E s (v ∘ g) T =
      inducedPartition ends' (E.image g) s v (f '' T) := by
  have hfilter : E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T) = E := by
    exact filter_eq_self.mpr hT
  have hfilter' : (E.image g).filter (fun e => (ends' e).1 ∈ f '' T ∧
      (ends' e).2 ∈ f '' T) = E.image g := by
    apply filter_eq_self.mpr
    intro j hj
    obtain ⟨e,heE,rfl⟩ := mem_image.mp hj
    rw [he e heE]
    exact ⟨Set.mem_image_of_mem f (hT e heE).1,Set.mem_image_of_mem f (hT e heE).2⟩
  unfold inducedPartition
  rw [hfilter,hfilter',powerset_image,sum_image]
  · apply sum_congr rfl
    intro A hA
    have hAE := mem_powerset.mp hA
    rw [induced_component_image f T hf ends ends' g A
      (fun e heA => hT e (hAE heA)) (fun e heA => he e (hAE heA)),prod_image]
    · rfl
    · intro a ha b hb hab
      exact hg (hAE ha) (hAE hb) hab
  · intro A hA B hB hAB
    exact (Finset.image_eq_image_iff_of_injOn hg
      (mem_powerset.mp hA) (mem_powerset.mp hB)).mp hAB

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V U I : Type*} [Fintype V] [Fintype U] [LinearOrder U]

def liftEdges (τ : U → V) (ends : I → V × V) (E : Finset I) : Finset (I × U × U) :=
  (E ×ˢ (univ ×ˢ univ)).filter (fun e => τ e.2.1 = (ends e.1).1 ∧ τ e.2.2 = (ends e.1).2)

def hardEdges (τ : U → V) : Finset (U × U) :=
  univ.filter (fun e => e.1 < e.2 ∧ τ e.1 = τ e.2)

def squarefreeMoment (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (T : Set V) : ℝ :=
  (∏ e ∈ E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T), (1 + v e)) *
    inducedPartition ends E s v T

omit [Fintype V] [LinearOrder U] in
lemma mem_liftEdges (τ : U → V) (ends : I → V × V) (E : Finset I) (e : I × U × U) :
    e ∈ liftEdges τ ends E ↔ e.1 ∈ E ∧ τ e.2.1 = (ends e.1).1 ∧ τ e.2.2 = (ends e.1).2 := by
  simp [liftEdges]

omit [Fintype V] [LinearOrder U] in
lemma liftEdges_filter_image (τ : U → V) (ends : I → V × V) (E : Finset I) (T : Set U) :
    ((liftEdges τ ends E).filter (fun e => e.2.1 ∈ T ∧ e.2.2 ∈ T)).image Prod.fst =
      E.filter (fun e => (ends e).1 ∈ τ '' T ∧ (ends e).2 ∈ τ '' T) := by
  ext e
  constructor
  · intro h
    obtain ⟨⟨d,i,j⟩,hlift,heq⟩ := mem_image.mp h
    change d = e at heq
    subst e
    obtain ⟨he,hij⟩ := mem_filter.mp hlift
    obtain ⟨hd,hi,hj⟩ := (mem_liftEdges _ _ _ _).mp he
    exact mem_filter.mpr ⟨hd,⟨i,hij.1,hi⟩,⟨j,hij.2,hj⟩⟩
  · intro h
    obtain ⟨he,⟨i,hi,hei⟩,⟨j,hj,hej⟩⟩ := mem_filter.mp h
    exact mem_image.mpr ⟨(e,i,j),mem_filter.mpr
      ⟨(mem_liftEdges _ _ _ _).mpr ⟨he,hei,hej⟩,hi,hj⟩,rfl⟩

omit [Fintype V] [LinearOrder U] in
lemma liftEdges_filter_inj (τ : U → V) (ends : I → V × V) (E : Finset I)
    (T : Set U) (hτ : Set.InjOn τ T) :
    Set.InjOn Prod.fst (↑((liftEdges τ ends E).filter (fun e => e.2.1 ∈ T ∧ e.2.2 ∈ T)) : Set (I × U × U)) := by
  rintro ⟨e,i,j⟩ h ⟨e',i',j'⟩ h' he
  obtain ⟨hm,hi,hj⟩ := mem_filter.mp h
  obtain ⟨hm',hi',hj'⟩ := mem_filter.mp h'
  obtain ⟨_,hei,hej⟩ := (mem_liftEdges _ _ _ _).mp hm
  obtain ⟨_,hei',hej'⟩ := (mem_liftEdges _ _ _ _).mp hm'
  change e = e' at he
  subst e'
  have hii := hτ hi hi' (hei.trans hei'.symm)
  have hjj := hτ hj hj' (hej.trans hej'.symm)
  exact Prod.ext rfl (Prod.ext hii hjj)

omit [Fintype V] in
lemma hardEdges_filter_empty_iff (τ : U → V) (T : Set U) :
    (hardEdges τ).filter (fun e => e.1 ∈ T ∧ e.2 ∈ T) = ∅ ↔ Set.InjOn τ T := by
  rw [Finset.eq_empty_iff_forall_notMem]
  simp only [mem_filter,hardEdges,mem_univ,true_and]
  constructor
  · intro h a ha b hb hab
    by_contra hne
    rcases lt_or_gt_of_ne hne with hab' | hba'
    · exact h (a,b) ⟨⟨hab',hab⟩,ha,hb⟩
    · exact h (b,a) ⟨⟨hba',hab.symm⟩,hb,ha⟩
  · intro h e he
    exact (ne_of_lt he.1.1) (h he.2.1 he.2.2 he.1.2)

omit [Fintype V] in
lemma inducedPartition_filter (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (T : Set V) :
    inducedPartition ends (E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)) s v T =
      inducedPartition ends E s v T := by
  unfold inducedPartition
  rw [filter_filter]
  simp only [and_self]

omit [Fintype V] in
lemma siteMoment_lift (τ : U → V) (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (T : Set U) :
    siteMoment Prod.snd id (liftEdges τ ends E) (hardEdges τ) s (v ∘ Prod.fst) T =
      if Set.InjOn τ T then squarefreeMoment ends E s v (τ '' T) else 0 := by
  rw [siteMoment_squarefree]
  simp only [id_eq]
  simp only [hardEdges_filter_empty_iff]
  split_ifs with hτ
  · let F := (liftEdges τ ends E).filter (fun e => e.2.1 ∈ T ∧ e.2.2 ∈ T)
    have hF : ∀ e ∈ F, e.2.1 ∈ T ∧ e.2.2 ∈ T := fun e he => (mem_filter.mp he).2
    have he : ∀ e ∈ F, ends (Prod.fst e) = (τ e.2.1,τ e.2.2) := by
      intro e he
      have h := (mem_liftEdges _ _ _ _).mp (mem_filter.mp he).1
      exact Prod.ext h.2.1.symm h.2.2.symm
    have hpart := inducedPartition_reindex τ T hτ Prod.snd ends Prod.fst F s v hF he
      (liftEdges_filter_inj τ ends E T hτ)
    rw [inducedPartition_filter] at hpart
    rw [liftEdges_filter_image] at hpart
    rw [inducedPartition_filter] at hpart
    unfold squarefreeMoment
    rw [hpart]
    congr 1
    rw [← liftEdges_filter_image τ ends E T,prod_image]
    · rfl
    · exact liftEdges_filter_inj τ ends E T hτ
  · rfl

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical
namespace BelowOne.FKPressure
variable {V I : Type*} [Fintype V]

def unscaledSitePolynomial (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) : (V → ℝ) → ℝ :=
  MultiaffineDerivative.polynomial (fun T => squarefreeMoment ends E s v (T : Set V))

lemma unscaledSitePolynomial_zero (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) : unscaledSitePolynomial ends E s v 0 = 1 := by
  rw [unscaledSitePolynomial,MultiaffineDerivative.polynomial_zero]
  simp [squarefreeMoment,inducedPartition,Nat.card_eq_fintype_card]

lemma injOn_range_iff {X Y Z : Type*} (f : Y → Z) (e : X ↪ Y) :
    Set.InjOn f (Set.range e) ↔ Function.Injective (f ∘ e) := by
  constructor
  · intro h x y hxy
    exact e.injective (h ⟨x,rfl⟩ ⟨y,rfl⟩ hxy)
  · intro h x hx y hy hxy
    obtain ⟨a,rfl⟩ := hx
    obtain ⟨b,rfl⟩ := hy
    exact congrArg e (h hxy)

lemma lifted_derivative {n k : ℕ} (τ : Fin n → V) (e : Fin k ↪ Fin n)
    (ends : I → V × V) (E : Finset I) (s : ℝ) (v : I → ℝ) :
    iteratedFDeriv ℝ k (unscaledSitePolynomial ends E s v) 0
      (MultiaffineDerivative.coordinate ∘ (τ ∘ e)) =
        siteMoment Prod.snd id (liftEdges τ ends E) (hardEdges τ) s
          (v ∘ Prod.fst) (Set.range e) := by
  rw [unscaledSitePolynomial,MultiaffineDerivative.polynomial_derivative,
    siteMoment_lift,injOn_range_iff]
  simp only [Finset.coe_image,Finset.coe_univ,Set.image_univ,Set.range_comp]
  split_ifs <;> rfl

lemma blockDerivative_lift {n : ℕ} (τ : Fin n → V)
    (ends : I → V × V) (E : Finset I) (s : ℝ) (v : I → ℝ) (r : Setoid (Fin n)) :
    PartitionCumulant.blockDerivative (unscaledSitePolynomial ends E s v) 0
      (MultiaffineDerivative.coordinate ∘ τ) r =
        PartitionCumulant.blockDerivative
          (sitePolynomial Prod.snd id (liftEdges τ ends E) (hardEdges τ) s (v ∘ Prod.fst)) 0
          MultiaffineDerivative.coordinate r := by
  unfold PartitionCumulant.blockDerivative
  apply prod_congr rfl
  intro i hi
  let c := OrderedPartitionBridge.ofSetoid r
  let e : Fin (c.partSize i) ↪ Fin n := ⟨c.emb i,(c.emb_strictMono i).injective⟩
  change iteratedFDeriv ℝ _ (unscaledSitePolynomial ends E s v) 0
    (MultiaffineDerivative.coordinate ∘ (τ ∘ e)) = _
  rw [lifted_derivative]
  rw [sitePolynomial,MultiaffineDerivative.polynomial_derivative,
    ite_eq_left (c.emb_strictMono i).injective]
  simp only [Finset.coe_image,Finset.coe_univ,Set.image_univ]
  rfl

theorem unscaledSitePolynomial_log_derivative {n : ℕ} (hn : 0 < n)
    (τ : Fin n → V) (ends : I → V × V) (E : Finset I) (s : ℝ) (v : I → ℝ) :
    iteratedFDeriv ℝ n (Real.log ∘ unscaledSitePolynomial ends E s v) 0
      (MultiaffineDerivative.coordinate ∘ τ) =
        connectedSum ⊥ Prod.snd id (liftEdges τ ends E) (hardEdges τ) s (v ∘ Prod.fst) := by
  rw [← sitePolynomial_log_derivative hn]
  have hf : ContDiffAt ℝ n (unscaledSitePolynomial ends E s v) 0 :=
    (MultiaffineDerivative.contDiff_polynomial _ n).contDiffAt
  have hg : ContDiffAt ℝ n (sitePolynomial Prod.snd id (liftEdges τ ends E)
      (hardEdges τ) s (v ∘ Prod.fst)) 0 :=
    (MultiaffineDerivative.contDiff_polynomial _ n).contDiffAt
  rw [PartitionCumulant.logarithmic_derivative_partition_sum hf
    (unscaledSitePolynomial_zero ends E s v) hn,
    PartitionCumulant.logarithmic_derivative_partition_sum hg
    (sitePolynomial_zero _ _ _ _ _ _) hn]
  simp_rw [blockDerivative_lift]

theorem unscaledSitePolynomial_log_derivative_nonpos {n : ℕ} (hn : 0 < n)
    (τ : Fin n → V) (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    iteratedFDeriv ℝ n (Real.log ∘ unscaledSitePolynomial ends E (-u) v) 0
      (MultiaffineDerivative.coordinate ∘ τ) ≤ 0 := by
  have : NeZero n := ⟨Nat.ne_zero_of_lt hn⟩
  rw [unscaledSitePolynomial_log_derivative hn]
  apply connectedSum_nonpos ⊥ Prod.snd id _ _ hu hu1
  intro e he
  exact hv e.1 ((mem_liftEdges _ _ _ _).mp he).1

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset
namespace BelowOne.ComponentProduct
attribute [local instance] Classical.propDecidable
variable {V Q : Type*} [Fintype V] [Fintype Q]

def saturated (f : V → Q) (T : Finset V) : Prop :=
  ∀ a b, f a = f b → (a ∈ T ↔ b ∈ T)

def preimage (f : V → Q) (J : Finset Q) : Finset V := univ.filter (fun v => f v ∈ J)

omit [Fintype Q] in
lemma mem_preimage (f : V → Q) (J : Finset Q) (v : V) :
    v ∈ preimage f J ↔ f v ∈ J := by simp [preimage]

omit [Fintype Q] in
lemma preimage_saturated (f : V → Q) (J : Finset Q) : saturated f (preimage f J) := by
  intro a b hab
  simp only [mem_preimage,hab]

omit [Fintype Q] in
lemma image_preimage (f : V → Q) (hf : Function.Surjective f) (J : Finset Q) :
    (preimage f J).image f = J := by
  ext q
  simp only [mem_image,mem_preimage]
  constructor
  · rintro ⟨v,hv,rfl⟩
    exact hv
  · intro hq
    obtain ⟨v,rfl⟩ := hf q
    exact ⟨v,hq,rfl⟩

omit [Fintype Q] in
lemma preimage_image (f : V → Q) (T : Finset V) (hT : saturated f T) :
    preimage f (T.image f) = T := by
  ext v
  simp only [mem_preimage,mem_image]
  constructor
  · rintro ⟨w,hw,hwv⟩
    exact (hT w v hwv).mp hw
  · intro hv
    exact ⟨v,hv,rfl⟩

omit [Fintype Q] in
lemma selected_product (f : V → Q) (J : Finset Q) (s : ℝ) (x : V → ℝ) :
    (∏ q ∈ J, s * ∏ v ∈ univ.filter (fun v => f v = q), x v) =
      s ^ J.card * ∏ v ∈ preimage f J, x v := by
  rw [prod_mul_distrib,prod_const]
  congr 1
  exact Finset.prod_fiberwise_eq_prod_filter univ J f x

lemma expansion (f : V → Q) (hf : Function.Surjective f) (s : ℝ) (x : V → ℝ) :
    (∏ q : Q, (1 + s * ∏ v ∈ univ.filter (fun v => f v = q), x v)) =
      ∑ T : Finset V, if saturated f T then
        s ^ (T.image f).card * ∏ v ∈ T, x v else 0 := by
  rw [prod_one_add]
  symm
  rw [← sum_filter]
  symm
  apply sum_bij (fun J hJ => preimage f J)
  · intro J hJ
    exact mem_filter.mpr ⟨mem_univ _,preimage_saturated f J⟩
  · intro J hJ K hK hJK
    have h := congrArg (Finset.image f) hJK
    simpa only [image_preimage f hf] using h
  · intro T hT
    refine ⟨T.image f,mem_powerset.mpr (subset_univ _),?_⟩
    exact preimage_image f T (mem_filter.mp hT).2
  · intro J hJ
    rw [selected_product,image_preimage f hf]

end BelowOne.ComponentProduct
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V I : Type*} [Fintype V]

omit [Fintype V] in
lemma reachable_induce_saturated (G : SimpleGraph V) (T : Finset V)
    (hT : ComponentProduct.saturated G.connectedComponentMk T) (x y : (T : Set V)) :
    (G.induce T).Reachable x y ↔ G.Reachable x.1 y.1 := by
  constructor
  · exact fun h => h.map (SimpleGraph.Embedding.induce _).toHom
  · rintro ⟨p⟩
    have hp : ∀ z ∈ p.support, z ∈ (T : Set V) := by
      intro z hz
      have he : G.connectedComponentMk x = G.connectedComponentMk z :=
        ConnectedComponent.sound ⟨p.takeUntil z hz⟩
      exact (hT x z he).mp x.property
    exact ⟨p.induce (T : Set V) hp⟩

omit [Fintype V] in
lemma component_card_induce_saturated (G : SimpleGraph V) (T : Finset V)
    (hT : ComponentProduct.saturated G.connectedComponentMk T) :
    Nat.card (G.induce T).ConnectedComponent = (T.image G.connectedComponentMk).card := by
  let f : ↥(T : Set V) → G.ConnectedComponent := fun x => G.connectedComponentMk x
  have hr : (G.induce T).reachableSetoid = Setoid.ker f := by
    apply Setoid.ext
    intro x y
    change (G.induce T).Reachable x y ↔ G.connectedComponentMk x = G.connectedComponentMk y
    rw [reachable_induce_saturated G T hT]
    exact ⟨ConnectedComponent.sound,ConnectedComponent.exact⟩
  have him : Set.range f = (↑(T.image G.connectedComponentMk) : Set G.ConnectedComponent) := by
    ext q
    simp only [Set.mem_range,Finset.mem_coe,mem_image,f]
    constructor
    · rintro ⟨x,rfl⟩
      exact ⟨x,x.property,rfl⟩
    · rintro ⟨x,hx,rfl⟩
      exact ⟨⟨x,hx⟩,rfl⟩
  change Nat.card (Quotient (G.induce T).reachableSetoid) = _
  rw [hr,Nat.card_congr (Setoid.quotientKerEquivRange f),him,Nat.card_eq_fintype_card]
  exact Fintype.card_coe _

omit [Fintype V] in
lemma saturated_graph_iff (ends : I → V × V) (A : Finset I) (T : Finset V) :
    ComponentProduct.saturated (graph ⊥ ends A).connectedComponentMk T ↔
      ∀ e ∈ A, ((ends e).1 ∈ T ↔ (ends e).2 ∈ T) := by
  constructor
  · intro h e he
    exact h _ _ (ConnectedComponent.sound (endpoints_reachable ⊥ ends he))
  · intro h a b hab
    have hr : (graph ⊥ ends A).Reachable a b := ConnectedComponent.exact hab
    obtain ⟨p⟩ := hr
    clear hab
    induction p with
    | nil => rfl
    | @cons a b c hab p ih =>
      have heq : a ∈ T ↔ b ∈ T := by
        obtain hfalse | ⟨e,he,hxy⟩ := (graph_adj_iff ⊥ ends A a b).mp hab
        · exact False.elim hfalse
        obtain ⟨hxy,_⟩ := (edge_adj _ _ _ _).mp hxy
        rcases hxy with ⟨rfl,rfl⟩ | ⟨rfl,rfl⟩
        · exact h e he
        · exact (h e he).symm
      exact heq.trans ih

def componentProduct (G : SimpleGraph V) (s : ℝ) (x : V → ℝ) : ℝ :=
  ∏ c : G.ConnectedComponent, (1 + s *
    ∏ v ∈ univ.filter (fun v => G.connectedComponentMk v = c), x v)

lemma componentProduct_expansion (ends : I → V × V) (A : Finset I)
    (s : ℝ) (x : V → ℝ) :
    componentProduct (graph ⊥ ends A) s x =
      ∑ T : Finset V, if ∀ e ∈ A, ((ends e).1 ∈ T ↔ (ends e).2 ∈ T) then
        s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent * ∏ v ∈ T, x v else 0 := by
  rw [componentProduct,ComponentProduct.expansion
    (graph ⊥ ends A).connectedComponentMk (fun q => Quot.mk_surjective q)]
  apply sum_congr rfl
  intro T hT
  by_cases h : ComponentProduct.saturated (graph ⊥ ends A).connectedComponentMk T
  · rw [ite_eq_left h,ite_eq_left ((saturated_graph_iff ends A T).mp h),component_card_induce_saturated _ T h]
  · rw [ite_eq_right h,ite_eq_right (fun hh => h ((saturated_graph_iff ends A T).mpr hh))]

end BelowOne.FKPressure
end
end

 
section

 
noncomputable section
open Finset
namespace BelowOne.SubsetSplitting
attribute [local instance] Classical.propDecidable
variable {I : Type*}

lemma sum_factorization_binary (E : Finset I) (p : I → Prop) (W W₁ W₂ : Finset I → ℝ)
    (hW : ∀ A ⊆ E, W A = W₁ (A.filter p) * W₂ (A.filter (fun e => ¬p e))) :
    (∑ A ∈ E.powerset, W A) =
      (∑ A ∈ (E.filter p).powerset, W₁ A) *
        (∑ A ∈ (E.filter (fun e => ¬p e)).powerset, W₂ A) := by
  have h := sum_factorization E (fun e => decide (p e)) W
    (fun b A => if b then W₁ A else W₂ A) (by
      intro A hA
      rw [Fintype.prod_bool]
      simpa only [Bool.false_eq_true,ite_false,ite_true,decide_eq_false_iff_not,
        decide_eq_true_eq] using hW A hA)
  simpa only [Fintype.prod_bool,fiber,Bool.false_eq_true,ite_false,ite_true,
    decide_eq_false_iff_not,decide_eq_true_eq,mul_comm] using h

end BelowOne.SubsetSplitting
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V I : Type*} [Fintype V]

omit [Fintype V] in
lemma graph_induce_inside_filter (G : SimpleGraph V) (ends : I → V × V)
    (A : Finset I) (T : Set V) :
    (graph G ends (A.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T))).induce T =
      (graph G ends A).induce T := by
  ext x y
  simp only [SimpleGraph.induce_adj,graph_adj_iff,mem_filter]
  constructor
  · rintro (h | ⟨e,⟨he,_⟩,hxy⟩)
    · exact Or.inl h
    · exact Or.inr ⟨e,he,hxy⟩
  · rintro (h | ⟨e,he,hxy⟩)
    · exact Or.inl h
    · refine Or.inr ⟨e,⟨he,?_⟩,hxy⟩
      rcases (edge_adj _ _ _ _).mp hxy with ⟨⟨hx,hy⟩ | ⟨hx,hy⟩,_⟩
      · exact ⟨hx ▸ x.property,hy ▸ y.property⟩
      · exact ⟨hy ▸ y.property,hx ▸ x.property⟩

omit [Fintype V] in
lemma confined_edge_sum (ends : I → V × V) (E : Finset I) (T : Set V)
    (s : ℝ) (v : I → ℝ) :
    (∑ A ∈ E.powerset, if ∀ e ∈ A, ((ends e).1 ∈ T ↔ (ends e).2 ∈ T) then
      (∏ e ∈ A, v e) * s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent else 0) =
      inducedPartition ends E s v T *
        ∏ e ∈ E.filter (fun e => (ends e).1 ∉ T ∧ (ends e).2 ∉ T), (1 + v e) := by
  trans ∑ A ∈ (E.filter (fun e => (ends e).1 ∈ T ↔ (ends e).2 ∈ T)).powerset,
    (∏ e ∈ A, v e) * s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent
  · have hh := sum_powerset_filter E (fun e => (ends e).1 ∈ T ↔ (ends e).2 ∈ T)
      (fun A => (∏ e ∈ A, v e) * s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent)
    simpa only [MultiaffineDerivative.ite_classical,filter_classical] using hh
  let F := E.filter (fun e => (ends e).1 ∈ T ↔ (ends e).2 ∈ T)
  have h := SubsetSplitting.sum_factorization_binary F
    (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T)
    (fun A => (∏ e ∈ A, v e) * s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent)
    (fun A => (∏ e ∈ A, v e) * s ^ Nat.card ((graph ⊥ ends A).induce T).ConnectedComponent)
    (fun A => ∏ e ∈ A, v e) (by
      intro A hA
      simp only [filter_classical]
      have hg := graph_induce_inside_filter (⊥ : SimpleGraph V) ends A T
      simp only [filter_classical] at hg
      rw [hg]
      rw [← Finset.prod_filter_mul_prod_filter_not A
        (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T) v]
      simp only [filter_classical]
      ring)
  have hi : F.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T) =
      E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T) := by
    ext e
    simp only [F,mem_filter]
    tauto
  have ho : F.filter (fun e => ¬((ends e).1 ∈ T ∧ (ends e).2 ∈ T)) =
      E.filter (fun e => (ends e).1 ∉ T ∧ (ends e).2 ∉ T) := by
    ext e
    simp only [F,mem_filter]
    tauto
  simp only [filter_classical] at h hi ho ⊢
  rw [hi,ho,← prod_one_add] at h
  simpa only [F,inducedPartition,filter_classical] using h

def percolationSitePolynomial (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (x : V → ℝ) : ℝ :=
  (∑ A ∈ E.powerset, (∏ e ∈ A, v e) * componentProduct (graph ⊥ ends A) s x) /
    ∏ e ∈ E, (1 + v e)

def percolationCoefficient (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (T : Finset V) : ℝ :=
  (inducedPartition ends E s v T *
    ∏ e ∈ E.filter (fun e => (ends e).1 ∉ T ∧ (ends e).2 ∉ T), (1 + v e)) /
      ∏ e ∈ E, (1 + v e)

lemma percolationSitePolynomial_eq (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) :
    percolationSitePolynomial ends E s v =
      MultiaffineDerivative.polynomial (percolationCoefficient ends E s v) := by
  funext x
  unfold percolationSitePolynomial
  simp_rw [componentProduct_expansion,mul_sum]
  rw [sum_comm]
  unfold MultiaffineDerivative.polynomial percolationCoefficient
  rw [sum_div]
  apply sum_congr rfl
  intro T hT
  rw [div_mul_eq_mul_div]
  congr 1
  have hh := confined_edge_sum ends E (T : Set V) s v
  simp only [Finset.mem_coe,filter_classical] at hh ⊢
  rw [← hh,sum_mul]
  apply sum_congr rfl
  intro A hA
  split_ifs <;> ring

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset
namespace BelowOne.FKPressure
attribute [local instance] Classical.propDecidable
variable {V I : Type*} [Fintype V]

def incidentProduct (ends : I → V × V) (E : Finset I) (v : I → ℝ) (i : V) : ℝ :=
  ∏ e ∈ E, (if (ends e).1 = i then 1 + v e else 1) *
    (if (ends e).2 = i then 1 + v e else 1)

def incidentScale (ends : I → V × V) (E : Finset I) (v : I → ℝ) (i : V) : ℝ :=
  (incidentProduct ends E v i)⁻¹

omit [Fintype V] in
lemma incidentProduct_pos (ends : I → V × V) (E : Finset I) (v : I → ℝ)
    (hv : ∀ e ∈ E, 0 ≤ v e) (i : V) : 0 < incidentProduct ends E v i := by
  unfold incidentProduct
  apply prod_pos
  intro e he
  apply mul_pos <;> split_ifs <;> linarith [hv e he]

omit [Fintype V] in
lemma incidentProduct_subprod (ends : I → V × V) (E : Finset I)
    (v : I → ℝ) (T : Finset V) :
    (∏ i ∈ T, incidentProduct ends E v i) =
      ∏ e ∈ E, (if (ends e).1 ∈ T then 1 + v e else 1) *
        (if (ends e).2 ∈ T then 1 + v e else 1) := by
  unfold incidentProduct
  rw [prod_comm]
  apply prod_congr rfl
  intro e he
  rw [prod_mul_distrib]
  simp only [prod_ite_eq]

omit [Fintype V] in
lemma incident_product_identity (ends : I → V × V) (E : Finset I)
    (v : I → ℝ) (T : Finset V) :
    (∏ e ∈ E, (1 + v e)) *
        (∏ e ∈ E.filter (fun e => (ends e).1 ∈ T ∧ (ends e).2 ∈ T), (1 + v e)) =
      (∏ i ∈ T, incidentProduct ends E v i) *
        (∏ e ∈ E.filter (fun e => (ends e).1 ∉ T ∧ (ends e).2 ∉ T), (1 + v e)) := by
  rw [incidentProduct_subprod]
  simp only [prod_filter]
  rw [← prod_mul_distrib,← prod_mul_distrib]
  apply prod_congr rfl
  intro e he
  split_ifs <;> simp_all

omit [Fintype V] in
lemma percolationCoefficient_rescale (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) (T : Finset V) :
    percolationCoefficient ends E s v T =
      (∏ i ∈ T, incidentScale ends E v i) * squarefreeMoment ends E s v T := by
  have hP : (∏ e ∈ E, (1 + v e)) ≠ 0 := ne_of_gt (prod_pos (fun e he => by linarith [hv e he]))
  have hD : (∏ i ∈ T, incidentProduct ends E v i) ≠ 0 :=
    ne_of_gt (prod_pos (fun i hi => incidentProduct_pos ends E v hv i))
  have hid := incident_product_identity ends E v T
  unfold percolationCoefficient squarefreeMoment incidentScale
  rw [prod_inv_distrib]
  simp only [Finset.mem_coe,filter_classical] at hid ⊢
  field_simp
  nlinarith [congrArg (fun r : ℝ => r * inducedPartition ends E s v T) hid]

lemma percolationSitePolynomial_rescale (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) (x : V → ℝ) :
    percolationSitePolynomial ends E s v x =
      unscaledSitePolynomial ends E s v (fun i => incidentScale ends E v i * x i) := by
  rw [percolationSitePolynomial_eq]
  unfold unscaledSitePolynomial MultiaffineDerivative.polynomial
  apply sum_congr rfl
  intro T hT
  rw [percolationCoefficient_rescale ends E s v hv T,prod_mul_distrib]
  ring

end BelowOne.FKPressure
end
end

 
section

 
noncomputable section
open Finset
open scoped Classical
namespace BelowOne.MultiaffineDerivative
variable {V : Type*} [Fintype V]

def diagonalEquiv (c : V → ℝ) (hc : ∀ i, c i ≠ 0) : (V → ℝ) ≃L[ℝ] (V → ℝ) where
  toFun x i := c i * x i
  invFun x i := (c i)⁻¹ * x i
  left_inv x := by ext i; simp [hc i]
  right_inv x := by ext i; simp [hc i]
  map_add' x y := by ext i; exact mul_add _ _ _
  map_smul' a x := by ext i; simp only [Pi.smul_apply,smul_eq_mul,RingHom.id_apply]; ring
  continuous_toFun := by fun_prop
  continuous_invFun := by fun_prop

omit [Fintype V] in
lemma diagonalEquiv_coordinate (c : V → ℝ) (hc : ∀ i, c i ≠ 0) (i : V) :
    diagonalEquiv c hc (coordinate i) = c i • coordinate i := by
  ext j
  by_cases h : i = j
  · subst j; simp [diagonalEquiv,coordinate]
  · simp [diagonalEquiv,coordinate,h]

lemma iteratedFDeriv_diagonal (f : (V → ℝ) → ℝ) (c : V → ℝ)
    (hc : ∀ i, c i ≠ 0) (n : ℕ) (τ : Fin n → V) :
    iteratedFDeriv ℝ n (fun x => f (fun i => c i * x i)) 0 (coordinate ∘ τ) =
      (∏ j, c (τ j)) * iteratedFDeriv ℝ n f 0 (coordinate ∘ τ) := by
  have h := (diagonalEquiv c hc).iteratedFDerivWithin_comp_right f
    (s := Set.univ) uniqueDiffOn_univ (x := 0) (Set.mem_univ _) n
  simp only [Set.preimage_univ,iteratedFDerivWithin_univ,map_zero] at h
  change iteratedFDeriv ℝ n (f ∘ diagonalEquiv c hc) 0 (coordinate ∘ τ) = _
  rw [h,ContinuousMultilinearMap.compContinuousLinearMap_apply]
  change iteratedFDeriv ℝ n f 0 (fun j => diagonalEquiv c hc (coordinate (τ j))) = _
  simp_rw [diagonalEquiv_coordinate]
  rw [ContinuousMultilinearMap.map_smul_univ]
  rfl

end BelowOne.MultiaffineDerivative
end
end

 
section

 

noncomputable section
open Finset SimpleGraph
open scoped Classical
namespace BelowOne.FKPressure
variable {V I : Type*} [Fintype V]

lemma percolationSitePolynomial_zero (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    percolationSitePolynomial ends E s v 0 = 1 := by
  rw [percolationSitePolynomial_rescale ends E s v hv]
  simp only [Pi.zero_apply,mul_zero]
  exact unscaledSitePolynomial_zero ends E s v

lemma percolationSitePolynomial_log_derivative_nonpos {n : ℕ} (hn : 0 < n)
    (τ : Fin n → V) (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    iteratedFDeriv ℝ n (Real.log ∘ percolationSitePolynomial ends E (-u) v) 0
      (MultiaffineDerivative.coordinate ∘ τ) ≤ 0 := by
  have heq : Real.log ∘ percolationSitePolynomial ends E (-u) v =
      fun x => (Real.log ∘ unscaledSitePolynomial ends E (-u) v)
        (fun i => incidentScale ends E v i * x i) := by
    funext x
    simp only [Function.comp_apply,percolationSitePolynomial_rescale ends E (-u) v hv]
  rw [heq,MultiaffineDerivative.iteratedFDeriv_diagonal]
  · apply mul_nonpos_of_nonneg_of_nonpos
    · exact prod_nonneg (fun i _ => (inv_pos.mpr (incidentProduct_pos ends E v hv (τ i))).le)
    · exact unscaledSitePolynomial_log_derivative_nonpos hn τ ends E hu hu1 v hv
  · intro i
    exact ne_of_gt (inv_pos.mpr (incidentProduct_pos ends E v hv i))

lemma componentProduct_pos (G : SimpleGraph V) {u : ℝ} (hu : 0 ≤ u)
    (hu1 : u < 1) (x : V → ℝ) (hx : ∀ i, x i ∈ Set.Icc 0 1) :
    0 < componentProduct G (-u) x := by
  unfold componentProduct
  apply prod_pos
  intro c hc
  have hb : (∏ i ∈ univ.filter (fun i => G.connectedComponentMk i = c), x i) ≤ 1 :=
    prod_le_one₀ (fun i _ => (hx i).1) (fun i _ => (hx i).2)
  nlinarith

lemma percolationSitePolynomial_pos (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 ≤ u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e)
    (x : V → ℝ) (hx : ∀ i, x i ∈ Set.Icc 0 1) :
    0 < percolationSitePolynomial ends E (-u) v x := by
  unfold percolationSitePolynomial
  apply div_pos
  · apply sum_pos'
    · intro A hA
      exact mul_nonneg (prod_nonneg (fun e he => hv e ((mem_powerset.mp hA) he)))
        (componentProduct_pos _ hu hu1 x hx).le
    · refine ⟨∅,mem_powerset.mpr (empty_subset E),?_⟩
      simpa only [prod_empty,one_mul] using componentProduct_pos (graph ⊥ ends ∅) hu hu1 x hx
  · exact prod_pos (fun e he => by linarith [hv e he])

end BelowOne.FKPressure
end
end

 
section

 

noncomputable section
open Finset
open scoped Classical
namespace BelowOne.PositiveMultilinear
open MultiaffineDerivative
variable {V : Type*} [Fintype V] {n : ℕ}

lemma sum_coordinates (x : V → ℝ) : ∑ i, x i • coordinate i = x := by
  ext j
  simp [coordinate,Pi.single_apply]

lemma expansion (F : ContinuousMultilinearMap ℝ (fun _ : Fin n => V → ℝ) ℝ)
    (x : Fin n → V → ℝ) :
    F x = ∑ τ : Fin n → V, (∏ j, x j (τ j)) * F (coordinate ∘ τ) := by
  conv_lhs => rw [show x = fun j => ∑ i, x j i • coordinate i by simp only [sum_coordinates]]
  rw [F.map_sum]
  apply sum_congr rfl
  intro τ hτ
  rw [F.map_smul_univ]
  rfl

lemma nonneg (F : ContinuousMultilinearMap ℝ (fun _ : Fin n => V → ℝ) ℝ)
    (hF : ∀ τ : Fin n → V, 0 ≤ F (coordinate ∘ τ)) (x : Fin n → V → ℝ)
    (hx : ∀ j i, 0 ≤ x j i) : 0 ≤ F x := by
  rw [expansion]
  exact sum_nonneg (fun τ _ => mul_nonneg (prod_nonneg (fun j _ => hx j (τ j))) (hF τ))

lemma ones_eq_sum (F : ContinuousMultilinearMap ℝ (fun _ : Fin n => V → ℝ) ℝ) :
    F (fun _ _ => 1) = ∑ τ : Fin n → V, F (coordinate ∘ τ) := by
  rw [expansion]
  simp only [prod_const_one,one_mul]

lemma norm_le_ones (F : ContinuousMultilinearMap ℝ (fun _ : Fin n => V → ℝ) ℝ)
    (hF : ∀ τ : Fin n → V, 0 ≤ F (coordinate ∘ τ)) : ‖F‖ ≤ F (fun _ _ => 1) := by
  apply F.opNorm_le_bound (nonneg F hF _ (fun _ _ => zero_le_one))
  intro x
  rw [expansion,ones_eq_sum,sum_mul]
  refine (norm_sum_le _ _).trans (sum_le_sum ?_)
  intro τ hτ
  rw [norm_mul,Real.norm_of_nonneg (hF τ),mul_comm]
  apply mul_le_mul_of_nonneg_left _ (hF τ)
  rw [norm_prod]
  apply prod_le_prod₀ (fun j _ => norm_nonneg _) (fun j _ => ?_)
  exact norm_le_pi_norm (x j) (τ j)

end BelowOne.PositiveMultilinear
end
end

 
section

 

noncomputable section
open scoped Topology NNReal ENNReal
open Filter Set FormalMultilinearSeries
namespace BelowOne.PositiveSeries

lemma scalar_apply_nonneg (c : ℕ → ℝ) (hc : ∀ n, 0 ≤ c n)
    (n : ℕ) (v : Fin n → ℝ) (hv : ∀ i, 0 ≤ v i) :
    0 ≤ ofScalars ℝ c n v := by
  simp only [ofScalars, _root_.smul_apply,
    ContinuousMultilinearMap.mkPiAlgebraFin_apply, smul_eq_mul, List.prod_ofFn]
  exact mul_nonneg (hc n) (Finset.prod_nonneg (fun i _ => hv i))

 

lemma summable_add_of_shift (c : ℕ → ℝ) (hc : ∀ n, 0 ≤ c n)
    {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y)
    (hxr : (‖x‖₊ : ℝ≥0∞) < (ofScalars ℝ c).radius)
    (hshift : Summable fun k => (ofScalars ℝ c).changeOrigin x k (fun _ => y)) :
    Summable fun n => ofScalars ℝ c n (fun _ => x+y) := by
  let p := ofScalars ℝ c
  let f : (Σ k l : ℕ, {s : Finset (Fin (k+l)) // s.card = l}) → ℝ :=
    fun s => p.changeOriginSeriesTerm s.1 s.2.1 s.2.2 s.2.2.2 (fun _ => x) (fun _ => y)
  have fnonneg : ∀ s, 0 ≤ f s := by
    intro s
    dsimp [f]
    rw [changeOriginSeriesTerm_apply]
    apply scalar_apply_nonneg c hc
    intro i
    dsimp only [Finset.piecewise]
    split_ifs <;> assumption
  have hinner (k : ℕ) : HasSum (fun s => f ⟨k,s⟩) (p.changeOrigin x k (fun _ => y)) := by
    apply ContinuousMultilinearMap.hasSum_eval
    have ht := (p.hasFPowerSeriesOnBall_changeOrigin k hxr.pos).hasSum
      (mem_eball_zero_iff.mpr hxr)
    rw [zero_add] at ht
    refine HasSum.sigma_of_hasSum ht (fun l => ?_) ?_
    · simp only [changeOriginSeries, sum_apply]
      exact hasSum_fintype _
    · refine .of_nnnorm_bounded (p.changeOriginSeries_summable_aux₂ hxr k) ?_
      intro s
      refine (ContinuousMultilinearMap.le_opNNNorm _ _).trans_eq ?_
      simp
  have hsf : Summable f := (summable_sigma_of_nonneg fnonneg).mpr
    ⟨fun k => (hinner k).summable, by simpa only [(hinner _).tsum_eq] using hshift⟩
  have hs := (changeOriginIndexEquiv.symm.summable_iff.mpr hsf).sigma
  apply hs.congr
  intro n
  change (∑' s : Finset (Fin n), f (changeOriginIndexEquiv.symm ⟨n,s⟩)) =
    p n (fun _ => x+y)
  rw [tsum_fintype, ← Pi.add_def, (p n).map_add_univ (fun _ => x) (fun _ => y)]
  simp_rw [← changeOriginSeriesTerm_changeOriginIndexEquiv_symm]
  rfl

lemma radius_add_le (c : ℕ → ℝ) (hc : ∀ n, 0 ≤ c n)
    (x y : ℝ≥0) (hx : (x : ℝ≥0∞) < (ofScalars ℝ c).radius)
    (hy : (y : ℝ≥0∞) < ((ofScalars ℝ c).changeOrigin (x : ℝ)).radius) :
    ((x+y : ℝ≥0) : ℝ≥0∞) ≤ (ofScalars ℝ c).radius := by
  have hs := summable_add_of_shift c hc x.coe_nonneg y.coe_nonneg
    (by simpa using hx) (((ofScalars ℝ c).changeOrigin (x : ℝ)).summable
      (by simpa only [mem_eball_zero_iff, enorm_eq_nnnorm, NNReal.nnnorm_eq] using hy))
  apply (ofScalars ℝ c).le_radius_of_summable (r := x+y)
  simpa only [ofScalars_norm, Real.norm_eq_abs, abs_of_nonneg (hc _),
    NNReal.coe_add, ofScalars_apply_eq, smul_eq_mul] using hs

 

theorem not_analytic_at_radius (c : ℕ → ℝ) (hc : ∀ n, 0 ≤ c n)
    (R : ℝ≥0) (hR : 0 < R) (hr : (ofScalars ℝ c).radius = R)
    (f : ℝ → ℝ) (heq : EqOn f (ofScalars ℝ c).sum (Ioo 0 (R : ℝ))) :
    ¬ AnalyticAt ℝ f (R : ℝ) := by
  intro ha
  obtain ⟨q,r,hq⟩ := ha
  obtain ⟨e,he0,he⟩ := ENNReal.lt_iff_exists_nnreal_btwn.mp
    (show (0 : ℝ≥0∞) < min (R : ℝ≥0∞) r from lt_min (by exact_mod_cast hR) hq.r_pos)
  have hepos : 0 < e := by exact_mod_cast he0
  have heR : e < R := by exact_mod_cast (lt_min_iff.mp he).1
  have her : (e : ℝ≥0∞) < r := (lt_min_iff.mp he).2
  let d : ℝ≥0 := e/4
  have hdpos : 0 < d := div_pos hepos (by norm_num)
  have hdR : d < R := by dsimp [d]; linarith
  have hde : 3*d < e := by dsimp [d]; linarith
  have hd_le_e : d ≤ e := by dsimp [d]; linarith
  let x : ℝ≥0 := R-d
  have hdx : x+d=R := tsub_add_cancel_of_le hdR.le
  have hxpos : 0 < x := tsub_pos_iff_lt.mpr hdR
  have hxR : x < R := by linarith
  have hxr : (x : ℝ≥0∞) < (ofScalars ℝ c).radius := by rw [hr]; exact_mod_cast hxR
  have hdr : (‖-(d : ℝ)‖₊ : ℝ≥0∞) < r := by
    simpa using (show (d : ℝ≥0∞) < r from (show (d : ℝ≥0∞) ≤ (e : ℝ≥0∞) by exact_mod_cast hd_le_e).trans_lt her)
  have hsright := hq.changeOrigin hdr
  have hcenter : (R : ℝ) + -(d : ℝ) = (x : ℝ) := by
    have hh : (x : ℝ)+(d : ℝ)=(R : ℝ) := by exact_mod_cast hdx
    linarith
  rw [hcenter] at hsright
  have hsleft := ((ofScalars ℝ c).hasFPowerSeriesOnBall hxr.pos).changeOrigin
    (show (‖(x : ℝ)‖₊ : ℝ≥0∞) < (ofScalars ℝ c).radius by simpa using hxr)
  rw [zero_add] at hsleft
  have hloc : (ofScalars ℝ c).sum =ᶠ[𝓝 (x : ℝ)] f := by
    filter_upwards [Ioo_mem_nhds (by exact_mod_cast hxpos) (by exact_mod_cast hxR)] with z hz
    exact (heq hz).symm
  have hseries := (hsleft.hasFPowerSeriesAt.congr hloc).eq_formalMultilinearSeries
    hsright.hasFPowerSeriesAt
  have hy : ((2*d : ℝ≥0) : ℝ≥0∞) < ((ofScalars ℝ c).changeOrigin (x : ℝ)).radius := by
    rw [hseries]
    apply lt_of_lt_of_le _ hsright.r_le
    simp only [nnnorm_neg, NNReal.nnnorm_eq]
    rw [lt_tsub_iff_right]
    calc
      ((2*d : ℝ≥0) : ℝ≥0∞) + (d : ℝ≥0∞) = ((3*d : ℝ≥0) : ℝ≥0∞) := by push_cast; ring
      _ < (e : ℝ≥0∞) := by exact_mod_cast hde
      _ < r := her
  have hh := radius_add_le c hc x (2*d) hxr hy
  rw [hr] at hh
  have hcast : x+2*d ≤ R := by exact_mod_cast hh
  linarith

 

theorem radius_gt_of_analytic_interval (c : ℕ → ℝ) (hc : ∀ n, 0 ≤ c n)
    (B : ℝ≥0) (f : ℝ → ℝ)
    (hp : HasFPowerSeriesAt f (ofScalars ℝ c) 0)
    (hf : AnalyticOnNhd ℝ f (Icc 0 (B : ℝ))) :
    (B : ℝ≥0∞) < (ofScalars ℝ c).radius := by
  by_contra hb
  have hle : (ofScalars ℝ c).radius ≤ B := le_of_not_gt hb
  have hfin : (ofScalars ℝ c).radius ≠ ⊤ := ne_top_of_le_ne_top ENNReal.coe_ne_top hle
  let R := (ofScalars ℝ c).radius.toNNReal
  have hr : (ofScalars ℝ c).radius = (R : ℝ≥0∞) := (ENNReal.coe_toNNReal hfin).symm
  have hR : 0 < R := by
    have hh := hp.radius_pos
    rw [hr] at hh
    exact_mod_cast hh
  have hRB : R ≤ B := by rw [hr] at hle; exact_mod_cast hle
  have hloc : f =ᶠ[𝓝 0] (ofScalars ℝ c).sum := by
    filter_upwards [hp.eventually_hasSum] with z hz
    simpa only [zero_add, FormalMultilinearSeries.sum] using hz.tsum_eq.symm
  have hfs : AnalyticOnNhd ℝ f (Ico 0 (R : ℝ)) :=
    hf.mono (fun z hz => ⟨hz.1, hz.2.le.trans (by exact_mod_cast hRB)⟩)
  have hps : AnalyticOnNhd ℝ (ofScalars ℝ c).sum (Ico 0 (R : ℝ)) := by
    apply (ofScalars ℝ c).analyticOnNhd.mono
    intro z hz
    rw [mem_eball_zero_iff, hr, enorm_eq_nnnorm, ENNReal.coe_lt_coe]
    exact_mod_cast (show ‖z‖ < (R : ℝ) by simpa [Real.norm_eq_abs, abs_of_nonneg hz.1] using hz.2)
  have heq := hfs.eqOn_of_preconnected_of_eventuallyEq hps isPreconnected_Ico
    (show (0 : ℝ) ∈ Ico 0 (R : ℝ) from ⟨le_rfl, by exact_mod_cast hR⟩) hloc
  exact not_analytic_at_radius c hc R hR hr f
    (heq.mono Ioo_subset_Ico_self) (hf R ⟨R.coe_nonneg, by exact_mod_cast hRB⟩)

end BelowOne.PositiveSeries
end
end

 
section

 

noncomputable section
open Finset Filter Set FormalMultilinearSeries
open scoped Topology NNReal ENNReal Classical
namespace BelowOne.PositiveAnalyticCube
open MultiaffineDerivative
variable {V : Type*} [Fintype V]

def cube : Set (V → ℝ) := {x | ∀ i, x i ∈ Set.Icc 0 1}
def diagonal : ℝ →L[ℝ] (V → ℝ) := ContinuousLinearMap.pi fun _ => ContinuousLinearMap.id ℝ ℝ

def taylor (f : (V → ℝ) → ℝ) : FormalMultilinearSeries ℝ (V → ℝ) ℝ :=
  fun n => (n.factorial : ℝ)⁻¹ • iteratedFDeriv ℝ n f 0

def diagonalCoefficient (f : (V → ℝ) → ℝ) (n : ℕ) : ℝ :=
  taylor f n (fun _ _ => 1)

lemma taylor_diagonal_eq {f : (V → ℝ) → ℝ} {p : FormalMultilinearSeries ℝ (V → ℝ) ℝ}
    {r : ℝ≥0∞} (hp : HasFPowerSeriesOnBall f p 0 r) (x : V → ℝ) (n : ℕ) :
    taylor f n (fun _ => x) = p n (fun _ => x) := by
  have h := hp.factorial_smul x n
  simp only [nsmul_eq_mul] at h
  rw [taylor,_root_.smul_apply,smul_eq_mul,←h]
  rw [←mul_assoc,inv_mul_cancel₀ (Nat.cast_ne_zero.mpr n.factorial_ne_zero),one_mul]

lemma diagonal_hasFPowerSeriesAt {f : (V → ℝ) → ℝ} (hf : AnalyticAt ℝ f 0) :
    HasFPowerSeriesAt (f ∘ diagonal) (ofScalars ℝ (diagonalCoefficient f)) 0 := by
  obtain ⟨p,r,hp⟩ := hf
  have hs : HasFPowerSeriesAt (f ∘ diagonal) (p.compContinuousLinearMap diagonal) 0 :=
    (show HasFPowerSeriesAt f p (diagonal (V := V) 0) by simpa using hp.hasFPowerSeriesAt).compContinuousLinearMap
  convert hs using 1
  ext n
  rw [FormalMultilinearSeries.apply_eq_prod_smul_coeff,FormalMultilinearSeries.apply_eq_prod_smul_coeff,
    coeff_ofScalars]
  congr 1
  rw [diagonalCoefficient,taylor_diagonal_eq hp]
  rfl

lemma taylor_coordinate_nonneg (f : (V → ℝ) → ℝ) (hf0 : 0 ≤ f 0)
    (hf : ∀ n, 0 < n → ∀ τ : Fin n → V,
      0 ≤ iteratedFDeriv ℝ n f 0 (coordinate ∘ τ)) (n : ℕ) (τ : Fin n → V) :
    0 ≤ taylor f n (coordinate ∘ τ) := by
  rw [taylor,_root_.smul_apply,smul_eq_mul]
  apply mul_nonneg (by positivity)
  by_cases hn : n = 0
  · subst n
    simpa only [iteratedFDeriv_zero_apply] using hf0
  · exact hf n (Nat.pos_of_ne_zero hn) τ

lemma diagonalCoefficient_nonneg (f : (V → ℝ) → ℝ) (hf0 : 0 ≤ f 0)
    (hf : ∀ n, 0 < n → ∀ τ : Fin n → V,
      0 ≤ iteratedFDeriv ℝ n f 0 (coordinate ∘ τ)) (n : ℕ) :
    0 ≤ diagonalCoefficient f n :=
  PositiveMultilinear.nonneg _ (taylor_coordinate_nonneg f hf0 hf n) _ (fun _ _ => zero_le_one)

lemma radius_le_of_norm_le {E F : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E]
    [NormedAddCommGroup F] [NormedSpace ℝ F]
    (p : FormalMultilinearSeries ℝ E ℝ) (q : FormalMultilinearSeries ℝ F ℝ)
    (h : ∀ n, ‖q n‖ ≤ ‖p n‖) : p.radius ≤ q.radius := by
  unfold FormalMultilinearSeries.radius
  apply iSup_le
  intro r
  apply iSup_le
  intro C
  apply iSup_le
  intro hC
  apply q.le_radius_of_bound C
  intro n
  exact (mul_le_mul_of_nonneg_right (h n) (by positivity)).trans (hC n)

lemma taylor_radius_gt_one (f : (V → ℝ) → ℝ) (hf0 : 0 ≤ f 0)
    (hf : ∀ n, 0 < n → ∀ τ : Fin n → V,
      0 ≤ iteratedFDeriv ℝ n f 0 (coordinate ∘ τ))
    (ha : AnalyticOnNhd ℝ f cube) : 1 < (taylor f).radius := by
  have ha0 : AnalyticAt ℝ f 0 := ha 0 (fun i => by simp)
  have hd : AnalyticOnNhd ℝ (f ∘ diagonal) (Set.Icc (0:ℝ) 1) := by
    intro x hx
    exact (ha (diagonal x) (fun _ => hx)).compContinuousLinearMap
  have hR := PositiveSeries.radius_gt_of_analytic_interval (diagonalCoefficient f)
    (diagonalCoefficient_nonneg f hf0 hf) 1 (f ∘ diagonal) (diagonal_hasFPowerSeriesAt ha0) hd
  apply hR.trans_le
  apply radius_le_of_norm_le
  intro n
  rw [ofScalars_norm,Real.norm_of_nonneg (diagonalCoefficient_nonneg f hf0 hf n)]
  exact PositiveMultilinear.norm_le_ones _ (taylor_coordinate_nonneg f hf0 hf n)

lemma hasFPowerSeriesAt_taylor {f : (V → ℝ) → ℝ} (hf : AnalyticAt ℝ f 0)
    (hR : 0 < (taylor f).radius) : HasFPowerSeriesAt f (taylor f) 0 := by
  obtain ⟨p,r,hp⟩ := hf
  refine ⟨min r (taylor f).radius, ?_⟩
  refine ⟨min_le_right _ _,lt_min hp.r_pos hR,?_⟩
  intro x hx
  have hx' : x ∈ Metric.eball 0 r := by
    exact mem_eball_zero_iff.mpr ((mem_eball_zero_iff.mp hx).trans_le (min_le_left _ _))
  simpa only [taylor,_root_.smul_apply] using hp.hasSum_iteratedFDeriv hx'

omit [Fintype V] in
lemma cube_convex : Convex ℝ (cube (V := V)) := by
  have hc : cube (V := V) = Set.pi Set.univ (fun _ : V => Set.Icc (0:ℝ) 1) := by
    ext x
    change (∀ i, x i ∈ Set.Icc 0 1) ↔ ∀ i ∈ Set.univ, x i ∈ Set.Icc 0 1
    simp only [Set.mem_univ, forall_true_left]
  rw [hc]
  exact convex_pi (fun _ _ => convex_Icc 0 1)

lemma cube_subset_eball {r : ℝ≥0∞} (hr : 1 < r) : cube (V := V) ⊆ Metric.eball 0 r := by
  intro x hx
  have hn : ‖x‖ ≤ 1 := by
    apply (pi_norm_le_iff_of_nonneg zero_le_one).mpr
    intro i
    exact (Real.norm_of_nonneg (hx i).1).trans_le (hx i).2
  apply mem_eball_zero_iff.mpr
  exact (show ‖x‖ₑ ≤ (1:ℝ≥0∞) by
    rw [enorm_eq_nnnorm,← ENNReal.coe_one,ENNReal.coe_le_coe]
    exact_mod_cast hn).trans_lt hr

lemma taylor_sum_eq_on_cube (f : (V → ℝ) → ℝ) (hf0 : 0 ≤ f 0)
    (hf : ∀ n, 0 < n → ∀ τ : Fin n → V,
      0 ≤ iteratedFDeriv ℝ n f 0 (coordinate ∘ τ))
    (ha : AnalyticOnNhd ℝ f cube) : Set.EqOn f (taylor f).sum cube := by
  have hR := taylor_radius_gt_one f hf0 hf ha
  have hp := hasFPowerSeriesAt_taylor (ha 0 (fun i => by simp)) (zero_lt_one.trans hR)
  have heq : f =ᶠ[𝓝 0] (taylor f).sum := by
    filter_upwards [hp.eventually_hasSum] with x hx
    simpa only [zero_add,FormalMultilinearSeries.sum] using hx.tsum_eq.symm
  exact ha.eqOn_of_preconnected_of_eventuallyEq
    ((taylor f).analyticOnNhd.mono (cube_subset_eball hR)) cube_convex.isPreconnected
      (fun i => by simp) heq

end BelowOne.PositiveAnalyticCube
end
end

 
section

 

noncomputable section
open Finset Filter Set FormalMultilinearSeries
open scoped Topology NNReal ENNReal Classical
namespace BelowOne.FKPressure
variable {V I : Type*} [Fintype V]

def pressure (ends : I → V × V) (E : Finset I) (u : ℝ) (v : I → ℝ)
    (x : V → ℝ) : ℝ := -Real.log (percolationSitePolynomial ends E (-u) v x)

lemma analyticAt_percolationSitePolynomial (ends : I → V × V) (E : Finset I)
    (s : ℝ) (v : I → ℝ) (x : V → ℝ) :
    AnalyticAt ℝ (percolationSitePolynomial ends E s v) x := by
  rw [percolationSitePolynomial_eq]
  unfold MultiaffineDerivative.polynomial
  apply Finset.analyticAt_fun_sum
  intro T hT
  apply AnalyticAt.mul analyticAt_const
  apply Finset.analyticAt_fun_prod
  intro i hi
  exact (ContinuousLinearMap.proj i : (V → ℝ) →L[ℝ] ℝ).analyticAt x

lemma pressure_zero (ends : I → V × V) (E : Finset I)
    (u : ℝ) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) : pressure ends E u v 0 = 0 := by
  simp only [pressure,percolationSitePolynomial_zero ends E (-u) v hv,Real.log_one,neg_zero]

lemma pressure_derivative_nonneg (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e)
    (n : ℕ) (hn : 0 < n) (τ : Fin n → V) :
    0 ≤ iteratedFDeriv ℝ n (pressure ends E u v) 0
      (MultiaffineDerivative.coordinate ∘ τ) := by
  change 0 ≤ iteratedFDeriv ℝ n (-(fun x => Real.log (percolationSitePolynomial ends E (-u) v x))) 0
    (MultiaffineDerivative.coordinate ∘ τ)
  rw [iteratedFDeriv_neg_apply,_root_.neg_apply]
  exact neg_nonneg.mpr (percolationSitePolynomial_log_derivative_nonpos hn τ ends E hu hu1 v hv)

lemma pressure_analyticOn_cube (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 ≤ u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    AnalyticOnNhd ℝ (pressure ends E u v) PositiveAnalyticCube.cube := by
  intro x hx
  exact ((analyticAt_log (percolationSitePolynomial_pos ends E hu hu1 v hv x hx)).comp
    (analyticAt_percolationSitePolynomial ends E (-u) v x)).neg

theorem pressure_expansion (ends : I → V × V) (E : Finset I)
    {u : ℝ} (hu : 0 < u) (hu1 : u < 1) (v : I → ℝ) (hv : ∀ e ∈ E, 0 ≤ v e) :
    let p := PositiveAnalyticCube.taylor (pressure ends E u v)
    1 < p.radius ∧
    (∀ n (τ : Fin n → V), 0 ≤ p n (MultiaffineDerivative.coordinate ∘ τ)) ∧
    (∀ x ∈ PositiveAnalyticCube.cube, HasSum (fun n => p n (fun _ => x)) (pressure ends E u v x)) := by
  dsimp only
  have hf0 : 0 ≤ pressure ends E u v 0 := (pressure_zero ends E u v hv).ge
  have hf := pressure_derivative_nonneg ends E hu hu1 v hv
  have ha := pressure_analyticOn_cube ends E hu.le hu1 v hv
  have hR := PositiveAnalyticCube.taylor_radius_gt_one _ hf0 hf ha
  refine ⟨hR,PositiveAnalyticCube.taylor_coordinate_nonneg _ hf0 hf,?_⟩
  intro x hx
  rw [PositiveAnalyticCube.taylor_sum_eq_on_cube _ hf0 hf ha hx]
  exact (PositiveAnalyticCube.taylor (pressure ends E u v)).summable
    (PositiveAnalyticCube.cube_subset_eball hR hx) |>.hasSum

end BelowOne.FKPressure
end
end

end OAI