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 8 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
3502 70020 152672.720529 165831.856008
3503 70040 152759.925186 165920.427115
3504 70060 152847.133896 166009.002087
3505 70080 152934.346658 166097.580922
3506 70100 153021.563470 166186.163620
3507 70120 153108.784332 166274.750179
3508 70140 153196.009242 166363.340598
3509 70160 153283.238198 166451.934874
3510 70180 153370.471199 166540.533008
3511 70200 153457.708244 166629.134998
3512 70220 153544.949331 166717.740841
3513 70240 153632.194458 166806.350538
3514 70260 153719.443625 166894.964086
3515 70280 153806.696829 166983.581484
3516 70300 153893.954070 167072.202732
3517 70320 153981.215346 167160.827826
3518 70340 154068.480656 167249.456767
3519 70360 154155.749997 167338.089553
3520 70380 154243.023370 167426.726183
3521 70400 154330.300772 167515.366655
3522 70420 154417.582201 167604.010967
3523 70440 154504.867657 167692.659119
3524 70460 154592.157139 167781.311110
3525 70480 154679.450644 167869.966937
3526 70500 154766.748171 167958.626600
3527 70520 154854.049719 168047.290097
3528 70540 154941.355286 168135.957427
3529 70560 155028.664872 168224.628588
3530 70580 155115.978474 168313.303580
3531 70600 155203.296091 168401.982401
3532 70620 155290.617722 168490.665049
3533 70640 155377.943366 168579.351523
3534 70660 155465.273020 168668.041823
3535 70680 155552.606685 168756.735946
3536 70700 155639.944357 168845.433891
3537 70720 155727.286036 168934.135658
3538 70740 155814.631721 169022.841244
3539 70760 155901.981409 169111.550648
3540 70780 155989.335101 169200.263869
3541 70800 156076.692793 169288.980907
3542 70820 156164.054485 169377.701758
3543 70840 156251.420176 169466.426423
3544 70860 156338.789863 169555.154899
3545 70880 156426.163547 169643.887186
3546 70900 156513.541224 169732.623282
3547 70920 156600.922894 169821.363185
3548 70940 156688.308556 169910.106896
3549 70960 156775.698208 169998.854411
3550 70980 156863.091849 170087.605730
3551 71000 156950.489476 170176.360852
3552 71020 157037.891090 170265.119775
3553 71040 157125.296688 170353.882499
3554 71060 157212.706270 170442.649020
3555 71080 157300.119833 170531.419340
3556 71100 157387.537376 170620.193455
3557 71120 157474.958899 170708.971365
3558 71140 157562.384399 170797.753068
3559 71160 157649.813875 170886.538564
3560 71180 157737.247326 170975.327850
3561 71200 157824.684751 171064.120926
3562 71220 157912.126147 171152.917791
3563 71240 157999.571515 171241.718442
3564 71260 158087.020851 171330.522879
3565 71280 158174.474156 171419.331100
3566 71300 158261.931427 171508.143104
3567 71320 158349.392664 171596.958890
3568 71340 158436.857864 171685.778457
3569 71360 158524.327026 171774.601802
3570 71380 158611.800150 171863.428926
3571 71400 158699.277234 171952.259826
3572 71420 158786.758276 172041.094502
3573 71440 158874.243274 172129.932951
3574 71460 158961.732229 172218.775173
3575 71480 159049.225138 172307.621167
3576 71500 159136.721999 172396.470931
3577 71520 159224.222812 172485.324463
3578 71540 159311.727576 172574.181764
3579 71560 159399.236288 172663.042830
3580 71580 159486.748947 172751.907662
3581 71600 159574.265553 172840.776257
3582 71620 159661.786103 172929.648615
3583 71640 159749.310597 173018.524734
3584 71660 159836.839032 173107.404612
3585 71680 159924.371409 173196.288250
3586 71700 160011.907724 173285.175645
3587 71720 160099.447978 173374.066795
3588 71740 160186.992168 173462.961701
3589 71760 160274.540293 173551.860360
3590 71780 160362.092352 173640.762771
3591 71800 160449.648344 173729.668934
3592 71820 160537.208267 173818.578846
3593 71840 160624.772120 173907.492506
3594 71860 160712.339901 173996.409913
3595 71880 160799.911609 174085.331067
3596 71900 160887.487243 174174.255964
3597 71920 160975.066802 174263.184606
3598 71940 161062.650283 174352.116989
3599 71960 161150.237687 174441.053113
3600 71980 161237.829010 174529.992977
3601 72000 161325.424253 174618.936578
3602 72020 161413.023413 174707.883917
3603 72040 161500.626490 174796.834992
3604 72060 161588.233482 174885.789801
3605 72080 161675.844388 174974.748343
3606 72100 161763.459205 175063.710617
3607 72120 161851.077934 175152.676622
3608 72140 161938.700573 175241.646356
3609 72160 162026.327120 175330.619818
3610 72180 162113.957574 175419.597007
3611 72200 162201.591933 175508.577922
3612 72220 162289.230197 175597.562561
3613 72240 162376.872364 175686.550924
3614 72260 162464.518433 175775.543008
3615 72280 162552.168402 175864.538813
3616 72300 162639.822270 175953.538337
3617 72320 162727.480035 176042.541579
3618 72340 162815.141697 176131.548538
3619 72360 162902.807254 176220.559213
3620 72380 162990.476705 176309.573602
3621 72400 163078.150048 176398.591704
3622 72420 163165.827282 176487.613518
3623 72440 163253.508406 176576.639043
3624 72460 163341.193418 176665.668277
3625 72480 163428.882318 176754.701219
3626 72500 163516.575103 176843.737868
3627 72520 163604.271772 176932.778223
3628 72540 163691.972325 177021.822281
3629 72560 163779.676760 177110.870044
3630 72580 163867.385075 177199.921507
3631 72600 163955.097269 177288.976672
3632 72620 164042.813341 177378.035536
3633 72640 164130.533289 177467.098098
3634 72660 164218.257113 177556.164357
3635 72680 164305.984811 177645.234312
3636 72700 164393.716381 177734.307961
3637 72720 164481.451822 177823.385304
3638 72740 164569.191134 177912.466338
3639 72760 164656.934314 178001.551063
3640 72780 164744.681362 178090.639478
3641 72800 164832.432275 178179.731581
3642 72820 164920.187054 178268.827371
3643 72840 165007.945695 178357.926846
3644 72860 165095.708199 178447.030007
3645 72880 165183.474564 178536.136850
3646 72900 165271.244788 178625.247376
3647 72920 165359.018871 178714.361583
3648 72940 165446.796810 178803.479469
3649 72960 165534.578605 178892.601034
3650 72980 165622.364254 178981.726276
3651 73000 165710.153757 179070.855194
3652 73020 165797.947111 179159.987787
3653 73040 165885.744315 179249.124053
3654 73060 165973.545369 179338.263991
3655 73080 166061.350270 179427.407601
3656 73100 166149.159018 179516.554880
3657 73120 166236.971611 179605.705828
3658 73140 166324.788048 179694.860444
3659 73160 166412.608328 179784.018726
3660 73180 166500.432449 179873.180672
3661 73200 166588.260410 179962.346283
3662 73220 166676.092210 180051.515556
3663 73240 166763.927847 180140.688490
3664 73260 166851.767321 180229.865084
3665 73280 166939.610629 180319.045338
3666 73300 167027.457771 180408.229249
3667 73320 167115.308745 180497.416816
3668 73340 167203.163551 180586.608039
3669 73360 167291.022186 180675.802916
3670 73380 167378.884649 180765.001445
3671 73400 167466.750940 180854.203626
3672 73420 167554.621057 180943.409458
3673 73440 167642.494998 181032.618939
3674 73460 167730.372763 181121.832068
3675 73480 167818.254350 181211.048843
3676 73500 167906.139757 181300.269264
3677 73520 167994.028984 181389.493330
3678 73540 168081.922029 181478.721039
3679 73560 168169.818892 181567.952389
3680 73580 168257.719570 181657.187381
3681 73600 168345.624062 181746.426012
3682 73620 168433.532367 181835.668281
3683 73640 168521.444484 181924.914188
3684 73660 168609.360412 182014.163730
3685 73680 168697.280149 182103.416908
3686 73700 168785.203695 182192.673718
3687 73720 168873.131046 182281.934162
3688 73740 168961.062204 182371.198236
3689 73760 169048.997166 182460.465940
3690 73780 169136.935930 182549.737273
3691 73800 169224.878496 182639.012234
3692 73820 169312.824863 182728.290821
3693 73840 169400.775029 182817.573033
3694 73860 169488.728993 182906.858869
3695 73880 169576.686753 182996.148328
3696 73900 169664.648309 183085.441409
3697 73920 169752.613659 183174.738110
3698 73940 169840.582802 183264.038430
3699 73960 169928.555736 183353.342369
3700 73980 170016.532460 183442.649924
3701 74000 170104.512974 183531.961095
3702 74020 170192.497276 183621.275880
3703 74040 170280.485364 183710.594279
3704 74060 170368.477237 183799.916289
3705 74080 170456.472894 183889.241911
3706 74100 170544.472335 183978.571142
3707 74120 170632.475556 184067.903982
3708 74140 170720.482558 184157.240430
3709 74160 170808.493339 184246.580483
3710 74180 170896.507898 184335.924142
3711 74200 170984.526233 184425.271404
3712 74220 171072.548344 184514.622269
3713 74240 171160.574228 184603.976736
3714 74260 171248.603886 184693.334803
3715 74280 171336.637315 184782.696468
3716 74300 171424.674514 184872.061732
3717 74320 171512.715482 184961.430593
3718 74340 171600.760218 185050.803049
3719 74360 171688.808720 185140.179100
3720 74380 171776.860988 185229.558744
3721 74400 171864.917020 185318.941980
3722 74420 171952.976815 185408.328807
3723 74440 172041.040371 185497.719223
3724 74460 172129.107687 185587.113228
3725 74480 172217.178763 185676.510821
3726 74500 172305.253596 185765.912000
3727 74520 172393.332187 185855.316763
3728 74540 172481.414532 185944.725111
3729 74560 172569.500632 186034.137042
3730 74580 172657.590484 186123.552554
3731 74600 172745.684088 186212.971646
3732 74620 172833.781443 186302.394317
3733 74640 172921.882547 186391.820567
3734 74660 173009.987398 186481.250394
3735 74680 173098.095997 186570.683796
3736 74700 173186.208341 186660.120773
3737 74720 173274.324429 186749.561323
3738 74740 173362.444260 186839.005446
3739 74760 173450.567833 186928.453139
3740 74780 173538.695147 187017.904403
3741 74800 173626.826200 187107.359235
3742 74820 173714.960991 187196.817635
3743 74840 173803.099519 187286.279601
3744 74860 173891.241783 187375.745133
3745 74880 173979.387781 187465.214229
3746 74900 174067.537513 187554.686888
3747 74920 174155.690976 187644.163108
3748 74940 174243.848170 187733.642890
3749 74960 174332.009094 187823.126231
3750 74980 174420.173746 187912.613130
3751 75000 174508.342125 188002.103587
3752 75020 174596.514230 188091.597599
3753 75040 174684.690060 188181.095167
3754 75060 174772.869613 188270.596288
3755 75080 174861.052889 188360.100962
3756 75100 174949.239885 188449.609187
3757 75120 175037.430602 188539.120963
3758 75140 175125.625037 188628.636288
3759 75160 175213.823189 188718.155161
3760 75180 175302.025057 188807.677581
3761 75200 175390.230641 188897.203547
3762 75220 175478.439938 188986.733057
3763 75240 175566.652948 189076.266111
3764 75260 175654.869669 189165.802707
3765 75280 175743.090100 189255.342845
3766 75300 175831.314240 189344.886522
3767 75320 175919.542088 189434.433738
3768 75340 176007.773642 189523.984493
3769 75360 176096.008902 189613.538783
3770 75380 176184.247866 189703.096610
3771 75400 176272.490532 189792.657970
3772 75420 176360.736901 189882.222864
3773 75440 176448.986969 189971.791290
3774 75460 176537.240737 190061.363247
3775 75480 176625.498203 190150.938734
3776 75500 176713.759366 190240.517749
3777 75520 176802.024225 190330.100292
3778 75540 176890.292778 190419.686362
3779 75560 176978.565024 190509.275956
3780 75580 177066.840963 190598.869075
3781 75600 177155.120592 190688.465717
3782 75620 177243.403911 190778.065881
3783 75640 177331.690918 190867.669566
3784 75660 177419.981613 190957.276770
3785 75680 177508.275994 191046.887493
3786 75700 177596.574060 191136.501733
3787 75720 177684.875809 191226.119490
3788 75740 177773.181241 191315.740761
3789 75760 177861.490354 191405.365547
3790 75780 177949.803148 191494.993845
3791 75800 178038.119620 191584.625656
3792 75820 178126.439770 191674.260977
3793 75840 178214.763597 191763.899807
3794 75860 178303.091099 191853.542146
3795 75880 178391.422275 191943.187993
3796 75900 178479.757124 192032.837345
3797 75920 178568.095645 192122.490203
3798 75940 178656.437837 192212.146564
3799 75960 178744.783698 192301.806428
3800 75980 178833.133227 192391.469794
3801 76000 178921.486424 192481.136661
3802 76020 179009.843286 192570.807027
3803 76040 179098.203813 192660.480891
3804 76060 179186.568004 192750.158253
3805 76080 179274.935857 192839.839111
3806 76100 179363.307371 192929.523464
3807 76120 179451.682545 193019.211311
3808 76140 179540.061378 193108.902650
3809 76160 179628.443869 193198.597481
3810 76180 179716.830016 193288.295803
3811 76200 179805.219818 193377.997614
3812 76220 179893.613275 193467.702914
3813 76240 179982.010384 193557.411701
3814 76260 180070.411145 193647.123974
3815 76280 180158.815557 193736.839732
3816 76300 180247.223618 193826.558974
3817 76320 180335.635328 193916.281699
3818 76340 180424.050684 194006.007906
3819 76360 180512.469687 194095.737593
3820 76380 180600.892334 194185.470760
3821 76400 180689.318625 194275.207405
3822 76420 180777.748558 194364.947527
3823 76440 180866.182133 194454.691126
3824 76460 180954.619348 194544.438200
3825 76480 181043.060201 194634.188748
3826 76500 181131.504692 194723.942769
3827 76520 181219.952820 194813.700261
3828 76540 181308.404584 194903.461224
3829 76560 181396.859981 194993.225657
3830 76580 181485.319012 195082.993559
3831 76600 181573.781675 195172.764928
3832 76620 181662.247968 195262.539763
3833 76640 181750.717891 195352.318063
3834 76660 181839.191442 195442.099828
3835 76680 181927.668621 195531.885056
3836 76700 182016.149426 195621.673745
3837 76720 182104.633856 195711.465896
3838 76740 182193.121910 195801.261506
3839 76760 182281.613586 195891.060575
3840 76780 182370.108884 195980.863102
3841 76800 182458.607803 196070.669085
3842 76820 182547.110340 196160.478524
3843 76840 182635.616496 196250.291417
3844 76860 182724.126268 196340.107763
3845 76880 182812.639657 196429.927561
3846 76900 182901.156659 196519.750810
3847 76920 182989.677276 196609.577510
3848 76940 183078.201504 196699.407658
3849 76960 183166.729344 196789.241254
3850 76980 183255.260794 196879.078297
3851 77000 183343.795853 196968.918786
3852 77020 183432.334519 197058.762719
3853 77040 183520.876792 197148.610095
3854 77060 183609.422671 197238.460915
3855 77080 183697.972154 197328.315175
3856 77100 183786.525239 197418.172876
3857 77120 183875.081927 197508.034015
3858 77140 183963.642216 197597.898593
3859 77160 184052.206105 197687.766608
3860 77180 184140.773592 197777.638059
3861 77200 184229.344676 197867.512945
3862 77220 184317.919357 197957.391265
3863 77240 184406.497633 198047.273017
3864 77260 184495.079503 198137.158201
3865 77280 184583.664966 198227.046816
3866 77300 184672.254020 198316.938860
3867 77320 184760.846666 198406.834332
3868 77340 184849.442900 198496.733232
3869 77360 184938.042723 198586.635558
3870 77380 185026.646134 198676.541310
3871 77400 185115.253130 198766.450485
3872 77420 185203.863711 198856.363084
3873 77440 185292.477876 198946.279104
3874 77460 185381.095624 199036.198546
3875 77480 185469.716953 199126.121408
3876 77500 185558.341863 199216.047688
3877 77520 185646.970352 199305.977386
3878 77540 185735.602419 199395.910501
3879 77560 185824.238064 199485.847031
3880 77580 185912.877284 199575.786976
3881 77600 186001.520079 199665.730334
3882 77620 186090.166447 199755.677105
3883 77640 186178.816389 199845.627287
3884 77660 186267.469901 199935.580880
3885 77680 186356.126984 200025.537882
3886 77700 186444.787636 200115.498292
3887 77720 186533.451856 200205.462109
3888 77740 186622.119643 200295.429332
3889 77760 186710.790996 200385.399960
3890 77780 186799.465913 200475.373992
3891 77800 186888.144394 200565.351428
3892 77820 186976.826438 200655.332264
3893 77840 187065.512042 200745.316502
3894 77860 187154.201207 200835.304140
3895 77880 187242.893931 200925.295176
3896 77900 187331.590213 201015.289610
3897 77920 187420.290052 201105.287440
3898 77940 187508.993447 201195.288666
3899 77960 187597.700396 201285.293286
3900 77980 187686.410899 201375.301300
3901 78000 187775.124954 201465.312706
3902 78020 187863.842561 201555.327503
3903 78040 187952.563717 201645.345691
3904 78060 188041.288423 201735.367268
3905 78080 188130.016677 201825.392233
3906 78100 188218.748477 201915.420585
3907 78120 188307.483823 202005.452324
3908 78140 188396.222714 202095.487447
3909 78160 188484.965149 202185.525954
3910 78180 188573.711126 202275.567845
3911 78200 188662.460644 202365.613117
3912 78220 188751.213702 202455.661770
3913 78240 188839.970299 202545.713803
3914 78260 188928.730435 202635.769215
3915 78280 189017.494107 202725.828004
3916 78300 189106.261315 202815.890170
3917 78320 189195.032058 202905.955712
3918 78340 189283.806335 202996.024628
3919 78360 189372.584143 203086.096918
3920 78380 189461.365484 203176.172581
3921 78400 189550.150354 203266.251615
3922 78420 189638.938754 203356.334019
3923 78440 189727.730682 203446.419793
3924 78460 189816.526137 203536.508935
3925 78480 189905.325117 203626.601445
3926 78500 189994.127623 203716.697321
3927 78520 190082.933652 203806.796562
3928 78540 190171.743204 203896.899167
3929 78560 190260.556277 203987.005136
3930 78580 190349.372871 204077.114467
3931 78600 190438.192984 204167.227159
3932 78620 190527.016615 204257.343211
3933 78640 190615.843764 204347.462622
3934 78660 190704.674428 204437.585391
3935 78680 190793.508608 204527.711518
3936 78700 190882.346301 204617.841000
3937 78720 190971.187507 204707.973837
3938 78740 191060.032225 204798.110029
3939 78760 191148.880453 204888.249573
3940 78780 191237.732191 204978.392469
3941 78800 191326.587438 205068.538716
3942 78820 191415.446192 205158.688313
3943 78840 191504.308451 205248.841258
3944 78860 191593.174217 205338.997552
3945 78880 191682.043486 205429.157192
3946 78900 191770.916258 205519.320178
3947 78920 191859.792532 205609.486508
3948 78940 191948.672307 205699.656183
3949 78960 192037.555582 205789.829200
3950 78980 192126.442356 205880.005558
3951 79000 192215.332627 205970.185258
3952 79020 192304.226394 206060.368297
3953 79040 192393.123657 206150.554674
3954 79060 192482.024415 206240.744389
3955 79080 192570.928666 206330.937441
3956 79100 192659.836408 206421.133829
3957 79120 192748.747642 206511.333550
3958 79140 192837.662367 206601.536606
3959 79160 192926.580580 206691.742994
3960 79180 193015.502281 206781.952713
3961 79200 193104.427469 206872.165763
3962 79220 193193.356142 206962.382142
3963 79240 193282.288300 207052.601850
3964 79260 193371.223942 207142.824885
3965 79280 193460.163067 207233.051247
3966 79300 193549.105673 207323.280934
3967 79320 193638.051759 207413.513946
3968 79340 193727.001325 207503.750281
3969 79360 193815.954369 207593.989938
3970 79380 193904.910890 207684.232917
3971 79400 193993.870887 207774.479216
3972 79420 194082.834360 207864.728834
3973 79440 194171.801306 207954.981771
3974 79460 194260.771725 208045.238025
3975 79480 194349.745616 208135.497596
3976 79500 194438.722978 208225.760482
3977 79520 194527.703810 208316.026682
3978 79540 194616.688111 208406.296196
3979 79560 194705.675879 208496.569021
3980 79580 194794.667113 208586.845159
3981 79600 194883.661813 208677.124606
3982 79620 194972.659978 208767.407363
3983 79640 195061.661606 208857.693429
3984 79660 195150.666696 208947.982801
3985 79680 195239.675247 209038.275480
3986 79700 195328.687259 209128.571464
3987 79720 195417.702730 209218.870753
3988 79740 195506.721658 209309.173345
3989 79760 195595.744044 209399.479239
3990 79780 195684.769886 209489.788435
3991 79800 195773.799183 209580.100931
3992 79820 195862.831934 209670.416726
3993 79840 195951.868137 209760.735820
3994 79860 196040.907792 209851.058211
3995 79880 196129.950898 209941.383899
3996 79900 196218.997454 210031.712882
3997 79920 196308.047458 210122.045159
3998 79940 196397.100910 210212.380729
3999 79960 196486.157808 210302.719592
4000 79980 196575.218152 210393.061747
4001 80000 196664.281940 210483.407192

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