Current task: —

E-028-traced-margin.py

Experiments: E-028 · script updated 08-19-2026

E-028-traced-margin.csv — 429.7 KB, updated 08-19-2026

← prev page 31 of 40 · 500/page · 20000 rows next →
# nmargin_traced_6_40
15002 300020 992082.024202
15003 300040 992179.330201
15004 300060 992276.637217
15005 300080 992373.945251
15006 300100 992471.254301
15007 300120 992568.564369
15008 300140 992665.875453
15009 300160 992763.187554
15010 300180 992860.500672
15011 300200 992957.814807
15012 300220 993055.129958
15013 300240 993152.446125
15014 300260 993249.763309
15015 300280 993347.081510
15016 300300 993444.400726
15017 300320 993541.720959
15018 300340 993639.042208
15019 300360 993736.364473
15020 300380 993833.687754
15021 300400 993931.012051
15022 300420 994028.337363
15023 300440 994125.663691
15024 300460 994222.991035
15025 300480 994320.319394
15026 300500 994417.648769
15027 300520 994514.979159
15028 300540 994612.310564
15029 300560 994709.642985
15030 300580 994806.976421
15031 300600 994904.310871
15032 300620 995001.646337
15033 300640 995098.982818
15034 300660 995196.320313
15035 300680 995293.658823
15036 300700 995390.998348
15037 300720 995488.338888
15038 300740 995585.680441
15039 300760 995683.023010
15040 300780 995780.366592
15041 300800 995877.711189
15042 300820 995975.056800
15043 300840 996072.403425
15044 300860 996169.751064
15045 300880 996267.099717
15046 300900 996364.449384
15047 300920 996461.800065
15048 300940 996559.151759
15049 300960 996656.504467
15050 300980 996753.858188
15051 301000 996851.212923
15052 301020 996948.568671
15053 301040 997045.925433
15054 301060 997143.283208
15055 301080 997240.641995
15056 301100 997338.001796
15057 301120 997435.362610
15058 301140 997532.724437
15059 301160 997630.087277
15060 301180 997727.451129
15061 301200 997824.815994
15062 301220 997922.181871
15063 301240 998019.548761
15064 301260 998116.916664
15065 301280 998214.285578
15066 301300 998311.655505
15067 301320 998409.026444
15068 301340 998506.398396
15069 301360 998603.771359
15070 301380 998701.145334
15071 301400 998798.520321
15072 301420 998895.896320
15073 301440 998993.273330
15074 301460 999090.651352
15075 301480 999188.030386
15076 301500 999285.410431
15077 301520 999382.791487
15078 301540 999480.173555
15079 301560 999577.556634
15080 301580 999674.940724
15081 301600 999772.325825
15082 301620 999869.711937
15083 301640 999967.099060
15084 301660 1000064.487194
15085 301680 1000161.876338
15086 301700 1000259.266493
15087 301720 1000356.657659
15088 301740 1000454.049835
15089 301760 1000551.443022
15090 301780 1000648.837219
15091 301800 1000746.232426
15092 301820 1000843.628643
15093 301840 1000941.025871
15094 301860 1001038.424108
15095 301880 1001135.823356
15096 301900 1001233.223613
15097 301920 1001330.624880
15098 301940 1001428.027157
15099 301960 1001525.430443
15100 301980 1001622.834739
15101 302000 1001720.240044
15102 302020 1001817.646359
15103 302040 1001915.053683
15104 302060 1002012.462017
15105 302080 1002109.871359
15106 302100 1002207.281711
15107 302120 1002304.693071
15108 302140 1002402.105440
15109 302160 1002499.518819
15110 302180 1002596.933206
15111 302200 1002694.348601
15112 302220 1002791.765006
15113 302240 1002889.182418
15114 302260 1002986.600839
15115 302280 1003084.020269
15116 302300 1003181.440707
15117 302320 1003278.862153
15118 302340 1003376.284607
15119 302360 1003473.708069
15120 302380 1003571.132539
15121 302400 1003668.558017
15122 302420 1003765.984503
15123 302440 1003863.411996
15124 302460 1003960.840497
15125 302480 1004058.270006
15126 302500 1004155.700522
15127 302520 1004253.132046
15128 302540 1004350.564576
15129 302560 1004447.998115
15130 302580 1004545.432660
15131 302600 1004642.868212
15132 302620 1004740.304772
15133 302640 1004837.742338
15134 302660 1004935.180911
15135 302680 1005032.620491
15136 302700 1005130.061077
15137 302720 1005227.502671
15138 302740 1005324.945270
15139 302760 1005422.388877
15140 302780 1005519.833489
15141 302800 1005617.279108
15142 302820 1005714.725733
15143 302840 1005812.173364
15144 302860 1005909.622002
15145 302880 1006007.071645
15146 302900 1006104.522294
15147 302920 1006201.973949
15148 302940 1006299.426610
15149 302960 1006396.880276
15150 302980 1006494.334948
15151 303000 1006591.790625
15152 303020 1006689.247308
15153 303040 1006786.704997
15154 303060 1006884.163690
15155 303080 1006981.623389
15156 303100 1007079.084093
15157 303120 1007176.545802
15158 303140 1007274.008515
15159 303160 1007371.472234
15160 303180 1007468.936957
15161 303200 1007566.402686
15162 303220 1007663.869419
15163 303240 1007761.337156
15164 303260 1007858.805898
15165 303280 1007956.275644
15166 303300 1008053.746395
15167 303320 1008151.218150
15168 303340 1008248.690909
15169 303360 1008346.164672
15170 303380 1008443.639439
15171 303400 1008541.115210
15172 303420 1008638.591985
15173 303440 1008736.069764
15174 303460 1008833.548546
15175 303480 1008931.028332
15176 303500 1009028.509122
15177 303520 1009125.990915
15178 303540 1009223.473711
15179 303560 1009320.957511
15180 303580 1009418.442314
15181 303600 1009515.928120
15182 303620 1009613.414929
15183 303640 1009710.902741
15184 303660 1009808.391556
15185 303680 1009905.881374
15186 303700 1010003.372195
15187 303720 1010100.864018
15188 303740 1010198.356844
15189 303760 1010295.850672
15190 303780 1010393.345503
15191 303800 1010490.841336
15192 303820 1010588.338172
15193 303840 1010685.836010
15194 303860 1010783.334850
15195 303880 1010880.834691
15196 303900 1010978.335535
15197 303920 1011075.837381
15198 303940 1011173.340229
15199 303960 1011270.844078
15200 303980 1011368.348929
15201 304000 1011465.854782
15202 304020 1011563.361636
15203 304040 1011660.869491
15204 304060 1011758.378348
15205 304080 1011855.888206
15206 304100 1011953.399066
15207 304120 1012050.910926
15208 304140 1012148.423787
15209 304160 1012245.937650
15210 304180 1012343.452513
15211 304200 1012440.968377
15212 304220 1012538.485242
15213 304240 1012636.003108
15214 304260 1012733.521974
15215 304280 1012831.041840
15216 304300 1012928.562707
15217 304320 1013026.084574
15218 304340 1013123.607442
15219 304360 1013221.131310
15220 304380 1013318.656177
15221 304400 1013416.182045
15222 304420 1013513.708913
15223 304440 1013611.236780
15224 304460 1013708.765648
15225 304480 1013806.295515
15226 304500 1013903.826382
15227 304520 1014001.358248
15228 304540 1014098.891114
15229 304560 1014196.424979
15230 304580 1014293.959843
15231 304600 1014391.495707
15232 304620 1014489.032570
15233 304640 1014586.570432
15234 304660 1014684.109293
15235 304680 1014781.649153
15236 304700 1014879.190012
15237 304720 1014976.731869
15238 304740 1015074.274725
15239 304760 1015171.818580
15240 304780 1015269.363434
15241 304800 1015366.909285
15242 304820 1015464.456136
15243 304840 1015562.003984
15244 304860 1015659.552831
15245 304880 1015757.102676
15246 304900 1015854.653519
15247 304920 1015952.205360
15248 304940 1016049.758199
15249 304960 1016147.312035
15250 304980 1016244.866870
15251 305000 1016342.422702
15252 305020 1016439.979532
15253 305040 1016537.537359
15254 305060 1016635.096184
15255 305080 1016732.656006
15256 305100 1016830.216826
15257 305120 1016927.778642
15258 305140 1017025.341456
15259 305160 1017122.905267
15260 305180 1017220.470075
15261 305200 1017318.035880
15262 305220 1017415.602681
15263 305240 1017513.170480
15264 305260 1017610.739275
15265 305280 1017708.309066
15266 305300 1017805.879854
15267 305320 1017903.451639
15268 305340 1018001.024420
15269 305360 1018098.598197
15270 305380 1018196.172971
15271 305400 1018293.748740
15272 305420 1018391.325506
15273 305440 1018488.903268
15274 305460 1018586.482025
15275 305480 1018684.061779
15276 305500 1018781.642528
15277 305520 1018879.224273
15278 305540 1018976.807013
15279 305560 1019074.390749
15280 305580 1019171.975480
15281 305600 1019269.561207
15282 305620 1019367.147929
15283 305640 1019464.735646
15284 305660 1019562.324358
15285 305680 1019659.914066
15286 305700 1019757.504768
15287 305720 1019855.096465
15288 305740 1019952.689157
15289 305760 1020050.282844
15290 305780 1020147.877526
15291 305800 1020245.473202
15292 305820 1020343.069872
15293 305840 1020440.667537
15294 305860 1020538.266197
15295 305880 1020635.865851
15296 305900 1020733.466498
15297 305920 1020831.068140
15298 305940 1020928.670776
15299 305960 1021026.274406
15300 305980 1021123.879030
15301 306000 1021221.484648
15302 306020 1021319.091260
15303 306040 1021416.698865
15304 306060 1021514.307463
15305 306080 1021611.917056
15306 306100 1021709.527641
15307 306120 1021807.139220
15308 306140 1021904.751793
15309 306160 1022002.365358
15310 306180 1022099.979917
15311 306200 1022197.595468
15312 306220 1022295.212013
15313 306240 1022392.829551
15314 306260 1022490.448081
15315 306280 1022588.067604
15316 306300 1022685.688120
15317 306320 1022783.309628
15318 306340 1022880.932129
15319 306360 1022978.555622
15320 306380 1023076.180108
15321 306400 1023173.805586
15322 306420 1023271.432056
15323 306440 1023369.059518
15324 306460 1023466.687973
15325 306480 1023564.317419
15326 306500 1023661.947857
15327 306520 1023759.579287
15328 306540 1023857.211709
15329 306560 1023954.845122
15330 306580 1024052.479527
15331 306600 1024150.114924
15332 306620 1024247.751311
15333 306640 1024345.388691
15334 306660 1024443.027061
15335 306680 1024540.666423
15336 306700 1024638.306776
15337 306720 1024735.948120
15338 306740 1024833.590455
15339 306760 1024931.233781
15340 306780 1025028.878097
15341 306800 1025126.523405
15342 306820 1025224.169703
15343 306840 1025321.816991
15344 306860 1025419.465271
15345 306880 1025517.114540
15346 306900 1025614.764800
15347 306920 1025712.416050
15348 306940 1025810.068291
15349 306960 1025907.721521
15350 306980 1026005.375742
15351 307000 1026103.030953
15352 307020 1026200.687153
15353 307040 1026298.344344
15354 307060 1026396.002524
15355 307080 1026493.661693
15356 307100 1026591.321853
15357 307120 1026688.983002
15358 307140 1026786.645140
15359 307160 1026884.308268
15360 307180 1026981.972385
15361 307200 1027079.637491
15362 307220 1027177.303586
15363 307240 1027274.970671
15364 307260 1027372.638744
15365 307280 1027470.307806
15366 307300 1027567.977858
15367 307320 1027665.648897
15368 307340 1027763.320926
15369 307360 1027860.993943
15370 307380 1027958.667949
15371 307400 1028056.342943
15372 307420 1028154.018926
15373 307440 1028251.695896
15374 307460 1028349.373856
15375 307480 1028447.052803
15376 307500 1028544.732738
15377 307520 1028642.413661
15378 307540 1028740.095572
15379 307560 1028837.778471
15380 307580 1028935.462358
15381 307600 1029033.147233
15382 307620 1029130.833095
15383 307640 1029228.519944
15384 307660 1029326.207782
15385 307680 1029423.896606
15386 307700 1029521.586418
15387 307720 1029619.277217
15388 307740 1029716.969003
15389 307760 1029814.661776
15390 307780 1029912.355537
15391 307800 1030010.050284
15392 307820 1030107.746018
15393 307840 1030205.442739
15394 307860 1030303.140446
15395 307880 1030400.839140
15396 307900 1030498.538821
15397 307920 1030596.239488
15398 307940 1030693.941142
15399 307960 1030791.643782
15400 307980 1030889.347408
15401 308000 1030987.052020
15402 308020 1031084.757618
15403 308040 1031182.464203
15404 308060 1031280.171773
15405 308080 1031377.880329
15406 308100 1031475.589871
15407 308120 1031573.300399
15408 308140 1031671.011912
15409 308160 1031768.724411
15410 308180 1031866.437896
15411 308200 1031964.152365
15412 308220 1032061.867821
15413 308240 1032159.584261
15414 308260 1032257.301687
15415 308280 1032355.020097
15416 308300 1032452.739493
15417 308320 1032550.459874
15418 308340 1032648.181239
15419 308360 1032745.903589
15420 308380 1032843.626925
15421 308400 1032941.351244
15422 308420 1033039.076549
15423 308440 1033136.802837
15424 308460 1033234.530111
15425 308480 1033332.258368
15426 308500 1033429.987610
15427 308520 1033527.717836
15428 308540 1033625.449046
15429 308560 1033723.181241
15430 308580 1033820.914419
15431 308600 1033918.648581
15432 308620 1034016.383727
15433 308640 1034114.119856
15434 308660 1034211.856970
15435 308680 1034309.595067
15436 308700 1034407.334147
15437 308720 1034505.074211
15438 308740 1034602.815258
15439 308760 1034700.557289
15440 308780 1034798.300302
15441 308800 1034896.044299
15442 308820 1034993.789279
15443 308840 1035091.535242
15444 308860 1035189.282188
15445 308880 1035287.030117
15446 308900 1035384.779028
15447 308920 1035482.528922
15448 308940 1035580.279799
15449 308960 1035678.031658
15450 308980 1035775.784500
15451 309000 1035873.538324
15452 309020 1035971.293131
15453 309040 1036069.048919
15454 309060 1036166.805690
15455 309080 1036264.563443
15456 309100 1036362.322178
15457 309120 1036460.081895
15458 309140 1036557.842593
15459 309160 1036655.604274
15460 309180 1036753.366936
15461 309200 1036851.130580
15462 309220 1036948.895205
15463 309240 1037046.660812
15464 309260 1037144.427400
15465 309280 1037242.194970
15466 309300 1037339.963521
15467 309320 1037437.733053
15468 309340 1037535.503566
15469 309360 1037633.275060
15470 309380 1037731.047535
15471 309400 1037828.820991
15472 309420 1037926.595428
15473 309440 1038024.370846
15474 309460 1038122.147244
15475 309480 1038219.924623
15476 309500 1038317.702982
15477 309520 1038415.482322
15478 309540 1038513.262642
15479 309560 1038611.043942
15480 309580 1038708.826223
15481 309600 1038806.609484
15482 309620 1038904.393724
15483 309640 1039002.178945
15484 309660 1039099.965146
15485 309680 1039197.752326
15486 309700 1039295.540486
15487 309720 1039393.329626
15488 309740 1039491.119746
15489 309760 1039588.910845
15490 309780 1039686.702923
15491 309800 1039784.495981
15492 309820 1039882.290018
15493 309840 1039980.085034
15494 309860 1040077.881030
15495 309880 1040175.678005
15496 309900 1040273.475958
15497 309920 1040371.274891
15498 309940 1040469.074802
15499 309960 1040566.875692
15500 309980 1040664.677561
15501 310000 1040762.480409

