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

← prev page 21 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
10002 200020 772173.393043 792025.893249
10003 200040 772274.099718 792127.430901
10004 200060 772374.807580 792228.969701
10005 200080 772475.516630 792330.509651
10006 200100 772576.226867 792432.050748
10007 200120 772676.938291 792533.592994
10008 200140 772777.650902 792635.136388
10009 200160 772878.364699 792736.680930
10010 200180 772979.079683 792838.226621
10011 200200 773079.795854 792939.773458
10012 200220 773180.513211 793041.321444
10013 200240 773281.231754 793142.870577
10014 200260 773381.951483 793244.420857
10015 200280 773482.672399 793345.972285
10016 200300 773583.394500 793447.524860
10017 200320 773684.117786 793549.078582
10018 200340 773784.842259 793650.633451
10019 200360 773885.567917 793752.189466
10020 200380 773986.294760 793853.746628
10021 200400 774087.022788 793955.304937
10022 200420 774187.752001 794056.864392
10023 200440 774288.482399 794158.424994
10024 200460 774389.213982 794259.986741
10025 200480 774489.946750 794361.549635
10026 200500 774590.680702 794463.113674
10027 200520 774691.415839 794564.678859
10028 200540 774792.152159 794666.245190
10029 200560 774892.889664 794767.812667
10030 200580 774993.628353 794869.381288
10031 200600 775094.368226 794970.951055
10032 200620 775195.109282 795072.521968
10033 200640 775295.851522 795174.094025
10034 200660 775396.594945 795275.667227
10035 200680 775497.339552 795377.241574
10036 200700 775598.085342 795478.817065
10037 200720 775698.832315 795580.393701
10038 200740 775799.580471 795681.971481
10039 200760 775900.329810 795783.550406
10040 200780 776001.080331 795885.130475
10041 200800 776101.832035 795986.711687
10042 200820 776202.584922 796088.294044
10043 200840 776303.338990 796189.877544
10044 200860 776404.094241 796291.462188
10045 200880 776504.850674 796393.047976
10046 200900 776605.608288 796494.634907
10047 200920 776706.367085 796596.222981
10048 200940 776807.127063 796697.812198
10049 200960 776907.888222 796799.402558
10050 200980 777008.650563 796900.994061
10051 201000 777109.414085 797002.586707
10052 201020 777210.178788 797104.180496
10053 201040 777310.944673 797205.775427
10054 201060 777411.711738 797307.371500
10055 201080 777512.479983 797408.968716
10056 201100 777613.249409 797510.567073
10057 201120 777714.020016 797612.166573
10058 201140 777814.791803 797713.767215
10059 201160 777915.564770 797815.368998
10060 201180 778016.338917 797916.971923
10061 201200 778117.114244 798018.575989
10062 201220 778217.890751 798120.181197
10063 201240 778318.668438 798221.787546
10064 201260 778419.447304 798323.395036
10065 201280 778520.227349 798425.003668
10066 201300 778621.008574 798526.613440
10067 201320 778721.790978 798628.224352
10068 201340 778822.574561 798729.836406
10069 201360 778923.359323 798831.449600
10070 201380 779024.145263 798933.063934
10071 201400 779124.932382 799034.679409
10072 201420 779225.720680 799136.296023
10073 201440 779326.510156 799237.913778
10074 201460 779427.300810 799339.532673
10075 201480 779528.092642 799441.152707
10076 201500 779628.885652 799542.773881
10077 201520 779729.679840 799644.396194
10078 201540 779830.475206 799746.019647
10079 201560 779931.271749 799847.644239
10080 201580 780032.069469 799949.269970
10081 201600 780132.868367 800050.896840
10082 201620 780233.668443 800152.524849
10083 201640 780334.469695 800254.153997
10084 201660 780435.272124 800355.784284
10085 201680 780536.075730 800457.415708
10086 201700 780636.880512 800559.048272
10087 201720 780737.686471 800660.681973
10088 201740 780838.493607 800762.316813
10089 201760 780939.301918 800863.952791
10090 201780 781040.111406 800965.589906
10091 201800 781140.922070 801067.228160
10092 201820 781241.733910 801168.867551
10093 201840 781342.546925 801270.508079
10094 201860 781443.361116 801372.149745
10095 201880 781544.176483 801473.792548
10096 201900 781644.993025 801575.436488
10097 201920 781745.810742 801677.081565
10098 201940 781846.629634 801778.727780
10099 201960 781947.449701 801880.375130
10100 201980 782048.270944 801982.023618
10101 202000 782149.093360 802083.673242
10102 202020 782249.916952 802185.324002
10103 202040 782350.741717 802286.975899
10104 202060 782451.567657 802388.628932
10105 202080 782552.394772 802490.283101
10106 202100 782653.223060 802591.938406
10107 202120 782754.052522 802693.594846
10108 202140 782854.883158 802795.252422
10109 202160 782955.714968 802896.911134
10110 202180 783056.547951 802998.570981
10111 202200 783157.382108 803100.231964
10112 202220 783258.217438 803201.894081
10113 202240 783359.053941 803303.557334
10114 202260 783459.891617 803405.221721
10115 202280 783560.730466 803506.887243
10116 202300 783661.570488 803608.553900
10117 202320 783762.411682 803710.221692
10118 202340 783863.254049 803811.890618
10119 202360 783964.097589 803913.560678
10120 202380 784064.942300 804015.231872
10121 202400 784165.788184 804116.904200
10122 202420 784266.635239 804218.577663
10123 202440 784367.483467 804320.252259
10124 202460 784468.332866 804421.927989
10125 202480 784569.183437 804523.604852
10126 202500 784670.035179 804625.282848
10127 202520 784770.888093 804726.961978
10128 202540 784871.742178 804828.642242
10129 202560 784972.597434 804930.323638
10130 202580 785073.453861 805032.006167
10131 202600 785174.311459 805133.689829
10132 202620 785275.170227 805235.374624
10133 202640 785376.030166 805337.060551
10134 202660 785476.891275 805438.747610
10135 202680 785577.753555 805540.435802
10136 202700 785678.617005 805642.125127
10137 202720 785779.481625 805743.815583
10138 202740 785880.347415 805845.507171
10139 202760 785981.214375 805947.199891
10140 202780 786082.082504 806048.893743
10141 202800 786182.951803 806150.588726
10142 202820 786283.822272 806252.284841
10143 202840 786384.693909 806353.982087
10144 202860 786485.566716 806455.680464
10145 202880 786586.440692 806557.379972
10146 202900 786687.315837 806659.080611
10147 202920 786788.192150 806760.782382
10148 202940 786889.069632 806862.485282
10149 202960 786989.948283 806964.189314
10150 202980 787090.828102 807065.894476
10151 203000 787191.709089 807167.600768
10152 203020 787292.591245 807269.308190
10153 203040 787393.474568 807371.016743
10154 203060 787494.359060 807472.726426
10155 203080 787595.244719 807574.437238
10156 203100 787696.131545 807676.149180
10157 203120 787797.019539 807777.862252
10158 203140 787897.908701 807879.576453
10159 203160 787998.799030 807981.291784
10160 203180 788099.690526 808083.008244
10161 203200 788200.583188 808184.725833
10162 203220 788301.477018 808286.444551
10163 203240 788402.372014 808388.164397
10164 203260 788503.268177 808489.885373
10165 203280 788604.165507 808591.607477
10166 203300 788705.064003 808693.330710
10167 203320 788805.963665 808795.055071
10168 203340 788906.864493 808896.780561
10169 203360 789007.766487 808998.507178
10170 203380 789108.669647 809100.234924
10171 203400 789209.573972 809201.963797
10172 203420 789310.479463 809303.693799
10173 203440 789411.386120 809405.424927
10174 203460 789512.293942 809507.157184
10175 203480 789613.202929 809608.890568
10176 203500 789714.113081 809710.625079
10177 203520 789815.024398 809812.360717
10178 203540 789915.936880 809914.097482
10179 203560 790016.850527 810015.835375
10180 203580 790117.765338 810117.574394
10181 203600 790218.681313 810219.314540
10182 203620 790319.598453 810321.055812
10183 203640 790420.516757 810422.798211
10184 203660 790521.436225 810524.541736
10185 203680 790622.356857 810626.286387
10186 203700 790723.278653 810728.032164
10187 203720 790824.201612 810829.779067
10188 203740 790925.125735 810931.527097
10189 203760 791026.051022 811033.276251
10190 203780 791126.977471 811135.026532
10191 203800 791227.905084 811236.777938
10192 203820 791328.833860 811338.530469
10193 203840 791429.763798 811440.284125
10194 203860 791530.694900 811542.038907
10195 203880 791631.627164 811643.794813
10196 203900 791732.560591 811745.551844
10197 203920 791833.495180 811847.310000
10198 203940 791934.430931 811949.069281
10199 203960 792035.367844 812050.829686
10200 203980 792136.305920 812152.591216
10201 204000 792237.245157 812254.353870
10202 204020 792338.185556 812356.117647
10203 204040 792439.127116 812457.882549
10204 204060 792540.069839 812559.648575
10205 204080 792641.013722 812661.415725
10206 204100 792741.958767 812763.183998
10207 204120 792842.904973 812864.953394
10208 204140 792943.852340 812966.723914
10209 204160 793044.800867 813068.495558
10210 204180 793145.750556 813170.268324
10211 204200 793246.701405 813272.042214
10212 204220 793347.653415 813373.817226
10213 204240 793448.606584 813475.593362
10214 204260 793549.560915 813577.370620
10215 204280 793650.516405 813679.149000
10216 204300 793751.473055 813780.928503
10217 204320 793852.430865 813882.709128
10218 204340 793953.389835 813984.490875
10219 204360 794054.349964 814086.273745
10220 204380 794155.311253 814188.057736
10221 204400 794256.273702 814289.842850
10222 204420 794357.237309 814391.629084
10223 204440 794458.202076 814493.416441
10224 204460 794559.168001 814595.204919
10225 204480 794660.135086 814696.994518
10226 204500 794761.103329 814798.785239
10227 204520 794862.072730 814900.577080
10228 204540 794963.043290 815002.370043
10229 204560 795064.015009 815104.164126
10230 204580 795164.987886 815205.959331
10231 204600 795265.961920 815307.755655
10232 204620 795366.937113 815409.553101
10233 204640 795467.913464 815511.351666
10234 204660 795568.890972 815613.151352
10235 204680 795669.869638 815714.952158
10236 204700 795770.849461 815816.754084
10237 204720 795871.830442 815918.557130
10238 204740 795972.812580 816020.361296
10239 204760 796073.795875 816122.166581
10240 204780 796174.780326 816223.972986
10241 204800 796275.765935 816325.780511
10242 204820 796376.752701 816427.589154
10243 204840 796477.740622 816529.398917
10244 204860 796578.729701 816631.209799
10245 204880 796679.719936 816733.021800
10246 204900 796780.711327 816834.834919
10247 204920 796881.703874 816936.649158
10248 204940 796982.697577 817038.464514
10249 204960 797083.692435 817140.280990
10250 204980 797184.688450 817242.098583
10251 205000 797285.685620 817343.917295
10252 205020 797386.683945 817445.737125
10253 205040 797487.683426 817547.558073
10254 205060 797588.684062 817649.380139
10255 205080 797689.685853 817751.203322
10256 205100 797790.688799 817853.027623
10257 205120 797891.692900 817954.853042
10258 205140 797992.698155 818056.679578
10259 205160 798093.704566 818158.507231
10260 205180 798194.712130 818260.336001
10261 205200 798295.720849 818362.165889
10262 205220 798396.730722 818463.996893
10263 205240 798497.741749 818565.829014
10264 205260 798598.753930 818667.662251
10265 205280 798699.767265 818769.496605
10266 205300 798800.781753 818871.332076
10267 205320 798901.797395 818973.168663
10268 205340 799002.814191 819075.006366
10269 205360 799103.832140 819176.845185
10270 205380 799204.851242 819278.685120
10271 205400 799305.871497 819380.526171
10272 205420 799406.892905 819482.368337
10273 205440 799507.915466 819584.211619
10274 205460 799608.939179 819686.056017
10275 205480 799709.964045 819787.901530
10276 205500 799810.990064 819889.748158
10277 205520 799912.017235 819991.595901
10278 205540 800013.045558 820093.444759
10279 205560 800114.075033 820195.294732
10280 205580 800215.105660 820297.145820
10281 205600 800316.137439 820398.998022
10282 205620 800417.170369 820500.851339
10283 205640 800518.204452 820602.705770
10284 205660 800619.239685 820704.561316
10285 205680 800720.276070 820806.417975
10286 205700 800821.313606 820908.275749
10287 205720 800922.352293 821010.134637
10288 205740 801023.392131 821111.994638
10289 205760 801124.433120 821213.855753
10290 205780 801225.475260 821315.717981
10291 205800 801326.518550 821417.581323
10292 205820 801427.562991 821519.445779
10293 205840 801528.608581 821621.311347
10294 205860 801629.655323 821723.178029
10295 205880 801730.703214 821825.045823
10296 205900 801831.752255 821926.914730
10297 205920 801932.802446 822028.784750
10298 205940 802033.853786 822130.655883
10299 205960 802134.906277 822232.528128
10300 205980 802235.959916 822334.401486
10301 206000 802337.014705 822436.275955
10302 206020 802438.070644 822538.151537
10303 206040 802539.127731 822640.028231
10304 206060 802640.185967 822741.906036
10305 206080 802741.245352 822843.784954
10306 206100 802842.305886 822945.664983
10307 206120 802943.367568 823047.546123
10308 206140 803044.430399 823149.428375
10309 206160 803145.494378 823251.311739
10310 206180 803246.559506 823353.196213
10311 206200 803347.625781 823455.081799
10312 206220 803448.693204 823556.968495
10313 206240 803549.761776 823658.856302
10314 206260 803650.831495 823760.745220
10315 206280 803751.902361 823862.635249
10316 206300 803852.974375 823964.526388
10317 206320 803954.047536 824066.418637
10318 206340 804055.121845 824168.311996
10319 206360 804156.197301 824270.206466
10320 206380 804257.273903 824372.102046
10321 206400 804358.351653 824473.998735
10322 206420 804459.430549 824575.896534
10323 206440 804560.510592 824677.795443
10324 206460 804661.591781 824779.695461
10325 206480 804762.674117 824881.596589
10326 206500 804863.757598 824983.498826
10327 206520 804964.842226 825085.402172
10328 206540 805065.928000 825187.306627
10329 206560 805167.014920 825289.212191
10330 206580 805268.102986 825391.118864
10331 206600 805369.192197 825493.026646
10332 206620 805470.282553 825594.935536
10333 206640 805571.374055 825696.845535
10334 206660 805672.466703 825798.756642
10335 206680 805773.560495 825900.668857
10336 206700 805874.655432 826002.582180
10337 206720 805975.751514 826104.496611
10338 206740 806076.848741 826206.412150
10339 206760 806177.947113 826308.328797
10340 206780 806279.046629 826410.246551
10341 206800 806380.147290 826512.165413
10342 206820 806481.249094 826614.085382
10343 206840 806582.352043 826716.006459
10344 206860 806683.456136 826817.928643
10345 206880 806784.561373 826919.851933
10346 206900 806885.667753 827021.776331
10347 206920 806986.775277 827123.701835
10348 206940 807087.883945 827225.628447
10349 206960 807188.993756 827327.556164
10350 206980 807290.104710 827429.484988
10351 207000 807391.216808 827531.414919
10352 207020 807492.330048 827633.345955
10353 207040 807593.444432 827735.278098
10354 207060 807694.559958 827837.211347
10355 207080 807795.676627 827939.145701
10356 207100 807896.794438 828041.081162
10357 207120 807997.913392 828143.017728
10358 207140 808099.033488 828244.955399
10359 207160 808200.154726 828346.894176
10360 207180 808301.277106 828448.834058
10361 207200 808402.400629 828550.775045
10362 207220 808503.525293 828652.717137
10363 207240 808604.651098 828754.660334
10364 207260 808705.778046 828856.604636
10365 207280 808806.906134 828958.550043
10366 207300 808908.035364 829060.496554
10367 207320 809009.165735 829162.444170
10368 207340 809110.297248 829264.392890
10369 207360 809211.429901 829366.342714
10370 207380 809312.563695 829468.293642
10371 207400 809413.698629 829570.245674
10372 207420 809514.834705 829672.198811
10373 207440 809615.971920 829774.153050
10374 207460 809717.110276 829876.108394
10375 207480 809818.249772 829978.064841
10376 207500 809919.390409 830080.022391
10377 207520 810020.532185 830181.981045
10378 207540 810121.675101 830283.940802
10379 207560 810222.819157 830385.901662
10380 207580 810323.964352 830487.863624
10381 207600 810425.110687 830589.826690
10382 207620 810526.258161 830691.790858
10383 207640 810627.406775 830793.756129
10384 207660 810728.556527 830895.722502
10385 207680 810829.707419 830997.689978
10386 207700 810930.859449 831099.658556
10387 207720 811032.012619 831201.628236
10388 207740 811133.166926 831303.599018
10389 207760 811234.322373 831405.570902
10390 207780 811335.478957 831507.543887
10391 207800 811436.636680 831609.517974
10392 207820 811537.795541 831711.493163
10393 207840 811638.955540 831813.469453
10394 207860 811740.116677 831915.446845
10395 207880 811841.278952 832017.425337
10396 207900 811942.442364 832119.404931
10397 207920 812043.606914 832221.385626
10398 207940 812144.772601 832323.367421
10399 207960 812245.939426 832425.350317
10400 207980 812347.107388 832527.334314
10401 208000 812448.276486 832629.319411
10402 208020 812549.446722 832731.305609
10403 208040 812650.618094 832833.292907
10404 208060 812751.790603 832935.281305
10405 208080 812852.964249 833037.270803
10406 208100 812954.139031 833139.261401
10407 208120 813055.314950 833241.253098
10408 208140 813156.492004 833343.245895
10409 208160 813257.670195 833445.239792
10410 208180 813358.849521 833547.234789
10411 208200 813460.029984 833649.230884
10412 208220 813561.211582 833751.228079
10413 208240 813662.394315 833853.226373
10414 208260 813763.578185 833955.225766
10415 208280 813864.763189 834057.226257
10416 208300 813965.949329 834159.227848
10417 208320 814067.136603 834261.230537
10418 208340 814168.325013 834363.234324
10419 208360 814269.514558 834465.239210
10420 208380 814370.705237 834567.245194
10421 208400 814471.897051 834669.252277
10422 208420 814573.089999 834771.260457
10423 208440 814674.284082 834873.269735
10424 208460 814775.479299 834975.280111
10425 208480 814876.675650 835077.291585
10426 208500 814977.873136 835179.304156
10427 208520 815079.071755 835281.317825
10428 208540 815180.271507 835383.332591
10429 208560 815281.472394 835485.348455
10430 208580 815382.674414 835587.365415
10431 208600 815483.877567 835689.383473
10432 208620 815585.081854 835791.402627
10433 208640 815686.287274 835893.422878
10434 208660 815787.493826 835995.444226
10435 208680 815888.701512 836097.466670
10436 208700 815989.910331 836199.490211
10437 208720 816091.120282 836301.514848
10438 208740 816192.331366 836403.540581
10439 208760 816293.543582 836505.567410
10440 208780 816394.756930 836607.595335
10441 208800 816495.971411 836709.624357
10442 208820 816597.187024 836811.654473
10443 208840 816698.403768 836913.685686
10444 208860 816799.621645 837015.717994
10445 208880 816900.840653 837117.751397
10446 208900 817002.060793 837219.785895
10447 208920 817103.282064 837321.821489
10448 208940 817204.504467 837423.858178
10449 208960 817305.728001 837525.895961
10450 208980 817406.952666 837627.934840
10451 209000 817508.178462 837729.974813
10452 209020 817609.405389 837832.015881
10453 209040 817710.633446 837934.058043
10454 209060 817811.862634 838036.101299
10455 209080 817913.092953 838138.145650
10456 209100 818014.324402 838240.191095
10457 209120 818115.556982 838342.237634
10458 209140 818216.790691 838444.285266
10459 209160 818318.025531 838546.333993
10460 209180 818419.261500 838648.383813
10461 209200 818520.498599 838750.434726
10462 209220 818621.736828 838852.486733
10463 209240 818722.976187 838954.539834
10464 209260 818824.216674 839056.594027
10465 209280 818925.458292 839158.649314
10466 209300 819026.701038 839260.705694
10467 209320 819127.944913 839362.763166
10468 209340 819229.189918 839464.821731
10469 209360 819330.436051 839566.881389
10470 209380 819431.683313 839668.942139
10471 209400 819532.931703 839771.003982
10472 209420 819634.181222 839873.066917
10473 209440 819735.431869 839975.130944
10474 209460 819836.683645 840077.196063
10475 209480 819937.936548 840179.262274
10476 209500 820039.190580 840281.329577
10477 209520 820140.445740 840383.397972
10478 209540 820241.702027 840485.467458
10479 209560 820342.959442 840587.538036
10480 209580 820444.217984 840689.609705
10481 209600 820545.477654 840791.682465
10482 209620 820646.738451 840893.756316
10483 209640 820748.000375 840995.831259
10484 209660 820849.263426 841097.907292
10485 209680 820950.527604 841199.984416
10486 209700 821051.792909 841302.062631
10487 209720 821153.059340 841404.141936
10488 209740 821254.326898 841506.222332
10489 209760 821355.595583 841608.303818
10490 209780 821456.865394 841710.386394
10491 209800 821558.136331 841812.470061
10492 209820 821659.408394 841914.554817
10493 209840 821760.681583 842016.640663
10494 209860 821861.955898 842118.727599
10495 209880 821963.231338 842220.815625
10496 209900 822064.507905 842322.904740
10497 209920 822165.785596 842424.994945
10498 209940 822267.064413 842527.086239
10499 209960 822368.344355 842629.178622
10500 209980 822469.625423 842731.272094
10501 210000 822570.907615 842833.366655

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())