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 17 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
8002 160020 573335.181455 591439.360185
8003 160040 573433.202071 591538.303172
8004 160060 573531.224213 591637.247631
8005 160080 573629.247882 591736.193562
8006 160100 573727.273076 591835.140965
8007 160120 573825.299795 591934.089840
8008 160140 573923.328040 592033.040185
8009 160160 574021.357811 592131.992002
8010 160180 574119.389105 592230.945289
8011 160200 574217.421925 592329.900048
8012 160220 574315.456269 592428.856276
8013 160240 574413.492137 592527.813975
8014 160260 574511.529530 592626.773144
8015 160280 574609.568446 592725.733783
8016 160300 574707.608886 592824.695891
8017 160320 574805.650849 592923.659469
8018 160340 574903.694335 593022.624516
8019 160360 575001.739344 593121.591032
8020 160380 575099.785876 593220.559017
8021 160400 575197.833930 593319.528471
8022 160420 575295.883507 593418.499393
8023 160440 575393.934606 593517.471783
8024 160460 575491.987227 593616.445641
8025 160480 575590.041369 593715.420967
8026 160500 575688.097032 593814.397760
8027 160520 575786.154217 593913.376021
8028 160540 575884.212923 594012.355749
8029 160560 575982.273150 594111.336944
8030 160580 576080.334897 594210.319606
8031 160600 576178.398165 594309.303734
8032 160620 576276.462953 594408.289329
8033 160640 576374.529260 594507.276390
8034 160660 576472.597088 594606.264917
8035 160680 576570.666435 594705.254909
8036 160700 576668.737301 594804.246367
8037 160720 576766.809686 594903.239290
8038 160740 576864.883590 595002.233679
8039 160760 576962.959013 595101.229532
8040 160780 577061.035954 595200.226850
8041 160800 577159.114413 595299.225633
8042 160820 577257.194391 595398.225880
8043 160840 577355.275886 595497.227591
8044 160860 577453.358899 595596.230766
8045 160880 577551.443429 595695.235405
8046 160900 577649.529476 595794.241507
8047 160920 577747.617040 595893.249072
8048 160940 577845.706121 595992.258101
8049 160960 577943.796718 596091.268592
8050 160980 578041.888832 596190.280546
8051 161000 578139.982461 596289.293963
8052 161020 578238.077607 596388.308841
8053 161040 578336.174268 596487.325182
8054 161060 578434.272445 596586.342985
8055 161080 578532.372136 596685.362249
8056 161100 578630.473343 596784.382975
8057 161120 578728.576065 596883.405162
8058 161140 578826.680301 596982.428810
8059 161160 578924.786052 597081.453919
8060 161180 579022.893316 597180.480489
8061 161200 579121.002095 597279.508519
8062 161220 579219.112387 597378.538009
8063 161240 579317.224193 597477.568959
8064 161260 579415.337512 597576.601369
8065 161280 579513.452344 597675.635239
8066 161300 579611.568690 597774.670568
8067 161320 579709.686547 597873.707356
8068 161340 579807.805917 597972.745604
8069 161360 579905.926800 598071.785310
8070 161380 580004.049194 598170.826475
8071 161400 580102.173101 598269.869098
8072 161420 580200.298518 598368.913179
8073 161440 580298.425448 598467.958719
8074 161460 580396.553888 598567.005716
8075 161480 580494.683839 598666.054171
8076 161500 580592.815301 598765.104083
8077 161520 580690.948274 598864.155452
8078 161540 580789.082757 598963.208279
8079 161560 580887.218750 599062.262562
8080 161580 580985.356253 599161.318302
8081 161600 581083.495266 599260.375498
8082 161620 581181.635788 599359.434150
8083 161640 581279.777819 599458.494259
8084 161660 581377.921360 599557.555823
8085 161680 581476.066409 599656.618842
8086 161700 581574.212967 599755.683317
8087 161720 581672.361033 599854.749247
8088 161740 581770.510607 599953.816633
8089 161760 581868.661690 600052.885473
8090 161780 581966.814280 600151.955767
8091 161800 582064.968378 600251.027516
8092 161820 582163.123983 600350.100719
8093 161840 582261.281096 600449.175376
8094 161860 582359.439715 600548.251487
8095 161880 582457.599841 600647.329051
8096 161900 582555.761473 600746.408069
8097 161920 582653.924612 600845.488540
8098 161940 582752.089257 600944.570463
8099 161960 582850.255408 601043.653840
8100 161980 582948.423064 601142.738669
8101 162000 583046.592226 601241.824950
8102 162020 583144.762893 601340.912684
8103 162040 583242.935065 601440.001870
8104 162060 583341.108742 601539.092507
8105 162080 583439.283924 601638.184596
8106 162100 583537.460610 601737.278136
8107 162120 583635.638800 601836.373127
8108 162140 583733.818494 601935.469569
8109 162160 583831.999692 602034.567462
8110 162180 583930.182394 602133.666806
8111 162200 584028.366599 602232.767600
8112 162220 584126.552307 602331.869844
8113 162240 584224.739518 602430.973538
8114 162260 584322.928231 602530.078682
8115 162280 584421.118447 602629.185275
8116 162300 584519.310166 602728.293318
8117 162320 584617.503386 602827.402810
8118 162340 584715.698109 602926.513750
8119 162360 584813.894333 603025.626140
8120 162380 584912.092058 603124.739978
8121 162400 585010.291285 603223.855264
8122 162420 585108.492013 603322.971999
8123 162440 585206.694242 603422.090181
8124 162460 585304.897971 603521.209811
8125 162480 585403.103201 603620.330889
8126 162500 585501.309931 603719.453414
8127 162520 585599.518161 603818.577386
8128 162540 585697.727891 603917.702806
8129 162560 585795.939120 604016.829672
8130 162580 585894.151848 604115.957984
8131 162600 585992.366076 604215.087743
8132 162620 586090.581803 604314.218948
8133 162640 586188.799029 604413.351599
8134 162660 586287.017753 604512.485695
8135 162680 586385.237975 604611.621238
8136 162700 586483.459695 604710.758225
8137 162720 586581.682914 604809.896658
8138 162740 586679.907630 604909.036536
8139 162760 586778.133844 605008.177858
8140 162780 586876.361554 605107.320625
8141 162800 586974.590762 605206.464836
8142 162820 587072.821467 605305.610492
8143 162840 587171.053668 605404.757591
8144 162860 587269.287366 605503.906135
8145 162880 587367.522560 605603.056121
8146 162900 587465.759251 605702.207551
8147 162920 587563.997437 605801.360425
8148 162940 587662.237118 605900.514741
8149 162960 587760.478295 605999.670500
8150 162980 587858.720967 606098.827702
8151 163000 587956.965134 606197.986346
8152 163020 588055.210796 606297.146432
8153 163040 588153.457953 606396.307960
8154 163060 588251.706604 606495.470930
8155 163080 588349.956749 606594.635341
8156 163100 588448.208388 606693.801194
8157 163120 588546.461521 606792.968488
8158 163140 588644.716147 606892.137223
8159 163160 588742.972266 606991.307399
8160 163180 588841.229879 607090.479016
8161 163200 588939.488985 607189.652072
8162 163220 589037.749583 607288.826569
8163 163240 589136.011674 607388.002506
8164 163260 589234.275257 607487.179883
8165 163280 589332.540332 607586.358700
8166 163300 589430.806899 607685.538955
8167 163320 589529.074958 607784.720650
8168 163340 589627.344508 607883.903784
8169 163360 589725.615550 607983.088357
8170 163380 589823.888082 608082.274369
8171 163400 589922.162106 608181.461819
8172 163420 590020.437620 608280.650707
8173 163440 590118.714624 608379.841033
8174 163460 590216.993119 608479.032797
8175 163480 590315.273103 608578.225998
8176 163500 590413.554578 608677.420637
8177 163520 590511.837542 608776.616713
8178 163540 590610.121995 608875.814227
8179 163560 590708.407938 608975.013177
8180 163580 590806.695369 609074.213564
8181 163600 590904.984290 609173.415387
8182 163620 591003.274699 609272.618646
8183 163640 591101.566596 609371.823342
8184 163660 591199.859982 609471.029473
8185 163680 591298.154855 609570.237040
8186 163700 591396.451216 609669.446043
8187 163720 591494.749065 609768.656480
8188 163740 591593.048401 609867.868353
8189 163760 591691.349224 609967.081661
8190 163780 591789.651534 610066.296403
8191 163800 591887.955331 610165.512580
8192 163820 591986.260614 610264.730191
8193 163840 592084.567384 610363.949236
8194 163860 592182.875640 610463.169715
8195 163880 592281.185381 610562.391628
8196 163900 592379.496609 610661.614975
8197 163920 592477.809321 610760.839754
8198 163940 592576.123519 610860.065967
8199 163960 592674.439203 610959.293613
8200 163980 592772.756371 611058.522691
8201 164000 592871.075023 611157.753202
8202 164020 592969.395160 611256.985145
8203 164040 593067.716782 611356.218521
8204 164060 593166.039887 611455.453328
8205 164080 593264.364476 611554.689567
8206 164100 593362.690549 611653.927238
8207 164120 593461.018106 611753.166340
8208 164140 593559.347145 611852.406873
8209 164160 593657.677668 611951.648837
8210 164180 593756.009673 612050.892232
8211 164200 593854.343161 612150.137057
8212 164220 593952.678132 612249.383313
8213 164240 594051.014584 612348.630999
8214 164260 594149.352519 612447.880115
8215 164280 594247.691935 612547.130660
8216 164300 594346.032833 612646.382636
8217 164320 594444.375213 612745.636040
8218 164340 594542.719073 612844.890874
8219 164360 594641.064415 612944.147137
8220 164380 594739.411237 613043.404829
8221 164400 594837.759540 613142.663949
8222 164420 594936.109323 613241.924498
8223 164440 595034.460587 613341.186475
8224 164460 595132.813330 613440.449880
8225 164480 595231.167553 613539.714713
8226 164500 595329.523256 613638.980974
8227 164520 595427.880438 613738.248662
8228 164540 595526.239099 613837.517777
8229 164560 595624.599239 613936.788320
8230 164580 595722.960858 614036.060289
8231 164600 595821.323955 614135.333685
8232 164620 595919.688531 614234.608507
8233 164640 596018.054585 614333.884756
8234 164660 596116.422117 614433.162431
8235 164680 596214.791126 614532.441531
8236 164700 596313.161613 614631.722058
8237 164720 596411.533578 614731.004010
8238 164740 596509.907019 614830.287387
8239 164760 596608.281937 614929.572189
8240 164780 596706.658332 615028.858417
8241 164800 596805.036204 615128.146069
8242 164820 596903.415552 615227.435145
8243 164840 597001.796376 615326.725646
8244 164860 597100.178675 615426.017571
8245 164880 597198.562451 615525.310920
8246 164900 597296.947702 615624.605693
8247 164920 597395.334428 615723.901890
8248 164940 597493.722629 615823.199510
8249 164960 597592.112306 615922.498553
8250 164980 597690.503457 616021.799019
8251 165000 597788.896082 616121.100907
8252 165020 597887.290182 616220.404219
8253 165040 597985.685755 616319.708953
8254 165060 598084.082803 616419.015109
8255 165080 598182.481324 616518.322687
8256 165100 598280.881319 616617.631687
8257 165120 598379.282787 616716.942109
8258 165140 598477.685728 616816.253952
8259 165160 598576.090142 616915.567216
8260 165180 598674.496028 617014.881901
8261 165200 598772.903387 617114.198008
8262 165220 598871.312219 617213.515535
8263 165240 598969.722522 617312.834482
8264 165260 599068.134297 617412.154850
8265 165280 599166.547544 617511.476638
8266 165300 599264.962262 617610.799846
8267 165320 599363.378451 617710.124473
8268 165340 599461.796112 617809.450520
8269 165360 599560.215243 617908.777987
8270 165380 599658.635845 618008.106872
8271 165400 599757.057918 618107.437177
8272 165420 599855.481460 618206.768900
8273 165440 599953.906473 618306.102042
8274 165460 600052.332956 618405.436602
8275 165480 600150.760908 618504.772581
8276 165500 600249.190329 618604.109977
8277 165520 600347.621220 618703.448792
8278 165540 600446.053580 618802.789023
8279 165560 600544.487409 618902.130673
8280 165580 600642.922706 619001.473739
8281 165600 600741.359472 619100.818223
8282 165620 600839.797706 619200.164123
8283 165640 600938.237408 619299.511440
8284 165660 601036.678577 619398.860174
8285 165680 601135.121215 619498.210324
8286 165700 601233.565320 619597.561890
8287 165720 601332.010892 619696.914871
8288 165740 601430.457931 619796.269269
8289 165760 601528.906437 619895.625082
8290 165780 601627.356409 619994.982310
8291 165800 601725.807848 620094.340954
8292 165820 601824.260753 620193.701012
8293 165840 601922.715125 620293.062485
8294 165860 602021.170962 620392.425373
8295 165880 602119.628264 620491.789675
8296 165900 602218.087032 620591.155391
8297 165920 602316.547266 620690.522521
8298 165940 602415.008964 620789.891065
8299 165960 602513.472127 620889.261023
8300 165980 602611.936755 620988.632394
8301 166000 602710.402847 621088.005178
8302 166020 602808.870403 621187.379376
8303 166040 602907.339424 621286.754986
8304 166060 603005.809908 621386.132009
8305 166080 603104.281856 621485.510444
8306 166100 603202.755267 621584.890291
8307 166120 603301.230142 621684.271551
8308 166140 603399.706479 621783.654222
8309 166160 603498.184280 621883.038305
8310 166180 603596.663542 621982.423800
8311 166200 603695.144268 622081.810706
8312 166220 603793.626456 622181.199023
8313 166240 603892.110105 622280.588751
8314 166260 603990.595217 622379.979890
8315 166280 604089.081790 622479.372440
8316 166300 604187.569824 622578.766399
8317 166320 604286.059320 622678.161769
8318 166340 604384.550277 622777.558549
8319 166360 604483.042695 622876.956739
8320 166380 604581.536573 622976.356338
8321 166400 604680.031912 623075.757347
8322 166420 604778.528711 623175.159765
8323 166440 604877.026970 623274.563592
8324 166460 604975.526688 623373.968828
8325 166480 605074.027867 623473.375472
8326 166500 605172.530505 623572.783525
8327 166520 605271.034602 623672.192987
8328 166540 605369.540158 623771.603856
8329 166560 605468.047173 623871.016133
8330 166580 605566.555647 623970.429818
8331 166600 605665.065579 624069.844911
8332 166620 605763.576970 624169.261411
8333 166640 605862.089818 624268.679318
8334 166660 605960.604125 624368.098632
8335 166680 606059.119889 624467.519353
8336 166700 606157.637110 624566.941480
8337 166720 606256.155789 624666.365014
8338 166740 606354.675925 624765.789954
8339 166760 606453.197518 624865.216300
8340 166780 606551.720567 624964.644052
8341 166800 606650.245073 625064.073209
8342 166820 606748.771035 625163.503772
8343 166840 606847.298453 625262.935740
8344 166860 606945.827327 625362.369114
8345 166880 607044.357657 625461.803892
8346 166900 607142.889442 625561.240075
8347 166920 607241.422683 625660.677662
8348 166940 607339.957378 625760.116654
8349 166960 607438.493529 625859.557049
8350 166980 607537.031134 625958.998849
8351 167000 607635.570194 626058.442053
8352 167020 607734.110708 626157.886660
8353 167040 607832.652676 626257.332670
8354 167060 607931.196098 626356.780084
8355 167080 608029.740974 626456.228901
8356 167100 608128.287303 626555.679120
8357 167120 608226.835086 626655.130742
8358 167140 608325.384322 626754.583767
8359 167160 608423.935010 626854.038194
8360 167180 608522.487152 626953.494023
8361 167200 608621.040746 627052.951254
8362 167220 608719.595792 627152.409886
8363 167240 608818.152290 627251.869920
8364 167260 608916.710241 627351.331356
8365 167280 609015.269643 627450.794192
8366 167300 609113.830497 627550.258429
8367 167320 609212.392802 627649.724068
8368 167340 609310.956558 627749.191106
8369 167360 609409.521765 627848.659546
8370 167380 609508.088423 627948.129385
8371 167400 609606.656531 628047.600624
8372 167420 609705.226090 628147.073263
8373 167440 609803.797099 628246.547302
8374 167460 609902.369558 628346.022740
8375 167480 610000.943467 628445.499578
8376 167500 610099.518826 628544.977815
8377 167520 610198.095633 628644.457450
8378 167540 610296.673890 628743.938484
8379 167560 610395.253596 628843.420917
8380 167580 610493.834751 628942.904748
8381 167600 610592.417355 629042.389977
8382 167620 610691.001406 629141.876604
8383 167640 610789.586907 629241.364629
8384 167660 610888.173855 629340.854052
8385 167680 610986.762251 629440.344872
8386 167700 611085.352094 629539.837089
8387 167720 611183.943385 629639.330703
8388 167740 611282.536123 629738.825714
8389 167760 611381.130309 629838.322121
8390 167780 611479.725941 629937.819925
8391 167800 611578.323020 630037.319126
8392 167820 611676.921545 630136.819722
8393 167840 611775.521517 630236.321715
8394 167860 611874.122934 630335.825103
8395 167880 611972.725798 630435.329886
8396 167900 612071.330107 630534.836065
8397 167920 612169.935862 630634.343639
8398 167940 612268.543062 630733.852609
8399 167960 612367.151707 630833.362973
8400 167980 612465.761797 630932.874731
8401 168000 612564.373331 631032.387884
8402 168020 612662.986311 631131.902431
8403 168040 612761.600734 631231.418372
8404 168060 612860.216602 631330.935708
8405 168080 612958.833914 631430.454436
8406 168100 613057.452669 631529.974559
8407 168120 613156.072868 631629.496074
8408 168140 613254.694510 631729.018983
8409 168160 613353.317596 631828.543285
8410 168180 613451.942124 631928.068979
8411 168200 613550.568095 632027.596066
8412 168220 613649.195509 632127.124545
8413 168240 613747.824365 632226.654417
8414 168260 613846.454663 632326.185681
8415 168280 613945.086404 632425.718336
8416 168300 614043.719586 632525.252383
8417 168320 614142.354210 632624.787822
8418 168340 614240.990275 632724.324651
8419 168360 614339.627781 632823.862872
8420 168380 614438.266729 632923.402484
8421 168400 614536.907117 633022.943487
8422 168420 614635.548946 633122.485880
8423 168440 614734.192215 633222.029663
8424 168460 614832.836925 633321.574837
8425 168480 614931.483074 633421.121400
8426 168500 615030.130664 633520.669354
8427 168520 615128.779693 633620.218697
8428 168540 615227.430162 633719.769429
8429 168560 615326.082070 633819.321551
8430 168580 615424.735417 633918.875061
8431 168600 615523.390203 634018.429961
8432 168620 615622.046427 634117.986249
8433 168640 615720.704090 634217.543926
8434 168660 615819.363192 634317.102991
8435 168680 615918.023732 634416.663445
8436 168700 616016.685709 634516.225286
8437 168720 616115.349125 634615.788515
8438 168740 616214.013977 634715.353131
8439 168760 616312.680268 634814.919136
8440 168780 616411.347995 634914.486527
8441 168800 616510.017159 635014.055305
8442 168820 616608.687761 635113.625470
8443 168840 616707.359798 635213.197022
8444 168860 616806.033272 635312.769960
8445 168880 616904.708183 635412.344285
8446 168900 617003.384529 635511.919996
8447 168920 617102.062311 635611.497092
8448 168940 617200.741529 635711.075575
8449 168960 617299.422182 635810.655443
8450 168980 617398.104271 635910.236696
8451 169000 617496.787795 636009.819335
8452 169020 617595.472753 636109.403358
8453 169040 617694.159146 636208.988767
8454 169060 617792.846974 636308.575560
8455 169080 617891.536235 636408.163738
8456 169100 617990.226931 636507.753300
8457 169120 618088.919061 636607.344246
8458 169140 618187.612625 636706.936576
8459 169160 618286.307622 636806.530289
8460 169180 618385.004052 636906.125386
8461 169200 618483.701916 637005.721867
8462 169220 618582.401212 637105.319731
8463 169240 618681.101942 637204.918978
8464 169260 618779.804103 637304.519607
8465 169280 618878.507698 637404.121620
8466 169300 618977.212724 637503.725014
8467 169320 619075.919182 637603.329791
8468 169340 619174.627072 637702.935951
8469 169360 619273.336394 637802.543492
8470 169380 619372.047147 637902.152415
8471 169400 619470.759332 638001.762719
8472 169420 619569.472947 638101.374405
8473 169440 619668.187993 638200.987472
8474 169460 619766.904470 638300.601920
8475 169480 619865.622378 638400.217748
8476 169500 619964.341715 638499.834958
8477 169520 620063.062483 638599.453548
8478 169540 620161.784681 638699.073518
8479 169560 620260.508308 638798.694868
8480 169580 620359.233365 638898.317598
8481 169600 620457.959851 638997.941708
8482 169620 620556.687766 639097.567198
8483 169640 620655.417111 639197.194066
8484 169660 620754.147884 639296.822314
8485 169680 620852.880085 639396.451941
8486 169700 620951.613715 639496.082947
8487 169720 621050.348773 639595.715332
8488 169740 621149.085259 639695.349095
8489 169760 621247.823173 639794.984236
8490 169780 621346.562514 639894.620755
8491 169800 621445.303283 639994.258653
8492 169820 621544.045479 640093.897928
8493 169840 621642.789103 640193.538580
8494 169860 621741.534152 640293.180610
8495 169880 621840.280629 640392.824017
8496 169900 621939.028532 640492.468801
8497 169920 622037.777862 640592.114962
8498 169940 622136.528617 640691.762500
8499 169960 622235.280798 640791.411414
8500 169980 622334.034406 640891.061705
8501 170000 622432.789438 640990.713371

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