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 11 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
5002 100020 287390.701328 302424.034884
5003 100040 287482.774333 302517.259570
5004 100060 287574.849970 302610.486778
5005 100080 287666.928239 302703.716509
5006 100100 287759.009138 302796.948760
5007 100120 287851.092668 302890.183532
5008 100140 287943.178827 302983.420824
5009 100160 288035.267615 303076.660635
5010 100180 288127.359031 303169.902966
5011 100200 288219.453075 303263.147814
5012 100220 288311.549746 303356.395180
5013 100240 288403.649044 303449.645064
5014 100260 288495.750967 303542.897464
5015 100280 288587.855516 303636.152379
5016 100300 288679.962689 303729.409810
5017 100320 288772.072486 303822.669756
5018 100340 288864.184907 303915.932216
5019 100360 288956.299950 304009.197190
5020 100380 289048.417615 304102.464676
5021 100400 289140.537902 304195.734675
5022 100420 289232.660809 304289.007186
5023 100440 289324.786337 304382.282208
5024 100460 289416.914484 304475.559740
5025 100480 289509.045251 304568.839783
5026 100500 289601.178635 304662.122335
5027 100520 289693.314638 304755.407395
5028 100540 289785.453258 304848.694965
5029 100560 289877.594494 304941.985041
5030 100580 289969.738346 305035.277625
5031 100600 290061.884813 305128.572716
5032 100620 290154.033895 305221.870312
5033 100640 290246.185592 305315.170414
5034 100660 290338.339901 305408.473021
5035 100680 290430.496823 305501.778131
5036 100700 290522.656358 305595.085746
5037 100720 290614.818504 305688.395863
5038 100740 290706.983261 305781.708483
5039 100760 290799.150629 305875.023605
5040 100780 290891.320606 305968.341228
5041 100800 290983.493193 306061.661351
5042 100820 291075.668387 306154.983975
5043 100840 291167.846190 306248.309098
5044 100860 291260.026600 306341.636721
5045 100880 291352.209617 306434.966841
5046 100900 291444.395240 306528.299460
5047 100920 291536.583468 306621.634575
5048 100940 291628.774302 306714.972188
5049 100960 291720.967739 306808.312296
5050 100980 291813.163780 306901.654900
5051 101000 291905.362424 306994.999999
5052 101020 291997.563670 307088.347592
5053 101040 292089.767518 307181.697678
5054 101060 292181.973968 307275.050258
5055 101080 292274.183018 307368.405331
5056 101100 292366.394668 307461.762895
5057 101120 292458.608917 307555.122951
5058 101140 292550.825765 307648.485498
5059 101160 292643.045211 307741.850535
5060 101180 292735.267254 307835.218062
5061 101200 292827.491895 307928.588078
5062 101220 292919.719132 308021.960582
5063 101240 293011.948964 308115.335574
5064 101260 293104.181392 308208.713054
5065 101280 293196.416414 308302.093021
5066 101300 293288.654029 308395.475473
5067 101320 293380.894239 308488.860412
5068 101340 293473.137040 308582.247835
5069 101360 293565.382434 308675.637744
5070 101380 293657.630419 308769.030136
5071 101400 293749.880996 308862.425011
5072 101420 293842.134162 308955.822369
5073 101440 293934.389918 309049.222209
5074 101460 294026.648263 309142.624531
5075 101480 294118.909196 309236.029335
5076 101500 294211.172717 309329.436618
5077 101520 294303.438826 309422.846382
5078 101540 294395.707521 309516.258625
5079 101560 294487.978802 309609.673346
5080 101580 294580.252668 309703.090546
5081 101600 294672.529119 309796.510224
5082 101620 294764.808154 309889.932378
5083 101640 294857.089773 309983.357009
5084 101660 294949.373975 310076.784116
5085 101680 295041.660759 310170.213699
5086 101700 295133.950125 310263.645756
5087 101720 295226.242073 310357.080287
5088 101740 295318.536600 310450.517292
5089 101760 295410.833708 310543.956770
5090 101780 295503.133395 310637.398721
5091 101800 295595.435661 310730.843143
5092 101820 295687.740505 310824.290037
5093 101840 295780.047927 310917.739402
5094 101860 295872.357925 311011.191237
5095 101880 295964.670500 311104.645542
5096 101900 296056.985651 311198.102315
5097 101920 296149.303377 311291.561558
5098 101940 296241.623677 311385.023268
5099 101960 296333.946552 311478.487445
5100 101980 296426.271999 311571.954090
5101 102000 296518.600020 311665.423201
5102 102020 296610.930613 311758.894777
5103 102040 296703.263777 311852.368819
5104 102060 296795.599512 311945.845325
5105 102080 296887.937818 312039.324295
5106 102100 296980.278694 312132.805728
5107 102120 297072.622138 312226.289625
5108 102140 297164.968152 312319.775984
5109 102160 297257.316733 312413.264804
5110 102180 297349.667882 312506.756086
5111 102200 297442.021597 312600.249828
5112 102220 297534.377879 312693.746030
5113 102240 297626.736726 312787.244692
5114 102260 297719.098139 312880.745813
5115 102280 297811.462115 312974.249392
5116 102300 297903.828656 313067.755429
5117 102320 297996.197760 313161.263923
5118 102340 298088.569426 313254.774874
5119 102360 298180.943655 313348.288281
5120 102380 298273.320445 313441.804143
5121 102400 298365.699796 313535.322461
5122 102420 298458.081707 313628.843233
5123 102440 298550.466178 313722.366458
5124 102460 298642.853208 313815.892138
5125 102480 298735.242797 313909.420269
5126 102500 298827.634943 314002.950853
5127 102520 298920.029647 314096.483889
5128 102540 299012.426907 314190.019376
5129 102560 299104.826724 314283.557313
5130 102580 299197.229096 314377.097700
5131 102600 299289.634023 314470.640537
5132 102620 299382.041505 314564.185822
5133 102640 299474.451540 314657.733556
5134 102660 299566.864129 314751.283737
5135 102680 299659.279270 314844.836366
5136 102700 299751.696963 314938.391441
5137 102720 299844.117208 315031.948962
5138 102740 299936.540003 315125.508929
5139 102760 300028.965349 315219.071341
5140 102780 300121.393244 315312.636197
5141 102800 300213.823688 315406.203497
5142 102820 300306.256681 315499.773240
5143 102840 300398.692222 315593.345425
5144 102860 300491.130310 315686.920053
5145 102880 300583.570945 315780.497123
5146 102900 300676.014126 315874.076634
5147 102920 300768.459853 315967.658585
5148 102940 300860.908124 316061.242976
5149 102960 300953.358940 316154.829806
5150 102980 301045.812300 316248.419075
5151 103000 301138.268203 316342.010783
5152 103020 301230.726648 316435.604928
5153 103040 301323.187636 316529.201510
5154 103060 301415.651165 316622.800529
5155 103080 301508.117235 316716.401984
5156 103100 301600.585845 316810.005875
5157 103120 301693.056995 316903.612200
5158 103140 301785.530684 316997.220960
5159 103160 301878.006912 317090.832154
5160 103180 301970.485678 317184.445781
5161 103200 302062.966981 317278.061840
5162 103220 302155.450821 317371.680332
5163 103240 302247.937197 317465.301256
5164 103260 302340.426108 317558.924611
5165 103280 302432.917555 317652.550396
5166 103300 302525.411536 317746.178611
5167 103320 302617.908052 317839.809256
5168 103340 302710.407100 317933.442329
5169 103360 302802.908681 318027.077831
5170 103380 302895.412795 318120.715761
5171 103400 302987.919440 318214.356118
5172 103420 303080.428616 318307.998901
5173 103440 303172.940322 318401.644111
5174 103460 303265.454558 318495.291746
5175 103480 303357.971324 318588.941807
5176 103500 303450.490618 318682.594292
5177 103520 303543.012440 318776.249200
5178 103540 303635.536790 318869.906533
5179 103560 303728.063667 318963.566288
5180 103580 303820.593070 319057.228465
5181 103600 303913.124999 319150.893064
5182 103620 304005.659453 319244.560085
5183 103640 304098.196432 319338.229526
5184 103660 304190.735934 319431.901387
5185 103680 304283.277961 319525.575668
5186 103700 304375.822510 319619.252368
5187 103720 304468.369582 319712.931486
5188 103740 304560.919175 319806.613022
5189 103760 304653.471290 319900.296976
5190 103780 304746.025925 319993.983346
5191 103800 304838.583080 320087.672133
5192 103820 304931.142755 320181.363336
5193 103840 305023.704948 320275.056954
5194 103860 305116.269660 320368.752986
5195 103880 305208.836890 320462.451433
5196 103900 305301.406637 320556.152293
5197 103920 305393.978900 320649.855566
5198 103940 305486.553680 320743.561252
5199 103960 305579.130975 320837.269350
5200 103980 305671.710785 320930.979859
5201 104000 305764.293109 321024.692779
5202 104020 305856.877947 321118.408110
5203 104040 305949.465298 321212.125850
5204 104060 306042.055161 321305.845999
5205 104080 306134.647537 321399.568557
5206 104100 306227.242424 321493.293524
5207 104120 306319.839822 321587.020898
5208 104140 306412.439730 321680.750679
5209 104160 306505.042148 321774.482867
5210 104180 306597.647076 321868.217460
5211 104200 306690.254511 321961.954459
5212 104220 306782.864455 322055.693863
5213 104240 306875.476907 322149.435672
5214 104260 306968.091865 322243.179884
5215 104280 307060.709330 322336.926500
5216 104300 307153.329300 322430.675518
5217 104320 307245.951775 322524.426939
5218 104340 307338.576756 322618.180762
5219 104360 307431.204240 322711.936985
5220 104380 307523.834228 322805.695609
5221 104400 307616.466718 322899.456634
5222 104420 307709.101711 322993.220058
5223 104440 307801.739206 323086.985880
5224 104460 307894.379202 323180.754102
5225 104480 307987.021699 323274.524721
5226 104500 308079.666696 323368.297738
5227 104520 308172.314192 323462.073152
5228 104540 308264.964187 323555.850962
5229 104560 308357.616681 323649.631167
5230 104580 308450.271673 323743.413768
5231 104600 308542.929161 323837.198764
5232 104620 308635.589147 323930.986154
5233 104640 308728.251629 324024.775938
5234 104660 308820.916606 324118.568115
5235 104680 308913.584078 324212.362684
5236 104700 309006.254045 324306.159645
5237 104720 309098.926505 324399.958998
5238 104740 309191.601459 324493.760742
5239 104760 309284.278906 324587.564877
5240 104780 309376.958845 324681.371401
5241 104800 309469.641275 324775.180315
5242 104820 309562.326197 324868.991618
5243 104840 309655.013609 324962.805309
5244 104860 309747.703511 325056.621388
5245 104880 309840.395903 325150.439854
5246 104900 309933.090783 325244.260706
5247 104920 310025.788152 325338.083946
5248 104940 310118.488008 325431.909570
5249 104960 310211.190351 325525.737580
5250 104980 310303.895181 325619.567975
5251 105000 310396.602497 325713.400753
5252 105020 310489.312299 325807.235915
5253 105040 310582.024585 325901.073461
5254 105060 310674.739356 325994.913388
5255 105080 310767.456611 326088.755698
5256 105100 310860.176349 326182.600389
5257 105120 310952.898569 326276.447461
5258 105140 311045.623272 326370.296913
5259 105160 311138.350456 326464.148746
5260 105180 311231.080121 326558.002957
5261 105200 311323.812267 326651.859548
5262 105220 311416.546893 326745.718516
5263 105240 311509.283998 326839.579863
5264 105260 311602.023581 326933.443586
5265 105280 311694.765643 327027.309687
5266 105300 311787.510183 327121.178163
5267 105320 311880.257200 327215.049015
5268 105340 311973.006693 327308.922242
5269 105360 312065.758662 327402.797844
5270 105380 312158.513107 327496.675820
5271 105400 312251.270027 327590.556170
5272 105420 312344.029421 327684.438892
5273 105440 312436.791288 327778.323987
5274 105460 312529.555629 327872.211454
5275 105480 312622.322443 327966.101292
5276 105500 312715.091729 328059.993501
5277 105520 312807.863486 328153.888081
5278 105540 312900.637715 328247.785030
5279 105560 312993.414414 328341.684349
5280 105580 313086.193582 328435.586037
5281 105600 313178.975220 328529.490093
5282 105620 313271.759327 328623.396516
5283 105640 313364.545903 328717.305307
5284 105660 313457.334945 328811.216465
5285 105680 313550.126456 328905.129989
5286 105700 313642.920432 328999.045878
5287 105720 313735.716875 329092.964133
5288 105740 313828.515783 329186.884752
5289 105760 313921.317156 329280.807736
5290 105780 314014.120994 329374.733083
5291 105800 314106.927295 329468.660793
5292 105820 314199.736060 329562.590865
5293 105840 314292.547287 329656.523300
5294 105860 314385.360977 329750.458096
5295 105880 314478.177128 329844.395253
5296 105900 314570.995741 329938.334771
5297 105920 314663.816814 330032.276649
5298 105940 314756.640347 330126.220886
5299 105960 314849.466339 330220.167481
5300 105980 314942.294791 330314.116436
5301 106000 315035.125700 330408.067748
5302 106020 315127.959068 330502.021417
5303 106040 315220.794893 330595.977444
5304 106060 315313.633174 330689.935826
5305 106080 315406.473912 330783.896565
5306 106100 315499.317105 330877.859659
5307 106120 315592.162754 330971.825107
5308 106140 315685.010857 331065.792910
5309 106160 315777.861414 331159.763066
5310 106180 315870.714424 331253.735576
5311 106200 315963.569888 331347.710439
5312 106220 316056.427803 331441.687653
5313 106240 316149.288171 331535.667219
5314 106260 316242.150989 331629.649137
5315 106280 316335.016259 331723.633405
5316 106300 316427.883979 331817.620023
5317 106320 316520.754148 331911.608991
5318 106340 316613.626766 332005.600307
5319 106360 316706.501833 332099.593973
5320 106380 316799.379348 332193.589986
5321 106400 316892.259311 332287.588347
5322 106420 316985.141720 332381.589055
5323 106440 317078.026576 332475.592110
5324 106460 317170.913877 332569.597510
5325 106480 317263.803624 332663.605256
5326 106500 317356.695816 332757.615347
5327 106520 317449.590452 332851.627783
5328 106540 317542.487531 332945.642562
5329 106560 317635.387054 333039.659685
5330 106580 317728.289019 333133.679150
5331 106600 317821.193426 333227.700958
5332 106620 317914.100275 333321.725109
5333 106640 318007.009565 333415.751600
5334 106660 318099.921295 333509.780432
5335 106680 318192.835465 333603.811605
5336 106700 318285.752074 333697.845117
5337 106720 318378.671122 333791.880969
5338 106740 318471.592609 333885.919160
5339 106760 318564.516533 333979.959689
5340 106780 318657.442894 334074.002556
5341 106800 318750.371692 334168.047760
5342 106820 318843.302927 334262.095300
5343 106840 318936.236596 334356.145178
5344 106860 319029.172701 334450.197390
5345 106880 319122.111240 334544.251939
5346 106900 319215.052214 334638.308822
5347 106920 319307.995620 334732.368039
5348 106940 319400.941460 334826.429590
5349 106960 319493.889732 334920.493474
5350 106980 319586.840436 335014.559690
5351 107000 319679.793571 335108.628239
5352 107020 319772.749137 335202.699120
5353 107040 319865.707133 335296.772332
5354 107060 319958.667558 335390.847875
5355 107080 320051.630413 335484.925748
5356 107100 320144.595697 335579.005950
5357 107120 320237.563408 335673.088482
5358 107140 320330.533547 335767.173342
5359 107160 320423.506113 335861.260530
5360 107180 320516.481106 335955.350046
5361 107200 320609.458524 336049.441890
5362 107220 320702.438368 336143.536059
5363 107240 320795.420637 336237.632555
5364 107260 320888.405330 336331.731377
5365 107280 320981.392447 336425.832524
5366 107300 321074.381987 336519.935995
5367 107320 321167.373950 336614.041791
5368 107340 321260.368335 336708.149910
5369 107360 321353.365142 336802.260352
5370 107380 321446.364370 336896.373116
5371 107400 321539.366019 336990.488203
5372 107420 321632.370087 337084.605612
5373 107440 321725.376575 337178.725341
5374 107460 321818.385483 337272.847391
5375 107480 321911.396808 337366.971762
5376 107500 322004.410552 337461.098451
5377 107520 322097.426713 337555.227460
5378 107540 322190.445291 337649.358788
5379 107560 322283.466286 337743.492433
5380 107580 322376.489696 337837.628396
5381 107600 322469.515521 337931.766676
5382 107620 322562.543762 338025.907273
5383 107640 322655.574416 338120.050186
5384 107660 322748.607485 338214.195414
5385 107680 322841.642966 338308.342957
5386 107700 322934.680860 338402.492815
5387 107720 323027.721167 338496.644987
5388 107740 323120.763885 338590.799473
5389 107760 323213.809014 338684.956272
5390 107780 323306.856553 338779.115383
5391 107800 323399.906503 338873.276806
5392 107820 323492.958862 338967.440541
5393 107840 323586.013630 339061.606587
5394 107860 323679.070807 339155.774943
5395 107880 323772.130391 339249.945610
5396 107900 323865.192383 339344.118586
5397 107920 323958.256782 339438.293871
5398 107940 324051.323587 339532.471465
5399 107960 324144.392798 339626.651367
5400 107980 324237.464414 339720.833576
5401 108000 324330.538436 339815.018093
5402 108020 324423.614861 339909.204916
5403 108040 324516.693690 340003.394045
5404 108060 324609.774922 340097.585480
5405 108080 324702.858557 340191.779220
5406 108100 324795.944594 340285.975264
5407 108120 324889.033033 340380.173613
5408 108140 324982.123873 340474.374265
5409 108160 325075.217113 340568.577220
5410 108180 325168.312754 340662.782478
5411 108200 325261.410794 340756.990038
5412 108220 325354.511233 340851.199900
5413 108240 325447.614070 340945.412062
5414 108260 325540.719306 341039.626526
5415 108280 325633.826939 341133.843289
5416 108300 325726.936969 341228.062353
5417 108320 325820.049395 341322.283715
5418 108340 325913.164217 341416.507376
5419 108360 326006.281435 341510.733335
5420 108380 326099.401047 341604.961592
5421 108400 326192.523054 341699.192146
5422 108420 326285.647455 341793.424997
5423 108440 326378.774249 341887.660144
5424 108460 326471.903435 341981.897586
5425 108480 326565.035014 342076.137324
5426 108500 326658.168985 342170.379357
5427 108520 326751.305347 342264.623683
5428 108540 326844.444099 342358.870304
5429 108560 326937.585242 342453.119218
5430 108580 327030.728774 342547.370424
5431 108600 327123.874696 342641.623923
5432 108620 327217.023006 342735.879713
5433 108640 327310.173704 342830.137795
5434 108660 327403.326790 342924.398168
5435 108680 327496.482263 343018.660831
5436 108700 327589.640123 343112.925783
5437 108720 327682.800368 343207.193025
5438 108740 327775.962999 343301.462556
5439 108760 327869.128015 343395.734375
5440 108780 327962.295416 343490.008482
5441 108800 328055.465200 343584.284877
5442 108820 328148.637368 343678.563558
5443 108840 328241.811919 343772.844525
5444 108860 328334.988852 343867.127779
5445 108880 328428.168168 343961.413318
5446 108900 328521.349864 344055.701142
5447 108920 328614.533942 344149.991250
5448 108940 328707.720400 344244.283642
5449 108960 328800.909237 344338.578318
5450 108980 328894.100454 344432.875276
5451 109000 328987.294050 344527.174518
5452 109020 329080.490024 344621.476041
5453 109040 329173.688376 344715.779845
5454 109060 329266.889106 344810.085931
5455 109080 329360.092212 344904.394297
5456 109100 329453.297694 344998.704943
5457 109120 329546.505552 345093.017869
5458 109140 329639.715785 345187.333074
5459 109160 329732.928393 345281.650557
5460 109180 329826.143376 345375.970319
5461 109200 329919.360732 345470.292358
5462 109220 330012.580461 345564.616674
5463 109240 330105.802563 345658.943267
5464 109260 330199.027036 345753.272136
5465 109280 330292.253882 345847.603281
5466 109300 330385.483099 345941.936701
5467 109320 330478.714686 346036.272396
5468 109340 330571.948644 346130.610365
5469 109360 330665.184971 346224.950607
5470 109380 330758.423667 346319.293123
5471 109400 330851.664732 346413.637912
5472 109420 330944.908165 346507.984973
5473 109440 331038.153965 346602.334306
5474 109460 331131.402133 346696.685910
5475 109480 331224.652667 346791.039785
5476 109500 331317.905567 346885.395930
5477 109520 331411.160833 346979.754346
5478 109540 331504.418464 347074.115030
5479 109560 331597.678459 347168.477984
5480 109580 331690.940818 347262.843206
5481 109600 331784.205541 347357.210696
5482 109620 331877.472627 347451.580453
5483 109640 331970.742075 347545.952478
5484 109660 332064.013885 347640.326769
5485 109680 332157.288057 347734.703326
5486 109700 332250.564590 347829.082148
5487 109720 332343.843483 347923.463236
5488 109740 332437.124736 348017.846588
5489 109760 332530.408349 348112.232205
5490 109780 332623.694320 348206.620085
5491 109800 332716.982651 348301.010228
5492 109820 332810.273339 348395.402634
5493 109840 332903.566384 348489.797302
5494 109860 332996.861786 348584.194232
5495 109880 333090.159545 348678.593423
5496 109900 333183.459660 348772.994875
5497 109920 333276.762130 348867.398587
5498 109940 333370.066955 348961.804559
5499 109960 333463.374135 349056.212790
5500 109980 333556.683668 349150.623280
5501 110000 333649.995555 349245.036029

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