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 6 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
2502 50020 67786.713418 79468.146389
2503 50040 67868.933421 79551.975965
2504 50060 67951.159625 79635.811424
2505 50080 68033.392026 79719.652765
2506 50100 68115.630620 79803.499984
2507 50120 68197.875404 79887.353079
2508 50140 68280.126376 79971.212046
2509 50160 68362.383532 80055.076882
2510 50180 68444.646869 80138.947584
2511 50200 68526.916384 80222.824150
2512 50220 68609.192073 80306.706576
2513 50240 68691.473934 80390.594859
2514 50260 68773.761963 80474.488997
2515 50280 68856.056157 80558.388986
2516 50300 68938.356513 80642.294823
2517 50320 69020.663027 80726.206506
2518 50340 69102.975698 80810.124032
2519 50360 69185.294520 80894.047397
2520 50380 69267.619492 80977.976598
2521 50400 69349.950610 81061.911633
2522 50420 69432.287870 81145.852499
2523 50440 69514.631271 81229.799192
2524 50460 69596.980808 81313.751710
2525 50480 69679.336479 81397.710050
2526 50500 69761.698280 81481.674209
2527 50520 69844.066209 81565.644183
2528 50540 69926.440261 81649.619971
2529 50560 70008.820435 81733.601568
2530 50580 70091.206726 81817.588973
2531 50600 70173.599132 81901.582181
2532 50620 70255.997650 81985.581191
2533 50640 70338.402276 82069.585999
2534 50660 70420.813008 82153.596603
2535 50680 70503.229842 82237.612999
2536 50700 70585.652775 82321.635184
2537 50720 70668.081804 82405.663156
2538 50740 70750.516926 82489.696912
2539 50760 70832.958138 82573.736449
2540 50780 70915.405437 82657.781763
2541 50800 70997.858820 82741.832853
2542 50820 71080.318283 82825.889714
2543 50840 71162.783823 82909.952345
2544 50860 71245.255438 82994.020742
2545 50880 71327.733125 83078.094902
2546 50900 71410.216880 83162.174823
2547 50920 71492.706700 83246.260501
2548 50940 71575.202582 83330.351935
2549 50960 71657.704523 83414.449120
2550 50980 71740.212520 83498.552054
2551 51000 71822.726570 83582.660734
2552 51020 71905.246670 83666.775157
2553 51040 71987.772817 83750.895320
2554 51060 72070.305008 83835.021222
2555 51080 72152.843239 83919.152857
2556 51100 72235.387508 84003.290225
2557 51120 72317.937812 84087.433321
2558 51140 72400.494147 84171.582143
2559 51160 72483.056511 84255.736689
2560 51180 72565.624900 84339.896955
2561 51200 72648.199312 84424.062938
2562 51220 72730.779743 84508.234636
2563 51240 72813.366190 84592.412046
2564 51260 72895.958652 84676.595164
2565 51280 72978.557123 84760.783989
2566 51300 73061.161602 84844.978517
2567 51320 73143.772085 84929.178745
2568 51340 73226.388569 85013.384672
2569 51360 73309.011052 85097.596292
2570 51380 73391.639530 85181.813605
2571 51400 73474.274001 85266.036607
2572 51420 73556.914460 85350.265295
2573 51440 73639.560906 85434.499667
2574 51460 73722.213336 85518.739719
2575 51480 73804.871745 85602.985449
2576 51500 73887.536132 85687.236855
2577 51520 73970.206493 85771.493932
2578 51540 74052.882826 85855.756679
2579 51560 74135.565127 85940.025093
2580 51580 74218.253393 86024.299170
2581 51600 74300.947622 86108.578908
2582 51620 74383.647810 86192.864305
2583 51640 74466.353955 86277.155357
2584 51660 74549.066053 86361.452062
2585 51680 74631.784102 86445.754417
2586 51700 74714.508099 86530.062419
2587 51720 74797.238040 86614.376065
2588 51740 74879.973922 86698.695352
2589 51760 74962.715743 86783.020279
2590 51780 75045.463501 86867.350841
2591 51800 75128.217190 86951.687037
2592 51820 75210.976810 87036.028863
2593 51840 75293.742357 87120.376317
2594 51860 75376.513827 87204.729395
2595 51880 75459.291219 87289.088096
2596 51900 75542.074529 87373.452417
2597 51920 75624.863754 87457.822354
2598 51940 75707.658891 87542.197904
2599 51960 75790.459937 87626.579066
2600 51980 75873.266890 87710.965837
2601 52000 75956.079746 87795.358213
2602 52020 76038.898503 87879.756192
2603 52040 76121.723158 87964.159771
2604 52060 76204.553707 88048.568947
2605 52080 76287.390148 88132.983719
2606 52100 76370.232478 88217.404082
2607 52120 76453.080694 88301.830034
2608 52140 76535.934793 88386.261574
2609 52160 76618.794772 88470.698696
2610 52180 76701.660629 88555.141400
2611 52200 76784.532360 88639.589682
2612 52220 76867.409962 88724.043540
2613 52240 76950.293433 88808.502971
2614 52260 77033.182770 88892.967972
2615 52280 77116.077969 88977.438541
2616 52300 77198.979029 89061.914674
2617 52320 77281.885946 89146.396369
2618 52340 77364.798717 89230.883624
2619 52360 77447.717339 89315.376435
2620 52380 77530.641810 89399.874801
2621 52400 77613.572126 89484.378717
2622 52420 77696.508285 89568.888183
2623 52440 77779.450284 89653.403194
2624 52460 77862.398120 89737.923748
2625 52480 77945.351790 89822.449843
2626 52500 78028.311292 89906.981476
2627 52520 78111.276621 89991.518644
2628 52540 78194.247777 90076.061345
2629 52560 78277.224755 90160.609575
2630 52580 78360.207553 90245.163333
2631 52600 78443.196169 90329.722615
2632 52620 78526.190598 90414.287419
2633 52640 78609.190839 90498.857742
2634 52660 78692.196888 90583.433581
2635 52680 78775.208744 90668.014934
2636 52700 78858.226402 90752.601799
2637 52720 78941.249860 90837.194172
2638 52740 79024.279115 90921.792051
2639 52760 79107.314165 91006.395433
2640 52780 79190.355007 91091.004315
2641 52800 79273.401637 91175.618696
2642 52820 79356.454053 91260.238572
2643 52840 79439.512253 91344.863940
2644 52860 79522.576232 91429.494798
2645 52880 79605.645990 91514.131144
2646 52900 79688.721522 91598.772975
2647 52920 79771.802826 91683.420287
2648 52940 79854.889899 91768.073079
2649 52960 79937.982739 91852.731348
2650 52980 80021.081342 91937.395092
2651 53000 80104.185706 92022.064306
2652 53020 80187.295828 92106.738990
2653 53040 80270.411706 92191.419140
2654 53060 80353.533335 92276.104754
2655 53080 80436.660715 92360.795829
2656 53100 80519.793841 92445.492362
2657 53120 80602.932711 92530.194352
2658 53140 80686.077323 92614.901794
2659 53160 80769.227673 92699.614688
2660 53180 80852.383759 92784.333029
2661 53200 80935.545578 92869.056816
2662 53220 81018.713127 92953.786046
2663 53240 81101.886404 93038.520717
2664 53260 81185.065406 93123.260825
2665 53280 81268.250130 93208.006368
2666 53300 81351.440572 93292.757344
2667 53320 81434.636732 93377.513749
2668 53340 81517.838605 93462.275582
2669 53360 81601.046189 93547.042840
2670 53380 81684.259481 93631.815520
2671 53400 81767.478479 93716.593620
2672 53420 81850.703180 93801.377137
2673 53440 81933.933581 93886.166068
2674 53460 82017.169679 93970.960412
2675 53480 82100.411471 94055.760164
2676 53500 82183.658956 94140.565324
2677 53520 82266.912129 94225.375888
2678 53540 82350.170990 94310.191853
2679 53560 82433.435533 94395.013218
2680 53580 82516.705758 94479.839979
2681 53600 82599.981661 94564.672134
2682 53620 82683.263240 94649.509681
2683 53640 82766.550491 94734.352617
2684 53660 82849.843413 94819.200939
2685 53680 82933.142002 94904.054645
2686 53700 83016.446255 94988.913732
2687 53720 83099.756171 95073.778198
2688 53740 83183.071746 95158.648041
2689 53760 83266.392978 95243.523257
2690 53780 83349.719863 95328.403844
2691 53800 83433.052400 95413.289800
2692 53820 83516.390586 95498.181122
2693 53840 83599.734417 95583.077807
2694 53860 83683.083892 95667.979854
2695 53880 83766.439007 95752.887259
2696 53900 83849.799760 95837.800021
2697 53920 83933.166148 95922.718135
2698 53940 84016.538168 96007.641601
2699 53960 84099.915818 96092.570415
2700 53980 84183.299096 96177.504576
2701 54000 84266.687998 96262.444079
2702 54020 84350.082522 96347.388924
2703 54040 84433.482665 96432.339108
2704 54060 84516.888424 96517.294627
2705 54080 84600.299797 96602.255479
2706 54100 84683.716782 96687.221663
2707 54120 84767.139375 96772.193175
2708 54140 84850.567573 96857.170014
2709 54160 84934.001376 96942.152175
2710 54180 85017.440778 97027.139658
2711 54200 85100.885779 97112.132459
2712 54220 85184.336375 97197.130577
2713 54240 85267.792563 97282.134008
2714 54260 85351.254342 97367.142750
2715 54280 85434.721708 97452.156800
2716 54300 85518.194658 97537.176157
2717 54320 85601.673191 97622.200818
2718 54340 85685.157304 97707.230780
2719 54360 85768.646993 97792.266040
2720 54380 85852.142257 97877.306597
2721 54400 85935.643092 97962.352447
2722 54420 86019.149496 98047.403589
2723 54440 86102.661466 98132.460020
2724 54460 86186.179001 98217.521738
2725 54480 86269.702096 98302.588739
2726 54500 86353.230751 98387.661022
2727 54520 86436.764961 98472.738584
2728 54540 86520.304724 98557.821423
2729 54560 86603.850039 98642.909535
2730 54580 86687.400901 98728.002920
2731 54600 86770.957309 98813.101574
2732 54620 86854.519260 98898.205495
2733 54640 86938.086751 98983.314680
2734 54660 87021.659781 99068.429127
2735 54680 87105.238345 99153.548834
2736 54700 87188.822442 99238.673798
2737 54720 87272.412069 99323.804017
2738 54740 87356.007224 99408.939488
2739 54760 87439.607903 99494.080209
2740 54780 87523.214105 99579.226177
2741 54800 87606.825826 99664.377390
2742 54820 87690.443065 99749.533846
2743 54840 87774.065818 99834.695543
2744 54860 87857.694083 99919.862477
2745 54880 87941.327858 100005.034646
2746 54900 88024.967140 100090.212048
2747 54920 88108.611926 100175.394681
2748 54940 88192.262213 100260.582542
2749 54960 88275.918000 100345.775629
2750 54980 88359.579284 100430.973939
2751 55000 88443.246062 100516.177470
2752 55020 88526.918331 100601.386220
2753 55040 88610.596089 100686.600186
2754 55060 88694.279334 100771.819365
2755 55080 88777.968063 100857.043756
2756 55100 88861.662273 100942.273356
2757 55120 88945.361962 101027.508163
2758 55140 89029.067128 101112.748174
2759 55160 89112.777767 101197.993387
2760 55180 89196.493877 101283.243799
2761 55200 89280.215456 101368.499408
2762 55220 89363.942502 101453.760212
2763 55240 89447.675011 101539.026208
2764 55260 89531.412981 101624.297394
2765 55280 89615.156410 101709.573768
2766 55300 89698.905295 101794.855327
2767 55320 89782.659633 101880.142069
2768 55340 89866.419423 101965.433992
2769 55360 89950.184661 102050.731092
2770 55380 90033.955346 102136.033369
2771 55400 90117.731474 102221.340819
2772 55420 90201.513043 102306.653439
2773 55440 90285.300050 102391.971229
2774 55460 90369.092494 102477.294185
2775 55480 90452.890371 102562.622305
2776 55500 90536.693679 102647.955587
2777 55520 90620.502416 102733.294028
2778 55540 90704.316579 102818.637626
2779 55560 90788.136165 102903.986378
2780 55580 90871.961173 102989.340283
2781 55600 90955.791599 103074.699338
2782 55620 91039.627441 103160.063541
2783 55640 91123.468697 103245.432888
2784 55660 91207.315364 103330.807379
2785 55680 91291.167440 103416.187011
2786 55700 91375.024922 103501.571780
2787 55720 91458.887808 103586.961686
2788 55740 91542.756095 103672.356726
2789 55760 91626.629780 103757.756896
2790 55780 91710.508863 103843.162196
2791 55800 91794.393339 103928.572623
2792 55820 91878.283206 104013.988174
2793 55840 91962.178463 104099.408847
2794 55860 92046.079106 104184.834639
2795 55880 92129.985133 104270.265550
2796 55900 92213.896541 104355.701575
2797 55920 92297.813329 104441.142713
2798 55940 92381.735493 104526.588962
2799 55960 92465.663032 104612.040319
2800 55980 92549.595942 104697.496782
2801 56000 92633.534222 104782.958349
2802 56020 92717.477869 104868.425017
2803 56040 92801.426880 104953.896784
2804 56060 92885.381254 105039.373648
2805 56080 92969.340986 105124.855606
2806 56100 93053.306076 105210.342657
2807 56120 93137.276521 105295.834797
2808 56140 93221.252318 105381.332025
2809 56160 93305.233465 105466.834339
2810 56180 93389.219960 105552.341735
2811 56200 93473.211799 105637.854213
2812 56220 93557.208981 105723.371768
2813 56240 93641.211504 105808.894401
2814 56260 93725.219364 105894.422107
2815 56280 93809.232559 105979.954884
2816 56300 93893.251088 106065.492732
2817 56320 93977.274947 106151.035646
2818 56340 94061.304134 106236.583625
2819 56360 94145.338647 106322.136667
2820 56380 94229.378483 106407.694770
2821 56400 94313.423640 106493.257930
2822 56420 94397.474116 106578.826146
2823 56440 94481.529908 106664.399416
2824 56460 94565.591013 106749.977738
2825 56480 94649.657430 106835.561108
2826 56500 94733.729156 106921.149525
2827 56520 94817.806189 107006.742987
2828 56540 94901.888525 107092.341492
2829 56560 94985.976164 107177.945036
2830 56580 95070.069102 107263.553618
2831 56600 95154.167337 107349.167236
2832 56620 95238.270866 107434.785888
2833 56640 95322.379688 107520.409570
2834 56660 95406.493800 107606.038282
2835 56680 95490.613199 107691.672020
2836 56700 95574.737884 107777.310783
2837 56720 95658.867851 107862.954568
2838 56740 95743.003099 107948.603373
2839 56760 95827.143624 108034.257196
2840 56780 95911.289426 108119.916034
2841 56800 95995.440501 108205.579886
2842 56820 96079.596846 108291.248749
2843 56840 96163.758461 108376.922621
2844 56860 96247.925341 108462.601499
2845 56880 96332.097486 108548.285382
2846 56900 96416.274892 108633.974268
2847 56920 96500.457558 108719.668153
2848 56940 96584.645480 108805.367037
2849 56960 96668.838656 108891.070916
2850 56980 96753.037085 108976.779788
2851 57000 96837.240764 109062.493652
2852 57020 96921.449690 109148.212505
2853 57040 97005.663862 109233.936345
2854 57060 97089.883276 109319.665169
2855 57080 97174.107931 109405.398976
2856 57100 97258.337824 109491.137763
2857 57120 97342.572952 109576.881528
2858 57140 97426.813315 109662.630269
2859 57160 97511.058908 109748.383984
2860 57180 97595.309730 109834.142671
2861 57200 97679.565779 109919.906326
2862 57220 97763.827052 110005.674949
2863 57240 97848.093547 110091.448537
2864 57260 97932.365262 110177.227088
2865 57280 98016.642193 110263.010600
2866 57300 98100.924340 110348.799069
2867 57320 98185.211699 110434.592496
2868 57340 98269.504269 110520.390876
2869 57360 98353.802047 110606.194208
2870 57380 98438.105030 110692.002490
2871 57400 98522.413217 110777.815720
2872 57420 98606.726605 110863.633895
2873 57440 98691.045192 110949.457013
2874 57460 98775.368976 111035.285073
2875 57480 98859.697953 111121.118071
2876 57500 98944.032123 111206.956007
2877 57520 99028.371482 111292.798877
2878 57540 99112.716029 111378.646679
2879 57560 99197.065760 111464.499412
2880 57580 99281.420675 111550.357073
2881 57600 99365.780770 111636.219661
2882 57620 99450.146043 111722.087172
2883 57640 99534.516492 111807.959605
2884 57660 99618.892115 111893.836957
2885 57680 99703.272910 111979.719227
2886 57700 99787.658873 112065.606413
2887 57720 99872.050003 112151.498511
2888 57740 99956.446298 112237.395521
2889 57760 100040.847755 112323.297440
2890 57780 100125.254372 112409.204265
2891 57800 100209.666147 112495.115996
2892 57820 100294.083078 112581.032628
2893 57840 100378.505162 112666.954162
2894 57860 100462.932397 112752.880593
2895 57880 100547.364780 112838.811921
2896 57900 100631.802310 112924.748143
2897 57920 100716.244985 113010.689257
2898 57940 100800.692801 113096.635260
2899 57960 100885.145757 113182.586152
2900 57980 100969.603850 113268.541929
2901 58000 101054.067079 113354.502589
2902 58020 101138.535441 113440.468131
2903 58040 101223.008934 113526.438552
2904 58060 101307.487555 113612.413850
2905 58080 101391.971302 113698.394024
2906 58100 101476.460174 113784.379070
2907 58120 101560.954167 113870.368987
2908 58140 101645.453280 113956.363773
2909 58160 101729.957510 114042.363426
2910 58180 101814.466855 114128.367943
2911 58200 101898.981313 114214.377323
2912 58220 101983.500882 114300.391563
2913 58240 102068.025560 114386.410662
2914 58260 102152.555343 114472.434616
2915 58280 102237.090231 114558.463425
2916 58300 102321.630220 114644.497086
2917 58320 102406.175309 114730.535596
2918 58340 102490.725496 114816.578955
2919 58360 102575.280777 114902.627159
2920 58380 102659.841152 114988.680207
2921 58400 102744.406617 115074.738097
2922 58420 102828.977170 115160.800826
2923 58440 102913.552810 115246.868393
2924 58460 102998.133535 115332.940795
2925 58480 103082.719341 115419.018030
2926 58500 103167.310226 115505.100097
2927 58520 103251.906190 115591.186993
2928 58540 103336.507228 115677.278716
2929 58560 103421.113340 115763.375264
2930 58580 103505.724523 115849.476635
2931 58600 103590.340774 115935.582827
2932 58620 103674.962092 116021.693838
2933 58640 103759.588475 116107.809666
2934 58660 103844.219919 116193.930308
2935 58680 103928.856424 116280.055763
2936 58700 104013.497986 116366.186029
2937 58720 104098.144604 116452.321104
2938 58740 104182.796276 116538.460985
2939 58760 104267.452998 116624.605670
2940 58780 104352.114770 116710.755159
2941 58800 104436.781589 116796.909447
2942 58820 104521.453453 116883.068534
2943 58840 104606.130359 116969.232417
2944 58860 104690.812305 117055.401095
2945 58880 104775.499290 117141.574564
2946 58900 104860.191311 117227.752824
2947 58920 104944.888366 117313.935873
2948 58940 105029.590453 117400.123707
2949 58960 105114.297569 117486.316326
2950 58980 105199.009713 117572.513726
2951 59000 105283.726882 117658.715907
2952 59020 105368.449074 117744.922866
2953 59040 105453.176288 117831.134601
2954 59060 105537.908520 117917.351109
2955 59080 105622.645769 118003.572390
2956 59100 105707.388032 118089.798441
2957 59120 105792.135308 118176.029260
2958 59140 105876.887594 118262.264844
2959 59160 105961.644888 118348.505193
2960 59180 106046.407188 118434.750303
2961 59200 106131.174492 118521.000174
2962 59220 106215.946798 118607.254802
2963 59240 106300.724104 118693.514186
2964 59260 106385.506406 118779.778324
2965 59280 106470.293705 118866.047214
2966 59300 106555.085996 118952.320854
2967 59320 106639.883278 119038.599242
2968 59340 106724.685550 119124.882375
2969 59360 106809.492808 119211.170252
2970 59380 106894.305051 119297.462872
2971 59400 106979.122276 119383.760231
2972 59420 107063.944482 119470.062328
2973 59440 107148.771666 119556.369160
2974 59460 107233.603826 119642.680727
2975 59480 107318.440961 119728.997026
2976 59500 107403.283067 119815.318054
2977 59520 107488.130144 119901.643811
2978 59540 107572.982188 119987.974293
2979 59560 107657.839198 120074.309500
2980 59580 107742.701171 120160.649428
2981 59600 107827.568106 120246.994077
2982 59620 107912.440000 120333.343443
2983 59640 107997.316852 120419.697526
2984 59660 108082.198659 120506.056322
2985 59680 108167.085418 120592.419831
2986 59700 108251.977129 120678.788050
2987 59720 108336.873788 120765.160977
2988 59740 108421.775395 120851.538610
2989 59760 108506.681946 120937.920948
2990 59780 108591.593439 121024.307987
2991 59800 108676.509874 121110.699727
2992 59820 108761.431246 121197.096166
2993 59840 108846.357555 121283.497300
2994 59860 108931.288798 121369.903130
2995 59880 109016.224974 121456.313651
2996 59900 109101.166079 121542.728863
2997 59920 109186.112113 121629.148764
2998 59940 109271.063072 121715.573351
2999 59960 109356.018955 121802.002623
3000 59980 109440.979761 121888.436578
3001 60000 109525.945486 121974.875213

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