Current task: —

E-029-convexity.py

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

E-029-convexity.csv — 429.7 KB, updated 08-19-2026

← prev page 13 of 40 · 500/page · 20000 rows next →
# nmargin_tilde
6002 120020 172415.829806
6003 120040 172497.685453
6004 120060 172579.544235
6005 120080 172661.406151
6006 120100 172743.271201
6007 120120 172825.139384
6008 120140 172907.010699
6009 120160 172988.885146
6010 120180 173070.762723
6011 120200 173152.643431
6012 120220 173234.527269
6013 120240 173316.414235
6014 120260 173398.304330
6015 120280 173480.197552
6016 120300 173562.093901
6017 120320 173643.993376
6018 120340 173725.895977
6019 120360 173807.801703
6020 120380 173889.710554
6021 120400 173971.622528
6022 120420 174053.537624
6023 120440 174135.455844
6024 120460 174217.377184
6025 120480 174299.301646
6026 120500 174381.229228
6027 120520 174463.159930
6028 120540 174545.093751
6029 120560 174627.030690
6030 120580 174708.970747
6031 120600 174790.913921
6032 120620 174872.860211
6033 120640 174954.809617
6034 120660 175036.762138
6035 120680 175118.717774
6036 120700 175200.676523
6037 120720 175282.638385
6038 120740 175364.603360
6039 120760 175446.571447
6040 120780 175528.542644
6041 120800 175610.516952
6042 120820 175692.494370
6043 120840 175774.474897
6044 120860 175856.458533
6045 120880 175938.445276
6046 120900 176020.435127
6047 120920 176102.428084
6048 120940 176184.424147
6049 120960 176266.423315
6050 120980 176348.425587
6051 121000 176430.430964
6052 121020 176512.439444
6053 121040 176594.451027
6054 121060 176676.465711
6055 121080 176758.483497
6056 121100 176840.504383
6057 121120 176922.528370
6058 121140 177004.555456
6059 121160 177086.585640
6060 121180 177168.618923
6061 121200 177250.655302
6062 121220 177332.694779
6063 121240 177414.737352
6064 121260 177496.783020
6065 121280 177578.831783
6066 121300 177660.883640
6067 121320 177742.938591
6068 121340 177824.996634
6069 121360 177907.057770
6070 121380 177989.121997
6071 121400 178071.189315
6072 121420 178153.259724
6073 121440 178235.333222
6074 121460 178317.409809
6075 121480 178399.489484
6076 121500 178481.572247
6077 121520 178563.658097
6078 121540 178645.747033
6079 121560 178727.839055
6080 121580 178809.934162
6081 121600 178892.032354
6082 121620 178974.133629
6083 121640 179056.237988
6084 121660 179138.345429
6085 121680 179220.455951
6086 121700 179302.569555
6087 121720 179384.686240
6088 121740 179466.806004
6089 121760 179548.928848
6090 121780 179631.054770
6091 121800 179713.183771
6092 121820 179795.315849
6093 121840 179877.451003
6094 121860 179959.589233
6095 121880 180041.730539
6096 121900 180123.874920
6097 121920 180206.022375
6098 121940 180288.172903
6099 121960 180370.326504
6100 121980 180452.483177
6101 122000 180534.642922
6102 122020 180616.805738
6103 122040 180698.971624
6104 122060 180781.140580
6105 122080 180863.312605
6106 122100 180945.487698
6107 122120 181027.665858
6108 122140 181109.847086
6109 122160 181192.031381
6110 122180 181274.218741
6111 122200 181356.409166
6112 122220 181438.602656
6113 122240 181520.799210
6114 122260 181602.998827
6115 122280 181685.201506
6116 122300 181767.407248
6117 122320 181849.616051
6118 122340 181931.827915
6119 122360 182014.042838
6120 122380 182096.260822
6121 122400 182178.481864
6122 122420 182260.705964
6123 122440 182342.933121
6124 122460 182425.163336
6125 122480 182507.396607
6126 122500 182589.632933
6127 122520 182671.872315
6128 122540 182754.114750
6129 122560 182836.360240
6130 122580 182918.608782
6131 122600 183000.860377
6132 122620 183083.115024
6133 122640 183165.372722
6134 122660 183247.633471
6135 122680 183329.897269
6136 122700 183412.164117
6137 122720 183494.434014
6138 122740 183576.706958
6139 122760 183658.982950
6140 122780 183741.261989
6141 122800 183823.544074
6142 122820 183905.829204
6143 122840 183988.117379
6144 122860 184070.408598
6145 122880 184152.702861
6146 122900 184235.000167
6147 122920 184317.300516
6148 122940 184399.603906
6149 122960 184481.910337
6150 122980 184564.219808
6151 123000 184646.532320
6152 123020 184728.847871
6153 123040 184811.166460
6154 123060 184893.488087
6155 123080 184975.812751
6156 123100 185058.140453
6157 123120 185140.471190
6158 123140 185222.804962
6159 123160 185305.141770
6160 123180 185387.481612
6161 123200 185469.824487
6162 123220 185552.170395
6163 123240 185634.519336
6164 123260 185716.871308
6165 123280 185799.226311
6166 123300 185881.584345
6167 123320 185963.945408
6168 123340 186046.309501
6169 123360 186128.676622
6170 123380 186211.046771
6171 123400 186293.419948
6172 123420 186375.796151
6173 123440 186458.175380
6174 123460 186540.557635
6175 123480 186622.942914
6176 123500 186705.331218
6177 123520 186787.722545
6178 123540 186870.116895
6179 123560 186952.514268
6180 123580 187034.914662
6181 123600 187117.318077
6182 123620 187199.724513
6183 123640 187282.133969
6184 123660 187364.546443
6185 123680 187446.961937
6186 123700 187529.380448
6187 123720 187611.801977
6188 123740 187694.226522
6189 123760 187776.654084
6190 123780 187859.084661
6191 123800 187941.518253
6192 123820 188023.954860
6193 123840 188106.394480
6194 123860 188188.837112
6195 123880 188271.282758
6196 123900 188353.731415
6197 123920 188436.183083
6198 123940 188518.637762
6199 123960 188601.095451
6200 123980 188683.556149
6201 124000 188766.019856
6202 124020 188848.486571
6203 124040 188930.956293
6204 124060 189013.429023
6205 124080 189095.904758
6206 124100 189178.383499
6207 124120 189260.865245
6208 124140 189343.349996
6209 124160 189425.837750
6210 124180 189508.328508
6211 124200 189590.822268
6212 124220 189673.319030
6213 124240 189755.818793
6214 124260 189838.321557
6215 124280 189920.827321
6216 124300 190003.336085
6217 124320 190085.847847
6218 124340 190168.362608
6219 124360 190250.880366
6220 124380 190333.401122
6221 124400 190415.924873
6222 124420 190498.451621
6223 124440 190580.981364
6224 124460 190663.514101
6225 124480 190746.049832
6226 124500 190828.588557
6227 124520 190911.130275
6228 124540 190993.674984
6229 124560 191076.222685
6230 124580 191158.773377
6231 124600 191241.327060
6232 124620 191323.883732
6233 124640 191406.443393
6234 124660 191489.006043
6235 124680 191571.571680
6236 124700 191654.140305
6237 124720 191736.711916
6238 124740 191819.286514
6239 124760 191901.864097
6240 124780 191984.444664
6241 124800 192067.028216
6242 124820 192149.614752
6243 124840 192232.204270
6244 124860 192314.796771
6245 124880 192397.392254
6246 124900 192479.990718
6247 124920 192562.592163
6248 124940 192645.196587
6249 124960 192727.803991
6250 124980 192810.414374
6251 125000 192893.027735
6252 125020 192975.644073
6253 125040 193058.263388
6254 125060 193140.885680
6255 125080 193223.510947
6256 125100 193306.139190
6257 125120 193388.770407
6258 125140 193471.404598
6259 125160 193554.041763
6260 125180 193636.681900
6261 125200 193719.325009
6262 125220 193801.971090
6263 125240 193884.620141
6264 125260 193967.272163
6265 125280 194049.927155
6266 125300 194132.585116
6267 125320 194215.246045
6268 125340 194297.909942
6269 125360 194380.576807
6270 125380 194463.246638
6271 125400 194545.919435
6272 125420 194628.595198
6273 125440 194711.273926
6274 125460 194793.955618
6275 125480 194876.640273
6276 125500 194959.327892
6277 125520 195042.018474
6278 125540 195124.712017
6279 125560 195207.408521
6280 125580 195290.107987
6281 125600 195372.810412
6282 125620 195455.515797
6283 125640 195538.224141
6284 125660 195620.935443
6285 125680 195703.649703
6286 125700 195786.366920
6287 125720 195869.087093
6288 125740 195951.810223
6289 125760 196034.536307
6290 125780 196117.265347
6291 125800 196199.997340
6292 125820 196282.732288
6293 125840 196365.470188
6294 125860 196448.211040
6295 125880 196530.954844
6296 125900 196613.701600
6297 125920 196696.451306
6298 125940 196779.203961
6299 125960 196861.959567
6300 125980 196944.718121
6301 126000 197027.479623
6302 126020 197110.244073
6303 126040 197193.011470
6304 126060 197275.781813
6305 126080 197358.555102
6306 126100 197441.331336
6307 126120 197524.110515
6308 126140 197606.892638
6309 126160 197689.677705
6310 126180 197772.465714
6311 126200 197855.256665
6312 126220 197938.050559
6313 126240 198020.847393
6314 126260 198103.647168
6315 126280 198186.449882
6316 126300 198269.255536
6317 126320 198352.064129
6318 126340 198434.875660
6319 126360 198517.690128
6320 126380 198600.507533
6321 126400 198683.327875
6322 126420 198766.151152
6323 126440 198848.977365
6324 126460 198931.806512
6325 126480 199014.638593
6326 126500 199097.473608
6327 126520 199180.311555
6328 126540 199263.152435
6329 126560 199345.996246
6330 126580 199428.842988
6331 126600 199511.692661
6332 126620 199594.545264
6333 126640 199677.400796
6334 126660 199760.259256
6335 126680 199843.120645
6336 126700 199925.984962
6337 126720 200008.852205
6338 126740 200091.722375
6339 126760 200174.595470
6340 126780 200257.471491
6341 126800 200340.350436
6342 126820 200423.232305
6343 126840 200506.117098
6344 126860 200589.004813
6345 126880 200671.895451
6346 126900 200754.789011
6347 126920 200837.685491
6348 126940 200920.584893
6349 126960 201003.487214
6350 126980 201086.392454
6351 127000 201169.300613
6352 127020 201252.211691
6353 127040 201335.125686
6354 127060 201418.042598
6355 127080 201500.962426
6356 127100 201583.885171
6357 127120 201666.810830
6358 127140 201749.739404
6359 127160 201832.670893
6360 127180 201915.605295
6361 127200 201998.542610
6362 127220 202081.482837
6363 127240 202164.425976
6364 127260 202247.372026
6365 127280 202330.320987
6366 127300 202413.272858
6367 127320 202496.227639
6368 127340 202579.185328
6369 127360 202662.145926
6370 127380 202745.109431
6371 127400 202828.075843
6372 127420 202911.045162
6373 127440 202994.017388
6374 127460 203076.992518
6375 127480 203159.970553
6376 127500 203242.951493
6377 127520 203325.935336
6378 127540 203408.922083
6379 127560 203491.911732
6380 127580 203574.904283
6381 127600 203657.899735
6382 127620 203740.898088
6383 127640 203823.899341
6384 127660 203906.903494
6385 127680 203989.910546
6386 127700 204072.920497
6387 127720 204155.933345
6388 127740 204238.949091
6389 127760 204321.967733
6390 127780 204404.989272
6391 127800 204488.013707
6392 127820 204571.041036
6393 127840 204654.071260
6394 127860 204737.104378
6395 127880 204820.140389
6396 127900 204903.179293
6397 127920 204986.221089
6398 127940 205069.265777
6399 127960 205152.313355
6400 127980 205235.363825
6401 128000 205318.417184
6402 128020 205401.473432
6403 128040 205484.532569
6404 128060 205567.594594
6405 128080 205650.659507
6406 128100 205733.727307
6407 128120 205816.797994
6408 128140 205899.871566
6409 128160 205982.948024
6410 128180 206066.027366
6411 128200 206149.109593
6412 128220 206232.194703
6413 128240 206315.282696
6414 128260 206398.373572
6415 128280 206481.467329
6416 128300 206564.563968
6417 128320 206647.663488
6418 128340 206730.765888
6419 128360 206813.871167
6420 128380 206896.979326
6421 128400 206980.090363
6422 128420 207063.204278
6423 128440 207146.321070
6424 128460 207229.440739
6425 128480 207312.563285
6426 128500 207395.688706
6427 128520 207478.817002
6428 128540 207561.948172
6429 128560 207645.082217
6430 128580 207728.219135
6431 128600 207811.358925
6432 128620 207894.501588
6433 128640 207977.647123
6434 128660 208060.795529
6435 128680 208143.946805
6436 128700 208227.100951
6437 128720 208310.257967
6438 128740 208393.417852
6439 128760 208476.580605
6440 128780 208559.746225
6441 128800 208642.914713
6442 128820 208726.086067
6443 128840 208809.260288
6444 128860 208892.437374
6445 128880 208975.617325
6446 128900 209058.800140
6447 128920 209141.985819
6448 128940 209225.174361
6449 128960 209308.365766
6450 128980 209391.560033
6451 129000 209474.757161
6452 129020 209557.957151
6453 129040 209641.160001
6454 129060 209724.365710
6455 129080 209807.574279
6456 129100 209890.785707
6457 129120 209973.999993
6458 129140 210057.217137
6459 129160 210140.437137
6460 129180 210223.659994
6461 129200 210306.885707
6462 129220 210390.114276
6463 129240 210473.345699
6464 129260 210556.579976
6465 129280 210639.817107
6466 129300 210723.057092
6467 129320 210806.299929
6468 129340 210889.545617
6469 129360 210972.794158
6470 129380 211056.045549
6471 129400 211139.299791
6472 129420 211222.556882
6473 129440 211305.816823
6474 129460 211389.079612
6475 129480 211472.345249
6476 129500 211555.613734
6477 129520 211638.885066
6478 129540 211722.159244
6479 129560 211805.436269
6480 129580 211888.716138
6481 129600 211971.998852
6482 129620 212055.284411
6483 129640 212138.572813
6484 129660 212221.864059
6485 129680 212305.158146
6486 129700 212388.455076
6487 129720 212471.754848
6488 129740 212555.057460
6489 129760 212638.362912
6490 129780 212721.671205
6491 129800 212804.982336
6492 129820 212888.296306
6493 129840 212971.613115
6494 129860 213054.932761
6495 129880 213138.255244
6496 129900 213221.580563
6497 129920 213304.908718
6498 129940 213388.239709
6499 129960 213471.573534
6500 129980 213554.910194
6501 130000 213638.249687

