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 14 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
6502 130020 428158.890523 444812.164044
6503 130040 428254.338114 444908.628650
6504 130060 428349.787641 445005.095118
6505 130080 428445.239103 445101.563448
6506 130100 428540.692501 445198.033639
6507 130120 428636.147833 445294.505691
6508 130140 428731.605100 445390.979604
6509 130160 428827.064300 445487.455378
6510 130180 428922.525435 445583.933011
6511 130200 429017.988503 445680.412504
6512 130220 429113.453504 445776.893857
6513 130240 429208.920438 445873.377068
6514 130260 429304.389305 445969.862139
6515 130280 429399.860103 446066.349068
6516 130300 429495.332834 446162.837855
6517 130320 429590.807495 446259.328500
6518 130340 429686.284088 446355.821002
6519 130360 429781.762612 446452.315361
6520 130380 429877.243065 446548.811578
6521 130400 429972.725449 446645.309650
6522 130420 430068.209763 446741.809579
6523 130440 430163.696006 446838.311364
6524 130460 430259.184177 446934.815004
6525 130480 430354.674278 447031.320500
6526 130500 430450.166307 447127.827850
6527 130520 430545.660264 447224.337055
6528 130540 430641.156148 447320.848114
6529 130560 430736.653960 447417.361027
6530 130580 430832.153698 447513.875793
6531 130600 430927.655364 447610.392413
6532 130620 431023.158955 447706.910886
6533 130640 431118.664473 447803.431211
6534 130660 431214.171916 447899.953388
6535 130680 431309.681284 447996.477418
6536 130700 431405.192578 448093.003299
6537 130720 431500.705795 448189.531031
6538 130740 431596.220937 448286.060615
6539 130760 431691.738003 448382.592049
6540 130780 431787.256993 448479.125333
6541 130800 431882.777906 448575.660468
6542 130820 431978.300741 448672.197452
6543 130840 432073.825499 448768.736285
6544 130860 432169.352179 448865.276967
6545 130880 432264.880782 448961.819499
6546 130900 432360.411305 449058.363878
6547 130920 432455.943750 449154.910106
6548 130940 432551.478116 449251.458181
6549 130960 432647.014402 449348.008104
6550 130980 432742.552608 449444.559874
6551 131000 432838.092734 449541.113490
6552 131020 432933.634780 449637.668954
6553 131040 433029.178744 449734.226263
6554 131060 433124.724628 449830.785418
6555 131080 433220.272430 449927.346418
6556 131100 433315.822150 450023.909264
6557 131120 433411.373788 450120.473955
6558 131140 433506.927343 450217.040490
6559 131160 433602.482816 450313.608869
6560 131180 433698.040205 450410.179092
6561 131200 433793.599510 450506.751159
6562 131220 433889.160732 450603.325069
6563 131240 433984.723869 450699.900822
6564 131260 434080.288922 450796.478418
6565 131280 434175.855890 450893.057856
6566 131300 434271.424773 450989.639135
6567 131320 434366.995570 451086.222257
6568 131340 434462.568281 451182.807220
6569 131360 434558.142906 451279.394023
6570 131380 434653.719445 451375.982668
6571 131400 434749.297896 451472.573153
6572 131420 434844.878260 451569.165478
6573 131440 434940.460537 451665.759642
6574 131460 435036.044725 451762.355646
6575 131480 435131.630825 451858.953489
6576 131500 435227.218837 451955.553171
6577 131520 435322.808760 452052.154691
6578 131540 435418.400593 452148.758050
6579 131560 435513.994337 452245.363246
6580 131580 435609.589991 452341.970280
6581 131600 435705.187554 452438.579151
6582 131620 435800.787027 452535.189858
6583 131640 435896.388409 452631.802403
6584 131660 435991.991700 452728.416783
6585 131680 436087.596899 452825.032999
6586 131700 436183.204006 452921.651051
6587 131720 436278.813021 453018.270939
6588 131740 436374.423943 453114.892661
6589 131760 436470.036772 453211.516217
6590 131780 436565.651507 453308.141609
6591 131800 436661.268150 453404.768834
6592 131820 436756.886698 453501.397892
6593 131840 436852.507152 453598.028784
6594 131860 436948.129511 453694.661510
6595 131880 437043.753775 453791.296067
6596 131900 437139.379944 453887.932458
6597 131920 437235.008017 453984.570680
6598 131940 437330.637995 454081.210734
6599 131960 437426.269876 454177.852620
6600 131980 437521.903660 454274.496336
6601 132000 437617.539347 454371.141884
6602 132020 437713.176937 454467.789262
6603 132040 437808.816430 454564.438470
6604 132060 437904.457824 454661.089508
6605 132080 438000.101120 454757.742376
6606 132100 438095.746318 454854.397073
6607 132120 438191.393416 454951.053598
6608 132140 438287.042415 455047.711953
6609 132160 438382.693315 455144.372136
6610 132180 438478.346114 455241.034146
6611 132200 438574.000814 455337.697984
6612 132220 438669.657412 455434.363650
6613 132240 438765.315910 455531.031143
6614 132260 438860.976306 455627.700462
6615 132280 438956.638601 455724.371608
6616 132300 439052.302794 455821.044580
6617 132320 439147.968884 455917.719377
6618 132340 439243.636872 456014.396001
6619 132360 439339.306757 456111.074449
6620 132380 439434.978539 456207.754722
6621 132400 439530.652217 456304.436819
6622 132420 439626.327791 456401.120741
6623 132440 439722.005261 456497.806487
6624 132460 439817.684626 456594.494056
6625 132480 439913.365886 456691.183448
6626 132500 440009.049041 456787.874664
6627 132520 440104.734090 456884.567702
6628 132540 440200.421034 456981.262562
6629 132560 440296.109871 457077.959244
6630 132580 440391.800601 457174.657748
6631 132600 440487.493225 457271.358074
6632 132620 440583.187741 457368.060220
6633 132640 440678.884150 457464.764187
6634 132660 440774.582451 457561.469975
6635 132680 440870.282644 457658.177582
6636 132700 440965.984728 457754.887010
6637 132720 441061.688703 457851.598257
6638 132740 441157.394569 457948.311323
6639 132760 441253.102326 458045.026208
6640 132780 441348.811972 458141.742912
6641 132800 441444.523509 458238.461434
6642 132820 441540.236935 458335.181773
6643 132840 441635.952250 458431.903931
6644 132860 441731.669453 458528.627905
6645 132880 441827.388546 458625.353697
6646 132900 441923.109526 458722.081306
6647 132920 442018.832394 458818.810730
6648 132940 442114.557150 458915.541971
6649 132960 442210.283793 459012.275028
6650 132980 442306.012323 459109.009900
6651 133000 442401.742739 459205.746587
6652 133020 442497.475041 459302.485089
6653 133040 442593.209229 459399.225405
6654 133060 442688.945303 459495.967536
6655 133080 442784.683262 459592.711480
6656 133100 442880.423106 459689.457238
6657 133120 442976.164835 459786.204809
6658 133140 443071.908447 459882.954194
6659 133160 443167.653944 459979.705390
6660 133180 443263.401324 460076.458400
6661 133200 443359.150587 460173.213221
6662 133220 443454.901734 460269.969854
6663 133240 443550.654762 460366.728298
6664 133260 443646.409674 460463.488554
6665 133280 443742.166467 460560.250620
6666 133300 443837.925141 460657.014496
6667 133320 443933.685697 460753.780183
6668 133340 444029.448134 460850.547680
6669 133360 444125.212452 460947.316986
6670 133380 444220.978650 461044.088101
6671 133400 444316.746728 461140.861026
6672 133420 444412.516685 461237.635759
6673 133440 444508.288522 461334.412300
6674 133460 444604.062238 461431.190649
6675 133480 444699.837832 461527.970806
6676 133500 444795.615305 461624.752770
6677 133520 444891.394656 461721.536542
6678 133540 444987.175885 461818.322120
6679 133560 445082.958991 461915.109505
6680 133580 445178.743974 462011.898695
6681 133600 445274.530834 462108.689692
6682 133620 445370.319570 462205.482494
6683 133640 445466.110183 462302.277101
6684 133660 445561.902671 462399.073513
6685 133680 445657.697034 462495.871730
6686 133700 445753.493273 462592.671751
6687 133720 445849.291387 462689.473576
6688 133740 445945.091375 462786.277205
6689 133760 446040.893237 462883.082637
6690 133780 446136.696973 462979.889872
6691 133800 446232.502582 463076.698910
6692 133820 446328.310065 463173.509751
6693 133840 446424.119420 463270.322393
6694 133860 446519.930648 463367.136838
6695 133880 446615.743748 463463.953083
6696 133900 446711.558721 463560.771131
6697 133920 446807.375564 463657.590979
6698 133940 446903.194279 463754.412627
6699 133960 446999.014865 463851.236076
6700 133980 447094.837321 463948.061325
6701 134000 447190.661648 464044.888374
6702 134020 447286.487845 464141.717222
6703 134040 447382.315911 464238.547869
6704 134060 447478.145847 464335.380315
6705 134080 447573.977652 464432.214559
6706 134100 447669.811325 464529.050602
6707 134120 447765.646867 464625.888442
6708 134140 447861.484276 464722.728080
6709 134160 447957.323554 464819.569515
6710 134180 448053.164698 464916.412747
6711 134200 448149.007710 465013.257776
6712 134220 448244.852589 465110.104601
6713 134240 448340.699334 465206.953222
6714 134260 448436.547945 465303.803639
6715 134280 448532.398422 465400.655851
6716 134300 448628.250764 465497.509858
6717 134320 448724.104972 465594.365660
6718 134340 448819.961044 465691.223257
6719 134360 448915.818981 465788.082648
6720 134380 449011.678782 465884.943832
6721 134400 449107.540447 465981.806811
6722 134420 449203.403975 466078.671582
6723 134440 449299.269367 466175.538147
6724 134460 449395.136621 466272.406504
6725 134480 449491.005738 466369.276654
6726 134500 449586.876718 466466.148595
6727 134520 449682.749559 466563.022329
6728 134540 449778.624262 466659.897854
6729 134560 449874.500826 466756.775170
6730 134580 449970.379252 466853.654277
6731 134600 450066.259537 466950.535174
6732 134620 450162.141684 467047.417862
6733 134640 450258.025690 467144.302339
6734 134660 450353.911556 467241.188607
6735 134680 450449.799281 467338.076663
6736 134700 450545.688866 467434.966509
6737 134720 450641.580309 467531.858143
6738 134740 450737.473611 467628.751566
6739 134760 450833.368771 467725.646777
6740 134780 450929.265788 467822.543776
6741 134800 451025.164663 467919.442562
6742 134820 451121.065396 468016.343136
6743 134840 451216.967985 468113.245497
6744 134860 451312.872430 468210.149644
6745 134880 451408.778732 468307.055577
6746 134900 451504.686890 468403.963297
6747 134920 451600.596903 468500.872802
6748 134940 451696.508772 468597.784092
6749 134960 451792.422496 468694.697168
6750 134980 451888.338074 468791.612028
6751 135000 451984.255506 468888.528673
6752 135020 452080.174793 468985.447103
6753 135040 452176.095933 469082.367316
6754 135060 452272.018927 469179.289313
6755 135080 452367.943773 469276.213093
6756 135100 452463.870473 469373.138656
6757 135120 452559.799024 469470.066002
6758 135140 452655.729428 469566.995130
6759 135160 452751.661684 469663.926040
6760 135180 452847.595791 469760.858732
6761 135200 452943.531749 469857.793206
6762 135220 453039.469559 469954.729460
6763 135240 453135.409218 470051.667496
6764 135260 453231.350728 470148.607312
6765 135280 453327.294088 470245.548909
6766 135300 453423.239297 470342.492286
6767 135320 453519.186356 470439.437442
6768 135340 453615.135263 470536.384378
6769 135360 453711.086020 470633.333093
6770 135380 453807.038624 470730.283586
6771 135400 453902.993077 470827.235858
6772 135420 453998.949377 470924.189909
6773 135440 454094.907524 471021.145737
6774 135460 454190.867519 471118.103343
6775 135480 454286.829360 471215.062726
6776 135500 454382.793048 471312.023886
6777 135520 454478.758582 471408.986823
6778 135540 454574.725962 471505.951537
6779 135560 454670.695187 471602.918026
6780 135580 454766.666257 471699.886292
6781 135600 454862.639173 471796.856332
6782 135620 454958.613933 471893.828149
6783 135640 455054.590537 471990.801740
6784 135660 455150.568985 472087.777105
6785 135680 455246.549276 472184.754245
6786 135700 455342.531411 472281.733159
6787 135720 455438.515389 472378.713847
6788 135740 455534.501210 472475.696308
6789 135760 455630.488873 472572.680542
6790 135780 455726.478378 472669.666549
6791 135800 455822.469724 472766.654329
6792 135820 455918.462912 472863.643881
6793 135840 456014.457942 472960.635205
6794 135860 456110.454812 473057.628301
6795 135880 456206.453522 473154.623167
6796 135900 456302.454073 473251.619805
6797 135920 456398.456463 473348.618214
6798 135940 456494.460694 473445.618393
6799 135960 456590.466763 473542.620343
6800 135980 456686.474671 473639.624062
6801 136000 456782.484418 473736.629551
6802 136020 456878.496003 473833.636809
6803 136040 456974.509427 473930.645836
6804 136060 457070.524687 474027.656632
6805 136080 457166.541786 474124.669196
6806 136100 457262.560721 474221.683529
6807 136120 457358.581493 474318.699629
6808 136140 457454.604102 474415.717496
6809 136160 457550.628546 474512.737131
6810 136180 457646.654827 474609.758533
6811 136200 457742.682943 474706.781701
6812 136220 457838.712894 474803.806636
6813 136240 457934.744680 474900.833337
6814 136260 458030.778301 474997.861803
6815 136280 458126.813755 475094.892035
6816 136300 458222.851044 475191.924033
6817 136320 458318.890167 475288.957795
6818 136340 458414.931123 475385.993321
6819 136360 458510.973911 475483.030612
6820 136380 458607.018533 475580.069667
6821 136400 458703.064987 475677.110485
6822 136420 458799.113273 475774.153067
6823 136440 458895.163391 475871.197412
6824 136460 458991.215340 475968.243519
6825 136480 459087.269121 476065.291390
6826 136500 459183.324732 476162.341022
6827 136520 459279.382174 476259.392416
6828 136540 459375.441446 476356.445572
6829 136560 459471.502549 476453.500490
6830 136580 459567.565480 476550.557168
6831 136600 459663.630241 476647.615607
6832 136620 459759.696831 476744.675807
6833 136640 459855.765250 476841.737766
6834 136660 459951.835497 476938.801486
6835 136680 460047.907573 477035.866965
6836 136700 460143.981476 477132.934203
6837 136720 460240.057206 477230.003201
6838 136740 460336.134764 477327.073957
6839 136760 460432.214149 477424.146471
6840 136780 460528.295360 477521.220744
6841 136800 460624.378397 477618.296774
6842 136820 460720.463260 477715.374562
6843 136840 460816.549949 477812.454107
6844 136860 460912.638463 477909.535409
6845 136880 461008.728803 478006.618468
6846 136900 461104.820967 478103.703283
6847 136920 461200.914955 478200.789854
6848 136940 461297.010767 478297.878181
6849 136960 461393.108404 478394.968263
6850 136980 461489.207863 478492.060100
6851 137000 461585.309146 478589.153693
6852 137020 461681.412252 478686.249040
6853 137040 461777.517180 478783.346141
6854 137060 461873.623931 478880.444997
6855 137080 461969.732503 478977.545606
6856 137100 462065.842898 479074.647968
6857 137120 462161.955113 479171.752084
6858 137140 462258.069150 479268.857952
6859 137160 462354.185007 479365.965574
6860 137180 462450.302685 479463.074947
6861 137200 462546.422182 479560.186072
6862 137220 462642.543500 479657.298949
6863 137240 462738.666637 479754.413578
6864 137260 462834.791593 479851.529957
6865 137280 462930.918369 479948.648088
6866 137300 463027.046962 480045.767969
6867 137320 463123.177374 480142.889600
6868 137340 463219.309605 480240.012981
6869 137360 463315.443652 480337.138112
6870 137380 463411.579517 480434.264992
6871 137400 463507.717200 480531.393621
6872 137420 463603.856698 480628.523999
6873 137440 463699.998014 480725.656126
6874 137460 463796.141145 480822.790001
6875 137480 463892.286093 480919.925623
6876 137500 463988.432856 481017.062994
6877 137520 464084.581434 481114.202112
6878 137540 464180.731827 481211.342976
6879 137560 464276.884035 481308.485588
6880 137580 464373.038057 481405.629946
6881 137600 464469.193893 481502.776050
6882 137620 464565.351543 481599.923900
6883 137640 464661.511006 481697.073496
6884 137660 464757.672283 481794.224837
6885 137680 464853.835372 481891.377923
6886 137700 464950.000274 481988.532754
6887 137720 465046.166988 482085.689329
6888 137740 465142.335513 482182.847649
6889 137760 465238.505851 482280.007712
6890 137780 465334.678000 482377.169519
6891 137800 465430.851959 482474.333070
6892 137820 465527.027730 482571.498363
6893 137840 465623.205311 482668.665399
6894 137860 465719.384702 482765.834178
6895 137880 465815.565902 482863.004699
6896 137900 465911.748913 482960.176961
6897 137920 466007.933732 483057.350965
6898 137940 466104.120360 483154.526711
6899 137960 466200.308797 483251.704197
6900 137980 466296.499042 483348.883425
6901 138000 466392.691095 483446.064392
6902 138020 466488.884956 483543.247100
6903 138040 466585.080624 483640.431548
6904 138060 466681.278100 483737.617735
6905 138080 466777.477382 483834.805662
6906 138100 466873.678470 483931.995328
6907 138120 466969.881365 484029.186732
6908 138140 467066.086066 484126.379875
6909 138160 467162.292572 484223.574756
6910 138180 467258.500883 484320.771374
6911 138200 467354.711000 484417.969731
6912 138220 467450.922921 484515.169825
6913 138240 467547.136646 484612.371655
6914 138260 467643.352176 484709.575223
6915 138280 467739.569509 484806.780527
6916 138300 467835.788646 484903.987567
6917 138320 467932.009586 485001.196342
6918 138340 468028.232329 485098.406854
6919 138360 468124.456874 485195.619101
6920 138380 468220.683222 485292.833083
6921 138400 468316.911371 485390.048799
6922 138420 468413.141323 485487.266250
6923 138440 468509.373075 485584.485435
6924 138460 468605.606629 485681.706354
6925 138480 468701.841984 485778.929007
6926 138500 468798.079139 485876.153393
6927 138520 468894.318094 485973.379512
6928 138540 468990.558849 486070.607363
6929 138560 469086.801404 486167.836947
6930 138580 469183.045757 486265.068264
6931 138600 469279.291910 486362.301312
6932 138620 469375.539862 486459.536092
6933 138640 469471.789612 486556.772603
6934 138660 469568.041160 486654.010845
6935 138680 469664.294506 486751.250818
6936 138700 469760.549649 486848.492521
6937 138720 469856.806590 486945.735955
6938 138740 469953.065327 487042.981118
6939 138760 470049.325861 487140.228011
6940 138780 470145.588192 487237.476633
6941 138800 470241.852318 487334.726985
6942 138820 470338.118240 487431.979065
6943 138840 470434.385958 487529.232874
6944 138860 470530.655470 487626.488411
6945 138880 470626.926778 487723.745675
6946 138900 470723.199880 487821.004668
6947 138920 470819.474776 487918.265388
6948 138940 470915.751466 488015.527835
6949 138960 471012.029950 488112.792008
6950 138980 471108.310227 488210.057908
6951 139000 471204.592297 488307.325535
6952 139020 471300.876160 488404.594887
6953 139040 471397.161815 488501.865965
6954 139060 471493.449262 488599.138769
6955 139080 471589.738502 488696.413297
6956 139100 471686.029532 488793.689550
6957 139120 471782.322354 488890.967528
6958 139140 471878.616968 488988.247230
6959 139160 471974.913371 489085.528656
6960 139180 472071.211565 489182.811806
6961 139200 472167.511549 489280.096679
6962 139220 472263.813323 489377.383275
6963 139240 472360.116887 489474.671595
6964 139260 472456.422239 489571.961636
6965 139280 472552.729381 489669.253400
6966 139300 472649.038311 489766.546886
6967 139320 472745.349029 489863.842094
6968 139340 472841.661536 489961.139023
6969 139360 472937.975830 490058.437673
6970 139380 473034.291911 490155.738045
6971 139400 473130.609780 490253.040136
6972 139420 473226.929435 490350.343949
6973 139440 473323.250877 490447.649481
6974 139460 473419.574106 490544.956733
6975 139480 473515.899120 490642.265704
6976 139500 473612.225920 490739.576395
6977 139520 473708.554505 490836.888804
6978 139540 473804.884875 490934.202932
6979 139560 473901.217030 491031.518779
6980 139580 473997.550970 491128.836344
6981 139600 474093.886693 491226.155626
6982 139620 474190.224201 491323.476626
6983 139640 474286.563492 491420.799343
6984 139660 474382.904566 491518.123777
6985 139680 474479.247424 491615.449928
6986 139700 474575.592064 491712.777795
6987 139720 474671.938487 491810.107379
6988 139740 474768.286691 491907.438678
6989 139760 474864.636678 492004.771693
6990 139780 474960.988446 492102.106423
6991 139800 475057.341995 492199.442868
6992 139820 475153.697326 492296.781028
6993 139840 475250.054437 492394.120902
6994 139860 475346.413328 492491.462491
6995 139880 475442.774000 492588.805793
6996 139900 475539.136451 492686.150809
6997 139920 475635.500681 492783.497538
6998 139940 475731.866691 492880.845981
6999 139960 475828.234480 492978.196136
7000 139980 475924.604048 493075.548004
7001 140000 476020.975393 493172.901583

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