Current task: —

E-023-finite-height-assembly.py

Experiments: E-023 · script updated 08-19-2026

E-023-assembly.csv — 710.1 KB, updated 08-19-2026

page 1 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
2 20 -45875.893723 -43305.210843
3 40 -51169.702466 -48378.454440
4 60 -54411.288377 -51473.452785
5 80 -56768.367148 -53718.072885
6 100 -58625.758870 -55483.102057
7 120 -60160.142614 -56938.507754
8 140 -61467.830010 -58176.832729
9 160 -62607.286685 -59254.200835
10 180 -63616.745562 -60207.284034
11 200 -64522.615724 -61061.395554
12 220 -65343.922912 -61834.760456
13 240 -66094.839059 -62540.946810
14 260 -66786.211248 -63190.334332
15 280 -67426.531022 -63791.044989
16 300 -68022.573532 -64349.556338
17 320 -68579.833103 -64871.119317
18 340 -69102.828502 -65360.050946
19 360 -69595.322130 -65819.944422
20 380 -70060.480758 -66253.823115
21 400 -70500.995596 -66664.255591
22 420 -70919.173477 -67053.442913
23 440 -71317.007122 -67423.285910
24 460 -71696.230004 -67775.437682
25 480 -72058.359701 -68111.345075
26 500 -72404.732519 -68432.281807
27 520 -72736.531427 -68739.375176
28 540 -73054.808791 -69033.627797
29 560 -73360.505027 -69315.935437
30 580 -73654.464042 -69587.101761
31 600 -73937.446075 -69847.850617
32 620 -74210.138481 -70098.836325
33 640 -74473.164815 -70340.652358
34 660 -74727.092543 -70573.838708
35 680 -74972.439616 -70798.888162
36 700 -75209.680112 -71016.251682
37 720 -75439.249083 -71226.343040
38 740 -75661.546757 -71429.542823
39 760 -75876.942182 -71626.201915
40 780 -76085.776408 -71816.644523
41 800 -76288.365271 -72001.170842
42 820 -76485.001832 -72180.059379
43 840 -76675.958537 -72353.569010
44 860 -76861.489116 -72521.940792
45 880 -77041.830278 -72685.399580
46 900 -77217.203205 -72844.155452
47 920 -77387.814897 -72998.404988
48 940 -77553.859366 -73148.332408
49 960 -77715.518710 -73294.110600
50 980 -77872.964078 -73435.902031
51 1000 -78026.356533 -73573.859582
52 1020 -78175.847843 -73708.127286
53 1040 -78321.581183 -73838.841008
54 1060 -78463.691780 -73966.129055
55 1080 -78602.307495 -74090.112731
56 1100 -78737.549353 -74210.906841
57 1120 -78869.532026 -74328.620150
58 1140 -78998.364274 -74443.355807
59 1160 -79124.149348 -74555.211724
60 1180 -79246.985361 -74664.280929
61 1200 -79366.965624 -74770.651890
62 1220 -79484.178960 -74874.408808
63 1240 -79598.709985 -74975.631892
64 1260 -79710.639379 -75074.397608
65 1280 -79820.044122 -75170.778911
66 1300 -79926.997726 -75264.845459
67 1320 -80031.570435 -75356.663811
68 1340 -80133.829423 -75446.297608
69 1360 -80233.838969 -75533.807744
70 1380 -80331.660627 -75619.252523
71 1400 -80427.353372 -75702.687807
72 1420 -80520.973753 -75784.167148
73 1440 -80612.576017 -75863.741919
74 1460 -80702.212238 -75941.461429
75 1480 -80789.932434 -76017.373033
76 1500 -80875.784670 -76091.522238
77 1520 -80959.815166 -76163.952797
78 1540 -81042.068384 -76234.706796
79 1560 -81122.587124 -76303.824745
80 1580 -81201.412601 -76371.345651
81 1600 -81278.584528 -76437.307094
82 1620 -81354.141183 -76501.745297
83 1640 -81428.119486 -76564.695190
84 1660 -81500.555056 -76626.190475
85 1680 -81571.482276 -76686.263677
86 1700 -81640.934348 -76744.946206
87 1720 -81708.943350 -76802.268402
88 1740 -81775.540285 -76858.259589
89 1760 -81840.755127 -76912.948114
90 1780 -81904.616871 -76966.361398
91 1800 -81967.153573 -77018.525968
92 1820 -82028.392389 -77069.467504
93 1840 -82088.359619 -77119.210869
94 1860 -82147.080736 -77167.780147
95 1880 -82204.580426 -77215.198675
96 1900 -82260.882620 -77261.489071
97 1920 -82316.010522 -77306.673269
98 1940 -82369.986640 -77350.772542
99 1960 -82422.832816 -77393.807532
100 1980 -82474.570248 -77435.798273
101 2000 -82525.219521 -77476.764212
102 2020 -82574.800624 -77516.724241
103 2040 -82623.332979 -77555.696708
104 2060 -82670.835460 -77593.699443
105 2080 -82717.326413 -77630.749780
106 2100 -82762.823678 -77666.864570
107 2120 -82807.344605 -77702.060201
108 2140 -82850.906074 -77736.352616
109 2160 -82893.524512 -77769.757331
110 2180 -82935.215907 -77802.289444
111 2200 -82975.995827 -77833.963657
112 2220 -83015.879433 -77864.794285
113 2240 -83054.881494 -77894.795275
114 2260 -83093.016398 -77923.980210
115 2280 -83130.298169 -77952.362332
116 2300 -83166.740478 -77979.954545
117 2320 -83202.356653 -78006.769433
118 2340 -83237.159693 -78032.819265
119 2360 -83271.162277 -78058.116009
120 2380 -83304.376778 -78082.671343
121 2400 -83336.815268 -78106.496660
122 2420 -83368.489530 -78129.603080
123 2440 -83399.411070 -78152.001462
124 2460 -83429.591120 -78173.702405
125 2480 -83459.040655 -78194.716263
126 2500 -83487.770390 -78215.053147
127 2520 -83515.790798 -78234.722939
128 2540 -83543.112113 -78253.735292
129 2560 -83569.744338 -78272.099644
130 2580 -83595.697250 -78289.825217
131 2600 -83620.980411 -78306.921032
132 2620 -83645.603172 -78323.395908
133 2640 -83669.574678 -78339.258470
134 2660 -83692.903876 -78354.517157
135 2680 -83715.599521 -78369.180226
136 2700 -83737.670179 -78383.255755
137 2720 -83759.124237 -78396.751652
138 2740 -83779.969903 -78409.675657
139 2760 -83800.215215 -78422.035349
140 2780 -83819.868042 -78433.838148
141 2800 -83838.936093 -78445.091322
142 2820 -83857.426919 -78455.801989
143 2840 -83875.347918 -78465.977123
144 2860 -83892.706336 -78475.623556
145 2880 -83909.509278 -78484.747985
146 2900 -83925.763706 -78493.356971
147 2920 -83941.476445 -78501.456948
148 2940 -83956.654185 -78509.054221
149 2960 -83971.303487 -78516.154973
150 2980 -83985.430785 -78522.765269
151 3000 -83999.042391 -78528.891055
152 3020 -84012.144493 -78534.538165
153 3040 -84024.743166 -78539.712320
154 3060 -84036.844369 -78544.419136
155 3080 -84048.453948 -78548.664123
156 3100 -84059.577643 -78552.452687
157 3120 -84070.221087 -78555.790136
158 3140 -84080.389811 -78558.681680
159 3160 -84090.089244 -78561.132433
160 3180 -84099.324717 -78563.147418
161 3200 -84108.101466 -78564.731566
162 3220 -84116.424634 -78565.889721
163 3240 -84124.299271 -78566.626641
164 3260 -84131.730340 -78566.946999
165 3280 -84138.722715 -78566.855386
166 3300 -84145.281187 -78566.356314
167 3320 -84151.410463 -78565.454216
168 3340 -84157.115171 -78564.153450
169 3360 -84162.399857 -78562.458296
170 3380 -84167.268993 -78560.372965
171 3400 -84171.726972 -78557.901594
172 3420 -84175.778117 -78555.048253
173 3440 -84179.426677 -78551.816942
174 3460 -84182.676830 -78548.211595
175 3480 -84185.532687 -78544.236082
176 3500 -84187.998289 -78539.894208
177 3520 -84190.077614 -78535.189716
178 3540 -84191.774575 -78530.126291
179 3560 -84193.093020 -78524.707554
180 3580 -84194.036738 -78518.937072
181 3600 -84194.609456 -78512.818353
182 3620 -84194.814844 -78506.354850
183 3640 -84194.656511 -78499.549961
184 3660 -84194.138013 -78492.407032
185 3680 -84193.262849 -78484.929356
186 3700 -84192.034463 -78477.120174
187 3720 -84190.456248 -78468.982679
188 3740 -84188.531545 -78460.520013
189 3760 -84186.263642 -78451.735272
190 3780 -84183.655779 -78442.631505
191 3800 -84180.711147 -78433.211712
192 3820 -84177.432890 -78423.478853
193 3840 -84173.824104 -78413.435839
194 3860 -84169.887838 -78403.085540
195 3880 -84165.627100 -78392.430785
196 3900 -84161.044850 -78381.474358
197 3920 -84156.144007 -78370.219006
198 3940 -84150.927446 -78358.667434
199 3960 -84145.398004 -78346.822308
200 3980 -84139.558473 -78334.686257
201 4000 -84133.411607 -78322.261869
202 4020 -84126.960123 -78309.551700
203 4040 -84120.206696 -78296.558267
204 4060 -84113.153965 -78283.284051
205 4080 -84105.804533 -78269.731499
206 4100 -84098.160966 -78255.903026
207 4120 -84090.225793 -78241.801009
208 4140 -84082.001511 -78227.427796
209 4160 -84073.490582 -78212.785702
210 4180 -84064.695432 -78197.877009
211 4200 -84055.618457 -78182.703969
212 4220 -84046.262019 -78167.268804
213 4240 -84036.628449 -78151.573704
214 4260 -84026.720047 -78135.620833
215 4280 -84016.539081 -78119.412322
216 4300 -84006.087791 -78102.950277
217 4320 -83995.368387 -78086.236776
218 4340 -83984.383048 -78069.273867
219 4360 -83973.133927 -78052.063574
220 4380 -83961.623147 -78034.607894
221 4400 -83949.852806 -78016.908796
222 4420 -83937.824973 -77998.968227
223 4440 -83925.541691 -77980.788107
224 4460 -83913.004977 -77962.370332
225 4480 -83900.216823 -77943.716773
226 4500 -83887.179194 -77924.829278
227 4520 -83873.894033 -77905.709672
228 4540 -83860.363256 -77886.359757
229 4560 -83846.588757 -77866.781312
230 4580 -83832.572406 -77846.976094
231 4600 -83818.316050 -77826.945838
232 4620 -83803.821512 -77806.692259
233 4640 -83789.090595 -77786.217050
234 4660 -83774.125077 -77765.521883
235 4680 -83758.926718 -77744.608409
236 4700 -83743.497254 -77723.478262
237 4720 -83727.838402 -77702.133053
238 4740 -83711.951856 -77680.574375
239 4760 -83695.839293 -77658.803801
240 4780 -83679.502367 -77636.822889
241 4800 -83662.942714 -77614.633173
242 4820 -83646.161951 -77592.236173
243 4840 -83629.161676 -77569.633390
244 4860 -83611.943468 -77546.826307
245 4880 -83594.508887 -77523.816389
246 4900 -83576.859476 -77500.605087
247 4920 -83558.996759 -77477.193832
248 4940 -83540.922245 -77453.584040
249 4960 -83522.637424 -77429.777110
250 4980 -83504.143767 -77405.774427
251 5000 -83485.442733 -77381.577358
252 5020 -83466.535761 -77357.187257
253 5040 -83447.424275 -77332.605459
254 5060 -83428.109683 -77307.833287
255 5080 -83408.593377 -77282.872049
256 5100 -83388.876734 -77257.723038
257 5120 -83368.961116 -77232.387533
258 5140 -83348.847869 -77206.866797
259 5160 -83328.538325 -77181.162082
260 5180 -83308.033801 -77155.274625
261 5200 -83287.335601 -77129.205648
262 5220 -83266.445014 -77102.956363
263 5240 -83245.363313 -77076.527966
264 5260 -83224.091762 -77049.921642
265 5280 -83202.631606 -77023.138561
266 5300 -83180.984081 -76996.179883
267 5320 -83159.150408 -76969.046755
268 5340 -83137.131796 -76941.740311
269 5360 -83114.929440 -76914.261674
270 5380 -83092.544523 -76886.611954
271 5400 -83069.978216 -76858.792250
272 5420 -83047.231677 -76830.803650
273 5440 -83024.306054 -76802.647231
274 5460 -83001.202481 -76774.324056
275 5480 -82977.922080 -76745.835181
276 5500 -82954.465964 -76717.181649
277 5520 -82930.835232 -76688.364492
278 5540 -82907.030974 -76659.384732
279 5560 -82883.054266 -76630.243381
280 5580 -82858.906177 -76600.941441
281 5600 -82834.587762 -76571.479902
282 5620 -82810.100067 -76541.859746
283 5640 -82785.444127 -76512.081945
284 5660 -82760.620967 -76482.147460
285 5680 -82735.631602 -76452.057244
286 5700 -82710.477037 -76421.812240
287 5720 -82685.158265 -76391.413380
288 5740 -82659.676272 -76360.861590
289 5760 -82634.032034 -76330.157784
290 5780 -82608.226517 -76299.302869
291 5800 -82582.260676 -76268.297743
292 5820 -82556.135459 -76237.143294
293 5840 -82529.851805 -76205.840402
294 5860 -82503.410641 -76174.389940
295 5880 -82476.812890 -76142.792770
296 5900 -82450.059461 -76111.049748
297 5920 -82423.151258 -76079.161721
298 5940 -82396.089174 -76047.129527
299 5960 -82368.874097 -76014.953998
300 5980 -82341.506902 -75982.635957
301 6000 -82313.988460 -75950.176220
302 6020 -82286.319631 -75917.575594
303 6040 -82258.501269 -75884.834879
304 6060 -82230.534218 -75851.954869
305 6080 -82202.419316 -75818.936348
306 6100 -82174.157393 -75785.780096
307 6120 -82145.749271 -75752.486883
308 6140 -82117.195764 -75719.057474
309 6160 -82088.497679 -75685.492625
310 6180 -82059.655816 -75651.793086
311 6200 -82030.670968 -75617.959601
312 6220 -82001.543920 -75583.992906
313 6240 -81972.275450 -75549.893733
314 6260 -81942.866329 -75515.662802
315 6280 -81913.317322 -75481.300833
316 6300 -81883.629187 -75446.808535
317 6320 -81853.802674 -75412.186613
318 6340 -81823.838528 -75377.435765
319 6360 -81793.737486 -75342.556681
320 6380 -81763.500279 -75307.550049
321 6400 -81733.127633 -75272.416548
322 6420 -81702.620265 -75237.156851
323 6440 -81671.978888 -75201.771627
324 6460 -81641.204208 -75166.261537
325 6480 -81610.296925 -75130.627238
326 6500 -81579.257732 -75094.869381
327 6520 -81548.087318 -75058.988610
328 6540 -81516.786364 -75022.985565
329 6560 -81485.355548 -74986.860880
330 6580 -81453.795538 -74950.615184
331 6600 -81422.107000 -74914.249100
332 6620 -81390.290594 -74877.763246
333 6640 -81358.346972 -74841.158235
334 6660 -81326.276783 -74804.434675
335 6680 -81294.080669 -74767.593167
336 6700 -81261.759268 -74730.634310
337 6720 -81229.313211 -74693.558696
338 6740 -81196.743125 -74656.366912
339 6760 -81164.049633 -74619.059541
340 6780 -81131.233349 -74581.637162
341 6800 -81098.294887 -74544.100347
342 6820 -81065.234851 -74506.449665
343 6840 -81032.053843 -74468.685678
344 6860 -80998.752460 -74430.808948
345 6880 -80965.331294 -74392.820028
346 6900 -80931.790931 -74354.719467
347 6920 -80898.131953 -74316.507813
348 6940 -80864.354937 -74278.185606
349 6960 -80830.460457 -74239.753383
350 6980 -80796.449081 -74201.211676
351 7000 -80762.321372 -74162.561015
352 7020 -80728.077890 -74123.801922
353 7040 -80693.719188 -74084.934919
354 7060 -80659.245818 -74045.960520
355 7080 -80624.658326 -74006.879238
356 7100 -80589.957253 -73967.691580
357 7120 -80555.143137 -73928.398050
358 7140 -80520.216510 -73888.999149
359 7160 -80485.177903 -73849.495370
360 7180 -80450.027840 -73809.887208
361 7200 -80414.766842 -73770.175150
362 7220 -80379.395425 -73730.359680
363 7240 -80343.914104 -73690.441279
364 7260 -80308.323386 -73650.420424
365 7280 -80272.623778 -73610.297589
366 7300 -80236.815779 -73570.073243
367 7320 -80200.899888 -73529.747852
368 7340 -80164.876598 -73489.321879
369 7360 -80128.746400 -73448.795783
370 7380 -80092.509778 -73408.170020
371 7400 -80056.167217 -73367.445041
372 7420 -80019.719194 -73326.621296
373 7440 -79983.166186 -73285.699229
374 7460 -79946.508663 -73244.679284
375 7480 -79909.747094 -73203.561898
376 7500 -79872.881945 -73162.347508
377 7520 -79835.913675 -73121.036545
378 7540 -79798.842744 -73079.629439
379 7560 -79761.669606 -73038.126615
380 7580 -79724.394712 -72996.528497
381 7600 -79687.018510 -72954.835503
382 7620 -79649.541444 -72913.048051
383 7640 -79611.963957 -72871.166555
384 7660 -79574.286486 -72829.191424
385 7680 -79536.509467 -72787.123066
386 7700 -79498.633330 -72744.961887
387 7720 -79460.658506 -72702.708287
388 7740 -79422.585420 -72660.362665
389 7760 -79384.414494 -72617.925419
390 7780 -79346.146149 -72575.396939
391 7800 -79307.780800 -72532.777618
392 7820 -79269.318862 -72490.067842
393 7840 -79230.760745 -72447.267997
394 7860 -79192.106857 -72404.378464
395 7880 -79153.357603 -72361.399623
396 7900 -79114.513386 -72318.331850
397 7920 -79075.574604 -72275.175520
398 7940 -79036.541655 -72231.931003
399 7960 -78997.414931 -72188.598670
400 7980 -78958.194824 -72145.178886
401 8000 -78918.881723 -72101.672014
402 8020 -78879.476013 -72058.078416
403 8040 -78839.978076 -72014.398452
404 8060 -78800.388294 -71970.632476
405 8080 -78760.707043 -71926.780843
406 8100 -78720.934700 -71882.843904
407 8120 -78681.071636 -71838.822008
408 8140 -78641.118222 -71794.715502
409 8160 -78601.074826 -71750.524730
410 8180 -78560.941811 -71706.250033
411 8200 -78520.719541 -71661.891752
412 8220 -78480.408376 -71617.450223
413 8240 -78440.008674 -71572.925782
414 8260 -78399.520790 -71528.318761
415 8280 -78358.945076 -71483.629492
416 8300 -78318.281884 -71438.858301
417 8320 -78277.531562 -71394.005515
418 8340 -78236.694455 -71349.071459
419 8360 -78195.770907 -71304.056454
420 8380 -78154.761260 -71258.960819
421 8400 -78113.665852 -71213.784873
422 8420 -78072.485021 -71168.528930
423 8440 -78031.219101 -71123.193305
424 8460 -77989.868424 -71077.778308
425 8480 -77948.433322 -71032.284248
426 8500 -77906.914122 -70986.711434
427 8520 -77865.311150 -70941.060170
428 8540 -77823.624730 -70895.330760
429 8560 -77781.855183 -70849.523505
430 8580 -77740.002831 -70803.638704
431 8600 -77698.067989 -70757.676654
432 8620 -77656.050975 -70711.637653
433 8640 -77613.952101 -70665.521992
434 8660 -77571.771680 -70619.329964
435 8680 -77529.510020 -70573.061859
436 8700 -77487.167430 -70526.717965
437 8720 -77444.744216 -70480.298568
438 8740 -77402.240680 -70433.803953
439 8760 -77359.657125 -70387.234402
440 8780 -77316.993852 -70340.590196
441 8800 -77274.251157 -70293.871614
442 8820 -77231.429338 -70247.078934
443 8840 -77188.528688 -70200.212431
444 8860 -77145.549501 -70153.272378
445 8880 -77102.492066 -70106.259049
446 8900 -77059.356674 -70059.172714
447 8920 -77016.143610 -70012.013642
448 8940 -76972.853161 -69964.782099
449 8960 -76929.485610 -69917.478351
450 8980 -76886.041239 -69870.102662
451 9000 -76842.520328 -69822.655295
452 9020 -76798.923155 -69775.136509
453 9040 -76755.249997 -69727.546565
454 9060 -76711.501130 -69679.885719
455 9080 -76667.676826 -69632.154228
456 9100 -76623.777358 -69584.352345
457 9120 -76579.802995 -69536.480324
458 9140 -76535.754006 -69488.538416
459 9160 -76491.630657 -69440.526871
460 9180 -76447.433215 -69392.445936
461 9200 -76403.161941 -69344.295860
462 9220 -76358.817100 -69296.076886
463 9240 -76314.398951 -69247.789259
464 9260 -76269.907752 -69199.433222
465 9280 -76225.343762 -69151.009015
466 9300 -76180.707237 -69102.516877
467 9320 -76135.998430 -69053.957047
468 9340 -76091.217595 -69005.329762
469 9360 -76046.364983 -68956.635257
470 9380 -76001.440844 -68907.873766
471 9400 -75956.445426 -68859.045521
472 9420 -75911.378977 -68810.150753
473 9440 -75866.241741 -68761.189693
474 9460 -75821.033964 -68712.162568
475 9480 -75775.755888 -68663.069607
476 9500 -75730.407753 -68613.911035
477 9520 -75684.989801 -68564.687076
478 9540 -75639.502269 -68515.397953
479 9560 -75593.945395 -68466.043890
480 9580 -75548.319415 -68416.625105
481 9600 -75502.624563 -68367.141820
482 9620 -75456.861072 -68317.594251
483 9640 -75411.029173 -68267.982616
484 9660 -75365.129098 -68218.307131
485 9680 -75319.161076 -68168.568010
486 9700 -75273.125334 -68118.765466
487 9720 -75227.022099 -68068.899712
488 9740 -75180.851596 -68018.970957
489 9760 -75134.614050 -67968.979412
490 9780 -75088.309683 -67918.925286
491 9800 -75041.938716 -67868.808785
492 9820 -74995.501370 -67818.630116
493 9840 -74948.997864 -67768.389483
494 9860 -74902.428416 -67718.087091
495 9880 -74855.793242 -67667.723143
496 9900 -74809.092558 -67617.297839
497 9920 -74762.326577 -67566.811380
498 9940 -74715.495514 -67516.263966
499 9960 -74668.599580 -67465.655796
500 9980 -74621.638986 -67414.987065
501 10000 -74574.613941 -67364.257970

