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 14 of 40 · 500/page · 20000 rows next →
# nmargin_tilde
6502 130020 213721.592014
6503 130040 213804.937173
6504 130060 213888.285164
6505 130080 213971.635987
6506 130100 214054.989640
6507 130120 214138.346124
6508 130140 214221.705438
6509 130160 214305.067581
6510 130180 214388.432553
6511 130200 214471.800353
6512 130220 214555.170981
6513 130240 214638.544436
6514 130260 214721.920717
6515 130280 214805.299824
6516 130300 214888.681757
6517 130320 214972.066515
6518 130340 215055.454097
6519 130360 215138.844503
6520 130380 215222.237732
6521 130400 215305.633783
6522 130420 215389.032657
6523 130440 215472.434353
6524 130460 215555.838869
6525 130480 215639.246206
6526 130500 215722.656364
6527 130520 215806.069340
6528 130540 215889.485135
6529 130560 215972.903749
6530 130580 216056.325180
6531 130600 216139.749429
6532 130620 216223.176494
6533 130640 216306.606376
6534 130660 216390.039073
6535 130680 216473.474585
6536 130700 216556.912912
6537 130720 216640.354053
6538 130740 216723.798007
6539 130760 216807.244773
6540 130780 216890.694353
6541 130800 216974.146744
6542 130820 217057.601946
6543 130840 217141.059959
6544 130860 217224.520782
6545 130880 217307.984415
6546 130900 217391.450857
6547 130920 217474.920107
6548 130940 217558.392165
6549 130960 217641.867031
6550 130980 217725.344704
6551 131000 217808.825183
6552 131020 217892.308468
6553 131040 217975.794558
6554 131060 218059.283452
6555 131080 218142.775151
6556 131100 218226.269654
6557 131120 218309.766960
6558 131140 218393.267068
6559 131160 218476.769979
6560 131180 218560.275690
6561 131200 218643.784203
6562 131220 218727.295516
6563 131240 218810.809629
6564 131260 218894.326542
6565 131280 218977.846253
6566 131300 219061.368762
6567 131320 219144.894070
6568 131340 219228.422174
6569 131360 219311.953075
6570 131380 219395.486772
6571 131400 219479.023264
6572 131420 219562.562552
6573 131440 219646.104634
6574 131460 219729.649510
6575 131480 219813.197180
6576 131500 219896.747642
6577 131520 219980.300897
6578 131540 220063.856943
6579 131560 220147.415781
6580 131580 220230.977409
6581 131600 220314.541828
6582 131620 220398.109036
6583 131640 220481.679033
6584 131660 220565.251819
6585 131680 220648.827393
6586 131700 220732.405755
6587 131720 220815.986903
6588 131740 220899.570838
6589 131760 220983.157559
6590 131780 221066.747065
6591 131800 221150.339356
6592 131820 221233.934431
6593 131840 221317.532290
6594 131860 221401.132932
6595 131880 221484.736357
6596 131900 221568.342565
6597 131920 221651.951553
6598 131940 221735.563323
6599 131960 221819.177874
6600 131980 221902.795204
6601 132000 221986.415314
6602 132020 222070.038203
6603 132040 222153.663870
6604 132060 222237.292316
6605 132080 222320.923538
6606 132100 222404.557538
6607 132120 222488.194314
6608 132140 222571.833865
6609 132160 222655.476192
6610 132180 222739.121293
6611 132200 222822.769169
6612 132220 222906.419819
6613 132240 222990.073241
6614 132260 223073.729436
6615 132280 223157.388403
6616 132300 223241.050142
6617 132320 223324.714652
6618 132340 223408.381932
6619 132360 223492.051982
6620 132380 223575.724802
6621 132400 223659.400390
6622 132420 223743.078747
6623 132440 223826.759871
6624 132460 223910.443763
6625 132480 223994.130422
6626 132500 224077.819847
6627 132520 224161.512038
6628 132540 224245.206994
6629 132560 224328.904714
6630 132580 224412.605199
6631 132600 224496.308447
6632 132620 224580.014459
6633 132640 224663.723232
6634 132660 224747.434768
6635 132680 224831.149066
6636 132700 224914.866124
6637 132720 224998.585943
6638 132740 225082.308522
6639 132760 225166.033861
6640 132780 225249.761958
6641 132800 225333.492813
6642 132820 225417.226427
6643 132840 225500.962797
6644 132860 225584.701925
6645 132880 225668.443808
6646 132900 225752.188448
6647 132920 225835.935842
6648 132940 225919.685992
6649 132960 226003.438895
6650 132980 226087.194552
6651 133000 226170.952963
6652 133020 226254.714125
6653 133040 226338.478040
6654 133060 226422.244707
6655 133080 226506.014124
6656 133100 226589.786292
6657 133120 226673.561210
6658 133140 226757.338878
6659 133160 226841.119294
6660 133180 226924.902459
6661 133200 227008.688371
6662 133220 227092.477032
6663 133240 227176.268438
6664 133260 227260.062592
6665 133280 227343.859491
6666 133300 227427.659135
6667 133320 227511.461524
6668 133340 227595.266658
6669 133360 227679.074535
6670 133380 227762.885155
6671 133400 227846.698518
6672 133420 227930.514623
6673 133440 228014.333470
6674 133460 228098.155058
6675 133480 228181.979387
6676 133500 228265.806455
6677 133520 228349.636264
6678 133540 228433.468811
6679 133560 228517.304097
6680 133580 228601.142121
6681 133600 228684.982882
6682 133620 228768.826381
6683 133640 228852.672616
6684 133660 228936.521586
6685 133680 229020.373293
6686 133700 229104.227734
6687 133720 229188.084910
6688 133740 229271.944819
6689 133760 229355.807462
6690 133780 229439.672838
6691 133800 229523.540946
6692 133820 229607.411786
6693 133840 229691.285357
6694 133860 229775.161659
6695 133880 229859.040691
6696 133900 229942.922453
6697 133920 230026.806945
6698 133940 230110.694165
6699 133960 230194.584113
6700 133980 230278.476789
6701 134000 230362.372192
6702 134020 230446.270322
6703 134040 230530.171178
6704 134060 230614.074760
6705 134080 230697.981067
6706 134100 230781.890098
6707 134120 230865.801854
6708 134140 230949.716333
6709 134160 231033.633535
6710 134180 231117.553460
6711 134200 231201.476107
6712 134220 231285.401476
6713 134240 231369.329565
6714 134260 231453.260375
6715 134280 231537.193905
6716 134300 231621.130155
6717 134320 231705.069124
6718 134340 231789.010811
6719 134360 231872.955216
6720 134380 231956.902338
6721 134400 232040.852178
6722 134420 232124.804734
6723 134440 232208.760005
6724 134460 232292.717993
6725 134480 232376.678695
6726 134500 232460.642111
6727 134520 232544.608242
6728 134540 232628.577086
6729 134560 232712.548643
6730 134580 232796.522912
6731 134600 232880.499893
6732 134620 232964.479585
6733 134640 233048.461988
6734 134660 233132.447102
6735 134680 233216.434925
6736 134700 233300.425458
6737 134720 233384.418699
6738 134740 233468.414649
6739 134760 233552.413306
6740 134780 233636.414671
6741 134800 233720.418743
6742 134820 233804.425521
6743 134840 233888.435004
6744 134860 233972.447193
6745 134880 234056.462087
6746 134900 234140.479685
6747 134920 234224.499987
6748 134940 234308.522992
6749 134960 234392.548699
6750 134980 234476.577109
6751 135000 234560.608221
6752 135020 234644.642034
6753 135040 234728.678547
6754 135060 234812.717761
6755 135080 234896.759675
6756 135100 234980.804287
6757 135120 235064.851599
6758 135140 235148.901608
6759 135160 235232.954315
6760 135180 235317.009720
6761 135200 235401.067821
6762 135220 235485.128618
6763 135240 235569.192111
6764 135260 235653.258299
6765 135280 235737.327182
6766 135300 235821.398759
6767 135320 235905.473030
6768 135340 235989.549994
6769 135360 236073.629650
6770 135380 236157.711999
6771 135400 236241.797039
6772 135420 236325.884771
6773 135440 236409.975193
6774 135460 236494.068305
6775 135480 236578.164107
6776 135500 236662.262598
6777 135520 236746.363778
6778 135540 236830.467646
6779 135560 236914.574201
6780 135580 236998.683444
6781 135600 237082.795374
6782 135620 237166.909989
6783 135640 237251.027290
6784 135660 237335.147277
6785 135680 237419.269948
6786 135700 237503.395303
6787 135720 237587.523342
6788 135740 237671.654063
6789 135760 237755.787468
6790 135780 237839.923555
6791 135800 237924.062323
6792 135820 238008.203773
6793 135840 238092.347903
6794 135860 238176.494714
6795 135880 238260.644204
6796 135900 238344.796373
6797 135920 238428.951221
6798 135940 238513.108747
6799 135960 238597.268951
6800 135980 238681.431832
6801 136000 238765.597390
6802 136020 238849.765624
6803 136040 238933.936533
6804 136060 239018.110118
6805 136080 239102.286378
6806 136100 239186.465311
6807 136120 239270.646919
6808 136140 239354.831200
6809 136160 239439.018153
6810 136180 239523.207779
6811 136200 239607.400076
6812 136220 239691.595045
6813 136240 239775.792684
6814 136260 239859.992994
6815 136280 239944.195973
6816 136300 240028.401622
6817 136320 240112.609939
6818 136340 240196.820925
6819 136360 240281.034578
6820 136380 240365.250899
6821 136400 240449.469886
6822 136420 240533.691540
6823 136440 240617.915860
6824 136460 240702.142845
6825 136480 240786.372495
6826 136500 240870.604809
6827 136520 240954.839787
6828 136540 241039.077428
6829 136560 241123.317733
6830 136580 241207.560699
6831 136600 241291.806328
6832 136620 241376.054618
6833 136640 241460.305568
6834 136660 241544.559180
6835 136680 241628.815451
6836 136700 241713.074381
6837 136720 241797.335971
6838 136740 241881.600219
6839 136760 241965.867124
6840 136780 242050.136688
6841 136800 242134.408908
6842 136820 242218.683785
6843 136840 242302.961318
6844 136860 242387.241506
6845 136880 242471.524349
6846 136900 242555.809847
6847 136920 242640.097999
6848 136940 242724.388804
6849 136960 242808.682263
6850 136980 242892.978374
6851 137000 242977.277137
6852 137020 243061.578552
6853 137040 243145.882617
6854 137060 243230.189334
6855 137080 243314.498700
6856 137100 243398.810717
6857 137120 243483.125382
6858 137140 243567.442696
6859 137160 243651.762659
6860 137180 243736.085268
6861 137200 243820.410526
6862 137220 243904.738430
6863 137240 243989.068980
6864 137260 244073.402176
6865 137280 244157.738017
6866 137300 244242.076503
6867 137320 244326.417633
6868 137340 244410.761407
6869 137360 244495.107825
6870 137380 244579.456885
6871 137400 244663.808588
6872 137420 244748.162932
6873 137440 244832.519918
6874 137460 244916.879545
6875 137480 245001.241812
6876 137500 245085.606720
6877 137520 245169.974266
6878 137540 245254.344452
6879 137560 245338.717276
6880 137580 245423.092738
6881 137600 245507.470838
6882 137620 245591.851574
6883 137640 245676.234948
6884 137660 245760.620957
6885 137680 245845.009602
6886 137700 245929.400882
6887 137720 246013.794797
6888 137740 246098.191346
6889 137760 246182.590528
6890 137780 246266.992344
6891 137800 246351.396792
6892 137820 246435.803873
6893 137840 246520.213585
6894 137860 246604.625929
6895 137880 246689.040903
6896 137900 246773.458508
6897 137920 246857.878742
6898 137940 246942.301606
6899 137960 247026.727099
6900 137980 247111.155220
6901 138000 247195.585969
6902 138020 247280.019345
6903 138040 247364.455349
6904 138060 247448.893978
6905 138080 247533.335234
6906 138100 247617.779116
6907 138120 247702.225622
6908 138140 247786.674753
6909 138160 247871.126508
6910 138180 247955.580886
6911 138200 248040.037888
6912 138220 248124.497512
6913 138240 248208.959758
6914 138260 248293.424627
6915 138280 248377.892116
6916 138300 248462.362226
6917 138320 248546.834956
6918 138340 248631.310306
6919 138360 248715.788275
6920 138380 248800.268864
6921 138400 248884.752070
6922 138420 248969.237894
6923 138440 249053.726336
6924 138460 249138.217395
6925 138480 249222.711070
6926 138500 249307.207361
6927 138520 249391.706267
6928 138540 249476.207789
6929 138560 249560.711925
6930 138580 249645.218675
6931 138600 249729.728039
6932 138620 249814.240016
6933 138640 249898.754605
6934 138660 249983.271807
6935 138680 250067.791620
6936 138700 250152.314045
6937 138720 250236.839080
6938 138740 250321.366725
6939 138760 250405.896981
6940 138780 250490.429845
6941 138800 250574.965319
6942 138820 250659.503400
6943 138840 250744.044090
6944 138860 250828.587387
6945 138880 250913.133291
6946 138900 250997.681802
6947 138920 251082.232919
6948 138940 251166.786641
6949 138960 251251.342968
6950 138980 251335.901900
6951 139000 251420.463435
6952 139020 251505.027575
6953 139040 251589.594318
6954 139060 251674.163663
6955 139080 251758.735611
6956 139100 251843.310160
6957 139120 251927.887311
6958 139140 252012.467062
6959 139160 252097.049414
6960 139180 252181.634366
6961 139200 252266.221917
6962 139220 252350.812067
6963 139240 252435.404816
6964 139260 252520.000162
6965 139280 252604.598107
6966 139300 252689.198648
6967 139320 252773.801785
6968 139340 252858.407519
6969 139360 252943.015849
6970 139380 253027.626774
6971 139400 253112.240293
6972 139420 253196.856407
6973 139440 253281.475114
6974 139460 253366.096415
6975 139480 253450.720309
6976 139500 253535.346795
6977 139520 253619.975873
6978 139540 253704.607542
6979 139560 253789.241802
6980 139580 253873.878653
6981 139600 253958.518094
6982 139620 254043.160125
6983 139640 254127.804744
6984 139660 254212.451953
6985 139680 254297.101749
6986 139700 254381.754133
6987 139720 254466.409104
6988 139740 254551.066663
6989 139760 254635.726807
6990 139780 254720.389537
6991 139800 254805.054853
6992 139820 254889.722754
6993 139840 254974.393239
6994 139860 255059.066308
6995 139880 255143.741960
6996 139900 255228.420196
6997 139920 255313.101014
6998 139940 255397.784414
6999 139960 255482.470396
7000 139980 255567.158960
7001 140000 255651.850103

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