E-028-traced-margin.json — 3.7 KB, updated 08-19-2026

JSON sidecar (click to expand)
{
  "experiment": "E-028",
  "task": "4.5a support: D-056 traced R-bound verification + margin rerun with A=10.5, B0=48 (honest C0 incl. log log term)",
  "script": "research/numerics/scripts/E-028-traced-margin.py",
  "checks": {
    "V_R_traced_bound_holds": {
      "pass": true,
      "points": [
        {
          "sigma": -2.5,
          "t": 10.0,
          "abs_R": 0.5364627998701963,
          "bound": 74.091519822774,
          "holds": true
        },
        {
          "sigma": -2.5,
          "t": 50.0,
          "abs_R": 1.8035543334872959,
          "bound": 89.48805904510499,
          "holds": true
        },
        {
          "sigma": -2.5,
          "t": 100.0,
          "abs_R": 2.726827160278793,
          "bound": 96.56221453948484,
          "holds": true
        },
        {
          "sigma": -2.5,
          "t": 500.0,
          "abs_R": 4.0305314954089475,
          "bound": 113.29530125676315,
          "holds": true
        },
        {
          "sigma": -2.5,
          "t": 1000.0,
          "abs_R": 4.7507714994958565,
          "bound": 120.5524094572705,
          "holds": true
        },
        {
          "sigma": 0.5,
          "t": 10.0,
          "abs_R": 0.232976073221556,
          "bound": 74.091519822774,
          "holds": true
        },
        {
          "sigma": 0.5,
          "t": 50.0,
          "abs_R": 1.0575019981720868,
          "bound": 89.48805904510499,
          "holds": true
        },
        {
          "sigma": 0.5,
          "t": 100.0,
          "abs_R": 1.3861083473172386,
          "bound": 96.56221453948484,
          "holds": true
        },
        {
          "sigma": 0.5,
          "t": 500.0,
          "abs_R": 2.191750394872167,
          "bound": 113.29530125676315,
          "holds": true
        },
        {
          "sigma": 0.5,
          "t": 1000.0,
          "abs_R": 2.535043296015203,
          "bound": 120.5524094572705,
          "holds": true
        },
        {
          "sigma": 2.0,
          "t": 10.0,
          "abs_R": 0.11614405022018243,
          "bound": 74.091519822774,
          "holds": true
        },
        {
          "sigma": 2.0,
          "t": 50.0,
          "abs_R": 0.47282316679931,
          "bound": 89.48805904510499,
          "holds": true
        },
        {
          "sigma": 2.0,
          "t": 100.0,
          "abs_R": 0.13345107201883163,
          "bound": 96.56221453948484,
          "holds": true
        },
        {
          "sigma": 2.0,
          "t": 500.0,
          "abs_R": 0.6914268199229783,
          "bound": 113.29530125676315,
          "holds": true
        },
        {
          "sigma": 2.0,
          "t": 1000.0,
          "abs_R": 0.6591087549534896,
          "bound": 120.5524094572705,
          "holds": true
        }
      ]
    },
    "V5_Z2_certified": {
      "pass": true,
      "partial": 0.5699599927023044,
      "tail": 1.4815510557964273e-05,
      "bound": 0.5699748082128623
    },
    "V2_margin_positive_convex": true
  },
  "key_values": {
    "M0_from_E023": 1.335180201468999,
    "N0_traced_10p5_48": 75451,
    "margin_N0": 2.396310426061973,
    "overlap_factor_vs_MV_5e5": 6.626817404673232,
    "margin_at": {
      "75451": 2.396310426061973,
      "100000": 92785.67601524561,
      "500000": 2006008.571308432,
      "1000000": 4755774.032035855,
      "1000000000": 8921657382.546026,
      "1000000000000": 12395765983564.96,
      "1000000000000000000": 1.9304326196882584e+19,
      "8999999999994000000000001": 2.4579625383799396e+26
    },
    "N1": 8999999999994000000000001
  },
  "elapsed_seconds": 3.7
}

