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 9 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
4002 80020 196753.349171 210573.755926
4003 80040 196842.419845 210664.107949
4004 80060 196931.493960 210754.463259
4005 80080 197020.571515 210844.821856
4006 80100 197109.652509 210935.183738
4007 80120 197198.736942 211025.548904
4008 80140 197287.824811 211115.917354
4009 80160 197376.916117 211206.289087
4010 80180 197466.010858 211296.664101
4011 80200 197555.109032 211387.042396
4012 80220 197644.210640 211477.423970
4013 80240 197733.315679 211567.808823
4014 80260 197822.424149 211658.196954
4015 80280 197911.536049 211748.588361
4016 80300 198000.651378 211838.983044
4017 80320 198089.770134 211929.381001
4018 80340 198178.892317 212019.782232
4019 80360 198268.017926 212110.186736
4020 80380 198357.146959 212200.594511
4021 80400 198446.279416 212291.005558
4022 80420 198535.415295 212381.419874
4023 80440 198624.554596 212471.837459
4024 80460 198713.697317 212562.258311
4025 80480 198802.843458 212652.682431
4026 80500 198891.993017 212743.109816
4027 80520 198981.145993 212833.540466
4028 80540 199070.302386 212923.974380
4029 80560 199159.462194 213014.411557
4030 80580 199248.625416 213104.851996
4031 80600 199337.792051 213195.295696
4032 80620 199426.962099 213285.742656
4033 80640 199516.135558 213376.192875
4034 80660 199605.312427 213466.646352
4035 80680 199694.492705 213557.103086
4036 80700 199783.676391 213647.563076
4037 80720 199872.863484 213738.026322
4038 80740 199962.053983 213828.492821
4039 80760 200051.247887 213918.962574
4040 80780 200140.445195 214009.435579
4041 80800 200229.645906 214099.911835
4042 80820 200318.850019 214190.391342
4043 80840 200408.057533 214280.874098
4044 80860 200497.268447 214371.360102
4045 80880 200586.482760 214461.849354
4046 80900 200675.700470 214552.341852
4047 80920 200764.921577 214642.837596
4048 80940 200854.146081 214733.336584
4049 80960 200943.373978 214823.838816
4050 80980 201032.605270 214914.344291
4051 81000 201121.839954 215004.853007
4052 81020 201211.078030 215095.364963
4053 81040 201300.319497 215185.880160
4054 81060 201389.564353 215276.398595
4055 81080 201478.812598 215366.920268
4056 81100 201568.064231 215457.445178
4057 81120 201657.319250 215547.973323
4058 81140 201746.577655 215638.504704
4059 81160 201835.839445 215729.039319
4060 81180 201925.104618 215819.577166
4061 81200 202014.373174 215910.118246
4062 81220 202103.645111 216000.662556
4063 81240 202192.920429 216091.210097
4064 81260 202282.199126 216181.760867
4065 81280 202371.481202 216272.314866
4066 81300 202460.766655 216362.872091
4067 81320 202550.055485 216453.432543
4068 81340 202639.347691 216543.996220
4069 81360 202728.643271 216634.563122
4070 81380 202817.942224 216725.133247
4071 81400 202907.244550 216815.706594
4072 81420 202996.550247 216906.283163
4073 81440 203085.859315 216996.862953
4074 81460 203175.171752 217087.445962
4075 81480 203264.487558 217178.032191
4076 81500 203353.806731 217268.621636
4077 81520 203443.129271 217359.214299
4078 81540 203532.455176 217449.810178
4079 81560 203621.784445 217540.409271
4080 81580 203711.117078 217631.011578
4081 81600 203800.453074 217721.617099
4082 81620 203889.792430 217812.225831
4083 81640 203979.135148 217902.837775
4084 81660 204068.481224 217993.452929
4085 81680 204157.830660 218084.071292
4086 81700 204247.183452 218174.692864
4087 81720 204336.539601 218265.317642
4088 81740 204425.899105 218355.945628
4089 81760 204515.261964 218446.576818
4090 81780 204604.628177 218537.211214
4091 81800 204693.997741 218627.848812
4092 81820 204783.370658 218718.489614
4093 81840 204872.746924 218809.133617
4094 81860 204962.126541 218899.780821
4095 81880 205051.509505 218990.431225
4096 81900 205140.895817 219081.084828
4097 81920 205230.285476 219171.741628
4098 81940 205319.678480 219262.401626
4099 81960 205409.074829 219353.064820
4100 81980 205498.474521 219443.731209
4101 82000 205587.877556 219534.400792
4102 82020 205677.283932 219625.073569
4103 82040 205766.693649 219715.749538
4104 82060 205856.106705 219806.428698
4105 82080 205945.523100 219897.111049
4106 82100 206034.942832 219987.796589
4107 82120 206124.365901 220078.485318
4108 82140 206213.792306 220169.177235
4109 82160 206303.222045 220259.872338
4110 82180 206392.655118 220350.570628
4111 82200 206482.091524 220441.272102
4112 82220 206571.531261 220531.976760
4113 82240 206660.974329 220622.684601
4114 82260 206750.420727 220713.395624
4115 82280 206839.870453 220804.109829
4116 82300 206929.323507 220894.827214
4117 82320 207018.779888 220985.547778
4118 82340 207108.239595 221076.271520
4119 82360 207197.702627 221166.998440
4120 82380 207287.168982 221257.728537
4121 82400 207376.638660 221348.461809
4122 82420 207466.111660 221439.198256
4123 82440 207555.587981 221529.937877
4124 82460 207645.067622 221620.680670
4125 82480 207734.550582 221711.426636
4126 82500 207824.036859 221802.175772
4127 82520 207913.526454 221892.928079
4128 82540 208003.019365 221983.683555
4129 82560 208092.515590 222074.442199
4130 82580 208182.015130 222165.204010
4131 82600 208271.517983 222255.968988
4132 82620 208361.024148 222346.737131
4133 82640 208450.533624 222437.508439
4134 82660 208540.046411 222528.282911
4135 82680 208629.562506 222619.060545
4136 82700 208719.081910 222709.841341
4137 82720 208808.604621 222800.625298
4138 82740 208898.130638 222891.412415
4139 82760 208987.659961 222982.202691
4140 82780 209077.192588 223072.996126
4141 82800 209166.728518 223163.792717
4142 82820 209256.267751 223254.592465
4143 82840 209345.810285 223345.395368
4144 82860 209435.356119 223436.201426
4145 82880 209524.905253 223527.010637
4146 82900 209614.457686 223617.823002
4147 82920 209704.013416 223708.638517
4148 82940 209793.572443 223799.457184
4149 82960 209883.134766 223890.279001
4150 82980 209972.700383 223981.103967
4151 83000 210062.269294 224071.932080
4152 83020 210151.841498 224162.763342
4153 83040 210241.416994 224253.597749
4154 83060 210330.995780 224344.435302
4155 83080 210420.577856 224435.275999
4156 83100 210510.163221 224526.119840
4157 83120 210599.751875 224616.966824
4158 83140 210689.343815 224707.816949
4159 83160 210778.939041 224798.670215
4160 83180 210868.537552 224889.526622
4161 83200 210958.139348 224980.386167
4162 83220 211047.744427 225071.248851
4163 83240 211137.352788 225162.114671
4164 83260 211226.964430 225252.983629
4165 83280 211316.579352 225343.855721
4166 83300 211406.197554 225434.730948
4167 83320 211495.819034 225525.609309
4168 83340 211585.443792 225616.490803
4169 83360 211675.071826 225707.375428
4170 83380 211764.703136 225798.263185
4171 83400 211854.337720 225889.154071
4172 83420 211943.975578 225980.048087
4173 83440 212033.616709 226070.945231
4174 83460 212123.261111 226161.845502
4175 83480 212212.908784 226252.748900
4176 83500 212302.559727 226343.655423
4177 83520 212392.213939 226434.565071
4178 83540 212481.871419 226525.477843
4179 83560 212571.532166 226616.393737
4180 83580 212661.196179 226707.312754
4181 83600 212750.863457 226798.234892
4182 83620 212840.533999 226889.160150
4183 83640 212930.207804 226980.088527
4184 83660 213019.884872 227071.020023
4185 83680 213109.565201 227161.954636
4186 83700 213199.248790 227252.892366
4187 83720 213288.935639 227343.833211
4188 83740 213378.625746 227434.777172
4189 83760 213468.319111 227525.724246
4190 83780 213558.015733 227616.674434
4191 83800 213647.715610 227707.627733
4192 83820 213737.418741 227798.584144
4193 83840 213827.125127 227889.543665
4194 83860 213916.834766 227980.506296
4195 83880 214006.547656 228071.472036
4196 83900 214096.263797 228162.440883
4197 83920 214185.983189 228253.412837
4198 83940 214275.705829 228344.387897
4199 83960 214365.431718 228435.366062
4200 83980 214455.160854 228526.347332
4201 84000 214544.893236 228617.331704
4202 84020 214634.628863 228708.319179
4203 84040 214724.367735 228799.309756
4204 84060 214814.109850 228890.303433
4205 84080 214903.855208 228981.300210
4206 84100 214993.603808 229072.300086
4207 84120 215083.355648 229163.303060
4208 84140 215173.110728 229254.309132
4209 84160 215262.869047 229345.318299
4210 84180 215352.630603 229436.330562
4211 84200 215442.395397 229527.345919
4212 84220 215532.163427 229618.364370
4213 84240 215621.934691 229709.385914
4214 84260 215711.709190 229800.410549
4215 84280 215801.486922 229891.438276
4216 84300 215891.267887 229982.469092
4217 84320 215981.052083 230073.502998
4218 84340 216070.839509 230164.539993
4219 84360 216160.630165 230255.580074
4220 84380 216250.424049 230346.623243
4221 84400 216340.221161 230437.669497
4222 84420 216430.021500 230528.718836
4223 84440 216519.825064 230619.771258
4224 84460 216609.631854 230710.826764
4225 84480 216699.441867 230801.885353
4226 84500 216789.255104 230892.947022
4227 84520 216879.071563 230984.011772
4228 84540 216968.891242 231075.079602
4229 84560 217058.714143 231166.150511
4230 84580 217148.540262 231257.224497
4231 84600 217238.369600 231348.301560
4232 84620 217328.202155 231439.381700
4233 84640 217418.037927 231530.464915
4234 84660 217507.876915 231621.551204
4235 84680 217597.719118 231712.640567
4236 84700 217687.564534 231803.733002
4237 84720 217777.413163 231894.828509
4238 84740 217867.265005 231985.927087
4239 84760 217957.120057 232077.028736
4240 84780 218046.978320 232168.133453
4241 84800 218136.839791 232259.241239
4242 84820 218226.704472 232350.352092
4243 84840 218316.572359 232441.466012
4244 84860 218406.443453 232532.582998
4245 84880 218496.317753 232623.703048
4246 84900 218586.195257 232714.826163
4247 84920 218676.075966 232805.952340
4248 84940 218765.959877 232897.081580
4249 84960 218855.846990 232988.213882
4250 84980 218945.737304 233079.349244
4251 85000 219035.630818 233170.487665
4252 85020 219125.527531 233261.629146
4253 85040 219215.427443 233352.773684
4254 85060 219305.330552 233443.921280
4255 85080 219395.236858 233535.071932
4256 85100 219485.146359 233626.225639
4257 85120 219575.059055 233717.382401
4258 85140 219664.974945 233808.542217
4259 85160 219754.894027 233899.705085
4260 85180 219844.816302 233990.871006
4261 85200 219934.741767 234082.039978
4262 85220 220024.670423 234173.211999
4263 85240 220114.602268 234264.387071
4264 85260 220204.537301 234355.565191
4265 85280 220294.475522 234446.746358
4266 85300 220384.416929 234537.930573
4267 85320 220474.361522 234629.117833
4268 85340 220564.309300 234720.308139
4269 85360 220654.260261 234811.501489
4270 85380 220744.214406 234902.697882
4271 85400 220834.171732 234993.897318
4272 85420 220924.132239 235085.099796
4273 85440 221014.095927 235176.305315
4274 85460 221104.062794 235267.513873
4275 85480 221194.032840 235358.725471
4276 85500 221284.006063 235449.940108
4277 85520 221373.982462 235541.157781
4278 85540 221463.962038 235632.378492
4279 85560 221553.944788 235723.602238
4280 85580 221643.930712 235814.829019
4281 85600 221733.919809 235906.058835
4282 85620 221823.912079 235997.291683
4283 85640 221913.907519 236088.527564
4284 85660 222003.906130 236179.766477
4285 85680 222093.907911 236271.008420
4286 85700 222183.912860 236362.253394
4287 85720 222273.920976 236453.501396
4288 85740 222363.932260 236544.752426
4289 85760 222453.946709 236636.006484
4290 85780 222543.964324 236727.263569
4291 85800 222633.985102 236818.523679
4292 85820 222724.009044 236909.786814
4293 85840 222814.036148 237001.052972
4294 85860 222904.066413 237092.322155
4295 85880 222994.099839 237183.594359
4296 85900 223084.136425 237274.869585
4297 85920 223174.176169 237366.147831
4298 85940 223264.219072 237457.429097
4299 85960 223354.265131 237548.713383
4300 85980 223444.314346 237640.000686
4301 86000 223534.366717 237731.291007
4302 86020 223624.422242 237822.584344
4303 86040 223714.480921 237913.880696
4304 86060 223804.542752 238005.180064
4305 86080 223894.607734 238096.482445
4306 86100 223984.675868 238187.787840
4307 86120 224074.747151 238279.096246
4308 86140 224164.821584 238370.407664
4309 86160 224254.899165 238461.722093
4310 86180 224344.979892 238553.039532
4311 86200 224435.063767 238644.359979
4312 86220 224525.150787 238735.683435
4313 86240 224615.240951 238827.009898
4314 86260 224705.334259 238918.339367
4315 86280 224795.430710 239009.671842
4316 86300 224885.530303 239101.007321
4317 86320 224975.633037 239192.345805
4318 86340 225065.738912 239283.687292
4319 86360 225155.847926 239375.031781
4320 86380 225245.960078 239466.379271
4321 86400 225336.075368 239557.729762
4322 86420 225426.193794 239649.083253
4323 86440 225516.315357 239740.439743
4324 86460 225606.440054 239831.799231
4325 86480 225696.567886 239923.161716
4326 86500 225786.698850 240014.527198
4327 86520 225876.832947 240105.895675
4328 86540 225966.970176 240197.267148
4329 86560 226057.110535 240288.641614
4330 86580 226147.254024 240380.019073
4331 86600 226237.400642 240471.399525
4332 86620 226327.550387 240562.782969
4333 86640 226417.703260 240654.169403
4334 86660 226507.859259 240745.558827
4335 86680 226598.018384 240836.951240
4336 86700 226688.180633 240928.346641
4337 86720 226778.346006 241019.745030
4338 86740 226868.514501 241111.146405
4339 86760 226958.686118 241202.550766
4340 86780 227048.860857 241293.958113
4341 86800 227139.038715 241385.368443
4342 86820 227229.219693 241476.781756
4343 86840 227319.403789 241568.198052
4344 86860 227409.591003 241659.617330
4345 86880 227499.781333 241751.039589
4346 86900 227589.974780 241842.464828
4347 86920 227680.171341 241933.893046
4348 86940 227770.371016 242025.324242
4349 86960 227860.573805 242116.758416
4350 86980 227950.779706 242208.195567
4351 87000 228040.988719 242299.635693
4352 87020 228131.200842 242391.078795
4353 87040 228221.416075 242482.524871
4354 87060 228311.634417 242573.973921
4355 87080 228401.855867 242665.425944
4356 87100 228492.080424 242756.880938
4357 87120 228582.308088 242848.338904
4358 87140 228672.538857 242939.799839
4359 87160 228762.772730 243031.263744
4360 87180 228853.009708 243122.730618
4361 87200 228943.249788 243214.200460
4362 87220 229033.492971 243305.673269
4363 87240 229123.739254 243397.149044
4364 87260 229213.988638 243488.627784
4365 87280 229304.241122 243580.109489
4366 87300 229394.496704 243671.594158
4367 87320 229484.755384 243763.081790
4368 87340 229575.017160 243854.572384
4369 87360 229665.282033 243946.065939
4370 87380 229755.550001 244037.562455
4371 87400 229845.821064 244129.061930
4372 87420 229936.095220 244220.564365
4373 87440 230026.372468 244312.069757
4374 87460 230116.652809 244403.578108
4375 87480 230206.936240 244495.089414
4376 87500 230297.222761 244586.603676
4377 87520 230387.512372 244678.120894
4378 87540 230477.805071 244769.641065
4379 87560 230568.100858 244861.164190
4380 87580 230658.399731 244952.690267
4381 87600 230748.701690 245044.219296
4382 87620 230839.006734 245135.751275
4383 87640 230929.314862 245227.286205
4384 87660 231019.626074 245318.824084
4385 87680 231109.940368 245410.364912
4386 87700 231200.257744 245501.908687
4387 87720 231290.578200 245593.455410
4388 87740 231380.901737 245685.005078
4389 87760 231471.228352 245776.557692
4390 87780 231561.558046 245868.113250
4391 87800 231651.890817 245959.671752
4392 87820 231742.226664 246051.233197
4393 87840 231832.565587 246142.797584
4394 87860 231922.907585 246234.364912
4395 87880 232013.252657 246325.935181
4396 87900 232103.600803 246417.508389
4397 87920 232193.952020 246509.084536
4398 87940 232284.306309 246600.663622
4399 87960 232374.663669 246692.245645
4400 87980 232465.024099 246783.830604
4401 88000 232555.387597 246875.418499
4402 88020 232645.754164 246967.009329
4403 88040 232736.123797 247058.603094
4404 88060 232826.496498 247150.199791
4405 88080 232916.872264 247241.799421
4406 88100 233007.251094 247333.401983
4407 88120 233097.632989 247425.007476
4408 88140 233188.017946 247516.615899
4409 88160 233278.405966 247608.227252
4410 88180 233368.797048 247699.841533
4411 88200 233459.191190 247791.458742
4412 88220 233549.588391 247883.078878
4413 88240 233639.988652 247974.701940
4414 88260 233730.391970 248066.327928
4415 88280 233820.798346 248157.956841
4416 88300 233911.207779 248249.588677
4417 88320 234001.620266 248341.223436
4418 88340 234092.035809 248432.861118
4419 88360 234182.454405 248524.501721
4420 88380 234272.876055 248616.145245
4421 88400 234363.300757 248707.791689
4422 88420 234453.728510 248799.441052
4423 88440 234544.159314 248891.093333
4424 88460 234634.593168 248982.748532
4425 88480 234725.030070 249074.406648
4426 88500 234815.470021 249166.067680
4427 88520 234905.913018 249257.731627
4428 88540 234996.359063 249349.398489
4429 88560 235086.808153 249441.068264
4430 88580 235177.260287 249532.740952
4431 88600 235267.715466 249624.416552
4432 88620 235358.173688 249716.095064
4433 88640 235448.634952 249807.776486
4434 88660 235539.099257 249899.460817
4435 88680 235629.566604 249991.148058
4436 88700 235720.036990 250082.838207
4437 88720 235810.510415 250174.531264
4438 88740 235900.986878 250266.227227
4439 88760 235991.466379 250357.926096
4440 88780 236081.948916 250449.627870
4441 88800 236172.434488 250541.332548
4442 88820 236262.923096 250633.040130
4443 88840 236353.414738 250724.750615
4444 88860 236443.909413 250816.464002
4445 88880 236534.407120 250908.180289
4446 88900 236624.907859 250999.899478
4447 88920 236715.411629 251091.621566
4448 88940 236805.918429 251183.346552
4449 88960 236896.428258 251275.074437
4450 88980 236986.941115 251366.805220
4451 89000 237077.457000 251458.538898
4452 89020 237167.975911 251550.275473
4453 89040 237258.497848 251642.014943
4454 89060 237349.022811 251733.757306
4455 89080 237439.550797 251825.502564
4456 89100 237530.081807 251917.250714
4457 89120 237620.615840 252009.001756
4458 89140 237711.152894 252100.755689
4459 89160 237801.692970 252192.512512
4460 89180 237892.236065 252284.272225
4461 89200 237982.782180 252376.034827
4462 89220 238073.331313 252467.800317
4463 89240 238163.883464 252559.568694
4464 89260 238254.438632 252651.339958
4465 89280 238344.996817 252743.114108
4466 89300 238435.558016 252834.891142
4467 89320 238526.122230 252926.671061
4468 89340 238616.689457 253018.453863
4469 89360 238707.259698 253110.239548
4470 89380 238797.832950 253202.028115
4471 89400 238888.409213 253293.819563
4472 89420 238978.988487 253385.613892
4473 89440 239069.570771 253477.411100
4474 89460 239160.156063 253569.211187
4475 89480 239250.744364 253661.014152
4476 89500 239341.335671 253752.819994
4477 89520 239431.929985 253844.628713
4478 89540 239522.527305 253936.440308
4479 89560 239613.127629 254028.254778
4480 89580 239703.730957 254120.072122
4481 89600 239794.337289 254211.892339
4482 89620 239884.946622 254303.715430
4483 89640 239975.558957 254395.541392
4484 89660 240066.174293 254487.370226
4485 89680 240156.792629 254579.201930
4486 89700 240247.413964 254671.036504
4487 89720 240338.038298 254762.873947
4488 89740 240428.665628 254854.714258
4489 89760 240519.295956 254946.557436
4490 89780 240609.929279 255038.403481
4491 89800 240700.565598 255130.252392
4492 89820 240791.204911 255222.104169
4493 89840 240881.847217 255313.958809
4494 89860 240972.492516 255405.816314
4495 89880 241063.140807 255497.676681
4496 89900 241153.792089 255589.539910
4497 89920 241244.446362 255681.406001
4498 89940 241335.103624 255773.274952
4499 89960 241425.763874 255865.146764
4500 89980 241516.427113 255957.021434
4501 90000 241607.093339 256048.898963

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