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 18 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
8502 170020 622531.545896 641090.366414
8503 170040 622630.303779 641190.020832
8504 170060 622729.063087 641289.676625
8505 170080 622827.823820 641389.333795
8506 170100 622926.585977 641488.992339
8507 170120 623025.349559 641588.652258
8508 170140 623124.114565 641688.313553
8509 170160 623222.880995 641787.976222
8510 170180 623321.648849 641887.640265
8511 170200 623420.418126 641987.305683
8512 170220 623519.188827 642086.972474
8513 170240 623617.960951 642186.640640
8514 170260 623716.734498 642286.310180
8515 170280 623815.509468 642385.981093
8516 170300 623914.285861 642485.653379
8517 170320 624013.063676 642585.327039
8518 170340 624111.842913 642685.002072
8519 170360 624210.623573 642784.678477
8520 170380 624309.405654 642884.356256
8521 170400 624408.189158 642984.035407
8522 170420 624506.974082 643083.715930
8523 170440 624605.760428 643183.397825
8524 170460 624704.548196 643283.081092
8525 170480 624803.337384 643382.765731
8526 170500 624902.127993 643482.451742
8527 170520 625000.920023 643582.139124
8528 170540 625099.713473 643681.827878
8529 170560 625198.508344 643781.518002
8530 170580 625297.304634 643881.209497
8531 170600 625396.102344 643980.902363
8532 170620 625494.901474 644080.596600
8533 170640 625593.702024 644180.292207
8534 170660 625692.503992 644279.989184
8535 170680 625791.307380 644379.687531
8536 170700 625890.112187 644479.387248
8537 170720 625988.918413 644579.088334
8538 170740 626087.726057 644678.790790
8539 170760 626186.535119 644778.494615
8540 170780 626285.345600 644878.199810
8541 170800 626384.157498 644977.906373
8542 170820 626482.970814 645077.614305
8543 170840 626581.785548 645177.323605
8544 170860 626680.601700 645277.034274
8545 170880 626779.419268 645376.746311
8546 170900 626878.238254 645476.459716
8547 170920 626977.058656 645576.174489
8548 170940 627075.880476 645675.890630
8549 170960 627174.703711 645775.608138
8550 170980 627273.528363 645875.327014
8551 171000 627372.354431 645975.047256
8552 171020 627471.181915 646074.768866
8553 171040 627570.010815 646174.491842
8554 171060 627668.841130 646274.216185
8555 171080 627767.672861 646373.941894
8556 171100 627866.506006 646473.668970
8557 171120 627965.340567 646573.397411
8558 171140 628064.176543 646673.127219
8559 171160 628163.013933 646772.858392
8560 171180 628261.852737 646872.590931
8561 171200 628360.692956 646972.324835
8562 171220 628459.534589 647072.060104
8563 171240 628558.377636 647171.796738
8564 171260 628657.222096 647271.534737
8565 171280 628756.067970 647371.274101
8566 171300 628854.915258 647471.014829
8567 171320 628953.763958 647570.756922
8568 171340 629052.614071 647670.500378
8569 171360 629151.465597 647770.245199
8570 171380 629250.318536 647869.991383
8571 171400 629349.172887 647969.738931
8572 171420 629448.028650 648069.487842
8573 171440 629546.885825 648169.238116
8574 171460 629645.744412 648268.989754
8575 171480 629744.604411 648368.742754
8576 171500 629843.465821 648468.497117
8577 171520 629942.328642 648568.252842
8578 171540 630041.192875 648668.009930
8579 171560 630140.058518 648767.768380
8580 171580 630238.925572 648867.528192
8581 171600 630337.794036 648967.289366
8582 171620 630436.663911 649067.051901
8583 171640 630535.535196 649166.815798
8584 171660 630634.407891 649266.581056
8585 171680 630733.281996 649366.347675
8586 171700 630832.157510 649466.115655
8587 171720 630931.034434 649565.884996
8588 171740 631029.912767 649665.655697
8589 171760 631128.792509 649765.427759
8590 171780 631227.673660 649865.201181
8591 171800 631326.556219 649964.975963
8592 171820 631425.440187 650064.752105
8593 171840 631524.325564 650164.529606
8594 171860 631623.212348 650264.308467
8595 171880 631722.100540 650364.088688
8596 171900 631820.990141 650463.870267
8597 171920 631919.881148 650563.653206
8598 171940 632018.773563 650663.437503
8599 171960 632117.667385 650763.223159
8600 171980 632216.562614 650863.010173
8601 172000 632315.459250 650962.798546
8602 172020 632414.357293 651062.588276
8603 172040 632513.256742 651162.379365
8604 172060 632612.157598 651262.171811
8605 172080 632711.059859 651361.965615
8606 172100 632809.963526 651461.760776
8607 172120 632908.868599 651561.557295
8608 172140 633007.775078 651661.355170
8609 172160 633106.682962 651761.154403
8610 172180 633205.592251 651860.954992
8611 172200 633304.502945 651960.756937
8612 172220 633403.415044 652060.560239
8613 172240 633502.328547 652160.364897
8614 172260 633601.243455 652260.170911
8615 172280 633700.159767 652359.978281
8616 172300 633799.077483 652459.787006
8617 172320 633897.996603 652559.597087
8618 172340 633996.917127 652659.408523
8619 172360 634095.839054 652759.221314
8620 172380 634194.762384 652859.035461
8621 172400 634293.687118 652958.850962
8622 172420 634392.613255 653058.667817
8623 172440 634491.540794 653158.486027
8624 172460 634590.469736 653258.305591
8625 172480 634689.400080 653358.126509
8626 172500 634788.331827 653457.948781
8627 172520 634887.264975 653557.772407
8628 172540 634986.199526 653657.597386
8629 172560 635085.135478 653757.423719
8630 172580 635184.072831 653857.251405
8631 172600 635283.011586 653957.080444
8632 172620 635381.951742 654056.910835
8633 172640 635480.893299 654156.742580
8634 172660 635579.836257 654256.575676
8635 172680 635678.780615 654356.410125
8636 172700 635777.726374 654456.245927
8637 172720 635876.673533 654556.083080
8638 172740 635975.622091 654655.921585
8639 172760 636074.572050 654755.761441
8640 172780 636173.523408 654855.602649
8641 172800 636272.476166 654955.445208
8642 172820 636371.430323 655055.289119
8643 172840 636470.385880 655155.134380
8644 172860 636569.342835 655254.980992
8645 172880 636668.301189 655354.828954
8646 172900 636767.260941 655454.678267
8647 172920 636866.222092 655554.528930
8648 172940 636965.184641 655654.380943
8649 172960 637064.148588 655754.234306
8650 172980 637163.113933 655854.089019
8651 173000 637262.080675 655953.945081
8652 173020 637361.048815 656053.802492
8653 173040 637460.018352 656153.661253
8654 173060 637558.989286 656253.521363
8655 173080 637657.961618 656353.382821
8656 173100 637756.935346 656453.245628
8657 173120 637855.910470 656553.109784
8658 173140 637954.886991 656652.975288
8659 173160 638053.864908 656752.842140
8660 173180 638152.844221 656852.710340
8661 173200 638251.824930 656952.579887
8662 173220 638350.807035 657052.450783
8663 173240 638449.790535 657152.323025
8664 173260 638548.775430 657252.196615
8665 173280 638647.761720 657352.071552
8666 173300 638746.749405 657451.947836
8667 173320 638845.738485 657551.825467
8668 173340 638944.728960 657651.704444
8669 173360 639043.720829 657751.584768
8670 173380 639142.714092 657851.466438
8671 173400 639241.708749 657951.349454
8672 173420 639340.704800 658051.233816
8673 173440 639439.702245 658151.119523
8674 173460 639538.701083 658251.006576
8675 173480 639637.701314 658350.894974
8676 173500 639736.702939 658450.784718
8677 173520 639835.705956 658550.675807
8678 173540 639934.710366 658650.568240
8679 173560 640033.716169 658750.462018
8680 173580 640132.723364 658850.357140
8681 173600 640231.731951 658950.253607
8682 173620 640330.741931 659050.151418
8683 173640 640429.753302 659150.050573
8684 173660 640528.766064 659249.951072
8685 173680 640627.780219 659349.852914
8686 173700 640726.795764 659449.756100
8687 173720 640825.812701 659549.660629
8688 173740 640924.831029 659649.566501
8689 173760 641023.850747 659749.473717
8690 173780 641122.871856 659849.382274
8691 173800 641221.894355 659949.292175
8692 173820 641320.918245 660049.203418
8693 173840 641419.943524 660149.116003
8694 173860 641518.970194 660249.029930
8695 173880 641617.998253 660348.945199
8696 173900 641717.027701 660448.861810
8697 173920 641816.058539 660548.779763
8698 173940 641915.090766 660648.699056
8699 173960 642014.124382 660748.619691
8700 173980 642113.159387 660848.541667
8701 174000 642212.195780 660948.464984
8702 174020 642311.233562 661048.389642
8703 174040 642410.272732 661148.315640
8704 174060 642509.313290 661248.242978
8705 174080 642608.355235 661348.171657
8706 174100 642707.398569 661448.101676
8707 174120 642806.443290 661548.033034
8708 174140 642905.489398 661647.965733
8709 174160 643004.536894 661747.899770
8710 174180 643103.585776 661847.835147
8711 174200 643202.636046 661947.771863
8712 174220 643301.687702 662047.709919
8713 174240 643400.740744 662147.649312
8714 174260 643499.795173 662247.590045
8715 174280 643598.850987 662347.532116
8716 174300 643697.908188 662447.475525
8717 174320 643796.966774 662547.420273
8718 174340 643896.026746 662647.366358
8719 174360 643995.088103 662747.313782
8720 174380 644094.150845 662847.262542
8721 174400 644193.214973 662947.212641
8722 174420 644292.280485 663047.164076
8723 174440 644391.347382 663147.116849
8724 174460 644490.415663 663247.070959
8725 174480 644589.485328 663347.026405
8726 174500 644688.556378 663446.983188
8727 174520 644787.628812 663546.941307
8728 174540 644886.702629 663646.900763
8729 174560 644985.777830 663746.861554
8730 174580 645084.854414 663846.823682
8731 174600 645183.932382 663946.787145
8732 174620 645283.011732 664046.751944
8733 174640 645382.092466 664146.718079
8734 174660 645481.174582 664246.685548
8735 174680 645580.258080 664346.654353
8736 174700 645679.342961 664446.624492
8737 174720 645778.429224 664546.595966
8738 174740 645877.516869 664646.568775
8739 174760 645976.605896 664746.542918
8740 174780 646075.696304 664846.518395
8741 174800 646174.788094 664946.495207
8742 174820 646273.881265 665046.473352
8743 174840 646372.975817 665146.452831
8744 174860 646472.071750 665246.433643
8745 174880 646571.169064 665346.415789
8746 174900 646670.267758 665446.399268
8747 174920 646769.367833 665546.384080
8748 174940 646868.469287 665646.370224
8749 174960 646967.572122 665746.357702
8750 174980 647066.676337 665846.346512
8751 175000 647165.781931 665946.336654
8752 175020 647264.888905 666046.328129
8753 175040 647363.997258 666146.320935
8754 175060 647463.106990 666246.315073
8755 175080 647562.218101 666346.310543
8756 175100 647661.330591 666446.307345
8757 175120 647760.444459 666546.305477
8758 175140 647859.559706 666646.304941
8759 175160 647958.676331 666746.305736
8760 175180 648057.794334 666846.307862
8761 175200 648156.913715 666946.311318
8762 175220 648256.034474 667046.316105
8763 175240 648355.156610 667146.322222
8764 175260 648454.280124 667246.329669
8765 175280 648553.405014 667346.338446
8766 175300 648652.531282 667446.348553
8767 175320 648751.658927 667546.359990
8768 175340 648850.787948 667646.372756
8769 175360 648949.918345 667746.386852
8770 175380 649049.050119 667846.402276
8771 175400 649148.183269 667946.419029
8772 175420 649247.317795 668046.437112
8773 175440 649346.453697 668146.456522
8774 175460 649445.590974 668246.477262
8775 175480 649544.729626 668346.499329
8776 175500 649643.869654 668446.522725
8777 175520 649743.011057 668546.547449
8778 175540 649842.153835 668646.573500
8779 175560 649941.297987 668746.600879
8780 175580 650040.443514 668846.629585
8781 175600 650139.590415 668946.659619
8782 175620 650238.738691 669046.690980
8783 175640 650337.888340 669146.723667
8784 175660 650437.039363 669246.757682
8785 175680 650536.191760 669346.793023
8786 175700 650635.345531 669446.829690
8787 175720 650734.500674 669546.867684
8788 175740 650833.657191 669646.907004
8789 175760 650932.815080 669746.947650
8790 175780 651031.974343 669846.989621
8791 175800 651131.134978 669947.032918
8792 175820 651230.296985 670047.077541
8793 175840 651329.460365 670147.123488
8794 175860 651428.625116 670247.170761
8795 175880 651527.791240 670347.219359
8796 175900 651626.958735 670447.269281
8797 175920 651726.127602 670547.320528
8798 175940 651825.297840 670647.373100
8799 175960 651924.469449 670747.426996
8800 175980 652023.642429 670847.482215
8801 176000 652122.816780 670947.538759
8802 176020 652221.992502 671047.596626
8803 176040 652321.169594 671147.655817
8804 176060 652420.348056 671247.716332
8805 176080 652519.527889 671347.778169
8806 176100 652618.709091 671447.841330
8807 176120 652717.891664 671547.905814
8808 176140 652817.075605 671647.971620
8809 176160 652916.260917 671748.038749
8810 176180 653015.447597 671848.107200
8811 176200 653114.635647 671948.176974
8812 176220 653213.825065 672048.248069
8813 176240 653313.015852 672148.320487
8814 176260 653412.208008 672248.394226
8815 176280 653511.401532 672348.469287
8816 176300 653610.596424 672448.545669
8817 176320 653709.792685 672548.623373
8818 176340 653808.990313 672648.702397
8819 176360 653908.189308 672748.782742
8820 176380 654007.389672 672848.864409
8821 176400 654106.591402 672948.947395
8822 176420 654205.794500 673049.031702
8823 176440 654304.998965 673149.117330
8824 176460 654404.204796 673249.204277
8825 176480 654503.411994 673349.292544
8826 176500 654602.620559 673449.382131
8827 176520 654701.830490 673549.473038
8828 176540 654801.041787 673649.565264
8829 176560 654900.254449 673749.658809
8830 176580 654999.468478 673849.753673
8831 176600 655098.683872 673949.849857
8832 176620 655197.900632 674049.947358
8833 176640 655297.118757 674150.046179
8834 176660 655396.338246 674250.146318
8835 176680 655495.559101 674350.247775
8836 176700 655594.781321 674450.350550
8837 176720 655694.004904 674550.454643
8838 176740 655793.229853 674650.560054
8839 176760 655892.456165 674750.666782
8840 176780 655991.683842 674850.774828
8841 176800 656090.912882 674950.884191
8842 176820 656190.143286 675050.994871
8843 176840 656289.375054 675151.106868
8844 176860 656388.608184 675251.220182
8845 176880 656487.842678 675351.334812
8846 176900 656587.078535 675451.450759
8847 176920 656686.315755 675551.568022
8848 176940 656785.554337 675651.686601
8849 176960 656884.794282 675751.806496
8850 176980 656984.035589 675851.927707
8851 177000 657083.278258 675952.050233
8852 177020 657182.522289 676052.174075
8853 177040 657281.767682 676152.299232
8854 177060 657381.014436 676252.425704
8855 177080 657480.262552 676352.553491
8856 177100 657579.512029 676452.682593
8857 177120 657678.762867 676552.813009
8858 177140 657778.015066 676652.944740
8859 177160 657877.268626 676753.077785
8860 177180 657976.523546 676853.212144
8861 177200 658075.779826 676953.347817
8862 177220 658175.037467 677053.484804
8863 177240 658274.296468 677153.623104
8864 177260 658373.556829 677253.762718
8865 177280 658472.818549 677353.903645
8866 177300 658572.081629 677454.045885
8867 177320 658671.346068 677554.189439
8868 177340 658770.611866 677654.334305
8869 177360 658869.879023 677754.480483
8870 177380 658969.147540 677854.627974
8871 177400 659068.417414 677954.776777
8872 177420 659167.688648 678054.926893
8873 177440 659266.961239 678155.078320
8874 177460 659366.235189 678255.231059
8875 177480 659465.510496 678355.385110
8876 177500 659564.787162 678455.540472
8877 177520 659664.065185 678555.697146
8878 177540 659763.344565 678655.855130
8879 177560 659862.625303 678756.014426
8880 177580 659961.907397 678856.175032
8881 177600 660061.190849 678956.336949
8882 177620 660160.475658 679056.500177
8883 177640 660259.761823 679156.664715
8884 177660 660359.049344 679256.830563
8885 177680 660458.338222 679356.997721
8886 177700 660557.628455 679457.166188
8887 177720 660656.920045 679557.335966
8888 177740 660756.212990 679657.507053
8889 177760 660855.507291 679757.679449
8890 177780 660954.802947 679857.853154
8891 177800 661054.099959 679958.028169
8892 177820 661153.398325 680058.204492
8893 177840 661252.698046 680158.382124
8894 177860 661351.999122 680258.561064
8895 177880 661451.301553 680358.741313
8896 177900 661550.605338 680458.922870
8897 177920 661649.910477 680559.105735
8898 177940 661749.216970 680659.289908
8899 177960 661848.524817 680759.475388
8900 177980 661947.834017 680859.662176
8901 178000 662047.144571 680959.850272
8902 178020 662146.456478 681060.039674
8903 178040 662245.769739 681160.230384
8904 178060 662345.084352 681260.422400
8905 178080 662444.400319 681360.615723
8906 178100 662543.717637 681460.810353
8907 178120 662643.036309 681561.006289
8908 178140 662742.356332 681661.203532
8909 178160 662841.677708 681761.402080
8910 178180 662941.000436 681861.601934
8911 178200 663040.324515 681961.803094
8912 178220 663139.649946 682062.005560
8913 178240 663238.976729 682162.209331
8914 178260 663338.304863 682262.414407
8915 178280 663437.634347 682362.620788
8916 178300 663536.965183 682462.828474
8917 178320 663636.297370 682563.037465
8918 178340 663735.630907 682663.247761
8919 178360 663834.965794 682763.459361
8920 178380 663934.302032 682863.672265
8921 178400 664033.639620 682963.886473
8922 178420 664132.978558 683064.101986
8923 178440 664232.318845 683164.318802
8924 178460 664331.660482 683264.536921
8925 178480 664431.003469 683364.756345
8926 178500 664530.347804 683464.977071
8927 178520 664629.693489 683565.199100
8928 178540 664729.040523 683665.422433
8929 178560 664828.388905 683765.647068
8930 178580 664927.738636 683865.873006
8931 178600 665027.089715 683966.100247
8932 178620 665126.442142 684066.328789
8933 178640 665225.795918 684166.558634
8934 178660 665325.151041 684266.789781
8935 178680 665424.507512 684367.022230
8936 178700 665523.865331 684467.255980
8937 178720 665623.224496 684567.491032
8938 178740 665722.585010 684667.727385
8939 178760 665821.946870 684767.965039
8940 178780 665921.310077 684868.203995
8941 178800 666020.674630 684968.444251
8942 178820 666120.040530 685068.685808
8943 178840 666219.407777 685168.928665
8944 178860 666318.776369 685269.172823
8945 178880 666418.146308 685369.418281
8946 178900 666517.517592 685469.665039
8947 178920 666616.890222 685569.913097
8948 178940 666716.264198 685670.162455
8949 178960 666815.639519 685770.413112
8950 178980 666915.016185 685870.665069
8951 179000 667014.394196 685970.918325
8952 179020 667113.773552 686071.172880
8953 179040 667213.154252 686171.428734
8954 179060 667312.536297 686271.685887
8955 179080 667411.919686 686371.944338
8956 179100 667511.304420 686472.204088
8957 179120 667610.690497 686572.465135
8958 179140 667710.077918 686672.727482
8959 179160 667809.466683 686772.991126
8960 179180 667908.856791 686873.256067
8961 179200 668008.248243 686973.522307
8962 179220 668107.641037 687073.789844
8963 179240 668207.035175 687174.058678
8964 179260 668306.430655 687274.328809
8965 179280 668405.827478 687374.600238
8966 179300 668505.225644 687474.872963
8967 179320 668604.625151 687575.146985
8968 179340 668704.026001 687675.422303
8969 179360 668803.428193 687775.698918
8970 179380 668902.831727 687875.976829
8971 179400 669002.236602 687976.256035
8972 179420 669101.642818 688076.536538
8973 179440 669201.050376 688176.818337
8974 179460 669300.459275 688277.101431
8975 179480 669399.869515 688377.385820
8976 179500 669499.281096 688477.671504
8977 179520 669598.694017 688577.958484
8978 179540 669698.108279 688678.246759
8979 179560 669797.523881 688778.536328
8980 179580 669896.940823 688878.827192
8981 179600 669996.359105 688979.119350
8982 179620 670095.778727 689079.412802
8983 179640 670195.199688 689179.707549
8984 179660 670294.621989 689280.003590
8985 179680 670394.045629 689380.300924
8986 179700 670493.470608 689480.599552
8987 179720 670592.896926 689580.899473
8988 179740 670692.324583 689681.200688
8989 179760 670791.753579 689781.503196
8990 179780 670891.183913 689881.806996
8991 179800 670990.615585 689982.112090
8992 179820 671090.048595 690082.418476
8993 179840 671189.482943 690182.726155
8994 179860 671288.918629 690283.035126
8995 179880 671388.355652 690383.345389
8996 179900 671487.794013 690483.656944
8997 179920 671587.233711 690583.969792
8998 179940 671686.674746 690684.283930
8999 179960 671786.117118 690784.599361
9000 179980 671885.560827 690884.916082
9001 180000 671985.005873 690985.234095

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