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 5 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
2002 40020 27529.896350 38362.295339
2003 40040 27608.536340 38442.732287
2004 40060 27687.184637 38523.177096
2005 40080 27765.841236 38603.629761
2006 40100 27844.506131 38684.090277
2007 40120 27923.179316 38764.558639
2008 40140 28001.860787 38845.034842
2009 40160 28080.550538 38925.518880
2010 40180 28159.248563 39006.010748
2011 40200 28237.954856 39086.510441
2012 40220 28316.669413 39167.017955
2013 40240 28395.392227 39247.533283
2014 40260 28474.123294 39328.056421
2015 40280 28552.862607 39408.587364
2016 40300 28631.610162 39489.126106
2017 40320 28710.365953 39569.672643
2018 40340 28789.129974 39650.226969
2019 40360 28867.902220 39730.789079
2020 40380 28946.682686 39811.358968
2021 40400 29025.471366 39891.936632
2022 40420 29104.268254 39972.522064
2023 40440 29183.073346 40053.115261
2024 40460 29261.886636 40133.716216
2025 40480 29340.708119 40214.324925
2026 40500 29419.537789 40294.941383
2027 40520 29498.375641 40375.565584
2028 40540 29577.221669 40456.197525
2029 40560 29656.075868 40536.837199
2030 40580 29734.938233 40617.484601
2031 40600 29813.808758 40698.139727
2032 40620 29892.687438 40778.802572
2033 40640 29971.574268 40859.473130
2034 40660 30050.469242 40940.151397
2035 40680 30129.372355 41020.837367
2036 40700 30208.283602 41101.531036
2037 40720 30287.202977 41182.232399
2038 40740 30366.130476 41262.941450
2039 40760 30445.066092 41343.658185
2040 40780 30524.009820 41424.382599
2041 40800 30602.961656 41505.114686
2042 40820 30681.921593 41585.854442
2043 40840 30760.889628 41666.601862
2044 40860 30839.865753 41747.356941
2045 40880 30918.849965 41828.119674
2046 40900 30997.842257 41908.890056
2047 40920 31076.842625 41989.668082
2048 40940 31155.851063 42070.453747
2049 40960 31234.867567 42151.247046
2050 40980 31313.892130 42232.047975
2051 41000 31392.924748 42312.856528
2052 41020 31471.965415 42393.672701
2053 41040 31551.014127 42474.496489
2054 41060 31630.070877 42555.327886
2055 41080 31709.135662 42636.166889
2056 41100 31788.208474 42717.013491
2057 41120 31867.289311 42797.867689
2058 41140 31946.378165 42878.729477
2059 41160 32025.475033 42959.598850
2060 41180 32104.579908 43040.475804
2061 41200 32183.692787 43121.360334
2062 41220 32262.813662 43202.252435
2063 41240 32341.942530 43283.152102
2064 41260 32421.079386 43364.059331
2065 41280 32500.224223 43444.974115
2066 41300 32579.377038 43525.896452
2067 41320 32658.537824 43606.826335
2068 41340 32737.706577 43687.763761
2069 41360 32816.883292 43768.708724
2070 41380 32896.067964 43849.661219
2071 41400 32975.260586 43930.621242
2072 41420 33054.461155 44011.588788
2073 41440 33133.669666 44092.563852
2074 41460 33212.886112 44173.546429
2075 41480 33292.110490 44254.536516
2076 41500 33371.342793 44335.534106
2077 41520 33450.583018 44416.539195
2078 41540 33529.831158 44497.551778
2079 41560 33609.087210 44578.571851
2080 41580 33688.351167 44659.599409
2081 41600 33767.623024 44740.634448
2082 41620 33846.902778 44821.676961
2083 41640 33926.190422 44902.726946
2084 41660 34005.485952 44983.784396
2085 41680 34084.789362 45064.849308
2086 41700 34164.100648 45145.921677
2087 41720 34243.419805 45227.001497
2088 41740 34322.746827 45308.088765
2089 41760 34402.081710 45389.183475
2090 41780 34481.424449 45470.285623
2091 41800 34560.775038 45551.395205
2092 41820 34640.133473 45632.512214
2093 41840 34719.499749 45713.636648
2094 41860 34798.873860 45794.768501
2095 41880 34878.255802 45875.907769
2096 41900 34957.645570 45957.054446
2097 41920 35037.043159 46038.208529
2098 41940 35116.448564 46119.370013
2099 41960 35195.861780 46200.538892
2100 41980 35275.282802 46281.715163
2101 42000 35354.711625 46362.898821
2102 42020 35434.148244 46444.089861
2103 42040 35513.592655 46525.288279
2104 42060 35593.044852 46606.494069
2105 42080 35672.504830 46687.707228
2106 42100 35751.972586 46768.927751
2107 42120 35831.448113 46850.155633
2108 42140 35910.931406 46931.390869
2109 42160 35990.422462 47012.633456
2110 42180 36069.921275 47093.883388
2111 42200 36149.427840 47175.140661
2112 42220 36228.942153 47256.405271
2113 42240 36308.464208 47337.677212
2114 42260 36387.994000 47418.956480
2115 42280 36467.531526 47500.243071
2116 42300 36547.076779 47581.536981
2117 42320 36626.629756 47662.838204
2118 42340 36706.190451 47744.146736
2119 42360 36785.758860 47825.462573
2120 42380 36865.334977 47906.785710
2121 42400 36944.918798 47988.116142
2122 42420 37024.510318 48069.453866
2123 42440 37104.109533 48150.798876
2124 42460 37183.716437 48232.151168
2125 42480 37263.331025 48313.510738
2126 42500 37342.953294 48394.877581
2127 42520 37422.583237 48476.251692
2128 42540 37502.220852 48557.633068
2129 42560 37581.866131 48639.021703
2130 42580 37661.519072 48720.417593
2131 42600 37741.179668 48801.820735
2132 42620 37820.847916 48883.231122
2133 42640 37900.523811 48964.648752
2134 42660 37980.207347 49046.073619
2135 42680 38059.898521 49127.505718
2136 42700 38139.597327 49208.945047
2137 42720 38219.303761 49290.391599
2138 42740 38299.017817 49371.845371
2139 42760 38378.739492 49453.306359
2140 42780 38458.468780 49534.774557
2141 42800 38538.205678 49616.249962
2142 42820 38617.950179 49697.732568
2143 42840 38697.702280 49779.222373
2144 42860 38777.461976 49860.719370
2145 42880 38857.229261 49942.223557
2146 42900 38937.004133 50023.734928
2147 42920 39016.786585 50105.253479
2148 42940 39096.576613 50186.779206
2149 42960 39176.374212 50268.312104
2150 42980 39256.179378 50349.852169
2151 43000 39335.992107 50431.399397
2152 43020 39415.812393 50512.953783
2153 43040 39495.640231 50594.515323
2154 43060 39575.475618 50676.084012
2155 43080 39655.318549 50757.659847
2156 43100 39735.169019 50839.242823
2157 43120 39815.027023 50920.832936
2158 43140 39894.892556 51002.430180
2159 43160 39974.765615 51084.034553
2160 43180 40054.646195 51165.646049
2161 43200 40134.534290 51247.264665
2162 43220 40214.429897 51328.890395
2163 43240 40294.333011 51410.523237
2164 43260 40374.243627 51492.163184
2165 43280 40454.161740 51573.810234
2166 43300 40534.087347 51655.464382
2167 43320 40614.020442 51737.125623
2168 43340 40693.961021 51818.793954
2169 43360 40773.909080 51900.469370
2170 43380 40853.864613 51982.151866
2171 43400 40933.827617 52063.841439
2172 43420 41013.798086 52145.538085
2173 43440 41093.776017 52227.241798
2174 43460 41173.761404 52308.952575
2175 43480 41253.754244 52390.670411
2176 43500 41333.754531 52472.395303
2177 43520 41413.762262 52554.127246
2178 43540 41493.777431 52635.866236
2179 43560 41573.800035 52717.612268
2180 43580 41653.830068 52799.365339
2181 43600 41733.867526 52881.125444
2182 43620 41813.912405 52962.892578
2183 43640 41893.964700 53044.666739
2184 43660 41974.024407 53126.447921
2185 43680 42054.091521 53208.236121
2186 43700 42134.166038 53290.031334
2187 43720 42214.247954 53371.833555
2188 43740 42294.337263 53453.642782
2189 43760 42374.433961 53535.459009
2190 43780 42454.538045 53617.282233
2191 43800 42534.649509 53699.112449
2192 43820 42614.768350 53780.949653
2193 43840 42694.894562 53862.793841
2194 43860 42775.028141 53944.645009
2195 43880 42855.169083 54026.503152
2196 43900 42935.317384 54108.368268
2197 43920 43015.473039 54190.240350
2198 43940 43095.636043 54272.119396
2199 43960 43175.806392 54354.005401
2200 43980 43255.984083 54435.898361
2201 44000 43336.169109 54517.798272
2202 44020 43416.361468 54599.705130
2203 44040 43496.561155 54681.618930
2204 44060 43576.768164 54763.539669
2205 44080 43656.982493 54845.467343
2206 44100 43737.204136 54927.401947
2207 44120 43817.433089 55009.343477
2208 44140 43897.669349 55091.291929
2209 44160 43977.912909 55173.247300
2210 44180 44058.163767 55255.209584
2211 44200 44138.421918 55337.178779
2212 44220 44218.687357 55419.154879
2213 44240 44298.960080 55501.137882
2214 44260 44379.240083 55583.127782
2215 44280 44459.527361 55665.124575
2216 44300 44539.821911 55747.128259
2217 44320 44620.123727 55829.138828
2218 44340 44700.432806 55911.156278
2219 44360 44780.749143 55993.180606
2220 44380 44861.072734 56075.211808
2221 44400 44941.403575 56157.249879
2222 44420 45021.741661 56239.294816
2223 44440 45102.086988 56321.346614
2224 44460 45182.439552 56403.405269
2225 44480 45262.799348 56485.470778
2226 44500 45343.166373 56567.543136
2227 44520 45423.540621 56649.622340
2228 44540 45503.922090 56731.708385
2229 44560 45584.310773 56813.801267
2230 44580 45664.706668 56895.900983
2231 44600 45745.109770 56978.007529
2232 44620 45825.520074 57060.120899
2233 44640 45905.937577 57142.241092
2234 44660 45986.362275 57224.368101
2235 44680 46066.794162 57306.501924
2236 44700 46147.233235 57388.642557
2237 44720 46227.679489 57470.789996
2238 44740 46308.132921 57552.944236
2239 44760 46388.593526 57635.105274
2240 44780 46469.061300 57717.273105
2241 44800 46549.536239 57799.447727
2242 44820 46630.018338 57881.629134
2243 44840 46710.507594 57963.817323
2244 44860 46791.004002 58046.012290
2245 44880 46871.507557 58128.214032
2246 44900 46952.018257 58210.422543
2247 44920 47032.536096 58292.637821
2248 44940 47113.061070 58374.859861
2249 44960 47193.593176 58457.088659
2250 44980 47274.132409 58539.324213
2251 45000 47354.678765 58621.566516
2252 45020 47435.232240 58703.815567
2253 45040 47515.792829 58786.071360
2254 45060 47596.360529 58868.333892
2255 45080 47676.935336 58950.603160
2256 45100 47757.517244 59032.879158
2257 45120 47838.106251 59115.161884
2258 45140 47918.702352 59197.451334
2259 45160 47999.305542 59279.747503
2260 45180 48079.915819 59362.050387
2261 45200 48160.533177 59444.359984
2262 45220 48241.157612 59526.676289
2263 45240 48321.789122 59608.999298
2264 45260 48402.427700 59691.329007
2265 45280 48483.073344 59773.665412
2266 45300 48563.726049 59856.008511
2267 45320 48644.385811 59938.358298
2268 45340 48725.052626 60020.714770
2269 45360 48805.726490 60103.077923
2270 45380 48886.407398 60185.447754
2271 45400 48967.095348 60267.824258
2272 45420 49047.790334 60350.207432
2273 45440 49128.492354 60432.597272
2274 45460 49209.201401 60514.993774
2275 45480 49289.917474 60597.396934
2276 45500 49370.640566 60679.806749
2277 45520 49451.370676 60762.223215
2278 45540 49532.107798 60844.646327
2279 45560 49612.851928 60927.076083
2280 45580 49693.603062 61009.512478
2281 45600 49774.361197 61091.955509
2282 45620 49855.126329 61174.405171
2283 45640 49935.898453 61256.861462
2284 45660 50016.677565 61339.324377
2285 45680 50097.463661 61421.793912
2286 45700 50178.256738 61504.270065
2287 45720 50259.056791 61586.752830
2288 45740 50339.863816 61669.242204
2289 45760 50420.677810 61751.738185
2290 45780 50501.498768 61834.240767
2291 45800 50582.326687 61916.749947
2292 45820 50663.161562 61999.265721
2293 45840 50744.003389 62081.788086
2294 45860 50824.852165 62164.317038
2295 45880 50905.707886 62246.852573
2296 45900 50986.570547 62329.394688
2297 45920 51067.440145 62411.943378
2298 45940 51148.316675 62494.498641
2299 45960 51229.200134 62577.060471
2300 45980 51310.090518 62659.628867
2301 46000 51390.987822 62742.203823
2302 46020 51471.892044 62824.785337
2303 46040 51552.803178 62907.373404
2304 46060 51633.721222 62989.968021
2305 46080 51714.646170 63072.569185
2306 46100 51795.578020 63155.176890
2307 46120 51876.516767 63237.791135
2308 46140 51957.462408 63320.411915
2309 46160 52038.414938 63403.039226
2310 46180 52119.374353 63485.673066
2311 46200 52200.340651 63568.313429
2312 46220 52281.313826 63650.960313
2313 46240 52362.293875 63733.613714
2314 46260 52443.280794 63816.273629
2315 46280 52524.274579 63898.940053
2316 46300 52605.275226 63981.612983
2317 46320 52686.282732 64064.292415
2318 46340 52767.297092 64146.978346
2319 46360 52848.318303 64229.670772
2320 46380 52929.346361 64312.369690
2321 46400 53010.381262 64395.075095
2322 46420 53091.423002 64477.786985
2323 46440 53172.471577 64560.505355
2324 46460 53253.526984 64643.230203
2325 46480 53334.589218 64725.961523
2326 46500 53415.658276 64808.699314
2327 46520 53496.734153 64891.443571
2328 46540 53577.816847 64974.194290
2329 46560 53658.906353 65056.951469
2330 46580 53740.002668 65139.715103
2331 46600 53821.105787 65222.485189
2332 46620 53902.215707 65305.261723
2333 46640 53983.332424 65388.044702
2334 46660 54064.455934 65470.834122
2335 46680 54145.586234 65553.629980
2336 46700 54226.723319 65636.432272
2337 46720 54307.867186 65719.240994
2338 46740 54389.017832 65802.056144
2339 46760 54470.175251 65884.877716
2340 46780 54551.339441 65967.705709
2341 46800 54632.510397 66050.540118
2342 46820 54713.688117 66133.380939
2343 46840 54794.872595 66216.228170
2344 46860 54876.063829 66299.081806
2345 46880 54957.261815 66381.941845
2346 46900 55038.466549 66464.808282
2347 46920 55119.678026 66547.681114
2348 46940 55200.896244 66630.560338
2349 46960 55282.121199 66713.445950
2350 46980 55363.352887 66796.337947
2351 47000 55444.591304 66879.236324
2352 47020 55525.836446 66962.141080
2353 47040 55607.088310 67045.052209
2354 47060 55688.346892 67127.969709
2355 47080 55769.612188 67210.893576
2356 47100 55850.884195 67293.823806
2357 47120 55932.162908 67376.760397
2358 47140 56013.448325 67459.703344
2359 47160 56094.740441 67542.652644
2360 47180 56176.039253 67625.608294
2361 47200 56257.344757 67708.570291
2362 47220 56338.656949 67791.538629
2363 47240 56419.975826 67874.513308
2364 47260 56501.301383 67957.494322
2365 47280 56582.633618 68040.481668
2366 47300 56663.972527 68123.475343
2367 47320 56745.318106 68206.475344
2368 47340 56826.670350 68289.481667
2369 47360 56908.029258 68372.494308
2370 47380 56989.394824 68455.513264
2371 47400 57070.767046 68538.538533
2372 47420 57152.145919 68621.570109
2373 47440 57233.531440 68704.607990
2374 47460 57314.923605 68787.652173
2375 47480 57396.322411 68870.702653
2376 47500 57477.727855 68953.759428
2377 47520 57559.139931 69036.822495
2378 47540 57640.558637 69119.891849
2379 47560 57721.983970 69202.967487
2380 47580 57803.415925 69286.049406
2381 47600 57884.854498 69369.137603
2382 47620 57966.299687 69452.232074
2383 47640 58047.751488 69535.332816
2384 47660 58129.209897 69618.439825
2385 47680 58210.674910 69701.553098
2386 47700 58292.146524 69784.672631
2387 47720 58373.624735 69867.798422
2388 47740 58455.109540 69950.930466
2389 47760 58536.600934 70034.068762
2390 47780 58618.098916 70117.213304
2391 47800 58699.603480 70200.364089
2392 47820 58781.114623 70283.521116
2393 47840 58862.632342 70366.684379
2394 47860 58944.156633 70449.853876
2395 47880 59025.687493 70533.029603
2396 47900 59107.224918 70616.211557
2397 47920 59188.768904 70699.399735
2398 47940 59270.319448 70782.594133
2399 47960 59351.876546 70865.794747
2400 47980 59433.440196 70949.001576
2401 48000 59515.010392 71032.214615
2402 48020 59596.587132 71115.433860
2403 48040 59678.170412 71198.659309
2404 48060 59759.760229 71281.890959
2405 48080 59841.356578 71365.128805
2406 48100 59922.959458 71448.372845
2407 48120 60004.568863 71531.623076
2408 48140 60086.184791 71614.879493
2409 48160 60167.807237 71698.142095
2410 48180 60249.436199 71781.410876
2411 48200 60331.071673 71864.685835
2412 48220 60412.713656 71947.966967
2413 48240 60494.362143 72031.254270
2414 48260 60576.017132 72114.547741
2415 48280 60657.678619 72197.847375
2416 48300 60739.346600 72281.153170
2417 48320 60821.021072 72364.465122
2418 48340 60902.702031 72447.783229
2419 48360 60984.389475 72531.107486
2420 48380 61066.083398 72614.437891
2421 48400 61147.783799 72697.774440
2422 48420 61229.490674 72781.117131
2423 48440 61311.204018 72864.465959
2424 48460 61392.923829 72947.820921
2425 48480 61474.650103 73031.182015
2426 48500 61556.382837 73114.549237
2427 48520 61638.122027 73197.922584
2428 48540 61719.867669 73281.302052
2429 48560 61801.619761 73364.687639
2430 48580 61883.378299 73448.079341
2431 48600 61965.143279 73531.477154
2432 48620 62046.914698 73614.881077
2433 48640 62128.692553 73698.291104
2434 48660 62210.476840 73781.707234
2435 48680 62292.267555 73865.129463
2436 48700 62374.064696 73948.557787
2437 48720 62455.868259 74031.992204
2438 48740 62537.678239 74115.432710
2439 48760 62619.494635 74198.879303
2440 48780 62701.317443 74282.331978
2441 48800 62783.146658 74365.790733
2442 48820 62864.982279 74449.255565
2443 48840 62946.824301 74532.726469
2444 48860 63028.672720 74616.203444
2445 48880 63110.527535 74699.686486
2446 48900 63192.388740 74783.175592
2447 48920 63274.256333 74866.670758
2448 48940 63356.130311 74950.171981
2449 48960 63438.010670 75033.679259
2450 48980 63519.897406 75117.192588
2451 49000 63601.790516 75200.711965
2452 49020 63683.689998 75284.237386
2453 49040 63765.595847 75367.768849
2454 49060 63847.508060 75451.306350
2455 49080 63929.426634 75534.849887
2456 49100 64011.351565 75618.399455
2457 49120 64093.282851 75701.955053
2458 49140 64175.220487 75785.516676
2459 49160 64257.164470 75869.084322
2460 49180 64339.114798 75952.657987
2461 49200 64421.071466 76036.237669
2462 49220 64503.034472 76119.823364
2463 49240 64585.003812 76203.415069
2464 49260 64666.979482 76287.012781
2465 49280 64748.961480 76370.616497
2466 49300 64830.949802 76454.226213
2467 49320 64912.944444 76537.841927
2468 49340 64994.945404 76621.463636
2469 49360 65076.952678 76705.091336
2470 49380 65158.966263 76788.725024
2471 49400 65240.986156 76872.364698
2472 49420 65323.012352 76956.010353
2473 49440 65405.044849 77039.661987
2474 49460 65487.083644 77123.319597
2475 49480 65569.128733 77206.983180
2476 49500 65651.180113 77290.652733
2477 49520 65733.237781 77374.328252
2478 49540 65815.301733 77458.009735
2479 49560 65897.371967 77541.697178
2480 49580 65979.448478 77625.390578
2481 49600 66061.531263 77709.089932
2482 49620 66143.620320 77792.795238
2483 49640 66225.715645 77876.506492
2484 49660 66307.817235 77960.223691
2485 49680 66389.925086 78043.946831
2486 49700 66472.039196 78127.675911
2487 49720 66554.159560 78211.410927
2488 49740 66636.286176 78295.151875
2489 49760 66718.419041 78378.898753
2490 49780 66800.558151 78462.651558
2491 49800 66882.703503 78546.410286
2492 49820 66964.855093 78630.174935
2493 49840 67047.012919 78713.945501
2494 49860 67129.176978 78797.721982
2495 49880 67211.347265 78881.504375
2496 49900 67293.523778 78965.292676
2497 49920 67375.706514 79049.086882
2498 49940 67457.895469 79132.886991
2499 49960 67540.090640 79216.692999
2500 49980 67622.292024 79300.504903
2501 50000 67704.499618 79384.322701

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