E-029-convexity.json — 2.9 KB, updated 08-19-2026

JSON sidecar (click to expand)
{
  "experiment": "E-029",
  "task": "4.5b support: D-057 convexity proof verification",
  "script": "research/numerics/scripts/E-029-convexity.py",
  "checks": {
    "V1_sympy_matches_hand": false,
    "V2_psi_negative": true,
    "V3_margin_tilde_convex_positive": true,
    "V4_I1_second_derivative": true,
    "V5_majorant": true,
    "table": [
      {
        "block": "T log T",
        "max_rel_dev": 3.86713266584926e-16,
        "max_abs_dev": 1.1102230246251565e-16,
        "psi_max_on_grid": -9.278201041106209e-12,
        "negative_everywhere": true
      },
      {
        "block": "T",
        "max_rel_dev": 0.0,
        "max_abs_dev": 0.0,
        "psi_max_on_grid": -3.225806451613944e-13,
        "negative_everywhere": true
      },
      {
        "block": "log T",
        "max_rel_dev": 2.884096233990709e-16,
        "max_abs_dev": 1.3877787807814457e-17,
        "psi_max_on_grid": -2.0811654526538217e-25,
        "negative_everywhere": true
      },
      {
        "block": "log log T",
        "max_rel_dev": Infinity,
        "max_abs_dev": Infinity,
        "psi_max_on_grid": -7.361493795231685e-27,
        "negative_everywhere": true
      },
      {
        "block": "T L1",
        "max_rel_dev": 4.18537251328277e-16,
        "max_abs_dev": 1.1102230246251565e-16,
        "psi_max_on_grid": -9.278201041106209e-12,
        "negative_everywhere": true
      },
      {
        "block": "T L2",
        "max_rel_dev": 4.87560538773749e-16,
        "max_abs_dev": 4.163336342344337e-17,
        "psi_max_on_grid": -1.0839608294029891e-12,
        "negative_everywhere": true
      },
      {
        "block": "L1^2",
        "max_rel_dev": 3.8295728938905445e-16,
        "max_abs_dev": 1.3877787807814457e-17,
        "psi_max_on_grid": -1.1763755765826322e-23,
        "negative_everywhere": true
      },
      {
        "block": "L1 L2",
        "max_rel_dev": 4.506079009424732e-16,
        "max_abs_dev": 1.3877787807814457e-17,
        "psi_max_on_grid": -9.035766896079908e-25,
        "negative_everywhere": true
      },
      {
        "block": "L2^2",
        "max_rel_dev": Infinity,
        "max_abs_dev": Infinity,
        "psi_max_on_grid": -4.920397462312061e-26,
        "negative_everywhere": true
      },
      {
        "block": "L1",
        "max_rel_dev": 2.2639034786574087e-16,
        "max_abs_dev": 6.938893903907228e-18,
        "psi_max_on_grid": -2.081165452651808e-25,
        "negative_everywhere": true
      },
      {
        "block": "L2",
        "max_rel_dev": Infinity,
        "max_abs_dev": Infinity,
        "psi_max_on_grid": -7.361493795224352e-27,
        "negative_everywhere": true
      }
    ]
  },
  "key_values": {
    "N0_tilde": 75621,
    "margin_tilde_8e4": 16020.1971966516,
    "margin_tilde_prime_8e4": 3.6877762759686448,
    "max_2n_B4d2": 0.18932637317273204
  },
  "elapsed_seconds": 1.0
}