E-023-assembly.json — 1.2 KB, updated 08-19-2026

JSON sidecar (click to expand)
{
  "experiment": "E-023",
  "task": "3.8 support: D-037 assembly (short-segment max, S_inf lower bound validation, margin/N0/N1)",
  "script": "research/numerics/scripts/E-023-finite-height-assembly.py",
  "checks": {
    "V1_sinf_lower_holds": true,
    "V2_margin_positive_convex": true,
    "V3_lambda_positive_3000": true
  },
  "key_values": {
    "M0_grid_max": 1.3323650421270834,
    "M0_lipschitz": 1.4075796709577815,
    "M0_use": 1.335180201468999,
    "C1": -1.1303307007539063,
    "C1_lower_form": -1.4189385332046727,
    "sinf_lower_min_slack": 9.520071987843544,
    "N0_measured_constants": 30217,
    "N0_traced_constants": 32871,
    "margin_N0_traced": 0.0795673057436943,
    "margin_at": {
      "32871": 0.0795673057436943,
      "100000": 287298.63095573196,
      "1000000": 5256686.503113639,
      "1000000000": 8938721534.500193,
      "1000000000000": 12396419838890.791,
      "1000000000000000000": 1.9304327085473796e+19,
      "8999999999994000000000001": 2.457962538414764e+26
    },
    "N1": 8999999999994000000000001,
    "N1_conservative_lagarias_window": 2.725978018024122e+21,
    "T0_platt_trudgian": 3000000000000.0
  },
  "elapsed_seconds": 56.2
}

