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 12 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
5502 110020 333743.309794 349339.451035
5503 110040 333836.626387 349433.868299
5504 110060 333929.945331 349528.287820
5505 110080 334023.266626 349622.709597
5506 110100 334116.590272 349717.133630
5507 110120 334209.916269 349811.559919
5508 110140 334303.244616 349905.988463
5509 110160 334396.575312 350000.419261
5510 110180 334489.908356 350094.852314
5511 110200 334583.243750 350189.287620
5512 110220 334676.581491 350283.725179
5513 110240 334769.921579 350378.164991
5514 110260 334863.264015 350472.607055
5515 110280 334956.608797 350567.051371
5516 110300 335049.955924 350661.497938
5517 110320 335143.305397 350755.946756
5518 110340 335236.657215 350850.397824
5519 110360 335330.011378 350944.851142
5520 110380 335423.367884 351039.306710
5521 110400 335516.726733 351133.764526
5522 110420 335610.087926 351228.224591
5523 110440 335703.451461 351322.686903
5524 110460 335796.817338 351417.151463
5525 110480 335890.185556 351511.618270
5526 110500 335983.556115 351606.087324
5527 110520 336076.929014 351700.558624
5528 110540 336170.304254 351795.032169
5529 110560 336263.681833 351889.507959
5530 110580 336357.061750 351983.985994
5531 110600 336450.444006 352078.466273
5532 110620 336543.828600 352172.948796
5533 110640 336637.215532 352267.433562
5534 110660 336730.604800 352361.920571
5535 110680 336823.996405 352456.409822
5536 110700 336917.390346 352550.901315
5537 110720 337010.786622 352645.395049
5538 110740 337104.185233 352739.891024
5539 110760 337197.586178 352834.389239
5540 110780 337290.989458 352928.889695
5541 110800 337384.395071 353023.392390
5542 110820 337477.803017 353117.897323
5543 110840 337571.213295 353212.404496
5544 110860 337664.625905 353306.913906
5545 110880 337758.040847 353401.425554
5546 110900 337851.458120 353495.939439
5547 110920 337944.877723 353590.455561
5548 110940 338038.299656 353684.973919
5549 110960 338131.723919 353779.494512
5550 110980 338225.150511 353874.017341
5551 111000 338318.579431 353968.542405
5552 111020 338412.010679 354063.069703
5553 111040 338505.444255 354157.599235
5554 111060 338598.880158 354252.131000
5555 111080 338692.318387 354346.664998
5556 111100 338785.758943 354441.201229
5557 111120 338879.201824 354535.739691
5558 111140 338972.647030 354630.280385
5559 111160 339066.094560 354724.823310
5560 111180 339159.544415 354819.368466
5561 111200 339252.996593 354913.915851
5562 111220 339346.451095 355008.465467
5563 111240 339439.907919 355103.017311
5564 111260 339533.367065 355197.571385
5565 111280 339626.828533 355292.127686
5566 111300 339720.292322 355386.686216
5567 111320 339813.758431 355481.246972
5568 111340 339907.226861 355575.809956
5569 111360 340000.697610 355670.375166
5570 111380 340094.170679 355764.942601
5571 111400 340187.646066 355859.512263
5572 111420 340281.123772 355954.084149
5573 111440 340374.603795 356048.658260
5574 111460 340468.086135 356143.234595
5575 111480 340561.570793 356237.813153
5576 111500 340655.057766 356332.393935
5577 111520 340748.547055 356426.976939
5578 111540 340842.038660 356521.562166
5579 111560 340935.532579 356616.149614
5580 111580 341029.028813 356710.739284
5581 111600 341122.527360 356805.331175
5582 111620 341216.028221 356899.925286
5583 111640 341309.531395 356994.521616
5584 111660 341403.036881 357089.120167
5585 111680 341496.544678 357183.720936
5586 111700 341590.054788 357278.323924
5587 111720 341683.567208 357372.929130
5588 111740 341777.081938 357467.536553
5589 111760 341870.598979 357562.146194
5590 111780 341964.118329 357656.758051
5591 111800 342057.639988 357751.372125
5592 111820 342151.163955 357845.988414
5593 111840 342244.690230 357940.606919
5594 111860 342338.218813 358035.227638
5595 111880 342431.749703 358129.850572
5596 111900 342525.282899 358224.475720
5597 111920 342618.818402 358319.103081
5598 111940 342712.356210 358413.732656
5599 111960 342805.896323 358508.364443
5600 111980 342899.438741 358602.998442
5601 112000 342992.983463 358697.634652
5602 112020 343086.530488 358792.273074
5603 112040 343180.079817 358886.913707
5604 112060 343273.631448 358981.556550
5605 112080 343367.185382 359076.201603
5606 112100 343460.741617 359170.848865
5607 112120 343554.300153 359265.498336
5608 112140 343647.860991 359360.150015
5609 112160 343741.424128 359454.803902
5610 112180 343834.989565 359549.459997
5611 112200 343928.557302 359644.118299
5612 112220 344022.127337 359738.778808
5613 112240 344115.699671 359833.441523
5614 112260 344209.274303 359928.106443
5615 112280 344302.851232 360022.773569
5616 112300 344396.430458 360117.442899
5617 112320 344490.011980 360212.114434
5618 112340 344583.595798 360306.788173
5619 112360 344677.181912 360401.464115
5620 112380 344770.770321 360496.142260
5621 112400 344864.361024 360590.822608
5622 112420 344957.954021 360685.505158
5623 112440 345051.549312 360780.189909
5624 112460 345145.146896 360874.876862
5625 112480 345238.746772 360969.566015
5626 112500 345332.348941 361064.257369
5627 112520 345425.953401 361158.950922
5628 112540 345519.560152 361253.646675
5629 112560 345613.169194 361348.344626
5630 112580 345706.780526 361443.044776
5631 112600 345800.394148 361537.747124
5632 112620 345894.010060 361632.451670
5633 112640 345987.628260 361727.158412
5634 112660 346081.248748 361821.867352
5635 112680 346174.871524 361916.578487
5636 112700 346268.496587 362011.291818
5637 112720 346362.123938 362106.007345
5638 112740 346455.753574 362200.725066
5639 112760 346549.385497 362295.444982
5640 112780 346643.019705 362390.167091
5641 112800 346736.656198 362484.891394
5642 112820 346830.294976 362579.617890
5643 112840 346923.936037 362674.346579
5644 112860 347017.579382 362769.077460
5645 112880 347111.225010 362863.810532
5646 112900 347204.872921 362958.545796
5647 112920 347298.523114 363053.283250
5648 112940 347392.175588 363148.022895
5649 112960 347485.830344 363242.764730
5650 112980 347579.487380 363337.508754
5651 113000 347673.146697 363432.254967
5652 113020 347766.808293 363527.003369
5653 113040 347860.472169 363621.753958
5654 113060 347954.138323 363716.506736
5655 113080 348047.806756 363811.261700
5656 113100 348141.477466 363906.018851
5657 113120 348235.150454 364000.778189
5658 113140 348328.825719 364095.539712
5659 113160 348422.503260 364190.303421
5660 113180 348516.183078 364285.069315
5661 113200 348609.865171 364379.837393
5662 113220 348703.549538 364474.607655
5663 113240 348797.236181 364569.380101
5664 113260 348890.925097 364664.154730
5665 113280 348984.616287 364758.931542
5666 113300 349078.309751 364853.710536
5667 113320 349172.005486 364948.491712
5668 113340 349265.703495 365043.275069
5669 113360 349359.403774 365138.060607
5670 113380 349453.106326 365232.848326
5671 113400 349546.811148 365327.638224
5672 113420 349640.518240 365422.430303
5673 113440 349734.227602 365517.224560
5674 113460 349827.939233 365612.020996
5675 113480 349921.653134 365706.819610
5676 113500 350015.369303 365801.620403
5677 113520 350109.087740 365896.423372
5678 113540 350202.808444 365991.228519
5679 113560 350296.531415 366086.035842
5680 113580 350390.256653 366180.845341
5681 113600 350483.984157 366275.657015
5682 113620 350577.713927 366370.470865
5683 113640 350671.445962 366465.286889
5684 113660 350765.180262 366560.105088
5685 113680 350858.916826 366654.925461
5686 113700 350952.655653 366749.748006
5687 113720 351046.396744 366844.572725
5688 113740 351140.140098 366939.399617
5689 113760 351233.885714 367034.228680
5690 113780 351327.633593 367129.059915
5691 113800 351421.383732 367223.893321
5692 113820 351515.136133 367318.728898
5693 113840 351608.890794 367413.566645
5694 113860 351702.647715 367508.406562
5695 113880 351796.406896 367603.248649
5696 113900 351890.168335 367698.092904
5697 113920 351983.932034 367792.939328
5698 113940 352077.697990 367887.787920
5699 113960 352171.466205 367982.638680
5700 113980 352265.236677 368077.491606
5701 114000 352359.009405 368172.346700
5702 114020 352452.784390 368267.203960
5703 114040 352546.561630 368362.063386
5704 114060 352640.341127 368456.924977
5705 114080 352734.122878 368551.788733
5706 114100 352827.906883 368646.654654
5707 114120 352921.693143 368741.522739
5708 114140 353015.481656 368836.392987
5709 114160 353109.272422 368931.265399
5710 114180 353203.065441 369026.139974
5711 114200 353296.860712 369121.016711
5712 114220 353390.658234 369215.895610
5713 114240 353484.458008 369310.776670
5714 114260 353578.260033 369405.659892
5715 114280 353672.064308 369500.545274
5716 114300 353765.870833 369595.432816
5717 114320 353859.679607 369690.322518
5718 114340 353953.490631 369785.214380
5719 114360 354047.303902 369880.108400
5720 114380 354141.119422 369975.004578
5721 114400 354234.937189 370069.902915
5722 114420 354328.757204 370164.803409
5723 114440 354422.579465 370259.706061
5724 114460 354516.403972 370354.610869
5725 114480 354610.230725 370449.517833
5726 114500 354704.059723 370544.426953
5727 114520 354797.890966 370639.338228
5728 114540 354891.724454 370734.251659
5729 114560 354985.560185 370829.167244
5730 114580 355079.398160 370924.084983
5731 114600 355173.238377 371019.004875
5732 114620 355267.080837 371113.926921
5733 114640 355360.925540 371208.851120
5734 114660 355454.772483 371303.777471
5735 114680 355548.621668 371398.705973
5736 114700 355642.473094 371493.636628
5737 114720 355736.326760 371588.569433
5738 114740 355830.182665 371683.504389
5739 114760 355924.040810 371778.441495
5740 114780 356017.901194 371873.380751
5741 114800 356111.763816 371968.322156
5742 114820 356205.628676 372063.265710
5743 114840 356299.495774 372158.211412
5744 114860 356393.365109 372253.159263
5745 114880 356487.236680 372348.109261
5746 114900 356581.110487 372443.061406
5747 114920 356674.986530 372538.015698
5748 114940 356768.864808 372632.972136
5749 114960 356862.745321 372727.930720
5750 114980 356956.628068 372822.891449
5751 115000 357050.513049 372917.854323
5752 115020 357144.400264 373012.819342
5753 115040 357238.289711 373107.786505
5754 115060 357332.181391 373202.755812
5755 115080 357426.075303 373297.727262
5756 115100 357519.971446 373392.700855
5757 115120 357613.869821 373487.676590
5758 115140 357707.770426 373582.654467
5759 115160 357801.673261 373677.634486
5760 115180 357895.578326 373772.616645
5761 115200 357989.485621 373867.600946
5762 115220 358083.395144 373962.587386
5763 115240 358177.306896 374057.575967
5764 115260 358271.220875 374152.566687
5765 115280 358365.137083 374247.559545
5766 115300 358459.055517 374342.554543
5767 115320 358552.976177 374437.551678
5768 115340 358646.899064 374532.550951
5769 115360 358740.824177 374627.552361
5770 115380 358834.751515 374722.555909
5771 115400 358928.681077 374817.561592
5772 115420 359022.612864 374912.569412
5773 115440 359116.546875 375007.579367
5774 115460 359210.483109 375102.591457
5775 115480 359304.421566 375197.605681
5776 115500 359398.362246 375292.622040
5777 115520 359492.305148 375387.640533
5778 115540 359586.250271 375482.661159
5779 115560 359680.197616 375577.683919
5780 115580 359774.147181 375672.708810
5781 115600 359868.098967 375767.735834
5782 115620 359962.052972 375862.764990
5783 115640 360056.009197 375957.796277
5784 115660 360149.967641 376052.829694
5785 115680 360243.928303 376147.865242
5786 115700 360337.891183 376242.902920
5787 115720 360431.856281 376337.942728
5788 115740 360525.823596 376432.984664
5789 115760 360619.793128 376528.028730
5790 115780 360713.764875 376623.074923
5791 115800 360807.738839 376718.123245
5792 115820 360901.715018 376813.173694
5793 115840 360995.693412 376908.226270
5794 115860 361089.674021 377003.280972
5795 115880 361183.656843 377098.337801
5796 115900 361277.641879 377193.396756
5797 115920 361371.629128 377288.457835
5798 115940 361465.618590 377383.521040
5799 115960 361559.610264 377478.586369
5800 115980 361653.604149 377573.653822
5801 116000 361747.600246 377668.723399
5802 116020 361841.598554 377763.795099
5803 116040 361935.599073 377858.868922
5804 116060 362029.601801 377953.944867
5805 116080 362123.606739 378049.022934
5806 116100 362217.613886 378144.103122
5807 116120 362311.623241 378239.185432
5808 116140 362405.634805 378334.269862
5809 116160 362499.648577 378429.356412
5810 116180 362593.664556 378524.445083
5811 116200 362687.682742 378619.535872
5812 116220 362781.703134 378714.628781
5813 116240 362875.725732 378809.723808
5814 116260 362969.750536 378904.820954
5815 116280 363063.777545 378999.920217
5816 116300 363157.806758 379095.021597
5817 116320 363251.838176 379190.125094
5818 116340 363345.871798 379285.230708
5819 116360 363439.907622 379380.338438
5820 116380 363533.945650 379475.448283
5821 116400 363627.985880 379570.560243
5822 116420 363722.028313 379665.674319
5823 116440 363816.072946 379760.790508
5824 116460 363910.119781 379855.908812
5825 116480 364004.168817 379951.029229
5826 116500 364098.220052 380046.151759
5827 116520 364192.273488 380141.276402
5828 116540 364286.329123 380236.403157
5829 116560 364380.386957 380331.532024
5830 116580 364474.446989 380426.663002
5831 116600 364568.509219 380521.796091
5832 116620 364662.573647 380616.931291
5833 116640 364756.640272 380712.068601
5834 116660 364850.709094 380807.208021
5835 116680 364944.780112 380902.349550
5836 116700 365038.853326 380997.493188
5837 116720 365132.928735 381092.638935
5838 116740 365227.006339 381187.786790
5839 116760 365321.086138 381282.936752
5840 116780 365415.168131 381378.088821
5841 116800 365509.252317 381473.242998
5842 116820 365603.338697 381568.399281
5843 116840 365697.427269 381663.557669
5844 116860 365791.518034 381758.718164
5845 116880 365885.610990 381853.880763
5846 116900 365979.706138 381949.045467
5847 116920 366073.803477 382044.212276
5848 116940 366167.903007 382139.381188
5849 116960 366262.004727 382234.552204
5850 116980 366356.108636 382329.725323
5851 117000 366450.214735 382424.900545
5852 117020 366544.323022 382520.077869
5853 117040 366638.433498 382615.257295
5854 117060 366732.546161 382710.438822
5855 117080 366826.661013 382805.622451
5856 117100 366920.778051 382900.808179
5857 117120 367014.897276 382995.996008
5858 117140 367109.018687 383091.185937
5859 117160 367203.142284 383186.377965
5860 117180 367297.268066 383281.572092
5861 117200 367391.396033 383376.768318
5862 117220 367485.526185 383471.966642
5863 117240 367579.658520 383567.167063
5864 117260 367673.793039 383662.369582
5865 117280 367767.929742 383757.574198
5866 117300 367862.068627 383852.780910
5867 117320 367956.209694 383947.989718
5868 117340 368050.352943 384043.200622
5869 117360 368144.498373 384138.413621
5870 117380 368238.645985 384233.628714
5871 117400 368332.795777 384328.845903
5872 117420 368426.947749 384424.065185
5873 117440 368521.101901 384519.286560
5874 117460 368615.258232 384614.510029
5875 117480 368709.416742 384709.735591
5876 117500 368803.577430 384804.963245
5877 117520 368897.740296 384900.192991
5878 117540 368991.905340 384995.424829
5879 117560 369086.072560 385090.658757
5880 117580 369180.241958 385185.894777
5881 117600 369274.413531 385281.132886
5882 117620 369368.587281 385376.373086
5883 117640 369462.763205 385471.615375
5884 117660 369556.941305 385566.859753
5885 117680 369651.121579 385662.106219
5886 117700 369745.304027 385757.354774
5887 117720 369839.488649 385852.605417
5888 117740 369933.675444 385947.858147
5889 117760 370027.864412 386043.112964
5890 117780 370122.055552 386138.369868
5891 117800 370216.248863 386233.628858
5892 117820 370310.444347 386328.889934
5893 117840 370404.642001 386424.153095
5894 117860 370498.841826 386519.418341
5895 117880 370593.043821 386614.685672
5896 117900 370687.247986 386709.955087
5897 117920 370781.454321 386805.226585
5898 117940 370875.662824 386900.500167
5899 117960 370969.873495 386995.775832
5900 117980 371064.086335 387091.053579
5901 118000 371158.301342 387186.333409
5902 118020 371252.518516 387281.615320
5903 118040 371346.737858 387376.899313
5904 118060 371440.959365 387472.185386
5905 118080 371535.183038 387567.473540
5906 118100 371629.408877 387662.763774
5907 118120 371723.636881 387758.056088
5908 118140 371817.867049 387853.350481
5909 118160 371912.099382 387948.646953
5910 118180 372006.333878 388043.945503
5911 118200 372100.570538 388139.246131
5912 118220 372194.809360 388234.548837
5913 118240 372289.050345 388329.853621
5914 118260 372383.293492 388425.160481
5915 118280 372477.538801 388520.469417
5916 118300 372571.786270 388615.780430
5917 118320 372666.035901 388711.093518
5918 118340 372760.287692 388806.408681
5919 118360 372854.541643 388901.725920
5920 118380 372948.797753 388997.045232
5921 118400 373043.056022 389092.366619
5922 118420 373137.316450 389187.690079
5923 118440 373231.579036 389283.015613
5924 118460 373325.843780 389378.343219
5925 118480 373420.110682 389473.672898
5926 118500 373514.379740 389569.004648
5927 118520 373608.650954 389664.338470
5928 118540 373702.924325 389759.674364
5929 118560 373797.199852 389855.012328
5930 118580 373891.477533 389950.352363
5931 118600 373985.757370 390045.694467
5932 118620 374080.039361 390141.038641
5933 118640 374174.323505 390236.384885
5934 118660 374268.609804 390331.733197
5935 118680 374362.898255 390427.083577
5936 118700 374457.188859 390522.436026
5937 118720 374551.481616 390617.790542
5938 118740 374645.776524 390713.147125
5939 118760 374740.073583 390808.505775
5940 118780 374834.372794 390903.866491
5941 118800 374928.674155 390999.229273
5942 118820 375022.977667 391094.594121
5943 118840 375117.283328 391189.961034
5944 118860 375211.591138 391285.330012
5945 118880 375305.901097 391380.701054
5946 118900 375400.213205 391476.074160
5947 118920 375494.527461 391571.449329
5948 118940 375588.843864 391666.826562
5949 118960 375683.162415 391762.205858
5950 118980 375777.483112 391857.587215
5951 119000 375871.805956 391952.970635
5952 119020 375966.130946 392048.356117
5953 119040 376060.458081 392143.743659
5954 119060 376154.787361 392239.133262
5955 119080 376249.118786 392334.524926
5956 119100 376343.452355 392429.918650
5957 119120 376437.788068 392525.314433
5958 119140 376532.125925 392620.712275
5959 119160 376626.465924 392716.112176
5960 119180 376720.808066 392811.514136
5961 119200 376815.152350 392906.918153
5962 119220 376909.498776 393002.324228
5963 119240 377003.847344 393097.732361
5964 119260 377098.198052 393193.142550
5965 119280 377192.550900 393288.554795
5966 119300 377286.905889 393383.969096
5967 119320 377381.263017 393479.385453
5968 119340 377475.622285 393574.803866
5969 119360 377569.983691 393670.224333
5970 119380 377664.347236 393765.646854
5971 119400 377758.712919 393861.071429
5972 119420 377853.080739 393956.498058
5973 119440 377947.450696 394051.926740
5974 119460 378041.822791 394147.357475
5975 119480 378136.197021 394242.790263
5976 119500 378230.573387 394338.225102
5977 119520 378324.951889 394433.661993
5978 119540 378419.332526 394529.100935
5979 119560 378513.715297 394624.541929
5980 119580 378608.100203 394719.984972
5981 119600 378702.487243 394815.430066
5982 119620 378796.876416 394910.877209
5983 119640 378891.267721 395006.326401
5984 119660 378985.661160 395101.777643
5985 119680 379080.056730 395197.230933
5986 119700 379174.454433 395292.686270
5987 119720 379268.854266 395388.143656
5988 119740 379363.256231 395483.603089
5989 119760 379457.660326 395579.064569
5990 119780 379552.066551 395674.528095
5991 119800 379646.474906 395769.993667
5992 119820 379740.885390 395865.461285
5993 119840 379835.298002 395960.930949
5994 119860 379929.712744 396056.402657
5995 119880 380024.129613 396151.876410
5996 119900 380118.548610 396247.352207
5997 119920 380212.969734 396342.830048
5998 119940 380307.392984 396438.309932
5999 119960 380401.818361 396533.791859
6000 119980 380496.245864 396629.275828
6001 120000 380590.675493 396724.761840

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