Source code

Python script (click to expand)
"""E-029 — task 4.5b support: D-057 convexity proof verification.

  V1. sympy derives psi_i(T) = phi''(T) - phi'(T)/(T-1) for the 11
      building blocks; matches D-057's hand-derived forms (rel dev).
  V2. psi_i(T) < 0 on T in [T0, 3.1e12] (log grid) — supports the
      hand sign arguments.
  V3. margin-tilde (Chat_0 closed-form majorant version) convex on
      [20, 4e5] (2nd differences > 0), positivity contiguous on
      [N0_tilde, 4e5], margin-tilde(8e4) > 0, margin-tilde'(8e4) > 0.
  V4. 2n|B4''(n)| < 1 on [20, 1e4] (I_I second-derivative bound).
  V5. B-tilde >= B pointwise on the E-028 grid (majorant sanity).

Outputs: research/numerics/data/E-029-convexity.csv (+ .json)
"""
import json
import sys
import time
from pathlib import Path

import mpmath as mp
import numpy as np
import sympy as sp

REPO = Path(__file__).resolve().parents[3]
DATA = REPO / "research" / "numerics" / "data"
CSV = DATA / "E-029-convexity.csv"
JSON = DATA / "E-029-convexity.json"

A_TR, B0_TR = 10.5, 48.0
M0 = 1.335180201468999
GAMMA = 0.577215664901532860606512090082402431
C1L = (GAMMA - 1 - np.log(2 * np.pi)) / 2 - GAMMA / 2
T0 = np.sqrt(20.0) + 1.0