Source code

Python script (click to expand)
"""E-023 — task 3.8 support: D-037 assembly computation.

Three components:
  S1. Short-segment max M0 = max |zeta'/zeta(-5/2+it)| for |t| <= 10.
      CLOSES THE D-036 GAP: the |R(s)| <= A log(t+2)+B0 bound was
      E-021-verified only for |t| >= 10; segment (III) passes within
      0.5 of the trivial pole at w = -2 (digamma pole), so the uniform
      bound fails near t = 0. Method: mpmath dps 30 grid (step 0.002)
      via the E-021 xi-route + sampled-Lipschitz correction x2 safety.
      (Only feeds an additive O(1) constant; slack is enormous.)
  S2. Validate the explicit S_inf lower bound used in D-037:
        S_inf(n) >= (1/2) n log n + (C1 - gamma/2) n - 4   (n >= 20)
      against E-021's EXACT S_inf (n <= 5000, validated 4.8e-25).
  S3. Assemble the full |S_f(n)| bound B(n) (D-036 constants, A = 0.9,
      B0 = 1.0, M0 from S1) and the margin
        margin(n) = Sinf_lower(n) - B(n) + 1;
      find N0 (margin > 0 and increasing); print margin at reference
      points and at N1 = (T0-1)^2, T0 = 3e12 (Platt-Trudgian 2021);
      conservative fallback N1* = T0^2/(4 log^2 T0) (Lagarias (1.16)).

Checks:
  V1. S_inf lower bound holds for all 20 <= n <= 5000 (exact values).
  V2. margin(n) > 0 for all n in [N0, 1e7] (grid, step 1) and margin
      convex (2nd differences > 0) on a log grid to N1 — with
      convexity + margin(N0) > 0 + margin'(N0) > 0 this gives
      margin(n) > 0 for ALL n in [N0, N1] (analytic in D-037).
  V3. lambda_n > 0 reconfirmed for n <= 3000 from E-021 exact CSV.

Outputs: research/numerics/data/E-023-assembly.csv (+ .json)
"""
import json
import sys
import time
from pathlib import Path

