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 24 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
11502 230020 924499.581846 945557.955907
11503 230040 924601.935303 945661.087998
11504 230060 924704.289776 945764.221074
11505 230080 924806.645266 945867.355136
11506 230100 924909.001773 945970.490183
11507 230120 925011.359296 946073.626215
11508 230140 925113.717836 946176.763232
11509 230160 925216.077392 946279.901234
11510 230180 925318.437965 946383.040221
11511 230200 925420.799553 946486.180192
11512 230220 925523.162158 946589.321148
11513 230240 925625.525778 946692.463089
11514 230260 925727.890415 946795.606014
11515 230280 925830.256067 946898.749924
11516 230300 925932.622735 947001.894817
11517 230320 926034.990418 947105.040695
11518 230340 926137.359117 947208.187557
11519 230360 926239.728831 947311.335403
11520 230380 926342.099561 947414.484232
11521 230400 926444.471306 947517.634046
11522 230420 926546.844065 947620.784843
11523 230440 926649.217840 947723.936624
11524 230460 926751.592630 947827.089388
11525 230480 926853.968434 947930.243136
11526 230500 926956.345253 948033.397867
11527 230520 927058.723087 948136.553581
11528 230540 927161.101935 948239.710278
11529 230560 927263.481798 948342.867959
11530 230580 927365.862675 948446.026622
11531 230600 927468.244566 948549.186268
11532 230620 927570.627471 948652.346897
11533 230640 927673.011391 948755.508509
11534 230660 927775.396324 948858.671103
11535 230680 927877.782271 948961.834680
11536 230700 927980.169232 949064.999239
11537 230720 928082.557206 949168.164780
11538 230740 928184.946194 949271.331304
11539 230760 928287.336195 949374.498809
11540 230780 928389.727210 949477.667297
11541 230800 928492.119238 949580.836767
11542 230820 928594.512279 949684.007218
11543 230840 928696.906333 949787.178651
11544 230860 928799.301400 949890.351066
11545 230880 928901.697479 949993.524462
11546 230900 929004.094572 950096.698840
11547 230920 929106.492677 950199.874199
11548 230940 929208.891795 950303.050540
11549 230960 929311.291925 950406.227861
11550 230980 929413.693068 950509.406164
11551 231000 929516.095223 950612.585448
11552 231020 929618.498390 950715.765712
11553 231040 929720.902569 950818.946957
11554 231060 929823.307760 950922.129184
11555 231080 929925.713963 951025.312390
11556 231100 930028.121178 951128.496578
11557 231120 930130.529404 951231.681745
11558 231140 930232.938642 951334.867893
11559 231160 930335.348892 951438.055021
11560 231180 930437.760153 951541.243130
11561 231200 930540.172425 951644.432218
11562 231220 930642.585709 951747.622287
11563 231240 930745.000003 951850.813335
11564 231260 930847.415309 951954.005363
11565 231280 930949.831625 952057.198371
11566 231300 931052.248953 952160.392359
11567 231320 931154.667291 952263.587326
11568 231340 931257.086639 952366.783272
11569 231360 931359.506998 952469.980198
11570 231380 931461.928368 952573.178102
11571 231400 931564.350748 952676.376987
11572 231420 931666.774138 952779.576850
11573 231440 931769.198538 952882.777692
11574 231460 931871.623949 952985.979513
11575 231480 931974.050369 953089.182312
11576 231500 932076.477799 953192.386091
11577 231520 932178.906239 953295.590848
11578 231540 932281.335689 953398.796583
11579 231560 932383.766148 953502.003297
11580 231580 932486.197616 953605.210989
11581 231600 932588.630094 953708.419659
11582 231620 932691.063582 953811.629308
11583 231640 932793.498078 953914.839934
11584 231660 932895.933584 954018.051539
11585 231680 932998.370098 954121.264121
11586 231700 933100.807622 954224.477681
11587 231720 933203.246154 954327.692219
11588 231740 933305.685695 954430.907734
11589 231760 933408.126244 954534.124227
11590 231780 933510.567802 954637.341697
11591 231800 933613.010369 954740.560145
11592 231820 933715.453943 954843.779570
11593 231840 933817.898526 954946.999971
11594 231860 933920.344117 955050.221350
11595 231880 934022.790716 955153.443706
11596 231900 934125.238323 955256.667038
11597 231920 934227.686938 955359.891348
11598 231940 934330.136561 955463.116634
11599 231960 934432.587191 955566.342896
11600 231980 934535.038829 955669.570135
11601 232000 934637.491474 955772.798351
11602 232020 934739.945127 955876.027542
11603 232040 934842.399787 955979.257710
11604 232060 934944.855454 956082.488854
11605 232080 935047.312128 956185.720974
11606 232100 935149.769809 956288.954070
11607 232120 935252.228497 956392.188142
11608 232140 935354.688191 956495.423189
11609 232160 935457.148893 956598.659212
11610 232180 935559.610601 956701.896211
11611 232200 935662.073315 956805.134185
11612 232220 935764.537036 956908.373134
11613 232240 935867.001763 957011.613059
11614 232260 935969.467497 957114.853959
11615 232280 936071.934236 957218.095834
11616 232300 936174.401982 957321.338684
11617 232320 936276.870733 957424.582509
11618 232340 936379.340490 957527.827308
11619 232360 936481.811253 957631.073083
11620 232380 936584.283022 957734.319832
11621 232400 936686.755796 957837.567556
11622 232420 936789.229576 957940.816254
11623 232440 936891.704361 958044.065926
11624 232460 936994.180151 958147.316573
11625 232480 937096.656946 958250.568194
11626 232500 937199.134746 958353.820789
11627 232520 937301.613552 958457.074357
11628 232540 937404.093362 958560.328900
11629 232560 937506.574177 958663.584417
11630 232580 937609.055997 958766.840907
11631 232600 937711.538821 958870.098371
11632 232620 937814.022650 958973.356809
11633 232640 937916.507483 959076.616220
11634 232660 938018.993320 959179.876604
11635 232680 938121.480162 959283.137962
11636 232700 938223.968008 959386.400293
11637 232720 938326.456857 959489.663596
11638 232740 938428.946711 959592.927873
11639 232760 938531.437569 959696.193123
11640 232780 938633.929430 959799.459345
11641 232800 938736.422295 959902.726541
11642 232820 938838.916163 960005.994709
11643 232840 938941.411035 960109.263849
11644 232860 939043.906910 960212.533962
11645 232880 939146.403788 960315.805047
11646 232900 939248.901670 960419.077104
11647 232920 939351.400555 960522.350134
11648 232940 939453.900442 960625.624136
11649 232960 939556.401333 960728.899109
11650 232980 939658.903226 960832.175055
11651 233000 939761.406122 960935.451972
11652 233020 939863.910020 961038.729861
11653 233040 939966.414921 961142.008722
11654 233060 940068.920825 961245.288554
11655 233080 940171.427730 961348.569358
11656 233100 940273.935638 961451.851133
11657 233120 940376.444548 961555.133879
11658 233140 940478.954460 961658.417597
11659 233160 940581.465374 961761.702285
11660 233180 940683.977290 961864.987945
11661 233200 940786.490208 961968.274575
11662 233220 940889.004127 962071.562176
11663 233240 940991.519047 962174.850748
11664 233260 941094.034969 962278.140291
11665 233280 941196.551893 962381.430804
11666 233300 941299.069818 962484.722287
11667 233320 941401.588744 962588.014741
11668 233340 941504.108670 962691.308165
11669 233360 941606.629598 962794.602559
11670 233380 941709.151527 962897.897924
11671 233400 941811.674457 963001.194258
11672 233420 941914.198387 963104.491562
11673 233440 942016.723318 963207.789836
11674 233460 942119.249249 963311.089080
11675 233480 942221.776181 963414.389293
11676 233500 942324.304113 963517.690476
11677 233520 942426.833045 963620.992629
11678 233540 942529.362978 963724.295750
11679 233560 942631.893910 963827.599841
11680 233580 942734.425843 963930.904901
11681 233600 942836.958775 964034.210931
11682 233620 942939.492707 964137.517929
11683 233640 943042.027638 964240.825896
11684 233660 943144.563570 964344.134832
11685 233680 943247.100500 964447.444736
11686 233700 943349.638430 964550.755610
11687 233720 943452.177359 964654.067451
11688 233740 943554.717288 964757.380262
11689 233760 943657.258215 964860.694040
11690 233780 943759.800142 964964.008787
11691 233800 943862.343067 965067.324502
11692 233820 943964.886992 965170.641185
11693 233840 944067.431914 965273.958836
11694 233860 944169.977836 965377.277455
11695 233880 944272.524756 965480.597042
11696 233900 944375.072674 965583.917596
11697 233920 944477.621591 965687.239119
11698 233940 944580.171506 965790.561608
11699 233960 944682.722419 965893.885065
11700 233980 944785.274331 965997.209490
11701 234000 944887.827240 966100.534882
11702 234020 944990.381147 966203.861241
11703 234040 945092.936052 966307.188567
11704 234060 945195.491954 966410.516860
11705 234080 945298.048854 966513.846120
11706 234100 945400.606752 966617.176346
11707 234120 945503.165646 966720.507540
11708 234140 945605.725539 966823.839700
11709 234160 945708.286428 966927.172827
11710 234180 945810.848315 967030.506920
11711 234200 945913.411198 967133.841980
11712 234220 946015.975078 967237.178005
11713 234240 946118.539956 967340.514997
11714 234260 946221.105830 967443.852955
11715 234280 946323.672700 967547.191880
11716 234300 946426.240567 967650.531770
11717 234320 946528.809431 967753.872625
11718 234340 946631.379291 967857.214447
11719 234360 946733.950147 967960.557234
11720 234380 946836.522000 968063.900987
11721 234400 946939.094848 968167.245705
11722 234420 947041.668693 968270.591389
11723 234440 947144.243533 968373.938038
11724 234460 947246.819369 968477.285652
11725 234480 947349.396201 968580.634232
11726 234500 947451.974028 968683.983776
11727 234520 947554.552851 968787.334285
11728 234540 947657.132670 968890.685759
11729 234560 947759.713483 968994.038198
11730 234580 947862.295292 969097.391601
11731 234600 947964.878096 969200.745969
11732 234620 948067.461895 969304.101302
11733 234640 948170.046689 969407.457599
11734 234660 948272.632478 969510.814860
11735 234680 948375.219262 969614.173086
11736 234700 948477.807040 969717.532275
11737 234720 948580.395813 969820.892429
11738 234740 948682.985581 969924.253546
11739 234760 948785.576342 970027.615628
11740 234780 948888.168098 970130.978673
11741 234800 948990.760849 970234.342682
11742 234820 949093.354593 970337.707654
11743 234840 949195.949331 970441.073590
11744 234860 949298.545064 970544.440490
11745 234880 949401.141790 970647.808352
11746 234900 949503.739510 970751.177178
11747 234920 949606.338223 970854.546967
11748 234940 949708.937930 970957.917719
11749 234960 949811.538631 971061.289434
11750 234980 949914.140325 971164.662112
11751 235000 950016.743012 971268.035753
11752 235020 950119.346692 971371.410356
11753 235040 950221.951366 971474.785922
11754 235060 950324.557032 971578.162451
11755 235080 950427.163692 971681.539942
11756 235100 950529.771344 971784.918395
11757 235120 950632.379988 971888.297810
11758 235140 950734.989626 971991.678188
11759 235160 950837.600256 972095.059528
11760 235180 950940.211878 972198.441830
11761 235200 951042.824493 972301.825093
11762 235220 951145.438100 972405.209319
11763 235240 951248.052699 972508.594506
11764 235260 951350.668290 972611.980654
11765 235280 951453.284873 972715.367765
11766 235300 951555.902448 972818.755836
11767 235320 951658.521015 972922.144869
11768 235340 951761.140574 973025.534864
11769 235360 951863.761124 973128.925819
11770 235380 951966.382665 973232.317736
11771 235400 952069.005198 973335.710613
11772 235420 952171.628723 973439.104452
11773 235440 952274.253238 973542.499251
11774 235460 952376.878745 973645.895011
11775 235480 952479.505242 973749.291732
11776 235500 952582.132731 973852.689413
11777 235520 952684.761210 973956.088055
11778 235540 952787.390681 974059.487657
11779 235560 952890.021142 974162.888219
11780 235580 952992.652593 974266.289742
11781 235600 953095.285035 974369.692225
11782 235620 953197.918467 974473.095667
11783 235640 953300.552890 974576.500070
11784 235660 953403.188303 974679.905432
11785 235680 953505.824706 974783.311755
11786 235700 953608.462099 974886.719037
11787 235720 953711.100482 974990.127278
11788 235740 953813.739854 975093.536479
11789 235760 953916.380217 975196.946639
11790 235780 954019.021569 975300.357759
11791 235800 954121.663911 975403.769838
11792 235820 954224.307242 975507.182876
11793 235840 954326.951562 975610.596873
11794 235860 954429.596872 975714.011829
11795 235880 954532.243171 975817.427744
11796 235900 954634.890459 975920.844617
11797 235920 954737.538736 976024.262450
11798 235940 954840.188002 976127.681240
11799 235960 954942.838256 976231.100990
11800 235980 955045.489500 976334.521698
11801 236000 955148.141732 976437.943364
11802 236020 955250.794953 976541.365988
11803 236040 955353.449162 976644.789571
11804 236060 955456.104359 976748.214112
11805 236080 955558.760545 976851.639610
11806 236100 955661.417719 976955.066067
11807 236120 955764.075881 977058.493481
11808 236140 955866.735031 977161.921853
11809 236160 955969.395168 977265.351183
11810 236180 956072.056294 977368.781470
11811 236200 956174.718407 977472.212715
11812 236220 956277.381508 977575.644917
11813 236240 956380.045597 977679.078076
11814 236260 956482.710672 977782.512192
11815 236280 956585.376736 977885.947266
11816 236300 956688.043786 977989.383297
11817 236320 956790.711824 978092.820284
11818 236340 956893.380848 978196.258228
11819 236360 956996.050860 978299.697129
11820 236380 957098.721858 978403.136987
11821 236400 957201.393844 978506.577801
11822 236420 957304.066816 978610.019572
11823 236440 957406.740774 978713.462299
11824 236460 957509.415719 978816.905983
11825 236480 957612.091651 978920.350622
11826 236500 957714.768569 979023.796218
11827 236520 957817.446473 979127.242770
11828 236540 957920.125363 979230.690278
11829 236560 958022.805239 979334.138741
11830 236580 958125.486101 979437.588161
11831 236600 958228.167949 979541.038536
11832 236620 958330.850783 979644.489867
11833 236640 958433.534603 979747.942153
11834 236660 958536.219408 979851.395394
11835 236680 958638.905198 979954.849591
11836 236700 958741.591974 980058.304743
11837 236720 958844.279736 980161.760851
11838 236740 958946.968482 980265.217913
11839 236760 959049.658214 980368.675931
11840 236780 959152.348931 980472.134903
11841 236800 959255.040632 980575.594830
11842 236820 959357.733319 980679.055712
11843 236840 959460.426990 980782.517548
11844 236860 959563.121646 980885.980339
11845 236880 959665.817287 980989.444085
11846 236900 959768.513912 981092.908784
11847 236920 959871.211521 981196.374438
11848 236940 959973.910115 981299.841047
11849 236960 960076.609693 981403.308609
11850 236980 960179.310255 981506.777125
11851 237000 960282.011801 981610.246596
11852 237020 960384.714331 981713.717020
11853 237040 960487.417845 981817.188398
11854 237060 960590.122342 981920.660729
11855 237080 960692.827824 982024.134014
11856 237100 960795.534288 982127.608253
11857 237120 960898.241737 982231.083445
11858 237140 961000.950168 982334.559590
11859 237160 961103.659583 982438.036689
11860 237180 961206.369982 982541.514740
11861 237200 961309.081363 982644.993745
11862 237220 961411.793727 982748.473703
11863 237240 961514.507075 982851.954613
11864 237260 961617.221405 982955.436476
11865 237280 961719.936718 983058.919293
11866 237300 961822.653013 983162.403061
11867 237320 961925.370291 983265.887782
11868 237340 962028.088552 983369.373456
11869 237360 962130.807795 983472.860082
11870 237380 962233.528020 983576.347660
11871 237400 962336.249228 983679.836190
11872 237420 962438.971417 983783.325673
11873 237440 962541.694589 983886.816107
11874 237460 962644.418743 983990.307494
11875 237480 962747.143878 984093.799832
11876 237500 962849.869995 984197.293122
11877 237520 962952.597094 984300.787363
11878 237540 963055.325174 984404.282557
11879 237560 963158.054236 984507.778701
11880 237580 963260.784279 984611.275797
11881 237600 963363.515304 984714.773845
11882 237620 963466.247310 984818.272843
11883 237640 963568.980296 984921.772793
11884 237660 963671.714264 985025.273693
11885 237680 963774.449213 985128.775545
11886 237700 963877.185143 985232.278347
11887 237720 963979.922053 985335.782101
11888 237740 964082.659944 985439.286805
11889 237760 964185.398815 985542.792459
11890 237780 964288.138667 985646.299064
11891 237800 964390.879500 985749.806620
11892 237820 964493.621313 985853.315126
11893 237840 964596.364106 985956.824582
11894 237860 964699.107879 986060.334988
11895 237880 964801.852632 986163.846344
11896 237900 964904.598365 986267.358651
11897 237920 965007.345077 986370.871907
11898 237940 965110.092770 986474.386113
11899 237960 965212.841442 986577.901269
11900 237980 965315.591094 986681.417374
11901 238000 965418.341725 986784.934429
11902 238020 965521.093336 986888.452434
11903 238040 965623.845926 986991.971388
11904 238060 965726.599495 987095.491291
11905 238080 965829.354043 987199.012143
11906 238100 965932.109570 987302.533944
11907 238120 966034.866076 987406.056695
11908 238140 966137.623561 987509.580394
11909 238160 966240.382025 987613.105043
11910 238180 966343.141468 987716.630640
11911 238200 966445.901889 987820.157186
11912 238220 966548.663288 987923.684680
11913 238240 966651.425666 988027.213123
11914 238260 966754.189022 988130.742514
11915 238280 966856.953357 988234.272854
11916 238300 966959.718669 988337.804142
11917 238320 967062.484960 988441.336378
11918 238340 967165.252228 988544.869562
11919 238360 967268.020474 988648.403694
11920 238380 967370.789698 988751.938774
11921 238400 967473.559900 988855.474802
11922 238420 967576.331079 988959.011778
11923 238440 967679.103236 989062.549701
11924 238460 967781.876370 989166.088572
11925 238480 967884.650482 989269.628390
11926 238500 967987.425570 989373.169156
11927 238520 968090.201636 989476.710869
11928 238540 968192.978679 989580.253529
11929 238560 968295.756699 989683.797136
11930 238580 968398.535695 989787.341691
11931 238600 968501.315669 989890.887192
11932 238620 968604.096619 989994.433640
11933 238640 968706.878545 990097.981035
11934 238660 968809.661448 990201.529377
11935 238680 968912.445328 990305.078665
11936 238700 969015.230184 990408.628900
11937 238720 969118.016016 990512.180081
11938 238740 969220.802824 990615.732209
11939 238760 969323.590608 990719.285283
11940 238780 969426.379368 990822.839303
11941 238800 969529.169104 990926.394269
11942 238820 969631.959816 991029.950181
11943 238840 969734.751504 991133.507039
11944 238860 969837.544167 991237.064843
11945 238880 969940.337805 991340.623593
11946 238900 970043.132419 991444.183288
11947 238920 970145.928008 991547.743929
11948 238940 970248.724573 991651.305515
11949 238960 970351.522113 991754.868047
11950 238980 970454.320627 991858.431524
11951 239000 970557.120117 991961.995946
11952 239020 970659.920581 992065.561314
11953 239040 970762.722021 992169.127626
11954 239060 970865.524435 992272.694884
11955 239080 970968.327823 992376.263086
11956 239100 971071.132186 992479.832233
11957 239120 971173.937524 992583.402325
11958 239140 971276.743836 992686.973361
11959 239160 971379.551122 992790.545342
11960 239180 971482.359382 992894.118268
11961 239200 971585.168616 992997.692138
11962 239220 971687.978825 993101.266952
11963 239240 971790.790007 993204.842710
11964 239260 971893.602163 993308.419413
11965 239280 971996.415293 993411.997059
11966 239300 972099.229396 993515.575649
11967 239320 972202.044473 993619.155184
11968 239340 972304.860523 993722.735662
11969 239360 972407.677546 993826.317083
11970 239380 972510.495543 993929.899449
11971 239400 972613.314513 994033.482757
11972 239420 972716.134456 994137.067010
11973 239440 972818.955372 994240.652205
11974 239460 972921.777261 994344.238344
11975 239480 973024.600123 994447.825426
11976 239500 973127.423958 994551.413451
11977 239520 973230.248765 994655.002419
11978 239540 973333.074544 994758.592330
11979 239560 973435.901296 994862.183184
11980 239580 973538.729021 994965.774980
11981 239600 973641.557718 995069.367719
11982 239620 973744.387386 995172.961401
11983 239640 973847.218027 995276.556025
11984 239660 973950.049640 995380.151592
11985 239680 974052.882225 995483.748100
11986 239700 974155.715782 995587.345552
11987 239720 974258.550311 995690.943945
11988 239740 974361.385811 995794.543280
11989 239760 974464.222282 995898.143557
11990 239780 974567.059725 996001.744776
11991 239800 974669.898140 996105.346937
11992 239820 974772.737526 996208.950040
11993 239840 974875.577883 996312.554084
11994 239860 974978.419211 996416.159070
11995 239880 975081.261510 996519.764997
11996 239900 975184.104780 996623.371866
11997 239920 975286.949021 996726.979676
11998 239940 975389.794233 996830.588427
11999 239960 975492.640415 996934.198119
12000 239980 975595.487568 997037.808752
12001 240000 975698.335691 997141.420326

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