def build_table():
    """The 11 building blocks: (name, phi(T) sympy, psi hand form)."""
    T = sp.symbols('T', positive=True)
    L1 = sp.log(T + 2)
    L2 = sp.log(sp.log(T + 2))
    phis = [
        ("T log T", T * sp.log(T),
         lambda t: 1 / t - (np.log(t) + 1) / (t - 1)),
        ("T", T,
         lambda t: -1 / (t - 1)),
        ("log T", sp.log(T),
         lambda t: -1 / t ** 2 - 1 / (t * (t - 1))),
        ("log log T", sp.log(sp.log(T)),
         lambda t: -(np.log(t) + 1) / (t * np.log(t)) ** 2
         - 1 / (t * np.log(t) * (t - 1))),
        ("T L1", T * L1,
         lambda t: (t + 4) / (t + 2) ** 2
         - np.log(t + 2) / (t - 1) - t / ((t + 2) * (t - 1))),
        ("T L2", T * L2,
         lambda t: (t * (np.log(t + 2) - 1) + 4 * np.log(t + 2))
         / ((t + 2) * np.log(t + 2)) ** 2
         - np.log(np.log(t + 2)) / (t - 1)
         - t / ((t + 2) * np.log(t + 2) * (t - 1))),
        ("L1^2", L1 ** 2,
         lambda t: 2 * (1 - np.log(t + 2)) / (t + 2) ** 2
         - 2 * np.log(t + 2) / ((t + 2) * (t - 1))),
        ("L1 L2", L1 * L2,
         lambda t: (1 / np.log(t + 2) - np.log(np.log(t + 2)) - 1)
         / (t + 2) ** 2
         - (np.log(np.log(t + 2)) + 1) / ((t + 2) * (t - 1))),
        ("L2^2", L2 ** 2,
         lambda t: 2 * (1 - np.log(np.log(t + 2))
                        * (np.log(t + 2) + 1))
         / ((t + 2) * np.log(t + 2)) ** 2
         - 2 * np.log(np.log(t + 2))
         / ((t + 2) * np.log(t + 2) * (t - 1))),
        ("L1", L1,
         lambda t: -1 / (t + 2) ** 2 - 1 / ((t + 2) * (t - 1))),
        ("L2", L2,
         lambda t: -(np.log(t + 2) + 1) / ((t + 2) * np.log(t + 2)) ** 2
         - 1 / ((t + 2) * np.log(t + 2) * (t - 1))),
    ]
    rows = []
    tpts = np.geomspace(T0, 1e6, 300)      # symbolic-match grid
    tpts_full = np.geomspace(T0, 3.1e12, 400)  # negativity grid
    for name, phi, psi_hand in phis:
        psi_sym = sp.lambdify(
            T, sp.simplify(sp.diff(phi, T, 2)
                           - sp.diff(phi, T) / (T - 1)), "numpy")
        v_sym = psi_sym(tpts)
        v_hand = psi_hand(tpts)
        diff = np.abs(v_sym - v_hand)
        rel = float(np.max(np.where(np.abs(v_hand) > 1e-15,
                                    diff / np.abs(v_hand), 0.0)))
        absd = float(np.max(diff))
        rows.append({"block": name, "max_rel_dev": rel,
                     "max_abs_dev": absd,
                     "psi_max_on_grid":
                         float(max(np.max(v_sym),
                                   np.max(psi_hand(tpts_full)))),
                     "negative_everywhere":
                         bool(np.max(v_sym) < 0
                              and np.max(psi_hand(tpts_full)) < 0)})
    return rows