import mpmath as mp
import numpy as np

REPO = Path(__file__).resolve().parents[3]
DATA = REPO / "research" / "numerics" / "data"
E021 = DATA / "E-021-sinf-sf-curve.csv"
CSV = DATA / "E-023-assembly.csv"
JSON = DATA / "E-023-assembly.json"

GAMMA = 0.577215664901532860606512090082402431
C1 = (GAMMA - 1 - np.log(2 * np.pi)) / 2          # -1.1303...
C1L = C1 - GAMMA / 2                              # -1.4189... (lower-bound form)
A_USE, B0_USE = 0.9, 1.0
T0_PLATT = 3.0e12


# ---------------------------------------------------------------- S1
def zeta_logder(s):
    """zeta'/zeta via xi'/xi minus explicit terms (E-021 route, dps 30)."""
    sm = mp.mpc(s.real, s.imag)

    def xi_mp(u):
        return 0.5 * u * (u - 1) * mp.pi ** (-u / 2) * mp.gamma(u / 2) \
            * mp.zeta(u)

    xi_log = mp.diff(xi_mp, sm) / xi_mp(sm)
    return complex(xi_log - 1 / sm - 1 / (sm - 1)
                   + mp.mpf(0.5) * mp.log(mp.pi)
                   - mp.mpf(0.5) * mp.digamma(sm / 2))