Source code

Python script (click to expand)
"""E-028 — task 4.5a support: D-056 traced R-bound verification + margin rerun.

Components:
  V-R. Verify the traced reference-point R-bound (D-056 Theorem R)
       |R(s)| <= 6.0 log(|t|+2) + 40,  sigma in [-5/2, 3], |t| >= 10
       at sample points sigma in {-2.5, 0.5, 2}, t in {10,50,100,500,1000}
       (R(s) = zeta'/zeta(s) - sum_{|gamma-t|<1} 1/(s-rho), zeta'/zeta
       via the E-021 xi-route at dps 30; zeros from the E-027 cache).
  V5.  Certified Z2 = -zeta'/zeta(2) <= 0.5699: partial sum to 1e4 plus
       tail bound (log N + 1)/N.
  S3'. Margin rerun with the TRACED constants A = 6.0, B0 = 40 (was:
       measured 0.9/1.0, first-estimate 2.0/2.0) and the I_I length fix
       2(sqrt(n)+1). Recompute N0, contiguity to 4e5, convexity grid to
       N1, margin references. M0 = 1.3352 imported from E-023 (S1).

Outputs: research/numerics/data/E-028-traced-margin.csv (+ .json)
"""
import csv as csvmod
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"
E023J = DATA / "E-023-assembly.json"
ZCACHE = DATA / "E-027-zeros-cache.npy"
CSV = DATA / "E-028-traced-margin.csv"
JSON = DATA / "E-028-traced-margin.json"