def Chat(t):
    return (0.433 * np.log(t + 2) + 0.886 * np.log(np.log(t + 2)) + 2.9)


def C0_honest(t):
    def F(u):
        return (u / (2 * np.pi)) * np.log(u / (2 * np.pi)) - u / (2 * np.pi) \
            + 7 / 8

    def Sb(u):
        return 0.137 * np.log(u) + 0.443 * np.log(np.log(u)) + 1.588
    return (F(t + 1) - F(t)) + Sb(t + 1) + Sb(t)


def B_tilde(n):
    n = np.asarray(n, dtype=np.float64)
    T = np.sqrt(n) + 1.0
    C = Chat(T)
    H = 3 * C ** 2 + A_TR * np.log(T + 2) + B0_TR
    twoN = 2 * ((T / (2 * np.pi)) * np.log(T / (2 * np.pi))
                - T / (2 * np.pi) + 7 / 8
                + 0.137 * np.log(T) + 0.443 * np.log(np.log(T)) + 1.588)
    I3 = 4 * T * ((2 / 3) * C + A_TR * np.log(T + 2) + B0_TR)
    I24 = 2 * (np.e ** 4 + 1) * (7 * H + 3.3)
    r = np.sqrt(n)
    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 + 40.0 * M0 + I24 + I1 + 1.0