def short_segment_max():
    mp.mp.dps = 30
    ts = np.arange(-10.0, 10.0 + 1e-12, 0.002)
    vals = np.array([abs(zeta_logder(complex(-2.5, t))) for t in ts])
    gmax = float(vals.max())
    # sampled Lipschitz constant (max adjacent slope), x2 safety
    lips = float(np.max(np.abs(np.diff(vals))) / 0.002)
    m0 = gmax + 0.002 * lips          # (h/2)*L would be 0.001*L; x2
    return gmax, lips, m0


# ------------------------------------------------------- D-036 pieces
def F_count(T):
    return (T / (2 * np.pi)) * np.log(T / (2 * np.pi)) - T / (2 * np.pi) \
        + 7 / 8


def S_bound(T):
    return 0.137 * np.log(T) + 0.443 * np.log(np.log(T)) + 1.588


def C0(t):
    return (F_count(t + 1) - F_count(t)) + S_bound(t + 1) + S_bound(t)


def H_bound(T, a, b0):
    return 3 * C0(T) ** 2 + a * np.log(T + 2) + b0


def B_bound(n, m0, a=A_USE, b0=B0_USE):
    """Full |S_f(n)| upper bound (D-036, variant-B contour, T = sqrt(n)+1)."""
    n = float(n)
    T = np.sqrt(n) + 1.0
    twoN = 2 * (F_count(T) + S_bound(T))
    I3_long = 4 * T * ((2 / 3) * C0(T) + a * np.log(T + 2) + b0)
    I3_short = 40.0 * m0                      # |k|<=2, length 20, max m0
    I24 = 2 * (np.e ** 4 + 1) * (7 * H_bound(T, a, b0) + 3.3)
    # I1 in log space: log I1 = log(2 sqrt(n) (e^{sqrt(n)/2}+1) 0.5699)
    #   - (2 sqrt(n) - 2) log 2  ->  hugely negative for sqrt(n) > 60
    r = np.sqrt(n)
    logI1 = np.log(2 * r * 0.5699) + r / 2 - (2 * r - 2) * np.log(2)
    I1 = np.exp(logI1) if logI1 > -700 else 0.0
    triv = 1.0
    return twoN + I3_long + I3_short + I24 + I1 + triv


