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 13 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
6002 120020 380685.107246 396820.249894
6003 120040 380779.541125 396915.739989
6004 120060 380873.977127 397011.232124
6005 120080 380968.415254 397106.726301
6006 120100 381062.855503 397202.222518
6007 120120 381157.297876 397297.720774
6008 120140 381251.742372 397393.221070
6009 120160 381346.188990 397488.723406
6010 120180 381440.637729 397584.227779
6011 120200 381535.088590 397679.734191
6012 120220 381629.541572 397775.242641
6013 120240 381723.996675 397870.753129
6014 120260 381818.453897 397966.265653
6015 120280 381912.913240 398061.780214
6016 120300 382007.374702 398157.296812
6017 120320 382101.838282 398252.815445
6018 120340 382196.303982 398348.336114
6019 120360 382290.771799 398443.858818
6020 120380 382385.241734 398539.383557
6021 120400 382479.713786 398634.910330
6022 120420 382574.187955 398730.439137
6023 120440 382668.664241 398825.969978
6024 120460 382763.142643 398921.502852
6025 120480 382857.623160 399017.037759
6026 120500 382952.105793 399112.574698
6027 120520 383046.590540 399208.113669
6028 120540 383141.077402 399303.654672
6029 120560 383235.566378 399399.197706
6030 120580 383330.057468 399494.742771
6031 120600 383424.550670 399590.289867
6032 120620 383519.045986 399685.838993
6033 120640 383613.543414 399781.390148
6034 120660 383708.042954 399876.943333
6035 120680 383802.544605 399972.498546
6036 120700 383897.048368 400068.055788
6037 120720 383991.554241 400163.615058
6038 120740 384086.062225 400259.176357
6039 120760 384180.572318 400354.739682
6040 120780 384275.084522 400450.305034
6041 120800 384369.598834 400545.872414
6042 120820 384464.115255 400641.441819
6043 120840 384558.633784 400737.013250
6044 120860 384653.154421 400832.586707
6045 120880 384747.677166 400928.162188
6046 120900 384842.202018 401023.739695
6047 120920 384936.728977 401119.319225
6048 120940 385031.258041 401214.900780
6049 120960 385125.789212 401310.484358
6050 120980 385220.322488 401406.069959
6051 121000 385314.857869 401501.657583
6052 121020 385409.395355 401597.247230
6053 121040 385503.934945 401692.838898
6054 121060 385598.476639 401788.432588
6055 121080 385693.020437 401884.028299
6056 121100 385787.566337 401979.626031
6057 121120 385882.114340 402075.225784
6058 121140 385976.664446 402170.827556
6059 121160 386071.216653 402266.431348
6060 121180 386165.770961 402362.037160
6061 121200 386260.327371 402457.644991
6062 121220 386354.885881 402553.254840
6063 121240 386449.446492 402648.866707
6064 121260 386544.009202 402744.480592
6065 121280 386638.574012 402840.096494
6066 121300 386733.140920 402935.714413
6067 121320 386827.709927 403031.334349
6068 121340 386922.281033 403126.956302
6069 121360 387016.854236 403222.580270
6070 121380 387111.429537 403318.206253
6071 121400 387206.006934 403413.834252
6072 121420 387300.586429 403509.464265
6073 121440 387395.168019 403605.096293
6074 121460 387489.751705 403700.730335
6075 121480 387584.337487 403796.366390
6076 121500 387678.925363 403892.004458
6077 121520 387773.515335 403987.644539
6078 121540 387868.107400 404083.286633
6079 121560 387962.701559 404178.930739
6080 121580 388057.297812 404274.576856
6081 121600 388151.896157 404370.224985
6082 121620 388246.496595 404465.875124
6083 121640 388341.099125 404561.527274
6084 121660 388435.703747 404657.181435
6085 121680 388530.310461 404752.837605
6086 121700 388624.919265 404848.495784
6087 121720 388719.530160 404944.155972
6088 121740 388814.143145 405039.818169
6089 121760 388908.758220 405135.482374
6090 121780 389003.375384 405231.148588
6091 121800 389097.994638 405326.816808
6092 121820 389192.615979 405422.487036
6093 121840 389287.239409 405518.159270
6094 121860 389381.864927 405613.833511
6095 121880 389476.492532 405709.509758
6096 121900 389571.122224 405805.188010
6097 121920 389665.754003 405900.868268
6098 121940 389760.387868 405996.550530
6099 121960 389855.023819 406092.234797
6100 121980 389949.661855 406187.921068
6101 122000 390044.301976 406283.609342
6102 122020 390138.944182 406379.299620
6103 122040 390233.588472 406474.991901
6104 122060 390328.234845 406570.686185
6105 122080 390422.883302 406666.382470
6106 122100 390517.533842 406762.080758
6107 122120 390612.186465 406857.781047
6108 122140 390706.841170 406953.483337
6109 122160 390801.497957 407049.187627
6110 122180 390896.156825 407144.893918
6111 122200 390990.817774 407240.602209
6112 122220 391085.480804 407336.312500
6113 122240 391180.145914 407432.024790
6114 122260 391274.813104 407527.739078
6115 122280 391369.482373 407623.455365
6116 122300 391464.153722 407719.173650
6117 122320 391558.827149 407814.893932
6118 122340 391653.502654 407910.616212
6119 122360 391748.180237 408006.340489
6120 122380 391842.859898 408102.066763
6121 122400 391937.541635 408197.795032
6122 122420 392032.225450 408293.525298
6123 122440 392126.911340 408389.257558
6124 122460 392221.599307 408484.991814
6125 122480 392316.289349 408580.728065
6126 122500 392410.981466 408676.466310
6127 122520 392505.675658 408772.206549
6128 122540 392600.371924 408867.948781
6129 122560 392695.070264 408963.693006
6130 122580 392789.770677 409059.439225
6131 122600 392884.473164 409155.187436
6132 122620 392979.177723 409250.937638
6133 122640 393073.884355 409346.689833
6134 122660 393168.593059 409442.444019
6135 122680 393263.303834 409538.200196
6136 122700 393358.016680 409633.958363
6137 122720 393452.731598 409729.718521
6138 122740 393547.448585 409825.480668
6139 122760 393642.167643 409921.244805
6140 122780 393736.888770 410017.010931
6141 122800 393831.611966 410112.779045
6142 122820 393926.337231 410208.549148
6143 122840 394021.064565 410304.321239
6144 122860 394115.793966 410400.095318
6145 122880 394210.525436 410495.871383
6146 122900 394305.258972 410591.649436
6147 122920 394399.994575 410687.429475
6148 122940 394494.732245 410783.211500
6149 122960 394589.471981 410878.995511
6150 122980 394684.213783 410974.781507
6151 123000 394778.957649 411070.569489
6152 123020 394873.703581 411166.359455
6153 123040 394968.451577 411262.151405
6154 123060 395063.201638 411357.945339
6155 123080 395157.953762 411453.741256
6156 123100 395252.707949 411549.539157
6157 123120 395347.464200 411645.339040
6158 123140 395442.222513 411741.140906
6159 123160 395536.982888 411836.944754
6160 123180 395631.745325 411932.750584
6161 123200 395726.509823 412028.558395
6162 123220 395821.276382 412124.368186
6163 123240 395916.045002 412220.179959
6164 123260 396010.815682 412315.993711
6165 123280 396105.588422 412411.809443
6166 123300 396200.363222 412507.627155
6167 123320 396295.140080 412603.446846
6168 123340 396389.918997 412699.268516
6169 123360 396484.699973 412795.092163
6170 123380 396579.483006 412890.917789
6171 123400 396674.268097 412986.745393
6172 123420 396769.055245 413082.574973
6173 123440 396863.844450 413178.406531
6174 123460 396958.635711 413274.240065
6175 123480 397053.429028 413370.075575
6176 123500 397148.224400 413465.913061
6177 123520 397243.021828 413561.752523
6178 123540 397337.821311 413657.593959
6179 123560 397432.622848 413753.437370
6180 123580 397527.426439 413849.282755
6181 123600 397622.232083 413945.130115
6182 123620 397717.039781 414040.979447
6183 123640 397811.849532 414136.830754
6184 123660 397906.661335 414232.684032
6185 123680 398001.475191 414328.539284
6186 123700 398096.291098 414424.396507
6187 123720 398191.109056 414520.255702
6188 123740 398285.929065 414616.116869
6189 123760 398380.751125 414711.980006
6190 123780 398475.575235 414807.845115
6191 123800 398570.401395 414903.712193
6192 123820 398665.229604 414999.581241
6193 123840 398760.059863 415095.452259
6194 123860 398854.892170 415191.325246
6195 123880 398949.726525 415287.200202
6196 123900 399044.562928 415383.077127
6197 123920 399139.401378 415478.956019
6198 123940 399234.241876 415574.836879
6199 123960 399329.084420 415670.719706
6200 123980 399423.929011 415766.604501
6201 124000 399518.775647 415862.491262
6202 124020 399613.624329 415958.379989
6203 124040 399708.475057 416054.270682
6204 124060 399803.327829 416150.163341
6205 124080 399898.182645 416246.057965
6206 124100 399993.039506 416341.954554
6207 124120 400087.898410 416437.853107
6208 124140 400182.759358 416533.753624
6209 124160 400277.622348 416629.656105
6210 124180 400372.487381 416725.560549
6211 124200 400467.354456 416821.466957
6212 124220 400562.223573 416917.375327
6213 124240 400657.094731 417013.285659
6214 124260 400751.967930 417109.197953
6215 124280 400846.843169 417205.112208
6216 124300 400941.720449 417301.028425
6217 124320 401036.599769 417396.946603
6218 124340 401131.481128 417492.866741
6219 124360 401226.364526 417588.788839
6220 124380 401321.249963 417684.712897
6221 124400 401416.137438 417780.638914
6222 124420 401511.026951 417876.566890
6223 124440 401605.918502 417972.496825
6224 124460 401700.812090 418068.428718
6225 124480 401795.707714 418164.362569
6226 124500 401890.605375 418260.298378
6227 124520 401985.505073 418356.236144
6228 124540 402080.406805 418452.175866
6229 124560 402175.310573 418548.117546
6230 124580 402270.216376 418644.061181
6231 124600 402365.124213 418740.006772
6232 124620 402460.034085 418835.954318
6233 124640 402554.945990 418931.903820
6234 124660 402649.859929 419027.855276
6235 124680 402744.775900 419123.808686
6236 124700 402839.693904 419219.764051
6237 124720 402934.613941 419315.721369
6238 124740 403029.536009 419411.680640
6239 124760 403124.460109 419507.641864
6240 124780 403219.386240 419603.605041
6241 124800 403314.314401 419699.570170
6242 124820 403409.244593 419795.537250
6243 124840 403504.176815 419891.506282
6244 124860 403599.111067 419987.477265
6245 124880 403694.047347 420083.450199
6246 124900 403788.985657 420179.425083
6247 124920 403883.925995 420275.401918
6248 124940 403978.868361 420371.380702
6249 124960 404073.812755 420467.361435
6250 124980 404168.759176 420563.344117
6251 125000 404263.707624 420659.328748
6252 125020 404358.658098 420755.315327
6253 125040 404453.610599 420851.303853
6254 125060 404548.565126 420947.294328
6255 125080 404643.521678 421043.286749
6256 125100 404738.480255 421139.281117
6257 125120 404833.440857 421235.277432
6258 125140 404928.403483 421331.275693
6259 125160 405023.368133 421427.275899
6260 125180 405118.334807 421523.278051
6261 125200 405213.303504 421619.282148
6262 125220 405308.274223 421715.288190
6263 125240 405403.246965 421811.296175
6264 125260 405498.221730 421907.306105
6265 125280 405593.198515 422003.317978
6266 125300 405688.177323 422099.331795
6267 125320 405783.158151 422195.347554
6268 125340 405878.140999 422291.365256
6269 125360 405973.125868 422387.384901
6270 125380 406068.112757 422483.406486
6271 125400 406163.101665 422579.430014
6272 125420 406258.092592 422675.455482
6273 125440 406353.085538 422771.482892
6274 125460 406448.080503 422867.512241
6275 125480 406543.077485 422963.543531
6276 125500 406638.076485 423059.576760
6277 125520 406733.077502 423155.611929
6278 125540 406828.080536 423251.649036
6279 125560 406923.085586 423347.688083
6280 125580 407018.092652 423443.729067
6281 125600 407113.101734 423539.771990
6282 125620 407208.112832 423635.816850
6283 125640 407303.125944 423731.863647
6284 125660 407398.141071 423827.912381
6285 125680 407493.158213 423923.963051
6286 125700 407588.177368 424020.015658
6287 125720 407683.198536 424116.070200
6288 125740 407778.221718 424212.126678
6289 125760 407873.246912 424308.185091
6290 125780 407968.274119 424404.245439
6291 125800 408063.303338 424500.307721
6292 125820 408158.334569 424596.371937
6293 125840 408253.367810 424692.438087
6294 125860 408348.403063 424788.506170
6295 125880 408443.440326 424884.576186
6296 125900 408538.479599 424980.648134
6297 125920 408633.520882 425076.722015
6298 125940 408728.564175 425172.797828
6299 125960 408823.609476 425268.875573
6300 125980 408918.656786 425364.955248
6301 126000 409013.706105 425461.036855
6302 126020 409108.757431 425557.120392
6303 126040 409203.810765 425653.205859
6304 126060 409298.866106 425749.293256
6305 126080 409393.923454 425845.382582
6306 126100 409488.982808 425941.473838
6307 126120 409584.044169 426037.567022
6308 126140 409679.107535 426133.662134
6309 126160 409774.172906 426229.759175
6310 126180 409869.240283 426325.858144
6311 126200 409964.309664 426421.959039
6312 126220 410059.381049 426518.061862
6313 126240 410154.454438 426614.166611
6314 126260 410249.529831 426710.273287
6315 126280 410344.607227 426806.381889
6316 126300 410439.686625 426902.492416
6317 126320 410534.768026 426998.604869
6318 126340 410629.851429 427094.719246
6319 126360 410724.936834 427190.835548
6320 126380 410820.024240 427286.953774
6321 126400 410915.113646 427383.073924
6322 126420 411010.205054 427479.195998
6323 126440 411105.298461 427575.319994
6324 126460 411200.393869 427671.445914
6325 126480 411295.491276 427767.573756
6326 126500 411390.590682 427863.703520
6327 126520 411485.692086 427959.835206
6328 126540 411580.795489 428055.968813
6329 126560 411675.900891 428152.104342
6330 126580 411771.008289 428248.241791
6331 126600 411866.117685 428344.381160
6332 126620 411961.229078 428440.522450
6333 126640 412056.342467 428536.665659
6334 126660 412151.457853 428632.810787
6335 126680 412246.575234 428728.957835
6336 126700 412341.694611 428825.106801
6337 126720 412436.815983 428921.257685
6338 126740 412531.939349 429017.410488
6339 126760 412627.064710 429113.565208
6340 126780 412722.192065 429209.721845
6341 126800 412817.321413 429305.880399
6342 126820 412912.452755 429402.040870
6343 126840 413007.586090 429498.203256
6344 126860 413102.721417 429594.367559
6345 126880 413197.858736 429690.533777
6346 126900 413292.998047 429786.701911
6347 126920 413388.139349 429882.871959
6348 126940 413483.282643 429979.043922
6349 126960 413578.427927 430075.217799
6350 126980 413673.575202 430171.393590
6351 127000 413768.724466 430267.571294
6352 127020 413863.875720 430363.750911
6353 127040 413959.028963 430459.932441
6354 127060 414054.184196 430556.115883
6355 127080 414149.341416 430652.301238
6356 127100 414244.500625 430748.488504
6357 127120 414339.661822 430844.677682
6358 127140 414434.825006 430940.868770
6359 127160 414529.990177 431037.061770
6360 127180 414625.157335 431133.256679
6361 127200 414720.326479 431229.453499
6362 127220 414815.497610 431325.652228
6363 127240 414910.670726 431421.852867
6364 127260 415005.845827 431518.055415
6365 127280 415101.022913 431614.259871
6366 127300 415196.201983 431710.466236
6367 127320 415291.383038 431806.674508
6368 127340 415386.566076 431902.884688
6369 127360 415481.751098 431999.096776
6370 127380 415576.938103 432095.310770
6371 127400 415672.127091 432191.526671
6372 127420 415767.318061 432287.744478
6373 127440 415862.511014 432383.964191
6374 127460 415957.705947 432480.185809
6375 127480 416052.902863 432576.409333
6376 127500 416148.101759 432672.634761
6377 127520 416243.302635 432768.862094
6378 127540 416338.505492 432865.091332
6379 127560 416433.710329 432961.322473
6380 127580 416528.917145 433057.555517
6381 127600 416624.125940 433153.790465
6382 127620 416719.336714 433250.027316
6383 127640 416814.549467 433346.266069
6384 127660 416909.764197 433442.506724
6385 127680 417004.980905 433538.749281
6386 127700 417100.199591 433634.993739
6387 127720 417195.420253 433731.240098
6388 127740 417290.642893 433827.488359
6389 127760 417385.867508 433923.738519
6390 127780 417481.094099 434019.990580
6391 127800 417576.322666 434116.244541
6392 127820 417671.553208 434212.500400
6393 127840 417766.785725 434308.758159
6394 127860 417862.020216 434405.017817
6395 127880 417957.256682 434501.279373
6396 127900 418052.495121 434597.542827
6397 127920 418147.735534 434693.808179
6398 127940 418242.977919 434790.075428
6399 127960 418338.222277 434886.344574
6400 127980 418433.468608 434982.615617
6401 128000 418528.716911 435078.888556
6402 128020 418623.967185 435175.163391
6403 128040 418719.219430 435271.440122
6404 128060 418814.473647 435367.718748
6405 128080 418909.729834 435463.999270
6406 128100 419004.987991 435560.281685
6407 128120 419100.248118 435656.565996
6408 128140 419195.510214 435752.852200
6409 128160 419290.774279 435849.140297
6410 128180 419386.040314 435945.430288
6411 128200 419481.308316 436041.722172
6412 128220 419576.578287 436138.015949
6413 128240 419671.850226 436234.311618
6414 128260 419767.124131 436330.609179
6415 128280 419862.400004 436426.908631
6416 128300 419957.677843 436523.209975
6417 128320 420052.957649 436619.513210
6418 128340 420148.239421 436715.818335
6419 128360 420243.523158 436812.125351
6420 128380 420338.808860 436908.434256
6421 128400 420434.096527 437004.745051
6422 128420 420529.386159 437101.057735
6423 128440 420624.677755 437197.372309
6424 128460 420719.971314 437293.688770
6425 128480 420815.266837 437390.007120
6426 128500 420910.564323 437486.327358
6427 128520 421005.863772 437582.649484
6428 128540 421101.165184 437678.973496
6429 128560 421196.468557 437775.299396
6430 128580 421291.773892 437871.627182
6431 128600 421387.081188 437967.956855
6432 128620 421482.390445 438064.288413
6433 128640 421577.701663 438160.621857
6434 128660 421673.014841 438256.957186
6435 128680 421768.329979 438353.294400
6436 128700 421863.647077 438449.633498
6437 128720 421958.966134 438545.974481
6438 128740 422054.287149 438642.317348
6439 128760 422149.610123 438738.662098
6440 128780 422244.935055 438835.008731
6441 128800 422340.261945 438931.357247
6442 128820 422435.590793 439027.707646
6443 128840 422530.921598 439124.059927
6444 128860 422626.254359 439220.414090
6445 128880 422721.589077 439316.770134
6446 128900 422816.925750 439413.128060
6447 128920 422912.264380 439509.487866
6448 128940 423007.604964 439605.849553
6449 128960 423102.947504 439702.213120
6450 128980 423198.291998 439798.578567
6451 129000 423293.638447 439894.945894
6452 129020 423388.986850 439991.315100
6453 129040 423484.337206 440087.686184
6454 129060 423579.689515 440184.059148
6455 129080 423675.043777 440280.433989
6456 129100 423770.399992 440376.810708
6457 129120 423865.758159 440473.189305
6458 129140 423961.118278 440569.569779
6459 129160 424056.480348 440665.952130
6460 129180 424151.844369 440762.336357
6461 129200 424247.210342 440858.722461
6462 129220 424342.578264 440955.110441
6463 129240 424437.948137 441051.500296
6464 129260 424533.319959 441147.892026
6465 129280 424628.693731 441244.285631
6466 129300 424724.069452 441340.681111
6467 129320 424819.447121 441437.078465
6468 129340 424914.826739 441533.477693
6469 129360 425010.208305 441629.878794
6470 129380 425105.591818 441726.281769
6471 129400 425200.977279 441822.686616
6472 129420 425296.364687 441919.093336
6473 129440 425391.754041 442015.501929
6474 129460 425487.145342 442111.912393
6475 129480 425582.538588 442208.324729
6476 129500 425677.933780 442304.738936
6477 129520 425773.330917 442401.155013
6478 129540 425868.729999 442497.572962
6479 129560 425964.131026 442593.992781
6480 129580 426059.533997 442690.414469
6481 129600 426154.938911 442786.838028
6482 129620 426250.345769 442883.263455
6483 129640 426345.754570 442979.690752
6484 129660 426441.165314 443076.119917
6485 129680 426536.578000 443172.550950
6486 129700 426631.992629 443268.983851
6487 129720 426727.409199 443365.418620
6488 129740 426822.827710 443461.855256
6489 129760 426918.248163 443558.293760
6490 129780 427013.670556 443654.734129
6491 129800 427109.094890 443751.176365
6492 129820 427204.521163 443847.620467
6493 129840 427299.949377 443944.066435
6494 129860 427395.379529 444040.514268
6495 129880 427490.811621 444136.963966
6496 129900 427586.245651 444233.415529
6497 129920 427681.681619 444329.868956
6498 129940 427777.119526 444426.324247
6499 129960 427872.559369 444522.781401
6500 129980 427968.001151 444619.240419
6501 130000 428063.444869 444715.701300

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