def B_honest(n):
    n = np.asarray(n, dtype=np.float64)
    T = np.sqrt(n) + 1.0
    C = C0_honest(T)
    H = 3 * C ** 2 + A_TR * np.log(T + 2) + B0_TR
    twoN = 2 * ((T / (2 * np.pi)) * np.log(T / (2 * np.pi))
                - T / (2 * np.pi) + 7 / 8
                + 0.137 * np.log(T) + 0.443 * np.log(np.log(T)) + 1.588)
    I3 = 4 * T * ((2 / 3) * C + A_TR * np.log(T + 2) + B0_TR)
    I24 = 2 * (np.e ** 4 + 1) * (7 * H + 3.3)
    r = np.sqrt(n)
    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 + 40.0 * M0 + I24 + I1 + 1.0


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


def B4_d2(n):
    """Second derivative of I_I(n) = 2(sqrt(n)+1)(e^{sqrt(n)/2}+1)*0.57
    * 2^{-(2 sqrt(n)-2)} via complex-step-free central differences at
    dps 40."""
    mp.mp.dps = 40

    def f(x):
        r = mp.sqrt(x)
        return 2 * (r + 1) * (mp.e ** (r / 2) + 1) * mp.mpf("0.57") \
            * 2 ** (-(2 * r - 2))

    h = mp.mpf("0.01")
    return float((f(n + h) - 2 * f(n) + f(n - h)) / h ** 2)


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("V1/V2: building-block table ...", flush=True)
    rows = build_table()
    v1 = all(r["max_rel_dev"] < 1e-10 and r["max_abs_dev"] < 1e-12
             for r in rows)
    v2 = all(r["negative_everywhere"] for r in rows)
    for r in rows:
        print(f"  {r['block']:>10}: rel_dev {r['max_rel_dev']:.2e}, "
              f"psi_max {r['psi_max_on_grid']:.3e}", flush=True)

    print("V3: margin-tilde convexity/positivity ...", flush=True)
    ngrid = np.arange(20, 400001)
    mt = margin_tilde(ngrid)
    conv = bool(np.all(np.diff(mt, 2) > 0))
    pos = ngrid[(mt > 0) & (np.gradient(mt, ngrid) > 0)]
    N0t = int(pos[0]) if len(pos) else -1
    cont = bool(np.all(mt[ngrid >= N0t] > 0)) if N0t > 0 else False
    m8 = float(margin_tilde(80000.0))
    d8 = float((margin_tilde(80000.5) - margin_tilde(79999.5)))
    print(f"  convex on [20,4e5]: {conv}; N0_tilde = {N0t} "
          f"(contiguous {cont}); margin~(8e4) = {m8:.1f}, "
          f"margin~'(8e4) = {d8:.1f}", flush=True)
    v3 = bool(conv and cont and m8 > 0 and d8 > 0)

    print("V4: I_I second-derivative bound ...", flush=True)
    n4 = np.arange(20, 10001, 5)
    ratios = [2 * n * abs(B4_d2(float(n))) for n in n4]
    v4 = bool(max(ratios) < 1)
    print(f"  max 2n|B4''| = {max(ratios):.4f} at n = "
          f"{n4[int(np.argmax(ratios))]} (< 1: {v4})", flush=True)

    print("V5: majorant sanity ...", flush=True)
    ng = np.arange(20, 400001, 20)
    v5 = bool(np.all(B_tilde(ng) >= B_honest(ng)))
    print(f"  B_tilde >= B_honest on grid: {v5}", flush=True)

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

    meta = {
        "experiment": "E-029",
        "task": "4.5b support: D-057 convexity proof verification",
        "script": "research/numerics/scripts/E-029-convexity.py",
        "checks": {
            "V1_sympy_matches_hand": v1,
            "V2_psi_negative": v2,
            "V3_margin_tilde_convex_positive": v3,
            "V4_I1_second_derivative": v4,
            "V5_majorant": v5,
            "table": rows,
        },
        "key_values": {"N0_tilde": N0t, "margin_tilde_8e4": m8,
                       "margin_tilde_prime_8e4": d8,
                       "max_2n_B4d2": float(max(ratios))},
        "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 all([v1, v2, v3, v4, v5]) else 1


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