def sinf_lower(n):
    n = np.asarray(n, dtype=np.float64)
    return 0.5 * n * np.log(n) + C1L * n - 4.0


def margin(n, m0, a=A_USE, b0=B0_USE):
    return sinf_lower(n) - B_bound(n, m0, a, b0) + 1.0


def find_N0(m0, a, b0):
    ngrid = np.arange(20, 400001)
    marg = np.array([margin(n, m0, a, b0) for n in ngrid])
    pos = ngrid[(marg > 0) & (np.gradient(marg, ngrid) > 0)]
    N0 = int(pos[0]) if len(pos) else -1
    cont = bool(np.all(marg[ngrid >= N0] > 0)) if N0 > 0 else False
    return N0, cont, float(marg[ngrid == N0][0]) if N0 > 0 else float("nan")


def main() -> int:
    if JSON.exists() and "--force" not in sys.argv:
        print(f"cached: {JSON} (use --force to recompute)")
        return 0
    t0 = time.time()

    print("S1: short-segment max ...", flush=True)
    gmax, lips, m0 = short_segment_max()
    print(f"  grid max = {gmax:.4f}, Lipschitz {lips:.1f}, "
          f"M0 = {m0:.4f}", flush=True)

    print("S2: S_inf lower-bound validation vs E-021 exact ...", flush=True)
    import csv as csvmod
    with open(E021) as f:
        rows = list(csvmod.DictReader(f))
    ns = np.array([int(r["n"]) for r in rows])
    sinf = np.array([float(r["S_inf"]) for r in rows])
    lam = np.array([float(r["lambda_n"]) for r in rows])
    mask = ns >= 20
    slack = sinf[mask] - sinf_lower(ns[mask])
    v1 = bool(np.all(slack > 0))
    print(f"  V1: min slack = {slack.min():.4f} at n = "
          f"{ns[mask][slack.argmin()]} (bound holds: {v1})", flush=True)
    v3 = bool(np.all(lam[ns <= 3000] > 0))
    print(f"  V3: lambda_n > 0 for n <= 3000 reconfirmed: {v3}", flush=True)

    print("S3: margin assembly ...", flush=True)
    # Two constant sets: measured (0.9, 1.0) and traced (2.0, 2.0, the
    # D-036 first-estimate analytic assembly).
    N0_meas, cont_meas, margN0_meas = find_N0(m0, 0.9, 1.0)
    N0_tr, cont_tr, margN0_tr = find_N0(m0, 2.0, 2.0)
    N0, cont = N0_tr, cont_tr
    print(f"  N0 (measured 0.9/1.0) = {N0_meas} (contiguous {cont_meas})")
    print(f"  N0 (traced   2.0/2.0) = {N0_tr} (contiguous {cont_tr})")

    logn = np.unique(np.logspace(np.log10(max(N0, 20)), np.log10(9e24),
                                 4000).astype(np.float64))
    bmarg = np.array([margin(n, m0, 2.0, 2.0) for n in logn])
    conv = bool(np.all(np.diff(bmarg, 2) > -1e-6 * bmarg[:-2].max()))

    N1 = int(3_000_000_000_000 - 1) ** 2
    N1_cons = T0_PLATT ** 2 / (4 * np.log(T0_PLATT) ** 2)
    refs = {int(n): float(margin(n, m0, 2.0, 2.0)) for n in
            [N0, 100000, 10 ** 6, 10 ** 9, 10 ** 12, 10 ** 18, N1]}
    print(f"  N0 = {N0} (traced; contiguous to 4e5: {cont}, "
          f"convex: {conv})")
    print(f"  margin(N0) = {refs[N0]:.1f}")
    print(f"  N1 = (T0-1)^2 = {N1:.6e};  N1* (Lagarias window) = "
          f"{N1_cons:.6e}")
    print(f"  margin(N1) = {refs[N1]:.3e}")

    v2 = bool(N0 > 0 and cont and conv and cont_meas)

    with open(CSV, "w", newline="") as f:
        w = csvmod.writer(f)
        w.writerow(["n", "margin_traced", "margin_measured"])
        for n in range(20, 400001, 20):
            w.writerow([n, f"{margin(n, m0, 2.0, 2.0):.6f}",
                        f"{margin(n, m0, 0.9, 1.0):.6f}"])

    meta = {
        "experiment": "E-023",
        "task": "3.8 support: D-037 assembly (short-segment max, S_inf "
                "lower bound validation, margin/N0/N1)",
        "script": "research/numerics/scripts/E-023-finite-height-assembly.py",
        "checks": {"V1_sinf_lower_holds": v1,
                   "V2_margin_positive_convex": v2,
                   "V3_lambda_positive_3000": v3},
        "key_values": {
            "M0_grid_max": gmax, "M0_lipschitz": lips, "M0_use": m0,
            "C1": C1, "C1_lower_form": C1L,
            "sinf_lower_min_slack": float(slack.min()),
            "N0_measured_constants": N0_meas,
            "N0_traced_constants": N0_tr,
            "margin_N0_traced": margN0_tr,
            "margin_at": {str(k): float(v) for k, v in refs.items()},
            "N1": N1, "N1_conservative_lagarias_window": N1_cons,
            "T0_platt_trudgian": T0_PLATT,
        },
        "elapsed_seconds": round(time.time() - t0, 1),
    }
    JSON.write_text(json.dumps(meta, indent=2))
    print(f"  wrote {CSV.name}, {JSON.name}; "
          f"elapsed {meta['elapsed_seconds']}s")
    return 0 if (v1 and v2 and v3) else 1


if __name__ == "__main__":
    sys.exit(main())