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 7 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
3002 60020 109610.916128 122061.318528
3003 60040 109695.891686 122147.766519
3004 60060 109780.872158 122234.219185
3005 60080 109865.857541 122320.676524
3006 60100 109950.847834 122407.138534
3007 60120 110035.843034 122493.605213
3008 60140 110120.843139 122580.076559
3009 60160 110205.848148 122666.552571
3010 60180 110290.858057 122753.033246
3011 60200 110375.872866 122839.518582
3012 60220 110460.892572 122926.008578
3013 60240 110545.917173 123012.503231
3014 60260 110630.946667 123099.002539
3015 60280 110715.981052 123185.506502
3016 60300 110801.020326 123272.015116
3017 60320 110886.064486 123358.528380
3018 60340 110971.113531 123445.046292
3019 60360 111056.167460 123531.568849
3020 60380 111141.226268 123618.096051
3021 60400 111226.289956 123704.627895
3022 60420 111311.358520 123791.164379
3023 60440 111396.431959 123877.705501
3024 60460 111481.510270 123964.251260
3025 60480 111566.593452 124050.801653
3026 60500 111651.681503 124137.356679
3027 60520 111736.774420 124223.916336
3028 60540 111821.872201 124310.480621
3029 60560 111906.974845 124397.049533
3030 60580 111992.082349 124483.623070
3031 60600 112077.194712 124570.201230
3032 60620 112162.311931 124656.784011
3033 60640 112247.434004 124743.371411
3034 60660 112332.560930 124829.963429
3035 60680 112417.692706 124916.560062
3036 60700 112502.829330 125003.161309
3037 60720 112587.970801 125089.767167
3038 60740 112673.117116 125176.377635
3039 60760 112758.268273 125262.992711
3040 60780 112843.424271 125349.612393
3041 60800 112928.585107 125436.236679
3042 60820 113013.750779 125522.865567
3043 60840 113098.921285 125609.499056
3044 60860 113184.096623 125696.137143
3045 60880 113269.276792 125782.779826
3046 60900 113354.461789 125869.427105
3047 60920 113439.651613 125956.078976
3048 60940 113524.846260 126042.735437
3049 60960 113610.045730 126129.396488
3050 60980 113695.250021 126216.062127
3051 61000 113780.459129 126302.732350
3052 61020 113865.673054 126389.407157
3053 61040 113950.891793 126476.086545
3054 61060 114036.115344 126562.770513
3055 61080 114121.343706 126649.459059
3056 61100 114206.576877 126736.152181
3057 61120 114291.814853 126822.849877
3058 61140 114377.057634 126909.552145
3059 61160 114462.305218 126996.258984
3060 61180 114547.557602 127082.970391
3061 61200 114632.814784 127169.686365
3062 61220 114718.076763 127256.406903
3063 61240 114803.343536 127343.132005
3064 61260 114888.615102 127429.861667
3065 61280 114973.891459 127516.595889
3066 61300 115059.172604 127603.334668
3067 61320 115144.458536 127690.078003
3068 61340 115229.749253 127776.825892
3069 61360 115315.044752 127863.578332
3070 61380 115400.345032 127950.335322
3071 61400 115485.650091 128037.096860
3072 61420 115570.959927 128123.862945
3073 61440 115656.274538 128210.633574
3074 61460 115741.593922 128297.408746
3075 61480 115826.918076 128384.188459
3076 61500 115912.247000 128470.972710
3077 61520 115997.580691 128557.761499
3078 61540 116082.919147 128644.554823
3079 61560 116168.262366 128731.352680
3080 61580 116253.610347 128818.155069
3081 61600 116338.963087 128904.961988
3082 61620 116424.320584 128991.773434
3083 61640 116509.682836 129078.589407
3084 61660 116595.049843 129165.409904
3085 61680 116680.421600 129252.234924
3086 61700 116765.798108 129339.064464
3087 61720 116851.179363 129425.898523
3088 61740 116936.565363 129512.737099
3089 61760 117021.956108 129599.580191
3090 61780 117107.351594 129686.427796
3091 61800 117192.751821 129773.279912
3092 61820 117278.156785 129860.136538
3093 61840 117363.566486 129946.997672
3094 61860 117448.980920 130033.863312
3095 61880 117534.400087 130120.733457
3096 61900 117619.823985 130207.608103
3097 61920 117705.252611 130294.487251
3098 61940 117790.685963 130381.370898
3099 61960 117876.124040 130468.259041
3100 61980 117961.566839 130555.151680
3101 62000 118047.014359 130642.048813
3102 62020 118132.466598 130728.950437
3103 62040 118217.923554 130815.856551
3104 62060 118303.385226 130902.767153
3105 62080 118388.851610 130989.682241
3106 62100 118474.322705 131076.601814
3107 62120 118559.798510 131163.525870
3108 62140 118645.279022 131250.454406
3109 62160 118730.764239 131337.387422
3110 62180 118816.254160 131424.324915
3111 62200 118901.748783 131511.266884
3112 62220 118987.248106 131598.213326
3113 62240 119072.752126 131685.164240
3114 62260 119158.260843 131772.119625
3115 62280 119243.774253 131859.079477
3116 62300 119329.292356 131946.043797
3117 62320 119414.815149 132033.012581
3118 62340 119500.342631 132119.985829
3119 62360 119585.874799 132206.963537
3120 62380 119671.411652 132293.945706
3121 62400 119756.953188 132380.932332
3122 62420 119842.499404 132467.923413
3123 62440 119928.050300 132554.918950
3124 62460 120013.605872 132641.918938
3125 62480 120099.166120 132728.923377
3126 62500 120184.731041 132815.932265
3127 62520 120270.300634 132902.945601
3128 62540 120355.874896 132989.963381
3129 62560 120441.453826 133076.985605
3130 62580 120527.037422 133164.012271
3131 62600 120612.625682 133251.043378
3132 62620 120698.218605 133338.078922
3133 62640 120783.816187 133425.118903
3134 62660 120869.418428 133512.163319
3135 62680 120955.025325 133599.212167
3136 62700 121040.636877 133686.265447
3137 62720 121126.253082 133773.323157
3138 62740 121211.873938 133860.385294
3139 62760 121297.499442 133947.451858
3140 62780 121383.129594 134034.522845
3141 62800 121468.764392 134121.598256
3142 62820 121554.403833 134208.678087
3143 62840 121640.047916 134295.762337
3144 62860 121725.696638 134382.851004
3145 62880 121811.349998 134469.944087
3146 62900 121897.007995 134557.041584
3147 62920 121982.670626 134644.143493
3148 62940 122068.337889 134731.249812
3149 62960 122154.009783 134818.360539
3150 62980 122239.686305 134905.475674
3151 63000 122325.367455 134992.595214
3152 63020 122411.053229 135079.719157
3153 63040 122496.743627 135166.847502
3154 63060 122582.438646 135253.980246
3155 63080 122668.138284 135341.117389
3156 63100 122753.842541 135428.258929
3157 63120 122839.551413 135515.404863
3158 63140 122925.264899 135602.555190
3159 63160 123010.982997 135689.709908
3160 63180 123096.705706 135776.869016
3161 63200 123182.433023 135864.032512
3162 63220 123268.164947 135951.200394
3163 63240 123353.901476 136038.372661
3164 63260 123439.642608 136125.549310
3165 63280 123525.388341 136212.730340
3166 63300 123611.138674 136299.915749
3167 63320 123696.893604 136387.105536
3168 63340 123782.653130 136474.299699
3169 63360 123868.417250 136561.498236
3170 63380 123954.185962 136648.701145
3171 63400 124039.959264 136735.908425
3172 63420 124125.737155 136823.120074
3173 63440 124211.519633 136910.336091
3174 63460 124297.306695 136997.556473
3175 63480 124383.098341 137084.781219
3176 63500 124468.894567 137172.010326
3177 63520 124554.695374 137259.243795
3178 63540 124640.500758 137346.481622
3179 63560 124726.310717 137433.723807
3180 63580 124812.125251 137520.970346
3181 63600 124897.944357 137608.221240
3182 63620 124983.768034 137695.476485
3183 63640 125069.596279 137782.736081
3184 63660 125155.429091 137870.000025
3185 63680 125241.266468 137957.268316
3186 63700 125327.108409 138044.540953
3187 63720 125412.954911 138131.817933
3188 63740 125498.805972 138219.099255
3189 63760 125584.661592 138306.384917
3190 63780 125670.521767 138393.674917
3191 63800 125756.386497 138480.969255
3192 63820 125842.255779 138568.267927
3193 63840 125928.129613 138655.570933
3194 63860 126014.007995 138742.878270
3195 63880 126099.890924 138830.189938
3196 63900 126185.778399 138917.505934
3197 63920 126271.670417 139004.826257
3198 63940 126357.566977 139092.150905
3199 63960 126443.468077 139179.479876
3200 63980 126529.373716 139266.813169
3201 64000 126615.283891 139354.150782
3202 64020 126701.198600 139441.492714
3203 64040 126787.117843 139528.838962
3204 64060 126873.041617 139616.189525
3205 64080 126958.969920 139703.544401
3206 64100 127044.902751 139790.903589
3207 64120 127130.840108 139878.267087
3208 64140 127216.781988 139965.634894
3209 64160 127302.728392 140053.007007
3210 64180 127388.679316 140140.383425
3211 64200 127474.634758 140227.764147
3212 64220 127560.594718 140315.149170
3213 64240 127646.559193 140402.538494
3214 64260 127732.528182 140489.932116
3215 64280 127818.501682 140577.330035
3216 64300 127904.479693 140664.732248
3217 64320 127990.462211 140752.138756
3218 64340 128076.449237 140839.549555
3219 64360 128162.440767 140926.964644
3220 64380 128248.436800 141014.384022
3221 64400 128334.437334 141101.807686
3222 64420 128420.442368 141189.235636
3223 64440 128506.451900 141276.667869
3224 64460 128592.465927 141364.104385
3225 64480 128678.484450 141451.545180
3226 64500 128764.507464 141538.990255
3227 64520 128850.534970 141626.439606
3228 64540 128936.566964 141713.893233
3229 64560 129022.603446 141801.351133
3230 64580 129108.644414 141888.813306
3231 64600 129194.689865 141976.279749
3232 64620 129280.739798 142063.750460
3233 64640 129366.794212 142151.225439
3234 64660 129452.853105 142238.704684
3235 64680 129538.916474 142326.188193
3236 64700 129624.984319 142413.675963
3237 64720 129711.056637 142501.167995
3238 64740 129797.133427 142588.664286
3239 64760 129883.214687 142676.164834
3240 64780 129969.300416 142763.669638
3241 64800 130055.390610 142851.178696
3242 64820 130141.485270 142938.692006
3243 64840 130227.584393 143026.209568
3244 64860 130313.687977 143113.731379
3245 64880 130399.796021 143201.257437
3246 64900 130485.908523 143288.787742
3247 64920 130572.025482 143376.322291
3248 64940 130658.146894 143463.861083
3249 64960 130744.272760 143551.404116
3250 64980 130830.403077 143638.951389
3251 65000 130916.537843 143726.502900
3252 65020 131002.677057 143814.058648
3253 65040 131088.820717 143901.618630
3254 65060 131174.968821 143989.182845
3255 65080 131261.121368 144076.751292
3256 65100 131347.278356 144164.323969
3257 65120 131433.439783 144251.900874
3258 65140 131519.605647 144339.482007
3259 65160 131605.775947 144427.067364
3260 65180 131691.950681 144514.656945
3261 65200 131778.129848 144602.250748
3262 65220 131864.313445 144689.848771
3263 65240 131950.501472 144777.451013
3264 65260 132036.693925 144865.057472
3265 65280 132122.890805 144952.668147
3266 65300 132209.092108 145040.283036
3267 65320 132295.297833 145127.902137
3268 65340 132381.507979 145215.525449
3269 65360 132467.722544 145303.152970
3270 65380 132553.941526 145390.784699
3271 65400 132640.164923 145478.420634
3272 65420 132726.392734 145566.060774
3273 65440 132812.624957 145653.705116
3274 65460 132898.861591 145741.353660
3275 65480 132985.102634 145829.006403
3276 65500 133071.348083 145916.663345
3277 65520 133157.597938 146004.324483
3278 65540 133243.852197 146091.989816
3279 65560 133330.110857 146179.659342
3280 65580 133416.373918 146267.333060
3281 65600 133502.641378 146355.010969
3282 65620 133588.913234 146442.693066
3283 65640 133675.189486 146530.379350
3284 65660 133761.470132 146618.069820
3285 65680 133847.755170 146705.764474
3286 65700 133934.044598 146793.463310
3287 65720 134020.338415 146881.166327
3288 65740 134106.636618 146968.873523
3289 65760 134192.939207 147056.584896
3290 65780 134279.246180 147144.300446
3291 65800 134365.557535 147232.020171
3292 65820 134451.873270 147319.744068
3293 65840 134538.193383 147407.472137
3294 65860 134624.517874 147495.204375
3295 65880 134710.846740 147582.940782
3296 65900 134797.179980 147670.681356
3297 65920 134883.517592 147758.426095
3298 65940 134969.859575 147846.174997
3299 65960 135056.205926 147933.928062
3300 65980 135142.556644 148021.685287
3301 66000 135228.911728 148109.446671
3302 66020 135315.271175 148197.212212
3303 66040 135401.634985 148284.981910
3304 66060 135488.003155 148372.755761
3305 66080 135574.375684 148460.533765
3306 66100 135660.752571 148548.315921
3307 66120 135747.133812 148636.102226
3308 66140 135833.519408 148723.892679
3309 66160 135919.909357 148811.687279
3310 66180 136006.303655 148899.486024
3311 66200 136092.702303 148987.288912
3312 66220 136179.105299 149075.095942
3313 66240 136265.512640 149162.907113
3314 66260 136351.924325 149250.722422
3315 66280 136438.340352 149338.541869
3316 66300 136524.760721 149426.365452
3317 66320 136611.185429 149514.193168
3318 66340 136697.614474 149602.025018
3319 66360 136784.047855 149689.860998
3320 66380 136870.485571 149777.701109
3321 66400 136956.927619 149865.545347
3322 66420 137043.373999 149953.393712
3323 66440 137129.824708 150041.246202
3324 66460 137216.279745 150129.102816
3325 66480 137302.739108 150216.963551
3326 66500 137389.202795 150304.828407
3327 66520 137475.670806 150392.697382
3328 66540 137562.143138 150480.570475
3329 66560 137648.619790 150568.447683
3330 66580 137735.100760 150656.329005
3331 66600 137821.586046 150744.214441
3332 66620 137908.075647 150832.103988
3333 66640 137994.569562 150919.997644
3334 66660 138081.067788 151007.895409
3335 66680 138167.570325 151095.797281
3336 66700 138254.077169 151183.703258
3337 66720 138340.588321 151271.613338
3338 66740 138427.103778 151359.527521
3339 66760 138513.623539 151447.445805
3340 66780 138600.147601 151535.368188
3341 66800 138686.675964 151623.294668
3342 66820 138773.208626 151711.225245
3343 66840 138859.745586 151799.159916
3344 66860 138946.286840 151887.098681
3345 66880 139032.832389 151975.041537
3346 66900 139119.382230 152062.988484
3347 66920 139205.936363 152150.939519
3348 66940 139292.494784 152238.894641
3349 66960 139379.057493 152326.853849
3350 66980 139465.624488 152414.817141
3351 67000 139552.195768 152502.784516
3352 67020 139638.771330 152590.755971
3353 67040 139725.351174 152678.731507
3354 67060 139811.935297 152766.711121
3355 67080 139898.523699 152854.694811
3356 67100 139985.116377 152942.682576
3357 67120 140071.713330 153030.674416
3358 67140 140158.314556 153118.670327
3359 67160 140244.920054 153206.670309
3360 67180 140331.529823 153294.674360
3361 67200 140418.143860 153382.682479
3362 67220 140504.762164 153470.694664
3363 67240 140591.384733 153558.710914
3364 67260 140678.011566 153646.731228
3365 67280 140764.642662 153734.755603
3366 67300 140851.278018 153822.784038
3367 67320 140937.917634 153910.816532
3368 67340 141024.561507 153998.853083
3369 67360 141111.209636 154086.893690
3370 67380 141197.862020 154174.938352
3371 67400 141284.518656 154262.987067
3372 67420 141371.179544 154351.039832
3373 67440 141457.844681 154439.096648
3374 67460 141544.514067 154527.157512
3375 67480 141631.187699 154615.222424
3376 67500 141717.865576 154703.291380
3377 67520 141804.547697 154791.364381
3378 67540 141891.234060 154879.441424
3379 67560 141977.924663 154967.522509
3380 67580 142064.619504 155055.607633
3381 67600 142151.318583 155143.696795
3382 67620 142238.021898 155231.789994
3383 67640 142324.729447 155319.887228
3384 67660 142411.441228 155407.988496
3385 67680 142498.157241 155496.093796
3386 67700 142584.877483 155584.203127
3387 67720 142671.601952 155672.316487
3388 67740 142758.330648 155760.433875
3389 67760 142845.063569 155848.555290
3390 67780 142931.800714 155936.680730
3391 67800 143018.542079 156024.810193
3392 67820 143105.287666 156112.943678
3393 67840 143192.037470 156201.081184
3394 67860 143278.791492 156289.222709
3395 67880 143365.549729 156377.368252
3396 67900 143452.312181 156465.517811
3397 67920 143539.078844 156553.671384
3398 67940 143625.849719 156641.828971
3399 67960 143712.624803 156729.990570
3400 67980 143799.404095 156818.156180
3401 68000 143886.187593 156906.325798
3402 68020 143972.975296 156994.499424
3403 68040 144059.767202 157082.677056
3404 68060 144146.563310 157170.858693
3405 68080 144233.363617 157259.044333
3406 68100 144320.168124 157347.233975
3407 68120 144406.976828 157435.427617
3408 68140 144493.789727 157523.625258
3409 68160 144580.606820 157611.826896
3410 68180 144667.428106 157700.032530
3411 68200 144754.253583 157788.242159
3412 68220 144841.083249 157876.455781
3413 68240 144927.917103 157964.673395
3414 68260 145014.755143 158052.894999
3415 68280 145101.597369 158141.120592
3416 68300 145188.443778 158229.350172
3417 68320 145275.294368 158317.583738
3418 68340 145362.149139 158405.821289
3419 68360 145449.008089 158494.062823
3420 68380 145535.871216 158582.308338
3421 68400 145622.738519 158670.557834
3422 68420 145709.609996 158758.811309
3423 68440 145796.485646 158847.068761
3424 68460 145883.365467 158935.330189
3425 68480 145970.249458 159023.595591
3426 68500 146057.137617 159111.864967
3427 68520 146144.029943 159200.138314
3428 68540 146230.926433 159288.415632
3429 68560 146317.827088 159376.696918
3430 68580 146404.731905 159464.982172
3431 68600 146491.640882 159553.271392
3432 68620 146578.554019 159641.564576
3433 68640 146665.471313 159729.861724
3434 68660 146752.392764 159818.162833
3435 68680 146839.318369 159906.467903
3436 68700 146926.248127 159994.776931
3437 68720 147013.182037 160083.089918
3438 68740 147100.120097 160171.406860
3439 68760 147187.062306 160259.727757
3440 68780 147274.008662 160348.052607
3441 68800 147360.959164 160436.381409
3442 68820 147447.913810 160524.714162
3443 68840 147534.872598 160613.050864
3444 68860 147621.835528 160701.391513
3445 68880 147708.802598 160789.736109
3446 68900 147795.773805 160878.084649
3447 68920 147882.749150 160966.437133
3448 68940 147969.728630 161054.793559
3449 68960 148056.712243 161143.153926
3450 68980 148143.699989 161231.518232
3451 69000 148230.691865 161319.886475
3452 69020 148317.687871 161408.258655
3453 69040 148404.688005 161496.634771
3454 69060 148491.692265 161585.014819
3455 69080 148578.700649 161673.398801
3456 69100 148665.713158 161761.786713
3457 69120 148752.729788 161850.178554
3458 69140 148839.750538 161938.574324
3459 69160 148926.775408 162026.974020
3460 69180 149013.804395 162115.377641
3461 69200 149100.837498 162203.785187
3462 69220 149187.874715 162292.196655
3463 69240 149274.916046 162380.612044
3464 69260 149361.961488 162469.031352
3465 69280 149449.011040 162557.454580
3466 69300 149536.064701 162645.881724
3467 69320 149623.122469 162734.312783
3468 69340 149710.184343 162822.747757
3469 69360 149797.250321 162911.186644
3470 69380 149884.320401 162999.629442
3471 69400 149971.394583 163088.076151
3472 69420 150058.472865 163176.526767
3473 69440 150145.555246 163264.981292
3474 69460 150232.641723 163353.439722
3475 69480 150319.732295 163441.902057
3476 69500 150406.826962 163530.368294
3477 69520 150493.925721 163618.838434
3478 69540 150581.028571 163707.312474
3479 69560 150668.135511 163795.790413
3480 69580 150755.246539 163884.272250
3481 69600 150842.361654 163972.757983
3482 69620 150929.480854 164061.247611
3483 69640 151016.604138 164149.741133
3484 69660 151103.731504 164238.238547
3485 69680 151190.862951 164326.739851
3486 69700 151277.998478 164415.245046
3487 69720 151365.138082 164503.754128
3488 69740 151452.281763 164592.267097
3489 69760 151539.429520 164680.783951
3490 69780 151626.581350 164769.304690
3491 69800 151713.737252 164857.829311
3492 69820 151800.897225 164946.357813
3493 69840 151888.061267 165034.890195
3494 69860 151975.229377 165123.426456
3495 69880 152062.401553 165211.966594
3496 69900 152149.577795 165300.510608
3497 69920 152236.758100 165389.058496
3498 69940 152323.942467 165477.610257
3499 69960 152411.130895 165566.165891
3500 69980 152498.323383 165654.725394
3501 70000 152585.519928 165743.288767

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