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 22 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
10502 210020 822672.190932 842935.462305
10503 210040 822773.475375 843037.559043
10504 210060 822874.760941 843139.656871
10505 210080 822976.047632 843241.755786
10506 210100 823077.335448 843343.855791
10507 210120 823178.624388 843445.956883
10508 210140 823279.914452 843548.059064
10509 210160 823381.205640 843650.162332
10510 210180 823482.497952 843752.266689
10511 210200 823583.791388 843854.372133
10512 210220 823685.085947 843956.478665
10513 210240 823786.381630 844058.586285
10514 210260 823887.678437 844160.694992
10515 210280 823988.976367 844262.804787
10516 210300 824090.275420 844364.915669
10517 210320 824191.575596 844467.027638
10518 210340 824292.876895 844569.140694
10519 210360 824394.179317 844671.254836
10520 210380 824495.482862 844773.370066
10521 210400 824596.787530 844875.486382
10522 210420 824698.093320 844977.603785
10523 210440 824799.400232 845079.722275
10524 210460 824900.708266 845181.841850
10525 210480 825002.017423 845283.962512
10526 210500 825103.327702 845386.084260
10527 210520 825204.639103 845488.207094
10528 210540 825305.951625 845590.331014
10529 210560 825407.265269 845692.456020
10530 210580 825508.580035 845794.582111
10531 210600 825609.895922 845896.709288
10532 210620 825711.212930 845998.837551
10533 210640 825812.531060 846100.966898
10534 210660 825913.850311 846203.097331
10535 210680 826015.170682 846305.228849
10536 210700 826116.492175 846407.361452
10537 210720 826217.814788 846509.495140
10538 210740 826319.138522 846611.629913
10539 210760 826420.463377 846713.765770
10540 210780 826521.789351 846815.902712
10541 210800 826623.116446 846918.040738
10542 210820 826724.444661 847020.179848
10543 210840 826825.773997 847122.320043
10544 210860 826927.104452 847224.461322
10545 210880 827028.436026 847326.603685
10546 210900 827129.768721 847428.747131
10547 210920 827231.102535 847530.891662
10548 210940 827332.437469 847633.037275
10549 210960 827433.773521 847735.183973
10550 210980 827535.110693 847837.331754
10551 211000 827636.448984 847939.480618
10552 211020 827737.788394 848041.630565
10553 211040 827839.128923 848143.781596
10554 211060 827940.470571 848245.933709
10555 211080 828041.813337 848348.086905
10556 211100 828143.157222 848450.241184
10557 211120 828244.502225 848552.396546
10558 211140 828345.848346 848654.552990
10559 211160 828447.195586 848756.710516
10560 211180 828548.543943 848858.869125
10561 211200 828649.893418 848961.028815
10562 211220 828751.244012 849063.189588
10563 211240 828852.595722 849165.351443
10564 211260 828953.948551 849267.514380
10565 211280 829055.302496 849369.678398
10566 211300 829156.657560 849471.843498
10567 211320 829258.013740 849574.009679
10568 211340 829359.371037 849676.176942
10569 211360 829460.729451 849778.345286
10570 211380 829562.088983 849880.514711
10571 211400 829663.449631 849982.685218
10572 211420 829764.811395 850084.856805
10573 211440 829866.174276 850187.029473
10574 211460 829967.538273 850289.203221
10575 211480 830068.903387 850391.378051
10576 211500 830170.269617 850493.553960
10577 211520 830271.636962 850595.730951
10578 211540 830373.005424 850697.909021
10579 211560 830474.375002 850800.088171
10580 211580 830575.745695 850902.268402
10581 211600 830677.117503 851004.449712
10582 211620 830778.490427 851106.632103
10583 211640 830879.864467 851208.815572
10584 211660 830981.239621 851311.000122
10585 211680 831082.615891 851413.185751
10586 211700 831183.993276 851515.372459
10587 211720 831285.371775 851617.560247
10588 211740 831386.751390 851719.749113
10589 211760 831488.132118 851821.939059
10590 211780 831589.513962 851924.130084
10591 211800 831690.896919 852026.322187
10592 211820 831792.280991 852128.515369
10593 211840 831893.666178 852230.709630
10594 211860 831995.052478 852332.904969
10595 211880 832096.439892 852435.101386
10596 211900 832197.828420 852537.298882
10597 211920 832299.218061 852639.497456
10598 211940 832400.608816 852741.697108
10599 211960 832502.000685 852843.897837
10600 211980 832603.393667 852946.099645
10601 212000 832704.787762 853048.302530
10602 212020 832806.182970 853150.506493
10603 212040 832907.579291 853252.711533
10604 212060 833008.976725 853354.917651
10605 212080 833110.375272 853457.124845
10606 212100 833211.774932 853559.333117
10607 212120 833313.175704 853661.542466
10608 212140 833414.577588 853763.752892
10609 212160 833515.980585 853865.964395
10610 212180 833617.384693 853968.176974
10611 212200 833718.789914 854070.390630
10612 212220 833820.196247 854172.605362
10613 212240 833921.603692 854274.821170
10614 212260 834023.012248 854377.038055
10615 212280 834124.421916 854479.256016
10616 212300 834225.832695 854581.475053
10617 212320 834327.244586 854683.695166
10618 212340 834428.657588 854785.916355
10619 212360 834530.071701 854888.138619
10620 212380 834631.486925 854990.361959
10621 212400 834732.903260 855092.586374
10622 212420 834834.320705 855194.811865
10623 212440 834935.739262 855297.038430
10624 212460 835037.158929 855399.266071
10625 212480 835138.579706 855501.494787
10626 212500 835240.001593 855603.724578
10627 212520 835341.424591 855705.955444
10628 212540 835442.848699 855808.187384
10629 212560 835544.273917 855910.420399
10630 212580 835645.700245 856012.654488
10631 212600 835747.127682 856114.889652
10632 212620 835848.556229 856217.125890
10633 212640 835949.985885 856319.363202
10634 212660 836051.416651 856421.601588
10635 212680 836152.848526 856523.841048
10636 212700 836254.281511 856626.081581
10637 212720 836355.715604 856728.323189
10638 212740 836457.150806 856830.565869
10639 212760 836558.587117 856932.809624
10640 212780 836660.024537 857035.054451
10641 212800 836761.463065 857137.300352
10642 212820 836862.902702 857239.547326
10643 212840 836964.343447 857341.795373
10644 212860 837065.785300 857444.044493
10645 212880 837167.228261 857546.294686
10646 212900 837268.672331 857648.545951
10647 212920 837370.117508 857750.798289
10648 212940 837471.563793 857853.051700
10649 212960 837573.011185 857955.306182
10650 212980 837674.459686 858057.561737
10651 213000 837775.909293 858159.818364
10652 213020 837877.360008 858262.076064
10653 213040 837978.811830 858364.334835
10654 213060 838080.264759 858466.594677
10655 213080 838181.718795 858568.855592
10656 213100 838283.173938 858671.117578
10657 213120 838384.630187 858773.380635
10658 213140 838486.087544 858875.644764
10659 213160 838587.546006 858977.909964
10660 213180 838689.005575 859080.176235
10661 213200 838790.466251 859182.443578
10662 213220 838891.928032 859284.711991
10663 213240 838993.390920 859386.981475
10664 213260 839094.854913 859489.252029
10665 213280 839196.320012 859591.523654
10666 213300 839297.786217 859693.796350
10667 213320 839399.253527 859796.070116
10668 213340 839500.721943 859898.344952
10669 213360 839602.191464 860000.620859
10670 213380 839703.662091 860102.897835
10671 213400 839805.133822 860205.175881
10672 213420 839906.606659 860307.454998
10673 213440 840008.080600 860409.735183
10674 213460 840109.555646 860512.016439
10675 213480 840211.031797 860614.298764
10676 213500 840312.509053 860716.582158
10677 213520 840413.987412 860818.866621
10678 213540 840515.466876 860921.152154
10679 213560 840616.947445 861023.438756
10680 213580 840718.429117 861125.726426
10681 213600 840819.911893 861228.015166
10682 213620 840921.395773 861330.304974
10683 213640 841022.880757 861432.595851
10684 213660 841124.366844 861534.887796
10685 213680 841225.854035 861637.180809
10686 213700 841327.342330 861739.474891
10687 213720 841428.831727 861841.770041
10688 213740 841530.322228 861944.066259
10689 213760 841631.813832 862046.363545
10690 213780 841733.306539 862148.661899
10691 213800 841834.800348 862250.961321
10692 213820 841936.295260 862353.261810
10693 213840 842037.791275 862455.563366
10694 213860 842139.288392 862557.865990
10695 213880 842240.786612 862660.169682
10696 213900 842342.285934 862762.474440
10697 213920 842443.786358 862864.780266
10698 213940 842545.287884 862967.087158
10699 213960 842646.790512 863069.395117
10700 213980 842748.294241 863171.704144
10701 214000 842849.799073 863274.014236
10702 214020 842951.305005 863376.325395
10703 214040 843052.812040 863478.637621
10704 214060 843154.320175 863580.950913
10705 214080 843255.829412 863683.265271
10706 214100 843357.339750 863785.580695
10707 214120 843458.851189 863887.897185
10708 214140 843560.363729 863990.214741
10709 214160 843661.877369 864092.533363
10710 214180 843763.392111 864194.853050
10711 214200 843864.907952 864297.173803
10712 214220 843966.424894 864399.495621
10713 214240 844067.942937 864501.818505
10714 214260 844169.462079 864604.142454
10715 214280 844270.982322 864706.467468
10716 214300 844372.503665 864808.793547
10717 214320 844474.026107 864911.120690
10718 214340 844575.549649 865013.448899
10719 214360 844677.074291 865115.778172
10720 214380 844778.600032 865218.108510
10721 214400 844880.126873 865320.439912
10722 214420 844981.654813 865422.772379
10723 214440 845083.183852 865525.105909
10724 214460 845184.713990 865627.440504
10725 214480 845286.245227 865729.776163
10726 214500 845387.777563 865832.112886
10727 214520 845489.310998 865934.450672
10728 214540 845590.845531 866036.789522
10729 214560 845692.381163 866139.129436
10730 214580 845793.917893 866241.470413
10731 214600 845895.455721 866343.812454
10732 214620 845996.994647 866446.155558
10733 214640 846098.534672 866548.499725
10734 214660 846200.075794 866650.844955
10735 214680 846301.618014 866753.191248
10736 214700 846403.161332 866855.538603
10737 214720 846504.705747 866957.887022
10738 214740 846606.251260 867060.236503
10739 214760 846707.797870 867162.587046
10740 214780 846809.345578 867264.938652
10741 214800 846910.894382 867367.291320
10742 214820 847012.444284 867469.645050
10743 214840 847113.995282 867571.999842
10744 214860 847215.547377 867674.355697
10745 214880 847317.100569 867776.712613
10746 214900 847418.654857 867879.070591
10747 214920 847520.210242 867981.429630
10748 214940 847621.766723 868083.789731
10749 214960 847723.324301 868186.150893
10750 214980 847824.882974 868288.513117
10751 215000 847926.442743 868390.876401
10752 215020 848028.003609 868493.240747
10753 215040 848129.565570 868595.606154
10754 215060 848231.128626 868697.972622
10755 215080 848332.692779 868800.340150
10756 215100 848434.258026 868902.708739
10757 215120 848535.824369 869005.078389
10758 215140 848637.391807 869107.449099
10759 215160 848738.960340 869209.820869
10760 215180 848840.529968 869312.193699
10761 215200 848942.100691 869414.567590
10762 215220 849043.672509 869516.942541
10763 215240 849145.245422 869619.318551
10764 215260 849246.819428 869721.695621
10765 215280 849348.394530 869824.073751
10766 215300 849449.970725 869926.452941
10767 215320 849551.548015 870028.833190
10768 215340 849653.126399 870131.214498
10769 215360 849754.705876 870233.596865
10770 215380 849856.286448 870335.980292
10771 215400 849957.868113 870438.364777
10772 215420 850059.450872 870540.750322
10773 215440 850161.034724 870643.136925
10774 215460 850262.619670 870745.524587
10775 215480 850364.205709 870847.913308
10776 215500 850465.792841 870950.303087
10777 215520 850567.381067 871052.693924
10778 215540 850668.970385 871155.085820
10779 215560 850770.560796 871257.478774
10780 215580 850872.152300 871359.872786
10781 215600 850973.744896 871462.267856
10782 215620 851075.338585 871564.663984
10783 215640 851176.933366 871667.061169
10784 215660 851278.529239 871769.459412
10785 215680 851380.126205 871871.858712
10786 215700 851481.724262 871974.259070
10787 215720 851583.323412 872076.660486
10788 215740 851684.923653 872179.062958
10789 215760 851786.524986 872281.466488
10790 215780 851888.127411 872383.871074
10791 215800 851989.730927 872486.276717
10792 215820 852091.335534 872588.683417
10793 215840 852192.941233 872691.091174
10794 215860 852294.548023 872793.499987
10795 215880 852396.155904 872895.909857
10796 215900 852497.764875 872998.320783
10797 215920 852599.374938 873100.732765
10798 215940 852700.986091 873203.145804
10799 215960 852802.598335 873305.559898
10800 215980 852904.211669 873407.975049
10801 216000 853005.826094 873510.391255
10802 216020 853107.441609 873612.808516
10803 216040 853209.058214 873715.226834
10804 216060 853310.675909 873817.646206
10805 216080 853412.294694 873920.066635
10806 216100 853513.914569 874022.488118
10807 216120 853615.535533 874124.910657
10808 216140 853717.157587 874227.334250
10809 216160 853818.780730 874329.758899
10810 216180 853920.404963 874432.184602
10811 216200 854022.030285 874534.611360
10812 216220 854123.656696 874637.039173
10813 216240 854225.284197 874739.468040
10814 216260 854326.912786 874841.897962
10815 216280 854428.542463 874944.328937
10816 216300 854530.173230 875046.760968
10817 216320 854631.805085 875149.194052
10818 216340 854733.438028 875251.628190
10819 216360 854835.072060 875354.063382
10820 216380 854936.707180 875456.499627
10821 216400 855038.343389 875558.936927
10822 216420 855139.980685 875661.375280
10823 216440 855241.619069 875763.814686
10824 216460 855343.258541 875866.255146
10825 216480 855444.899100 875968.696659
10826 216500 855546.540747 876071.139225
10827 216520 855648.183482 876173.582844
10828 216540 855749.827304 876276.027516
10829 216560 855851.472213 876378.473240
10830 216580 855953.118209 876480.920018
10831 216600 856054.765292 876583.367848
10832 216620 856156.413463 876685.816730
10833 216640 856258.062719 876788.266665
10834 216660 856359.713063 876890.717652
10835 216680 856461.364493 876993.169692
10836 216700 856563.017010 877095.622783
10837 216720 856664.670613 877198.076926
10838 216740 856766.325302 877300.532121
10839 216760 856867.981077 877402.988368
10840 216780 856969.637938 877505.445667
10841 216800 857071.295885 877607.904017
10842 216820 857172.954918 877710.363418
10843 216840 857274.615037 877812.823871
10844 216860 857376.276241 877915.285375
10845 216880 857477.938531 878017.747930
10846 216900 857579.601905 878120.211536
10847 216920 857681.266366 878222.676192
10848 216940 857782.931911 878325.141900
10849 216960 857884.598541 878427.608658
10850 216980 857986.266256 878530.076467
10851 217000 858087.935056 878632.545326
10852 217020 858189.604941 878735.015236
10853 217040 858291.275910 878837.486196
10854 217060 858392.947964 878939.958206
10855 217080 858494.621101 879042.431266
10856 217100 858596.295324 879144.905376
10857 217120 858697.970630 879247.380536
10858 217140 858799.647020 879349.856745
10859 217160 858901.324494 879452.334005
10860 217180 859003.003052 879554.812313
10861 217200 859104.682694 879657.291671
10862 217220 859206.363419 879759.772079
10863 217240 859308.045228 879862.253535
10864 217260 859409.728120 879964.736041
10865 217280 859511.412095 880067.219595
10866 217300 859613.097153 880169.704199
10867 217320 859714.783295 880272.189851
10868 217340 859816.470519 880374.676552
10869 217360 859918.158826 880477.164301
10870 217380 860019.848216 880579.653099
10871 217400 860121.538688 880682.142945
10872 217420 860223.230243 880784.633839
10873 217440 860324.922880 880887.125782
10874 217460 860426.616599 880989.618772
10875 217480 860528.311401 881092.112811
10876 217500 860630.007284 881194.607897
10877 217520 860731.704249 881297.104031
10878 217540 860833.402296 881399.601213
10879 217560 860935.101425 881502.099442
10880 217580 861036.801636 881604.598718
10881 217600 861138.502928 881707.099042
10882 217620 861240.205301 881809.600413
10883 217640 861341.908755 881912.102831
10884 217660 861443.613291 882014.606295
10885 217680 861545.318908 882117.110807
10886 217700 861647.025605 882219.616366
10887 217720 861748.733383 882322.122971
10888 217740 861850.442242 882424.630623
10889 217760 861952.152182 882527.139321
10890 217780 862053.863202 882629.649065
10891 217800 862155.575303 882732.159856
10892 217820 862257.288483 882834.671693
10893 217840 862359.002744 882937.184576
10894 217860 862460.718085 883039.698504
10895 217880 862562.434506 883142.213479
10896 217900 862664.152006 883244.729499
10897 217920 862765.870587 883347.246565
10898 217940 862867.590247 883449.764676
10899 217960 862969.310986 883552.283833
10900 217980 863071.032805 883654.804035
10901 218000 863172.755703 883757.325282
10902 218020 863274.479680 883859.847574
10903 218040 863376.204736 883962.370912
10904 218060 863477.930871 884064.895294
10905 218080 863579.658085 884167.420721
10906 218100 863681.386378 884269.947192
10907 218120 863783.115749 884372.474708
10908 218140 863884.846199 884475.003268
10909 218160 863986.577728 884577.532873
10910 218180 864088.310334 884680.063522
10911 218200 864190.044019 884782.595215
10912 218220 864291.778781 884885.127952
10913 218240 864393.514622 884987.661733
10914 218260 864495.251540 885090.196558
10915 218280 864596.989537 885192.732427
10916 218300 864698.728610 885295.269339
10917 218320 864800.468762 885397.807295
10918 218340 864902.209991 885500.346294
10919 218360 865003.952297 885602.886336
10920 218380 865105.695680 885705.427422
10921 218400 865207.440140 885807.969550
10922 218420 865309.185677 885910.512722
10923 218440 865410.932292 886013.056936
10924 218460 865512.679982 886115.602193
10925 218480 865614.428750 886218.148493
10926 218500 865716.178594 886320.695836
10927 218520 865817.929514 886423.244220
10928 218540 865919.681511 886525.793648
10929 218560 866021.434584 886628.344117
10930 218580 866123.188733 886730.895629
10931 218600 866224.943958 886833.448182
10932 218620 866326.700259 886936.001778
10933 218640 866428.457636 887038.556415
10934 218660 866530.216088 887141.112094
10935 218680 866631.975616 887243.668815
10936 218700 866733.736219 887346.226577
10937 218720 866835.497898 887448.785380
10938 218740 866937.260651 887551.345225
10939 218760 867039.024480 887653.906111
10940 218780 867140.789384 887756.468039
10941 218800 867242.555363 887859.031007
10942 218820 867344.322417 887961.595016
10943 218840 867446.090545 888064.160066
10944 218860 867547.859748 888166.726156
10945 218880 867649.630025 888269.293287
10946 218900 867751.401377 888371.861459
10947 218920 867853.173803 888474.430670
10948 218940 867954.947303 888577.000922
10949 218960 868056.721877 888679.572215
10950 218980 868158.497525 888782.144547
10951 219000 868260.274246 888884.717919
10952 219020 868362.052042 888987.292331
10953 219040 868463.830911 889089.867783
10954 219060 868565.610853 889192.444275
10955 219080 868667.391869 889295.021806
10956 219100 868769.173958 889397.600376
10957 219120 868870.957121 889500.179986
10958 219140 868972.741356 889602.760635
10959 219160 869074.526664 889705.342323
10960 219180 869176.313045 889807.925050
10961 219200 869278.100499 889910.508816
10962 219220 869379.889025 890013.093621
10963 219240 869481.678624 890115.679464
10964 219260 869583.469296 890218.266346
10965 219280 869685.261039 890320.854267
10966 219300 869787.053855 890423.443226
10967 219320 869888.847743 890526.033223
10968 219340 869990.642702 890628.624258
10969 219360 870092.438734 890731.216332
10970 219380 870194.235837 890833.809443
10971 219400 870296.034012 890936.403592
10972 219420 870397.833259 891038.998779
10973 219440 870499.633577 891141.595004
10974 219460 870601.434966 891244.192266
10975 219480 870703.237427 891346.790566
10976 219500 870805.040958 891449.389903
10977 219520 870906.845561 891551.990278
10978 219540 871008.651234 891654.591689
10979 219560 871110.457978 891757.194138
10980 219580 871212.265793 891859.797623
10981 219600 871314.074678 891962.402145
10982 219620 871415.884634 892065.007704
10983 219640 871517.695660 892167.614300
10984 219660 871619.507757 892270.221932
10985 219680 871721.320923 892372.830601
10986 219700 871823.135160 892475.440305
10987 219720 871924.950466 892578.051046
10988 219740 872026.766842 892680.662824
10989 219760 872128.584288 892783.275637
10990 219780 872230.402804 892885.889486
10991 219800 872332.222389 892988.504371
10992 219820 872434.043043 893091.120291
10993 219840 872535.864767 893193.737247
10994 219860 872637.687559 893296.355239
10995 219880 872739.511421 893398.974266
10996 219900 872841.336352 893501.594328
10997 219920 872943.162351 893604.215426
10998 219940 873044.989420 893706.837559
10999 219960 873146.817557 893809.460726
11000 219980 873248.646762 893912.084928
11001 220000 873350.477036 894014.710166

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