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 4 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
1502 30020 -10611.780616 -742.404323
1503 30040 -10538.171251 -666.719596
1504 30060 -10464.549604 -591.023286
1505 30080 -10390.915686 -515.315402
1506 30100 -10317.269508 -439.595956
1507 30120 -10243.611082 -363.864958
1508 30140 -10169.940420 -288.122418
1509 30160 -10096.257531 -212.368348
1510 30180 -10022.562429 -136.602757
1511 30200 -9948.855123 -60.825656
1512 30220 -9875.135625 14.962943
1513 30240 -9801.403947 90.763032
1514 30260 -9727.660098 166.574599
1515 30280 -9653.904092 242.397634
1516 30300 -9580.135938 318.232127
1517 30320 -9506.355649 394.078066
1518 30340 -9432.563234 469.935442
1519 30360 -9358.758705 545.804245
1520 30380 -9284.942074 621.684463
1521 30400 -9211.113350 697.576087
1522 30420 -9137.272546 773.479107
1523 30440 -9063.419672 849.393511
1524 30460 -8989.554740 925.319290
1525 30480 -8915.677759 1001.256434
1526 30500 -8841.788742 1077.204931
1527 30520 -8767.887699 1153.164773
1528 30540 -8693.974641 1229.135948
1529 30560 -8620.049579 1305.118447
1530 30580 -8546.112524 1381.112259
1531 30600 -8472.163486 1457.117374
1532 30620 -8398.202477 1533.133782
1533 30640 -8324.229507 1609.161473
1534 30660 -8250.244588 1685.200436
1535 30680 -8176.247730 1761.250663
1536 30700 -8102.238943 1837.312141
1537 30720 -8028.218239 1913.384862
1538 30740 -7954.185628 1989.468816
1539 30760 -7880.141121 2065.563992
1540 30780 -7806.084729 2141.670380
1541 30800 -7732.016463 2217.787971
1542 30820 -7657.936332 2293.916754
1543 30840 -7583.844348 2370.056719
1544 30860 -7509.740522 2446.207857
1545 30880 -7435.624863 2522.370157
1546 30900 -7361.497383 2598.543610
1547 30920 -7287.358093 2674.728205
1548 30940 -7213.207002 2750.923934
1549 30960 -7139.044121 2827.130785
1550 30980 -7064.869461 2903.348749
1551 31000 -6990.683033 2979.577816
1552 31020 -6916.484847 3055.817976
1553 31040 -6842.274912 3132.069220
1554 31060 -6768.053241 3208.331538
1555 31080 -6693.819843 3284.604920
1556 31100 -6619.574728 3360.889356
1557 31120 -6545.317908 3437.184837
1558 31140 -6471.049392 3513.491352
1559 31160 -6396.769191 3589.808892
1560 31180 -6322.477316 3666.137447
1561 31200 -6248.173775 3742.477008
1562 31220 -6173.858581 3818.827565
1563 31240 -6099.531743 3895.189109
1564 31260 -6025.193272 3971.561629
1565 31280 -5950.843177 4047.945116
1566 31300 -5876.481470 4124.339560
1567 31320 -5802.108159 4200.744953
1568 31340 -5727.723257 4277.161283
1569 31360 -5653.326772 4353.588542
1570 31380 -5578.918715 4430.026721
1571 31400 -5504.499096 4506.475809
1572 31420 -5430.067926 4582.935797
1573 31440 -5355.625214 4659.406676
1574 31460 -5281.170970 4735.888436
1575 31480 -5206.705206 4812.381067
1576 31500 -5132.227930 4888.884561
1577 31520 -5057.739153 4965.398907
1578 31540 -4983.238885 5041.924097
1579 31560 -4908.727136 5118.460121
1580 31580 -4834.203916 5195.006970
1581 31600 -4759.669235 5271.564633
1582 31620 -4685.123103 5348.133102
1583 31640 -4610.565530 5424.712368
1584 31660 -4535.996527 5501.302421
1585 31680 -4461.416102 5577.903252
1586 31700 -4386.824266 5654.514851
1587 31720 -4312.221029 5731.137210
1588 31740 -4237.606401 5807.770318
1589 31760 -4162.980391 5884.414167
1590 31780 -4088.343010 5961.068748
1591 31800 -4013.694267 6037.734050
1592 31820 -3939.034173 6114.410066
1593 31840 -3864.362737 6191.096786
1594 31860 -3789.679968 6267.794200
1595 31880 -3714.985877 6344.502299
1596 31900 -3640.280474 6421.221075
1597 31920 -3565.563768 6497.950518
1598 31940 -3490.835769 6574.690619
1599 31960 -3416.096486 6651.441369
1600 31980 -3341.345930 6728.202758
1601 32000 -3266.584110 6804.974779
1602 32020 -3191.811036 6881.757421
1603 32040 -3117.026718 6958.550675
1604 32060 -3042.231164 7035.354533
1605 32080 -2967.424386 7112.168986
1606 32100 -2892.606391 7188.994024
1607 32120 -2817.777191 7265.829638
1608 32140 -2742.936795 7342.675820
1609 32160 -2668.085212 7419.532561
1610 32180 -2593.222451 7496.399851
1611 32200 -2518.348523 7573.277682
1612 32220 -2443.463437 7650.166044
1613 32240 -2368.567202 7727.064930
1614 32260 -2293.659828 7803.974329
1615 32280 -2218.741325 7880.894233
1616 32300 -2143.811702 7957.824633
1617 32320 -2068.870968 8034.765521
1618 32340 -1993.919132 8111.716887
1619 32360 -1918.956205 8188.678722
1620 32380 -1843.982196 8265.651019
1621 32400 -1768.997114 8342.633767
1622 32420 -1694.000968 8419.626958
1623 32440 -1618.993768 8496.630584
1624 32460 -1543.975523 8573.644635
1625 32480 -1468.946243 8650.669104
1626 32500 -1393.905937 8727.703980
1627 32520 -1318.854614 8804.749256
1628 32540 -1243.792284 8881.804922
1629 32560 -1168.718955 8958.870971
1630 32580 -1093.634637 9035.947393
1631 32600 -1018.539340 9113.034179
1632 32620 -943.433073 9190.131321
1633 32640 -868.315844 9267.238811
1634 32660 -793.187663 9344.356640
1635 32680 -718.048540 9421.484798
1636 32700 -642.898483 9498.623279
1637 32720 -567.737501 9575.772072
1638 32740 -492.565605 9652.931169
1639 32760 -417.382802 9730.100562
1640 32780 -342.189102 9807.280242
1641 32800 -266.984515 9884.470201
1642 32820 -191.769049 9961.670430
1643 32840 -116.542713 10038.880921
1644 32860 -41.305517 10116.101665
1645 32880 33.942531 10193.332653
1646 32900 109.201421 10270.573878
1647 32920 184.471145 10347.825331
1648 32940 259.751693 10425.087002
1649 32960 335.043056 10502.358885
1650 32980 410.345226 10579.640970
1651 33000 485.658193 10656.933249
1652 33020 560.981948 10734.235713
1653 33040 636.316483 10811.548355
1654 33060 711.661789 10888.871166
1655 33080 787.017856 10966.204137
1656 33100 862.384676 11043.547260
1657 33120 937.762239 11120.900528
1658 33140 1013.150537 11198.263930
1659 33160 1088.549562 11275.637460
1660 33180 1163.959304 11353.021109
1661 33200 1239.379753 11430.414868
1662 33220 1314.810903 11507.818730
1663 33240 1390.252743 11585.232686
1664 33260 1465.705265 11662.656727
1665 33280 1541.168460 11740.090847
1666 33300 1616.642319 11817.535035
1667 33320 1692.126834 11894.989285
1668 33340 1767.621995 11972.453588
1669 33360 1843.127795 12049.927936
1670 33380 1918.644224 12127.412320
1671 33400 1994.171273 12204.906733
1672 33420 2069.708935 12282.411166
1673 33440 2145.257199 12359.925611
1674 33460 2220.816058 12437.450061
1675 33480 2296.385503 12514.984506
1676 33500 2371.965526 12592.528939
1677 33520 2447.556117 12670.083353
1678 33540 2523.157267 12747.647737
1679 33560 2598.768969 12825.222086
1680 33580 2674.391214 12902.806390
1681 33600 2750.023993 12980.400642
1682 33620 2825.667298 13058.004833
1683 33640 2901.321120 13135.618955
1684 33660 2976.985450 13213.243002
1685 33680 3052.660280 13290.876964
1686 33700 3128.345602 13368.520833
1687 33720 3204.041407 13446.174602
1688 33740 3279.747686 13523.838263
1689 33760 3355.464431 13601.511807
1690 33780 3431.191633 13679.195227
1691 33800 3506.929285 13756.888515
1692 33820 3582.677377 13834.591663
1693 33840 3658.435901 13912.304663
1694 33860 3734.204849 13990.027507
1695 33880 3809.984213 14067.760188
1696 33900 3885.773983 14145.502697
1697 33920 3961.574152 14223.255027
1698 33940 4037.384712 14301.017169
1699 33960 4113.205653 14378.789116
1700 33980 4189.036967 14456.570861
1701 34000 4264.878647 14534.362394
1702 34020 4340.730684 14612.163710
1703 34040 4416.593069 14689.974799
1704 34060 4492.465794 14767.795654
1705 34080 4568.348852 14845.626267
1706 34100 4644.242233 14923.466631
1707 34120 4720.145929 15001.316737
1708 34140 4796.059933 15079.176579
1709 34160 4871.984236 15157.046148
1710 34180 4947.918829 15234.925436
1711 34200 5023.863705 15312.814436
1712 34220 5099.818855 15390.713141
1713 34240 5175.784272 15468.621542
1714 34260 5251.759946 15546.539633
1715 34280 5327.745870 15624.467404
1716 34300 5403.742036 15702.404849
1717 34320 5479.748435 15780.351961
1718 34340 5555.765060 15858.308730
1719 34360 5631.791902 15936.275151
1720 34380 5707.828954 16014.251214
1721 34400 5783.876206 16092.236914
1722 34420 5859.933651 16170.232241
1723 34440 5936.001281 16248.237189
1724 34460 6012.079089 16326.251750
1725 34480 6088.167065 16404.275917
1726 34500 6164.265202 16482.309682
1727 34520 6240.373491 16560.353037
1728 34540 6316.491926 16638.405974
1729 34560 6392.620497 16716.468488
1730 34580 6468.759197 16794.540569
1731 34600 6544.908018 16872.622211
1732 34620 6621.066951 16950.713406
1733 34640 6697.235990 17028.814146
1734 34660 6773.415126 17106.924425
1735 34680 6849.604350 17185.044234
1736 34700 6925.803656 17263.173567
1737 34720 7002.013035 17341.312416
1738 34740 7078.232479 17419.460773
1739 34760 7154.461981 17497.618632
1740 34780 7230.701532 17575.785984
1741 34800 7306.951126 17653.962822
1742 34820 7383.210753 17732.149140
1743 34840 7459.480406 17810.344929
1744 34860 7535.760077 17888.550183
1745 34880 7612.049758 17966.764894
1746 34900 7688.349443 18044.989055
1747 34920 7764.659122 18123.222658
1748 34940 7840.978788 18201.465696
1749 34960 7917.308433 18279.718163
1750 34980 7993.648050 18357.980050
1751 35000 8069.997630 18436.251350
1752 35020 8146.357167 18514.532056
1753 35040 8222.726652 18592.822162
1754 35060 8299.106077 18671.121659
1755 35080 8375.495435 18749.430541
1756 35100 8451.894719 18827.748799
1757 35120 8528.303920 18906.076428
1758 35140 8604.723030 18984.413420
1759 35160 8681.152043 19062.759768
1760 35180 8757.590951 19141.115464
1761 35200 8834.039745 19219.480502
1762 35220 8910.498419 19297.854873
1763 35240 8986.966964 19376.238572
1764 35260 9063.445373 19454.631591
1765 35280 9139.933639 19533.033923
1766 35300 9216.431754 19611.445561
1767 35320 9292.939709 19689.866497
1768 35340 9369.457499 19768.296725
1769 35360 9445.985115 19846.736237
1770 35380 9522.522549 19925.185027
1771 35400 9599.069794 20003.643088
1772 35420 9675.626843 20082.110412
1773 35440 9752.193689 20160.586992
1774 35460 9828.770322 20239.072821
1775 35480 9905.356737 20317.567893
1776 35500 9981.952925 20396.072200
1777 35520 10058.558880 20474.585735
1778 35540 10135.174593 20553.108492
1779 35560 10211.800058 20631.640463
1780 35580 10288.435266 20710.181642
1781 35600 10365.080210 20788.732021
1782 35620 10441.734884 20867.291593
1783 35640 10518.399279 20945.860352
1784 35660 10595.073389 21024.438291
1785 35680 10671.757205 21103.025402
1786 35700 10748.450720 21181.621680
1787 35720 10825.153928 21260.227116
1788 35740 10901.866820 21338.841704
1789 35760 10978.589390 21417.465437
1790 35780 11055.321629 21496.098309
1791 35800 11132.063532 21574.740312
1792 35820 11208.815090 21653.391440
1793 35840 11285.576296 21732.051685
1794 35860 11362.347142 21810.721042
1795 35880 11439.127622 21889.399502
1796 35900 11515.917729 21968.087060
1797 35920 11592.717454 22046.783708
1798 35940 11669.526792 22125.489440
1799 35960 11746.345733 22204.204249
1800 35980 11823.174272 22282.928128
1801 36000 11900.012401 22361.661070
1802 36020 11976.860113 22440.403069
1803 36040 12053.717401 22519.154118
1804 36060 12130.584257 22597.914210
1805 36080 12207.460674 22676.683338
1806 36100 12284.346645 22755.461497
1807 36120 12361.242164 22834.248678
1808 36140 12438.147222 22913.044875
1809 36160 12515.061813 22991.850082
1810 36180 12591.985929 23070.664292
1811 36200 12668.919563 23149.487498
1812 36220 12745.862709 23228.319694
1813 36240 12822.815360 23307.160873
1814 36260 12899.777507 23386.011028
1815 36280 12976.749144 23464.870153
1816 36300 13053.730264 23543.738241
1817 36320 13130.720861 23622.615285
1818 36340 13207.720925 23701.501279
1819 36360 13284.730452 23780.396217
1820 36380 13361.749434 23859.300091
1821 36400 13438.777863 23938.212895
1822 36420 13515.815733 24017.134622
1823 36440 13592.863037 24096.065267
1824 36460 13669.919767 24175.004821
1825 36480 13746.985917 24253.953280
1826 36500 13824.061480 24332.910636
1827 36520 13901.146449 24411.876883
1828 36540 13978.240816 24490.852014
1829 36560 14055.344576 24569.836022
1830 36580 14132.457720 24648.828902
1831 36600 14209.580242 24727.830647
1832 36620 14286.712136 24806.841250
1833 36640 14363.853394 24885.860705
1834 36660 14441.004009 24964.889005
1835 36680 14518.163974 25043.926144
1836 36700 14595.333283 25122.972116
1837 36720 14672.511929 25202.026913
1838 36740 14749.699904 25281.090530
1839 36760 14826.897202 25360.162960
1840 36780 14904.103817 25439.244197
1841 36800 14981.319741 25518.334235
1842 36820 15058.544967 25597.433066
1843 36840 15135.779489 25676.540685
1844 36860 15213.023299 25755.657085
1845 36880 15290.276392 25834.782259
1846 36900 15367.538760 25913.916203
1847 36920 15444.810396 25993.058908
1848 36940 15522.091294 26072.210369
1849 36960 15599.381447 26151.370580
1850 36980 15676.680848 26230.539533
1851 37000 15753.989490 26309.717224
1852 37020 15831.307368 26388.903645
1853 37040 15908.634473 26468.098790
1854 37060 15985.970799 26547.302653
1855 37080 16063.316340 26626.515227
1856 37100 16140.671089 26705.736507
1857 37120 16218.035039 26784.966486
1858 37140 16295.408183 26864.205158
1859 37160 16372.790516 26943.452516
1860 37180 16450.182029 27022.708555
1861 37200 16527.582717 27101.973267
1862 37220 16604.992573 27181.246648
1863 37240 16682.411590 27260.528690
1864 37260 16759.839762 27339.819388
1865 37280 16837.277081 27419.118735
1866 37300 16914.723542 27498.426724
1867 37320 16992.179138 27577.743351
1868 37340 17069.643862 27657.068609
1869 37360 17147.117707 27736.402490
1870 37380 17224.600668 27815.744991
1871 37400 17302.092737 27895.096103
1872 37420 17379.593908 27974.455822
1873 37440 17457.104174 28053.824140
1874 37460 17534.623529 28133.201052
1875 37480 17612.151967 28212.586552
1876 37500 17689.689480 28291.980634
1877 37520 17767.236062 28371.383291
1878 37540 17844.791707 28450.794517
1879 37560 17922.356409 28530.214307
1880 37580 17999.930160 28609.642654
1881 37600 18077.512955 28689.079552
1882 37620 18155.104786 28768.524995
1883 37640 18232.705648 28847.978977
1884 37660 18310.315533 28927.441492
1885 37680 18387.934436 29006.912534
1886 37700 18465.562350 29086.392097
1887 37720 18543.199269 29165.880175
1888 37740 18620.845186 29245.376762
1889 37760 18698.500095 29324.881851
1890 37780 18776.163989 29404.395438
1891 37800 18853.836862 29483.917515
1892 37820 18931.518708 29563.448078
1893 37840 19009.209520 29642.987119
1894 37860 19086.909292 29722.534633
1895 37880 19164.618017 29802.090614
1896 37900 19242.335689 29881.655057
1897 37920 19320.062303 29961.227954
1898 37940 19397.797850 30040.809300
1899 37960 19475.542326 30120.399090
1900 37980 19553.295724 30199.997317
1901 38000 19631.058037 30279.603976
1902 38020 19708.829260 30359.219060
1903 38040 19786.609385 30438.842564
1904 38060 19864.398407 30518.474481
1905 38080 19942.196320 30598.114806
1906 38100 20020.003116 30677.763534
1907 38120 20097.818791 30757.420657
1908 38140 20175.643337 30837.086170
1909 38160 20253.476748 30916.760068
1910 38180 20331.319018 30996.442345
1911 38200 20409.170142 31076.132994
1912 38220 20487.030112 31155.832010
1913 38240 20564.898922 31235.539388
1914 38260 20642.776567 31315.255120
1915 38280 20720.663040 31394.979202
1916 38300 20798.558334 31474.711627
1917 38320 20876.462445 31554.452391
1918 38340 20954.375364 31634.201486
1919 38360 21032.297088 31713.958908
1920 38380 21110.227608 31793.724651
1921 38400 21188.166919 31873.498708
1922 38420 21266.115016 31953.281074
1923 38440 21344.071891 32033.071743
1924 38460 21422.037539 32112.870710
1925 38480 21500.011953 32192.677969
1926 38500 21577.995128 32272.493514
1927 38520 21655.987057 32352.317339
1928 38540 21733.987734 32432.149439
1929 38560 21811.997154 32511.989808
1930 38580 21890.015310 32591.838440
1931 38600 21968.042195 32671.695330
1932 38620 22046.077805 32751.560471
1933 38640 22124.122132 32831.433859
1934 38660 22202.175172 32911.315487
1935 38680 22280.236917 32991.205351
1936 38700 22358.307362 33071.103443
1937 38720 22436.386501 33151.009759
1938 38740 22514.474327 33230.924293
1939 38760 22592.570836 33310.847040
1940 38780 22670.676020 33390.777993
1941 38800 22748.789874 33470.717147
1942 38820 22826.912391 33550.664497
1943 38840 22905.043567 33630.620036
1944 38860 22983.183394 33710.583760
1945 38880 23061.331867 33790.555663
1946 38900 23139.488980 33870.535739
1947 38920 23217.654727 33950.523982
1948 38940 23295.829102 34030.520388
1949 38960 23374.012099 34110.524949
1950 38980 23452.203713 34190.537662
1951 39000 23530.403937 34270.558520
1952 39020 23608.612765 34350.587518
1953 39040 23686.830191 34430.624650
1954 39060 23765.056210 34510.669911
1955 39080 23843.290816 34590.723295
1956 39100 23921.534003 34670.784797
1957 39120 23999.785764 34750.854412
1958 39140 24078.046095 34830.932132
1959 39160 24156.314989 34911.017955
1960 39180 24234.592440 34991.111873
1961 39200 24312.878443 35071.213881
1962 39220 24391.172991 35151.323974
1963 39240 24469.476079 35231.442146
1964 39260 24547.787702 35311.568393
1965 39280 24626.107853 35391.702708
1966 39300 24704.436526 35471.845085
1967 39320 24782.773716 35551.995521
1968 39340 24861.119416 35632.154009
1969 39360 24939.473622 35712.320543
1970 39380 25017.836327 35792.495119
1971 39400 25096.207526 35872.677730
1972 39420 25174.587212 35952.868373
1973 39440 25252.975380 36033.067040
1974 39460 25331.372025 36113.273727
1975 39480 25409.777140 36193.488429
1976 39500 25488.190721 36273.711139
1977 39520 25566.612760 36353.941854
1978 39540 25645.043252 36434.180566
1979 39560 25723.482193 36514.427272
1980 39580 25801.929575 36594.681965
1981 39600 25880.385393 36674.944640
1982 39620 25958.849642 36755.215292
1983 39640 26037.322316 36835.493916
1984 39660 26115.803409 36915.780506
1985 39680 26194.292916 36996.075057
1986 39700 26272.790831 37076.377564
1987 39720 26351.297148 37156.688021
1988 39740 26429.811861 37237.006423
1989 39760 26508.334966 37317.332765
1990 39780 26586.866456 37397.667042
1991 39800 26665.406325 37478.009247
1992 39820 26743.954569 37558.359377
1993 39840 26822.511181 37638.717425
1994 39860 26901.076156 37719.083387
1995 39880 26979.649488 37799.457257
1996 39900 27058.231172 37879.839031
1997 39920 27136.821202 37960.228701
1998 39940 27215.419573 38040.626265
1999 39960 27294.026279 38121.031715
2000 39980 27372.641314 38201.445048
2001 40000 27451.264672 38281.866258

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