GAMMA = 0.577215664901532860606512090082402431
C1L = (GAMMA - 1 - np.log(2 * np.pi)) / 2 - GAMMA / 2
A_TR, B0_TR = 10.5, 48.0
T0_PLATT = 3.0e12


def zeta_logder(s):
    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 check_R_bound(zeros):
    mp.mp.dps = 30
    out = []
    for sigma in (-2.5, 0.5, 2.0):
        for t in (10.0, 50.0, 100.0, 500.0, 1000.0):
            s = complex(sigma, t)
            val = zeta_logder(s)
            near = [g for g in zeros if abs(g - t) < 1.0]
            r = val - sum(1.0 / (s - complex(0.5, g)) for g in near)
            bound = 10.5 * np.log(t + 2) + 48.0
            out.append({"sigma": sigma, "t": t, "abs_R": abs(r),
                        "bound": bound, "holds": bool(abs(r) <= bound)})
    return out


def certified_Z2(N=10 ** 6):
    """-zeta'/zeta(2) = sum Lambda(n)/n^2; partial sum + tail bound
    (log N + 1)/N. True value 0.569955...; certify <= 0.5700."""
    lam = np.zeros(N + 1)
    sieve = np.zeros(N + 1, dtype=np.int64)
    for p in range(2, N + 1):
        if sieve[p] == 0:
            sieve[p::p] = p
    for n in range(2, N + 1):
        p = sieve[n]
        if p > 0 and abs(p ** round(np.log(n) / np.log(p)) - n) < 0.5:
            lam[n] = np.log(p)
    part = float(np.sum(lam[2:] / np.arange(2, N + 1) ** 2))
    tail = (np.log(N) + 1) / N
    return part, tail, part + tail


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):
    return 3 * C0(T) ** 2 + A_TR * np.log(T + 2) + B0_TR


