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 19 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
9002 180020 672084.452254 691085.553399
9003 180040 672183.899972 691185.873994
9004 180060 672283.349027 691286.195880
9005 180080 672382.799417 691386.519056
9006 180100 672482.251142 691486.843523
9007 180120 672581.704204 691587.169280
9008 180140 672681.158601 691687.496327
9009 180160 672780.614333 691787.824664
9010 180180 672880.071400 691888.154290
9011 180200 672979.529802 691988.485207
9012 180220 673078.989539 692088.817412
9013 180240 673178.450610 692189.150908
9014 180260 673277.913016 692289.485692
9015 180280 673377.376756 692389.821765
9016 180300 673476.841830 692490.159127
9017 180320 673576.308238 692590.497778
9018 180340 673675.775979 692690.837717
9019 180360 673775.245055 692791.178945
9020 180380 673874.715464 692891.521461
9021 180400 673974.187206 692991.865265
9022 180420 674073.660281 693092.210356
9023 180440 674173.134689 693192.556736
9024 180460 674272.610429 693292.904403
9025 180480 674372.087503 693393.253357
9026 180500 674471.565908 693493.603599
9027 180520 674571.045646 693593.955128
9028 180540 674670.526716 693694.307943
9029 180560 674770.009118 693794.662046
9030 180580 674869.492852 693895.017435
9031 180600 674968.977918 693995.374110
9032 180620 675068.464314 694095.732072
9033 180640 675167.952042 694196.091320
9034 180660 675267.441101 694296.451854
9035 180680 675366.931491 694396.813674
9036 180700 675466.423212 694497.176779
9037 180720 675565.916264 694597.541170
9038 180740 675665.410645 694697.906846
9039 180760 675764.906358 694798.273807
9040 180780 675864.403400 694898.642054
9041 180800 675963.901772 694999.011585
9042 180820 676063.401474 695099.382401
9043 180840 676162.902505 695199.754502
9044 180860 676262.404866 695300.127887
9045 180880 676361.908556 695400.502556
9046 180900 676461.413576 695500.878509
9047 180920 676560.919924 695601.255746
9048 180940 676660.427601 695701.634267
9049 180960 676759.936606 695802.014072
9050 180980 676859.446940 695902.395160
9051 181000 676958.958603 696002.777531
9052 181020 677058.471593 696103.161186
9053 181040 677157.985912 696203.546123
9054 181060 677257.501558 696303.932343
9055 181080 677357.018532 696404.319846
9056 181100 677456.536833 696504.708632
9057 181120 677556.056462 696605.098700
9058 181140 677655.577417 696705.490050
9059 181160 677755.099700 696805.882682
9060 181180 677854.623309 696906.276595
9061 181200 677954.148245 697006.671791
9062 181220 678053.674508 697107.068268
9063 181240 678153.202097 697207.466027
9064 181260 678252.731012 697307.865067
9065 181280 678352.261253 697408.265387
9066 181300 678451.792820 697508.666989
9067 181320 678551.325712 697609.069872
9068 181340 678650.859930 697709.474035
9069 181360 678750.395473 697809.879479
9070 181380 678849.932341 697910.286203
9071 181400 678949.470535 698010.694207
9072 181420 679049.010053 698111.103491
9073 181440 679148.550896 698211.514055
9074 181460 679248.093063 698311.925899
9075 181480 679347.636555 698412.339022
9076 181500 679447.181371 698512.753424
9077 181520 679546.727511 698613.169106
9078 181540 679646.274974 698713.586067
9079 181560 679745.823762 698814.004307
9080 181580 679845.373873 698914.423825
9081 181600 679944.925307 699014.844622
9082 181620 680044.478064 699115.266697
9083 181640 680144.032145 699215.690051
9084 181660 680243.587548 699316.114683
9085 181680 680343.144274 699416.540593
9086 181700 680442.702323 699516.967780
9087 181720 680542.261693 699617.396246
9088 181740 680641.822386 699717.825988
9089 181760 680741.384402 699818.257008
9090 181780 680840.947739 699918.689306
9091 181800 680940.512397 700019.122880
9092 181820 681040.078378 700119.557731
9093 181840 681139.645679 700219.993859
9094 181860 681239.214302 700320.431263
9095 181880 681338.784246 700420.869944
9096 181900 681438.355511 700521.309901
9097 181920 681537.928097 700621.751134
9098 181940 681637.502003 700722.193643
9099 181960 681737.077229 700822.637428
9100 181980 681836.653776 700923.082488
9101 182000 681936.231643 701023.528824
9102 182020 682035.810830 701123.976435
9103 182040 682135.391336 701224.425322
9104 182060 682234.973163 701324.875483
9105 182080 682334.556308 701425.326919
9106 182100 682434.140773 701525.779630
9107 182120 682533.726557 701626.233615
9108 182140 682633.313660 701726.688875
9109 182160 682732.902081 701827.145409
9110 182180 682832.491822 701927.603217
9111 182200 682932.082881 702028.062299
9112 182220 683031.675258 702128.522655
9113 182240 683131.268953 702228.984284
9114 182260 683230.863966 702329.447187
9115 182280 683330.460297 702429.911363
9116 182300 683430.057945 702530.376812
9117 182320 683529.656911 702630.843534
9118 182340 683629.257195 702731.311529
9119 182360 683728.858795 702831.780797
9120 182380 683828.461713 702932.251337
9121 182400 683928.065947 703032.723149
9122 182420 684027.671498 703133.196234
9123 182440 684127.278366 703233.670591
9124 182460 684226.886550 703334.146219
9125 182480 684326.496050 703434.623120
9126 182500 684426.106866 703535.101292
9127 182520 684525.718998 703635.580735
9128 182540 684625.332445 703736.061450
9129 182560 684724.947208 703836.543435
9130 182580 684824.563287 703937.026692
9131 182600 684924.180681 704037.511220
9132 182620 685023.799389 704137.997018
9133 182640 685123.419413 704238.484087
9134 182660 685223.040752 704338.972426
9135 182680 685322.663404 704439.462035
9136 182700 685422.287372 704539.952914
9137 182720 685521.912653 704640.445063
9138 182740 685621.539249 704740.938482
9139 182760 685721.167159 704841.433171
9140 182780 685820.796382 704941.929128
9141 182800 685920.426919 705042.426356
9142 182820 686020.058769 705142.924852
9143 182840 686119.691933 705243.424617
9144 182860 686219.326410 705343.925651
9145 182880 686318.962199 705444.427954
9146 182900 686418.599302 705544.931525
9147 182920 686518.237717 705645.436364
9148 182940 686617.877445 705745.942472
9149 182960 686717.518485 705846.449848
9150 182980 686817.160837 705946.958491
9151 183000 686916.804501 706047.468403
9152 183020 687016.449476 706147.979581
9153 183040 687116.095764 706248.492028
9154 183060 687215.743363 706349.005741
9155 183080 687315.392273 706449.520722
9156 183100 687415.042495 706550.036970
9157 183120 687514.694027 706650.554484
9158 183140 687614.346871 706751.073265
9159 183160 687714.001025 706851.593313
9160 183180 687813.656489 706952.114627
9161 183200 687913.313264 707052.637207
9162 183220 688012.971349 707153.161053
9163 183240 688112.630745 707253.686165
9164 183260 688212.291450 707354.212543
9165 183280 688311.953465 707454.740186
9166 183300 688411.616789 707555.269095
9167 183320 688511.281423 707655.799269
9168 183340 688610.947366 707756.330708
9169 183360 688710.614618 707856.863412
9170 183380 688810.283179 707957.397381
9171 183400 688909.953049 708057.932615
9172 183420 689009.624228 708158.469113
9173 183440 689109.296714 708259.006875
9174 183460 689208.970510 708359.545902
9175 183480 689308.645613 708460.086193
9176 183500 689408.322024 708560.627747
9177 183520 689507.999743 708661.170565
9178 183540 689607.678770 708761.714647
9179 183560 689707.359105 708862.259993
9180 183580 689807.040746 708962.806601
9181 183600 689906.723695 709063.354473
9182 183620 690006.407951 709163.903608
9183 183640 690106.093513 709264.454005
9184 183660 690205.780382 709365.005665
9185 183680 690305.468558 709465.558588
9186 183700 690405.158041 709566.112773
9187 183720 690504.848829 709666.668220
9188 183740 690604.540923 709767.224930
9189 183760 690704.234324 709867.782901
9190 183780 690803.929030 709968.342134
9191 183800 690903.625041 710068.902629
9192 183820 691003.322358 710169.464385
9193 183840 691103.020981 710270.027402
9194 183860 691202.720908 710370.591681
9195 183880 691302.422141 710471.157220
9196 183900 691402.124678 710571.724021
9197 183920 691501.828519 710672.292082
9198 183940 691601.533666 710772.861403
9199 183960 691701.240116 710873.431985
9200 183980 691800.947871 710974.003827
9201 184000 691900.656929 711074.576930
9202 184020 692000.367292 711175.151292
9203 184040 692100.078958 711275.726914
9204 184060 692199.791927 711376.303795
9205 184080 692299.506200 711476.881936
9206 184100 692399.221776 711577.461337
9207 184120 692498.938655 711678.041996
9208 184140 692598.656837 711778.623915
9209 184160 692698.376322 711879.207092
9210 184180 692798.097109 711979.791529
9211 184200 692897.819199 712080.377223
9212 184220 692997.542591 712180.964176
9213 184240 693097.267285 712281.552388
9214 184260 693196.993280 712382.141857
9215 184280 693296.720578 712482.732585
9216 184300 693396.449177 712583.324570
9217 184320 693496.179078 712683.917813
9218 184340 693595.910280 712784.512313
9219 184360 693695.642783 712885.108071
9220 184380 693795.376587 712985.705086
9221 184400 693895.111692 713086.303358
9222 184420 693994.848097 713186.902887
9223 184440 694094.585803 713287.503673
9224 184460 694194.324809 713388.105715
9225 184480 694294.065116 713488.709014
9226 184500 694393.806722 713589.313569
9227 184520 694493.549628 713689.919380
9228 184540 694593.293834 713790.526447
9229 184560 694693.039340 713891.134770
9230 184580 694792.786145 713991.744349
9231 184600 694892.534249 714092.355183
9232 184620 694992.283652 714192.967272
9233 184640 695092.034354 714293.580617
9234 184660 695191.786355 714394.195217
9235 184680 695291.539654 714494.811072
9236 184700 695391.294252 714595.428182
9237 184720 695491.050148 714696.046546
9238 184740 695590.807343 714796.666165
9239 184760 695690.565835 714897.287038
9240 184780 695790.325625 714997.909165
9241 184800 695890.086712 715098.532546
9242 184820 695989.849098 715199.157181
9243 184840 696089.612780 715299.783070
9244 184860 696189.377760 715400.410213
9245 184880 696289.144036 715501.038608
9246 184900 696388.911610 715601.668258
9247 184920 696488.680480 715702.299160
9248 184940 696588.450647 715802.931315
9249 184960 696688.222110 715903.564723
9250 184980 696787.994869 716004.199384
9251 185000 696887.768925 716104.835297
9252 185020 696987.544276 716205.472463
9253 185040 697087.320923 716306.110881
9254 185060 697187.098866 716406.750551
9255 185080 697286.878104 716507.391473
9256 185100 697386.658637 716608.033647
9257 185120 697486.440466 716708.677072
9258 185140 697586.223589 716809.321749
9259 185160 697686.008008 716909.967677
9260 185180 697785.793721 717010.614856
9261 185200 697885.580728 717111.263286
9262 185220 697985.369030 717211.912967
9263 185240 698085.158626 717312.563899
9264 185260 698184.949516 717413.216082
9265 185280 698284.741700 717513.869515
9266 185300 698384.535178 717614.524198
9267 185320 698484.329949 717715.180131
9268 185340 698584.126013 717815.837315
9269 185360 698683.923371 717916.495748
9270 185380 698783.722022 718017.155431
9271 185400 698883.521966 718117.816363
9272 185420 698983.323203 718218.478545
9273 185440 699083.125732 718319.141976
9274 185460 699182.929554 718419.806656
9275 185480 699282.734668 718520.472585
9276 185500 699382.541074 718621.139762
9277 185520 699482.348772 718721.808189
9278 185540 699582.157762 718822.477864
9279 185560 699681.968044 718923.148787
9280 185580 699781.779617 719023.820958
9281 185600 699881.592481 719124.494377
9282 185620 699981.406637 719225.169045
9283 185640 700081.222084 719325.844960
9284 185660 700181.038822 719426.522122
9285 185680 700280.856850 719527.200533
9286 185700 700380.676169 719627.880190
9287 185720 700480.496779 719728.561094
9288 185740 700580.318679 719829.243246
9289 185760 700680.141869 719929.926644
9290 185780 700779.966349 720030.611289
9291 185800 700879.792118 720131.297181
9292 185820 700979.619178 720231.984319
9293 185840 701079.447526 720332.672703
9294 185860 701179.277165 720433.362333
9295 185880 701279.108092 720534.053210
9296 185900 701378.940308 720634.745332
9297 185920 701478.773814 720735.438699
9298 185940 701578.608608 720836.133313
9299 185960 701678.444690 720936.829171
9300 185980 701778.282061 721037.526275
9301 186000 701878.120721 721138.224624
9302 186020 701977.960668 721238.924218
9303 186040 702077.801903 721339.625056
9304 186060 702177.644426 721440.327140
9305 186080 702277.488237 721541.030467
9306 186100 702377.333335 721641.735039
9307 186120 702477.179721 721742.440855
9308 186140 702577.027393 721843.147915
9309 186160 702676.876353 721943.856219
9310 186180 702776.726600 722044.565767
9311 186200 702876.578133 722145.276558
9312 186220 702976.430953 722245.988593
9313 186240 703076.285059 722346.701871
9314 186260 703176.140451 722447.416392
9315 186280 703275.997130 722548.132157
9316 186300 703375.855094 722648.849164
9317 186320 703475.714344 722749.567413
9318 186340 703575.574880 722850.286905
9319 186360 703675.436702 722951.007640
9320 186380 703775.299808 723051.729617
9321 186400 703875.164200 723152.452836
9322 186420 703975.029877 723253.177297
9323 186440 704074.896838 723353.902999
9324 186460 704174.765085 723454.629944
9325 186480 704274.634615 723555.358130
9326 186500 704374.505431 723656.087557
9327 186520 704474.377530 723756.818225
9328 186540 704574.250914 723857.550135
9329 186560 704674.125581 723958.283285
9330 186580 704774.001532 724059.017677
9331 186600 704873.878767 724159.753308
9332 186620 704973.757285 724260.490181
9333 186640 705073.637087 724361.228293
9334 186660 705173.518172 724461.967646
9335 186680 705273.400539 724562.708239
9336 186700 705373.284190 724663.450072
9337 186720 705473.169123 724764.193144
9338 186740 705573.055339 724864.937456
9339 186760 705672.942837 724965.683008
9340 186780 705772.831617 725066.429799
9341 186800 705872.721680 725167.177829
9342 186820 705972.613024 725267.927097
9343 186840 706072.505650 725368.677605
9344 186860 706172.399558 725469.429352
9345 186880 706272.294747 725570.182337
9346 186900 706372.191218 725670.936560
9347 186920 706472.088969 725771.692022
9348 186940 706571.988002 725872.448722
9349 186960 706671.888315 725973.206660
9350 186980 706771.789909 726073.965836
9351 187000 706871.692784 726174.726249
9352 187020 706971.596939 726275.487900
9353 187040 707071.502374 726376.250788
9354 187060 707171.409090 726477.014914
9355 187080 707271.317085 726577.780276
9356 187100 707371.226360 726678.546876
9357 187120 707471.136915 726779.314712
9358 187140 707571.048749 726880.083786
9359 187160 707670.961862 726980.854095
9360 187180 707770.876255 727081.625641
9361 187200 707870.791926 727182.398423
9362 187220 707970.708876 727283.172441
9363 187240 708070.627106 727383.947695
9364 187260 708170.546613 727484.724185
9365 187280 708270.467399 727585.501911
9366 187300 708370.389463 727686.280872
9367 187320 708470.312806 727787.061068
9368 187340 708570.237426 727887.842500
9369 187360 708670.163324 727988.625166
9370 187380 708770.090500 728089.409068
9371 187400 708870.018953 728190.194204
9372 187420 708969.948683 728290.980575
9373 187440 709069.879691 728391.768180
9374 187460 709169.811975 728492.557020
9375 187480 709269.745537 728593.347094
9376 187500 709369.680375 728694.138402
9377 187520 709469.616490 728794.930944
9378 187540 709569.553881 728895.724719
9379 187560 709669.492548 728996.519728
9380 187580 709769.432491 729097.315970
9381 187600 709869.373711 729198.113446
9382 187620 709969.316206 729298.912155
9383 187640 710069.259977 729399.712097
9384 187660 710169.205023 729500.513272
9385 187680 710269.151345 729601.315679
9386 187700 710369.098941 729702.119320
9387 187720 710469.047813 729802.924192
9388 187740 710568.997960 729903.730297
9389 187760 710668.949381 730004.537634
9390 187780 710768.902077 730105.346203
9391 187800 710868.856048 730206.156003
9392 187820 710968.811293 730306.967036
9393 187840 711068.767811 730407.779300
9394 187860 711168.725604 730508.592795
9395 187880 711268.684671 730609.407522
9396 187900 711368.645011 730710.223480
9397 187920 711468.606625 730811.040668
9398 187940 711568.569512 730911.859088
9399 187960 711668.533672 731012.678738
9400 187980 711768.499105 731113.499619
9401 188000 711868.465812 731214.321730
9402 188020 711968.433791 731315.145071
9403 188040 712068.403042 731415.969643
9404 188060 712168.373566 731516.795444
9405 188080 712268.345362 731617.622476
9406 188100 712368.318431 731718.450737
9407 188120 712468.292771 731819.280227
9408 188140 712568.268384 731920.110947
9409 188160 712668.245268 732020.942896
9410 188180 712768.223423 732121.776074
9411 188200 712868.202850 732222.610481
9412 188220 712968.183548 732323.446117
9413 188240 713068.165517 732424.282981
9414 188260 713168.148757 732525.121074
9415 188280 713268.133268 732625.960396
9416 188300 713368.119050 732726.800945
9417 188320 713468.106102 732827.642723
9418 188340 713568.094424 732928.485729
9419 188360 713668.084017 733029.329962
9420 188380 713768.074879 733130.175423
9421 188400 713868.067012 733231.022112
9422 188420 713968.060414 733331.870028
9423 188440 714068.055085 733432.719171
9424 188460 714168.051027 733533.569541
9425 188480 714268.048237 733634.421138
9426 188500 714368.046716 733735.273962
9427 188520 714468.046465 733836.128012
9428 188540 714568.047482 733936.983289
9429 188560 714668.049768 734037.839792
9430 188580 714768.053322 734138.697522
9431 188600 714868.058145 734239.556478
9432 188620 714968.064236 734340.416659
9433 188640 715068.071595 734441.278066
9434 188660 715168.080222 734542.140699
9435 188680 715268.090117 734643.004557
9436 188700 715368.101279 734743.869641
9437 188720 715468.113709 734844.735950
9438 188740 715568.127406 734945.603484
9439 188760 715668.142371 735046.472243
9440 188780 715768.158602 735147.342226
9441 188800 715868.176100 735248.213434
9442 188820 715968.194865 735349.085867
9443 188840 716068.214896 735449.959524
9444 188860 716168.236194 735550.834405
9445 188880 716268.258758 735651.710510
9446 188900 716368.282588 735752.587839
9447 188920 716468.307684 735853.466392
9448 188940 716568.334046 735954.346169
9449 188960 716668.361674 736055.227169
9450 188980 716768.390567 736156.109392
9451 189000 716868.420725 736256.992838
9452 189020 716968.452149 736357.877508
9453 189040 717068.484838 736458.763400
9454 189060 717168.518791 736559.650515
9455 189080 717268.554010 736660.538853
9456 189100 717368.590493 736761.428413
9457 189120 717468.628240 736862.319196
9458 189140 717568.667252 736963.211200
9459 189160 717668.707528 737064.104427
9460 189180 717768.749068 737164.998876
9461 189200 717868.791871 737265.894546
9462 189220 717968.835939 737366.791438
9463 189240 718068.881270 737467.689551
9464 189260 718168.927864 737568.588886
9465 189280 718268.975722 737669.489442
9466 189300 718369.024843 737770.391219
9467 189320 718469.075226 737871.294217
9468 189340 718569.126873 737972.198435
9469 189360 718669.179782 738073.103875
9470 189380 718769.233954 738174.010534
9471 189400 718869.289388 738274.918414
9472 189420 718969.346084 738375.827514
9473 189440 719069.404043 738476.737835
9474 189460 719169.463263 738577.649375
9475 189480 719269.523745 738678.562135
9476 189500 719369.585489 738779.476114
9477 189520 719469.648494 738880.391313
9478 189540 719569.712760 738981.307731
9479 189560 719669.778288 739082.225368
9480 189580 719769.845076 739183.144225
9481 189600 719869.913126 739284.064300
9482 189620 719969.982436 739384.985594
9483 189640 720070.053007 739485.908107
9484 189660 720170.124838 739586.831838
9485 189680 720270.197930 739687.756788
9486 189700 720370.272281 739788.682955
9487 189720 720470.347893 739889.610341
9488 189740 720570.424764 739990.538945
9489 189760 720670.502895 740091.468766
9490 189780 720770.582286 740192.399805
9491 189800 720870.662936 740293.332062
9492 189820 720970.744845 740394.265535
9493 189840 721070.828014 740495.200226
9494 189860 721170.912441 740596.136134
9495 189880 721270.998127 740697.073259
9496 189900 721371.085072 740798.011601
9497 189920 721471.173275 740898.951159
9498 189940 721571.262736 740999.891934
9499 189960 721671.353456 741100.833925
9500 189980 721771.445434 741201.777133
9501 190000 721871.538670 741302.721556

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