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 16 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
7502 150020 524520.729169 542156.729394
7503 150040 524617.958256 542254.909225
7504 150060 524715.188986 542353.090638
7505 150080 524812.421358 542451.273635
7506 150100 524909.655372 542549.458213
7507 150120 525006.891027 542647.644374
7508 150140 525104.128324 542745.832117
7509 150160 525201.367263 542844.021442
7510 150180 525298.607842 542942.212348
7511 150200 525395.850062 543040.404836
7512 150220 525493.093923 543138.598904
7513 150240 525590.339424 543236.794554
7514 150260 525687.586564 543334.991784
7515 150280 525784.835345 543433.190594
7516 150300 525882.085765 543531.390984
7517 150320 525979.337824 543629.592954
7518 150340 526076.591523 543727.796504
7519 150360 526173.846860 543826.001633
7520 150380 526271.103835 543924.208341
7521 150400 526368.362449 544022.416628
7522 150420 526465.622701 544120.626494
7523 150440 526562.884590 544218.837938
7524 150460 526660.148117 544317.050960
7525 150480 526757.413282 544415.265561
7526 150500 526854.680083 544513.481739
7527 150520 526951.948521 544611.699494
7528 150540 527049.218596 544709.918827
7529 150560 527146.490306 544808.139737
7530 150580 527243.763653 544906.362223
7531 150600 527341.038636 545004.586286
7532 150620 527438.315254 545102.811925
7533 150640 527535.593507 545201.039141
7534 150660 527632.873396 545299.267932
7535 150680 527730.154919 545397.498299
7536 150700 527827.438077 545495.730241
7537 150720 527924.722869 545593.963758
7538 150740 528022.009295 545692.198850
7539 150760 528119.297354 545790.435517
7540 150780 528216.587048 545888.673758
7541 150800 528313.878374 545986.913573
7542 150820 528411.171334 546085.154962
7543 150840 528508.465927 546183.397925
7544 150860 528605.762152 546281.642462
7545 150880 528703.060009 546379.888571
7546 150900 528800.359498 546478.136254
7547 150920 528897.660619 546576.385509
7548 150940 528994.963372 546674.636337
7549 150960 529092.267756 546772.888737
7550 150980 529189.573771 546871.142709
7551 151000 529286.881417 546969.398254
7552 151020 529384.190694 547067.655369
7553 151040 529481.501601 547165.914056
7554 151060 529578.814138 547264.174314
7555 151080 529676.128305 547362.436143
7556 151100 529773.444101 547460.699543
7557 151120 529870.761527 547558.964513
7558 151140 529968.080581 547657.231053
7559 151160 530065.401265 547755.499163
7560 151180 530162.723577 547853.768843
7561 151200 530260.047518 547952.040093
7562 151220 530357.373087 548050.312911
7563 151240 530454.700283 548148.587299
7564 151260 530552.029108 548246.863255
7565 151280 530649.359559 548345.140780
7566 151300 530746.691638 548443.419873
7567 151320 530844.025344 548541.700534
7568 151340 530941.360676 548639.982764
7569 151360 531038.697635 548738.266560
7570 151380 531136.036219 548836.551924
7571 151400 531233.376430 548934.838856
7572 151420 531330.718267 549033.127354
7573 151440 531428.061728 549131.417419
7574 151460 531525.406815 549229.709050
7575 151480 531622.753527 549328.002247
7576 151500 531720.101864 549426.297011
7577 151520 531817.451825 549524.593340
7578 151540 531914.803410 549622.891235
7579 151560 532012.156619 549721.190694
7580 151580 532109.511452 549819.491719
7581 151600 532206.867909 549917.794309
7582 151620 532304.225988 550016.098464
7583 151640 532401.585691 550114.404182
7584 151660 532498.947016 550212.711465
7585 151680 532596.309964 550311.020312
7586 151700 532693.674534 550409.330722
7587 151720 532791.040726 550507.642696
7588 151740 532888.408540 550605.956233
7589 151760 532985.777975 550704.271333
7590 151780 533083.149031 550802.587995
7591 151800 533180.521709 550900.906220
7592 151820 533277.896007 550999.226008
7593 151840 533375.271926 551097.547357
7594 151860 533472.649465 551195.870268
7595 151880 533570.028624 551294.194741
7596 151900 533667.409403 551392.520775
7597 151920 533764.791802 551490.848370
7598 151940 533862.175820 551589.177526
7599 151960 533959.561457 551687.508243
7600 151980 534056.948712 551785.840520
7601 152000 534154.337587 551884.174357
7602 152020 534251.728080 551982.509754
7603 152040 534349.120190 552080.846711
7604 152060 534446.513919 552179.185227
7605 152080 534543.909265 552277.525303
7606 152100 534641.306229 552375.866937
7607 152120 534738.704810 552474.210130
7608 152140 534836.105008 552572.554882
7609 152160 534933.506822 552670.901192
7610 152180 535030.910253 552769.249061
7611 152200 535128.315300 552867.598487
7612 152220 535225.721962 552965.949471
7613 152240 535323.130241 553064.302012
7614 152260 535420.540135 553162.656110
7615 152280 535517.951644 553261.011765
7616 152300 535615.364768 553359.368977
7617 152320 535712.779507 553457.727746
7618 152340 535810.195860 553556.088070
7619 152360 535907.613828 553654.449951
7620 152380 536005.033409 553752.813388
7621 152400 536102.454605 553851.178380
7622 152420 536199.877413 553949.544927
7623 152440 536297.301835 554047.913029
7624 152460 536394.727871 554146.282687
7625 152480 536492.155518 554244.653899
7626 152500 536589.584779 554343.026665
7627 152520 536687.015651 554441.400986
7628 152540 536784.448136 554539.776860
7629 152560 536881.882233 554638.154288
7630 152580 536979.317941 554736.533270
7631 152600 537076.755260 554834.913805
7632 152620 537174.194191 554933.295893
7633 152640 537271.634732 555031.679534
7634 152660 537369.076884 555130.064727
7635 152680 537466.520647 555228.451473
7636 152700 537563.966019 555326.839770
7637 152720 537661.413002 555425.229620
7638 152740 537758.861594 555523.621021
7639 152760 537856.311795 555622.013974
7640 152780 537953.763606 555720.408478
7641 152800 538051.217025 555818.804533
7642 152820 538148.672054 555917.202139
7643 152840 538246.128690 556015.601295
7644 152860 538343.586935 556114.002001
7645 152880 538441.046788 556212.404258
7646 152900 538538.508249 556310.808064
7647 152920 538635.971317 556409.213420
7648 152940 538733.435992 556507.620325
7649 152960 538830.902275 556606.028780
7650 152980 538928.370164 556704.438783
7651 153000 539025.839660 556802.850335
7652 153020 539123.310762 556901.263436
7653 153040 539220.783470 556999.678084
7654 153060 539318.257784 557098.094281
7655 153080 539415.733703 557196.512026
7656 153100 539513.211228 557294.931318
7657 153120 539610.690358 557393.352157
7658 153140 539708.171093 557491.774543
7659 153160 539805.653432 557590.198477
7660 153180 539903.137376 557688.623957
7661 153200 540000.622924 557787.050983
7662 153220 540098.110076 557885.479555
7663 153240 540195.598831 557983.909674
7664 153260 540293.089190 558082.341338
7665 153280 540390.581152 558180.774548
7666 153300 540488.074717 558279.209303
7667 153320 540585.569885 558377.645603
7668 153340 540683.066655 558476.083448
7669 153360 540780.565028 558574.522837
7670 153380 540878.065002 558672.963771
7671 153400 540975.566578 558771.406249
7672 153420 541073.069756 558869.850271
7673 153440 541170.574535 558968.295836
7674 153460 541268.080915 559066.742945
7675 153480 541365.588896 559165.191597
7676 153500 541463.098478 559263.641792
7677 153520 541560.609660 559362.093530
7678 153540 541658.122442 559460.546811
7679 153560 541755.636823 559559.001634
7680 153580 541853.152805 559657.457999
7681 153600 541950.670386 559755.915906
7682 153620 542048.189565 559854.375354
7683 153640 542145.710344 559952.836344
7684 153660 542243.232722 560051.298875
7685 153680 542340.756698 560149.762947
7686 153700 542438.282272 560248.228560
7687 153720 542535.809444 560346.695713
7688 153740 542633.338214 560445.164407
7689 153760 542730.868581 560543.634641
7690 153780 542828.400546 560642.106414
7691 153800 542925.934108 560740.579727
7692 153820 543023.469266 560839.054580
7693 153840 543121.006021 560937.530972
7694 153860 543218.544372 561036.008902
7695 153880 543316.084320 561134.488372
7696 153900 543413.625863 561232.969379
7697 153920 543511.169001 561331.451926
7698 153940 543608.713736 561429.936010
7699 153960 543706.260065 561528.421632
7700 153980 543803.807989 561626.908791
7701 154000 543901.357508 561725.397488
7702 154020 543998.908621 561823.887722
7703 154040 544096.461328 561922.379493
7704 154060 544194.015630 562020.872801
7705 154080 544291.571525 562119.367645
7706 154100 544389.129013 562217.864026
7707 154120 544486.688095 562316.361942
7708 154140 544584.248770 562414.861394
7709 154160 544681.811037 562513.362382
7710 154180 544779.374897 562611.864905
7711 154200 544876.940350 562710.368963
7712 154220 544974.507394 562808.874556
7713 154240 545072.076030 562907.381684
7714 154260 545169.646258 563005.890347
7715 154280 545267.218078 563104.400543
7716 154300 545364.791488 563202.912274
7717 154320 545462.366489 563301.425538
7718 154340 545559.943081 563399.940336
7719 154360 545657.521263 563498.456667
7720 154380 545755.101036 563596.974531
7721 154400 545852.682398 563695.493928
7722 154420 545950.265350 563794.014858
7723 154440 546047.849892 563892.537320
7724 154460 546145.436023 563991.061315
7725 154480 546243.023743 564089.586841
7726 154500 546340.613051 564188.113899
7727 154520 546438.203949 564286.642489
7728 154540 546535.796434 564385.172610
7729 154560 546633.390508 564483.704262
7730 154580 546730.986169 564582.237445
7731 154600 546828.583418 564680.772159
7732 154620 546926.182255 564779.308403
7733 154640 547023.782678 564877.846178
7734 154660 547121.384689 564976.385482
7735 154680 547218.988286 565074.926316
7736 154700 547316.593469 565173.468680
7737 154720 547414.200239 565272.012573
7738 154740 547511.808595 565370.557995
7739 154760 547609.418537 565469.104946
7740 154780 547707.030064 565567.653425
7741 154800 547804.643176 565666.203433
7742 154820 547902.257873 565764.754969
7743 154840 547999.874155 565863.308033
7744 154860 548097.492022 565961.862625
7745 154880 548195.111473 566060.418745
7746 154900 548292.732508 566158.976392
7747 154920 548390.355127 566257.535566
7748 154940 548487.979330 566356.096266
7749 154960 548585.605116 566454.658494
7750 154980 548683.232485 566553.222248
7751 155000 548780.861437 566651.787528
7752 155020 548878.491972 566750.354334
7753 155040 548976.124090 566848.922665
7754 155060 549073.757789 566947.492523
7755 155080 549171.393071 567046.063906
7756 155100 549269.029934 567144.636813
7757 155120 549366.668379 567243.211246
7758 155140 549464.308406 567341.787203
7759 155160 549561.950013 567440.364685
7760 155180 549659.593201 567538.943691
7761 155200 549757.237970 567637.524221
7762 155220 549854.884320 567736.106275
7763 155240 549952.532249 567834.689852
7764 155260 550050.181758 567933.274953
7765 155280 550147.832847 568031.861576
7766 155300 550245.485516 568130.449723
7767 155320 550343.139764 568229.039392
7768 155340 550440.795590 568327.630584
7769 155360 550538.452996 568426.223298
7770 155380 550636.111980 568524.817534
7771 155400 550733.772542 568623.413292
7772 155420 550831.434683 568722.010571
7773 155440 550929.098401 568820.609372
7774 155460 551026.763697 568919.209694
7775 155480 551124.430570 569017.811537
7776 155500 551222.099021 569116.414900
7777 155520 551319.769048 569215.019784
7778 155540 551417.440652 569313.626188
7779 155560 551515.113832 569412.234112
7780 155580 551612.788589 569510.843556
7781 155600 551710.464922 569609.454520
7782 155620 551808.142830 569708.067003
7783 155640 551905.822314 569806.681005
7784 155660 552003.503374 569905.296526
7785 155680 552101.186008 570003.913566
7786 155700 552198.870217 570102.532124
7787 155720 552296.556001 570201.152200
7788 155740 552394.243360 570299.773795
7789 155760 552491.932292 570398.396907
7790 155780 552589.622798 570497.021538
7791 155800 552687.314878 570595.647685
7792 155820 552785.008532 570694.275350
7793 155840 552882.703759 570792.904531
7794 155860 552980.400558 570891.535229
7795 155880 553078.098931 570990.167444
7796 155900 553175.798876 571088.801175
7797 155920 553273.500393 571187.436423
7798 155940 553371.203483 571286.073186
7799 155960 553468.908144 571384.711464
7800 155980 553566.614377 571483.351259
7801 156000 553664.322182 571581.992568
7802 156020 553762.031557 571680.635392
7803 156040 553859.742504 571779.279731
7804 156060 553957.455021 571877.925585
7805 156080 554055.169109 571976.572953
7806 156100 554152.884767 572075.221835
7807 156120 554250.601994 572173.872231
7808 156140 554348.320792 572272.524141
7809 156160 554446.041160 572371.177564
7810 156180 554543.763096 572469.832501
7811 156200 554641.486602 572568.488950
7812 156220 554739.211677 572667.146912
7813 156240 554836.938320 572765.806387
7814 156260 554934.666532 572864.467374
7815 156280 555032.396312 572963.129874
7816 156300 555130.127660 573061.793885
7817 156320 555227.860575 573160.459408
7818 156340 555325.595059 573259.126443
7819 156360 555423.331109 573357.794989
7820 156380 555521.068727 573456.465045
7821 156400 555618.807911 573555.136613
7822 156420 555716.548662 573653.809692
7823 156440 555814.290979 573752.484280
7824 156460 555912.034863 573851.160379
7825 156480 556009.780312 573949.837989
7826 156500 556107.527327 574048.517107
7827 156520 556205.275908 574147.197736
7828 156540 556303.026054 574245.879873
7829 156560 556400.777764 574344.563520
7830 156580 556498.531040 574443.248676
7831 156600 556596.285880 574541.935340
7832 156620 556694.042285 574640.623513
7833 156640 556791.800253 574739.313194
7834 156660 556889.559786 574838.004383
7835 156680 556987.320882 574936.697080
7836 156700 557085.083541 575035.391285
7837 156720 557182.847764 575134.086997
7838 156740 557280.613550 575232.784216
7839 156760 557378.380898 575331.482942
7840 156780 557476.149809 575430.183175
7841 156800 557573.920282 575528.884914
7842 156820 557671.692317 575627.588159
7843 156840 557769.465914 575726.292911
7844 156860 557867.241073 575824.999169
7845 156880 557965.017793 575923.706932
7846 156900 558062.796074 576022.416201
7847 156920 558160.575916 576121.126975
7848 156940 558258.357319 576219.839254
7849 156960 558356.140283 576318.553037
7850 156980 558453.924806 576417.268326
7851 157000 558551.710890 576515.985118
7852 157020 558649.498533 576614.703415
7853 157040 558747.287736 576713.423216
7854 157060 558845.078498 576812.144520
7855 157080 558942.870820 576910.867328
7856 157100 559040.664700 577009.591639
7857 157120 559138.460139 577108.317454
7858 157140 559236.257136 577207.044771
7859 157160 559334.055692 577305.773591
7860 157180 559431.855806 577404.503913
7861 157200 559529.657477 577503.235738
7862 157220 559627.460706 577601.969064
7863 157240 559725.265492 577700.703892
7864 157260 559823.071835 577799.440222
7865 157280 559920.879736 577898.178053
7866 157300 560018.689192 577996.917386
7867 157320 560116.500206 578095.658219
7868 157340 560214.312775 578194.400553
7869 157360 560312.126900 578293.144387
7870 157380 560409.942582 578391.889722
7871 157400 560507.759818 578490.636557
7872 157420 560605.578610 578589.384892
7873 157440 560703.398957 578688.134726
7874 157460 560801.220859 578786.886060
7875 157480 560899.044315 578885.638893
7876 157500 560996.869326 578984.393225
7877 157520 561094.695891 579083.149055
7878 157540 561192.524010 579181.906385
7879 157560 561290.353683 579280.665212
7880 157580 561388.184909 579379.425538
7881 157600 561486.017688 579478.187362
7882 157620 561583.852021 579576.950683
7883 157640 561681.687906 579675.715502
7884 157660 561779.525344 579774.481819
7885 157680 561877.364334 579873.249632
7886 157700 561975.204877 579972.018942
7887 157720 562073.046971 580070.789749
7888 157740 562170.890617 580169.562052
7889 157760 562268.735815 580268.335852
7890 157780 562366.582564 580367.111147
7891 157800 562464.430863 580465.887939
7892 157820 562562.280714 580564.666226
7893 157840 562660.132115 580663.446008
7894 157860 562757.985067 580762.227285
7895 157880 562855.839569 580861.010058
7896 157900 562953.695620 580959.794325
7897 157920 563051.553222 581058.580087
7898 157940 563149.412372 581157.367343
7899 157960 563247.273072 581256.156093
7900 157980 563345.135322 581354.946337
7901 158000 563442.999119 581453.738074
7902 158020 563540.864466 581552.531305
7903 158040 563638.731361 581651.326030
7904 158060 563736.599804 581750.122247
7905 158080 563834.469794 581848.919957
7906 158100 563932.341333 581947.719160
7907 158120 564030.214419 582046.519855
7908 158140 564128.089052 582145.322043
7909 158160 564225.965233 582244.125722
7910 158180 564323.842960 582342.930893
7911 158200 564421.722233 582441.737556
7912 158220 564519.603053 582540.545710
7913 158240 564617.485419 582639.355355
7914 158260 564715.369331 582738.166491
7915 158280 564813.254789 582836.979118
7916 158300 564911.141792 582935.793235
7917 158320 565009.030341 583034.608842
7918 158340 565106.920434 583133.425940
7919 158360 565204.812072 583232.244527
7920 158380 565302.705255 583331.064605
7921 158400 565400.599983 583429.886171
7922 158420 565498.496254 583528.709227
7923 158440 565596.394069 583627.533772
7924 158460 565694.293428 583726.359805
7925 158480 565792.194330 583825.187327
7926 158500 565890.096776 583924.016338
7927 158520 565988.000765 584022.846837
7928 158540 566085.906296 584121.678823
7929 158560 566183.813370 584220.512298
7930 158580 566281.721987 584319.347260
7931 158600 566379.632145 584418.183709
7932 158620 566477.543846 584517.021646
7933 158640 566575.457088 584615.861069
7934 158660 566673.371872 584714.701979
7935 158680 566771.288197 584813.544375
7936 158700 566869.206063 584912.388258
7937 158720 566967.125470 585011.233627
7938 158740 567065.046417 585110.080482
7939 158760 567162.968905 585208.928822
7940 158780 567260.892933 585307.778648
7941 158800 567358.818501 585406.629959
7942 158820 567456.745608 585505.482755
7943 158840 567554.674256 585604.337036
7944 158860 567652.604442 585703.192801
7945 158880 567750.536167 585802.050051
7946 158900 567848.469432 585900.908785
7947 158920 567946.404235 585999.769003
7948 158940 568044.340576 586098.630704
7949 158960 568142.278455 586197.493890
7950 158980 568240.217873 586296.358558
7951 159000 568338.158828 586395.224710
7952 159020 568436.101321 586494.092345
7953 159040 568534.045351 586592.961462
7954 159060 568631.990918 586691.832062
7955 159080 568729.938022 586790.704144
7956 159100 568827.886662 586889.577708
7957 159120 568925.836839 586988.452754
7958 159140 569023.788553 587087.329282
7959 159160 569121.741802 587186.207291
7960 159180 569219.696587 587285.086782
7961 159200 569317.652907 587383.967753
7962 159220 569415.610763 587482.850205
7963 159240 569513.570154 587581.734138
7964 159260 569611.531080 587680.619552
7965 159280 569709.493540 587779.506445
7966 159300 569807.457535 587878.394819
7967 159320 569905.423064 587977.284672
7968 159340 570003.390127 588076.176005
7969 159360 570101.358724 588175.068817
7970 159380 570199.328855 588273.963108
7971 159400 570297.300519 588372.858879
7972 159420 570395.273716 588471.756128
7973 159440 570493.248446 588570.654856
7974 159460 570591.224709 588669.555061
7975 159480 570689.202504 588768.456746
7976 159500 570787.181831 588867.359907
7977 159520 570885.162690 588966.264547
7978 159540 570983.145082 589065.170664
7979 159560 571081.129005 589164.078259
7980 159580 571179.114459 589262.987330
7981 159600 571277.101444 589361.897879
7982 159620 571375.089961 589460.809904
7983 159640 571473.080008 589559.723405
7984 159660 571571.071586 589658.638383
7985 159680 571669.064693 589757.554837
7986 159700 571767.059332 589856.472767
7987 159720 571865.055499 589955.392172
7988 159740 571963.053197 590054.313053
7989 159760 572061.052424 590153.235408
7990 159780 572159.053180 590252.159239
7991 159800 572257.055465 590351.084545
7992 159820 572355.059279 590450.011325
7993 159840 572453.064622 590548.939580
7994 159860 572551.071493 590647.869309
7995 159880 572649.079892 590746.800512
7996 159900 572747.089819 590845.733188
7997 159920 572845.101274 590944.667338
7998 159940 572943.114256 591043.602962
7999 159960 573041.128765 591142.540058
8000 159980 573139.144801 591241.478628
8001 160000 573237.162365 591340.418670

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