def B_bound(n, m0):
    n = np.asarray(n, dtype=np.float64)
    T = np.sqrt(n) + 1.0
    twoN = 2 * (F_count(T) + S_bound(T))
    I3_long = 4 * T * ((2 / 3) * C0(T) + A_TR * np.log(T + 2) + B0_TR)
    I3_short = 40.0 * m0
    I24 = 2 * (np.e ** 4 + 1) * (7 * H_bound(T) + 3.3)
    r = np.sqrt(n)
    # I_I length fix: 2T = 2(sqrt(n)+1)
    logI1 = np.log(2 * (r + 1) * 0.5700) + r / 2 - (2 * r - 2) * np.log(2)
    I1 = np.where(logI1 > -700, np.exp(np.maximum(logI1, -745)), 0.0)
    return twoN + I3_long + I3_short + I24 + I1 + 1.0


def margin(n, m0):
    n = np.asarray(n, dtype=np.float64)
    return 0.5 * n * np.log(n) + C1L * n - 4.0 - B_bound(n, m0) + 1.0


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

    m0 = json.loads(E023J.read_text())["key_values"]["M0_use"]
    print(f"M0 imported from E-023: {m0}")

    print("V-R: traced R-bound verification ...", flush=True)
    zeros = np.load(ZCACHE)
    rpts = check_R_bound(zeros)
    vr = all(p["holds"] for p in rpts)
    worst = max(rpts, key=lambda p: p["abs_R"] / p["bound"])
    print(f"  all {len(rpts)} points hold: {vr}; worst ratio "
          f"{worst['abs_R']/worst['bound']:.3f} at "
          f"sigma={worst['sigma']}, t={worst['t']}", flush=True)

    print("V5: certified Z2 ...", flush=True)
    part, tail, z2 = certified_Z2()
    v5 = bool(z2 <= 0.5700)
    print(f"  partial {part:.6f} + tail {tail:.6f} = {z2:.6f} "
          f"<= 0.5700: {v5}", flush=True)

    print("S3': margin rerun with traced constants ...", flush=True)
    ngrid = np.arange(20, 400001)
    marg = margin(ngrid, m0)
    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
    margN0 = float(marg[ngrid == N0][0]) if N0 > 0 else float("nan")
    print(f"  N0 (traced 6.0/40) = {N0} (contiguous to 4e5: {cont}, "
          f"margin(N0) = {margN0:.1f})", flush=True)

    logn = np.unique(np.logspace(np.log10(max(N0, 20)), np.log10(9e24),
                                 4000).astype(np.float64))
    bmarg = margin(logn, m0)
    conv = bool(np.all(np.diff(bmarg, 2) > -1e-6 * bmarg[:-2].max()))

    N1 = int(3_000_000_000_000 - 1) ** 2
    refs = {int(n): float(margin(n, m0)) for n in
            [N0, 100000, 500000, 10 ** 6, 10 ** 9, 10 ** 12, 10 ** 18,
             N1]}
    for k, v in refs.items():
        print(f"  margin({k:.3e}) = {v:.4e}", flush=True)
    v2 = bool(N0 > 0 and cont and conv and N0 < 500000)
    print(f"  convexity grid: {conv}; N0 < 5e5 (MV coverage): "
          f"{N0 < 500000}", flush=True)

    with open(CSV, "w", newline="") as f:
        w = csvmod.writer(f)
        w.writerow(["n", "margin_traced_6_40"])
        for n in range(20, 400001, 20):
            w.writerow([n, f"{float(margin(n, m0)):.6f}"])

    meta = {
        "experiment": "E-028",
        "task": "4.5a support: D-056 traced R-bound verification + "
                "margin rerun with A=10.5, B0=48 (honest C0 incl. log log term)",
        "script": "research/numerics/scripts/E-028-traced-margin.py",
        "checks": {
            "V_R_traced_bound_holds": {"pass": vr, "points": rpts},
            "V5_Z2_certified": {"pass": v5, "partial": part,
                                "tail": tail, "bound": z2},
            "V2_margin_positive_convex": v2,
        },
        "key_values": {
            "M0_from_E023": m0,
            "N0_traced_10p5_48": N0,
            "margin_N0": margN0,
            "overlap_factor_vs_MV_5e5": 500000 / N0 if N0 > 0 else None,
            "margin_at": {str(k): v for k, v in refs.items()},
            "N1": N1,
        },
        "elapsed_seconds": round(time.time() - t0, 1),
    }
    JSON.write_text(json.dumps(meta, indent=2))
    print(f"  wrote {CSV.name}, {JSON.name}; elapsed "
          f"{meta['elapsed_seconds']}s")
    return 0 if (vr and v5 and v2) else 1


if __name__ == "__main__":
    sys.exit(main())