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 20 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
9502 190020 721971.633163 741403.667195
9503 190040 722071.728914 741504.614050
9504 190060 722171.825922 741605.562121
9505 190080 722271.924188 741706.511407
9506 190100 722372.023711 741807.461909
9507 190120 722472.124491 741908.413625
9508 190140 722572.226528 742009.366557
9509 190160 722672.329821 742110.320704
9510 190180 722772.434372 742211.276065
9511 190200 722872.540178 742312.232642
9512 190220 722972.647241 742413.190432
9513 190240 723072.755560 742514.149437
9514 190260 723172.865135 742615.109656
9515 190280 723272.975965 742716.071090
9516 190300 723373.088052 742817.033737
9517 190320 723473.201394 742917.997598
9518 190340 723573.315991 743018.962673
9519 190360 723673.431843 743119.928961
9520 190380 723773.548951 743220.896462
9521 190400 723873.667313 743321.865177
9522 190420 723973.786931 743422.835105
9523 190440 724073.907803 743523.806246
9524 190460 724174.029929 743624.778599
9525 190480 724274.153310 743725.752165
9526 190500 724374.277945 743826.726944
9527 190520 724474.403834 743927.702935
9528 190540 724574.530977 744028.680139
9529 190560 724674.659374 744129.658554
9530 190580 724774.789024 744230.638182
9531 190600 724874.919928 744331.619021
9532 190620 724975.052085 744432.601072
9533 190640 725075.185496 744533.584334
9534 190660 725175.320159 744634.568808
9535 190680 725275.456075 744735.554493
9536 190700 725375.593244 744836.541390
9537 190720 725475.731666 744937.529497
9538 190740 725575.871339 745038.518815
9539 190760 725676.012266 745139.509344
9540 190780 725776.154444 745240.501083
9541 190800 725876.297874 745341.494033
9542 190820 725976.442557 745442.488193
9543 190840 726076.588491 745543.483563
9544 190860 726176.735676 745644.480143
9545 190880 726276.884113 745745.477933
9546 190900 726377.033801 745846.476932
9547 190920 726477.184740 745947.477142
9548 190940 726577.336931 746048.478560
9549 190960 726677.490372 746149.481188
9550 190980 726777.645063 746250.485025
9551 191000 726877.801006 746351.490072
9552 191020 726977.958198 746452.496327
9553 191040 727078.116641 746553.503790
9554 191060 727178.276334 746654.512463
9555 191080 727278.437277 746755.522343
9556 191100 727378.599470 746856.533432
9557 191120 727478.762912 746957.545730
9558 191140 727578.927604 747058.559235
9559 191160 727679.093545 747159.573948
9560 191180 727779.260735 747260.589869
9561 191200 727879.429175 747361.606997
9562 191220 727979.598863 747462.625333
9563 191240 728079.769800 747563.644877
9564 191260 728179.941986 747664.665627
9565 191280 728280.115420 747765.687585
9566 191300 728380.290103 747866.710749
9567 191320 728480.466034 747967.735120
9568 191340 728580.643212 748068.760698
9569 191360 728680.821639 748169.787483
9570 191380 728781.001314 748270.815473
9571 191400 728881.182236 748371.844670
9572 191420 728981.364405 748472.875073
9573 191440 729081.547822 748573.906682
9574 191460 729181.732486 748674.939497
9575 191480 729281.918397 748775.973517
9576 191500 729382.105555 748877.008743
9577 191520 729482.293959 748978.045175
9578 191540 729582.483611 749079.082811
9579 191560 729682.674508 749180.121653
9580 191580 729782.866652 749281.161699
9581 191600 729883.060042 749382.202951
9582 191620 729983.254678 749483.245407
9583 191640 730083.450560 749584.289067
9584 191660 730183.647688 749685.333932
9585 191680 730283.846061 749786.380002
9586 191700 730384.045679 749887.427275
9587 191720 730484.246543 749988.475753
9588 191740 730584.448652 750089.525434
9589 191760 730684.652006 750190.576319
9590 191780 730784.856605 750291.628407
9591 191800 730885.062448 750392.681699
9592 191820 730985.269537 750493.736195
9593 191840 731085.477869 750594.791893
9594 191860 731185.687446 750695.848794
9595 191880 731285.898266 750796.906899
9596 191900 731386.110331 750897.966206
9597 191920 731486.323640 750999.026715
9598 191940 731586.538192 751100.088427
9599 191960 731686.753988 751201.151342
9600 191980 731786.971027 751302.215459
9601 192000 731887.189309 751403.280777
9602 192020 731987.408835 751504.347298
9603 192040 732087.629603 751605.415020
9604 192060 732187.851615 751706.483944
9605 192080 732288.074869 751807.554069
9606 192100 732388.299365 751908.625396
9607 192120 732488.525104 752009.697924
9608 192140 732588.752085 752110.771653
9609 192160 732688.980308 752211.846583
9610 192180 732789.209773 752312.922714
9611 192200 732889.440480 752414.000046
9612 192220 732989.672428 752515.078578
9613 192240 733089.905618 752616.158310
9614 192260 733190.140050 752717.239243
9615 192280 733290.375722 752818.321376
9616 192300 733390.612636 752919.404708
9617 192320 733490.850790 753020.489241
9618 192340 733591.090186 753121.574973
9619 192360 733691.330822 753222.661905
9620 192380 733791.572698 753323.750036
9621 192400 733891.815815 753424.839367
9622 192420 733992.060172 753525.929896
9623 192440 734092.305769 753627.021625
9624 192460 734192.552606 753728.114552
9625 192480 734292.800683 753829.208679
9626 192500 734393.049999 753930.304003
9627 192520 734493.300555 754031.400527
9628 192540 734593.552350 754132.498248
9629 192560 734693.805385 754233.597168
9630 192580 734794.059658 754334.697286
9631 192600 734894.315171 754435.798602
9632 192620 734994.571922 754536.901115
9633 192640 735094.829912 754638.004826
9634 192660 735195.089140 754739.109735
9635 192680 735295.349606 754840.215841
9636 192700 735395.611311 754941.323144
9637 192720 735495.874254 755042.431644
9638 192740 735596.138435 755143.541342
9639 192760 735696.403853 755244.652236
9640 192780 735796.670509 755345.764326
9641 192800 735896.938403 755446.877614
9642 192820 735997.207534 755547.992097
9643 192840 736097.477902 755649.107777
9644 192860 736197.749507 755750.224653
9645 192880 736298.022349 755851.342725
9646 192900 736398.296427 755952.461993
9647 192920 736498.571743 756053.582456
9648 192940 736598.848295 756154.704115
9649 192960 736699.126083 756255.826970
9650 192980 736799.405107 756356.951020
9651 193000 736899.685367 756458.076265
9652 193020 736999.966864 756559.202705
9653 193040 737100.249596 756660.330340
9654 193060 737200.533563 756761.459169
9655 193080 737300.818766 756862.589193
9656 193100 737401.105205 756963.720412
9657 193120 737501.392878 757064.852825
9658 193140 737601.681787 757165.986432
9659 193160 737701.971930 757267.121234
9660 193180 737802.263308 757368.257229
9661 193200 737902.555921 757469.394418
9662 193220 738002.849769 757570.532801
9663 193240 738103.144850 757671.672377
9664 193260 738203.441166 757772.813146
9665 193280 738303.738716 757873.955109
9666 193300 738404.037500 757975.098265
9667 193320 738504.337517 758076.242614
9668 193340 738604.638768 758177.388156
9669 193360 738704.941253 758278.534890
9670 193380 738805.244970 758379.682817
9671 193400 738905.549921 758480.831937
9672 193420 739005.856105 758581.982248
9673 193440 739106.163522 758683.133752
9674 193460 739206.472172 758784.286448
9675 193480 739306.782054 758885.440336
9676 193500 739407.093169 758986.595415
9677 193520 739507.405516 759087.751687
9678 193540 739607.719096 759188.909149
9679 193560 739708.033907 759290.067803
9680 193580 739808.349950 759391.227648
9681 193600 739908.667225 759492.388684
9682 193620 740008.985732 759593.550912
9683 193640 740109.305470 759694.714329
9684 193660 740209.626439 759795.878938
9685 193680 740309.948639 759897.044737
9686 193700 740410.272071 759998.211727
9687 193720 740510.596733 760099.379906
9688 193740 740610.922627 760200.549276
9689 193760 740711.249751 760301.719836
9690 193780 740811.578105 760402.891586
9691 193800 740911.907689 760504.064525
9692 193820 741012.238504 760605.238654
9693 193840 741112.570549 760706.413972
9694 193860 741212.903824 760807.590480
9695 193880 741313.238329 760908.768177
9696 193900 741413.574063 761009.947063
9697 193920 741513.911026 761111.127137
9698 193940 741614.249219 761212.308401
9699 193960 741714.588642 761313.490853
9700 193980 741814.929293 761414.674494
9701 194000 741915.271173 761515.859323
9702 194020 742015.614282 761617.045340
9703 194040 742115.958619 761718.232545
9704 194060 742216.304185 761819.420938
9705 194080 742316.650980 761920.610519
9706 194100 742416.999002 762021.801288
9707 194120 742517.348253 762122.993244
9708 194140 742617.698732 762224.186388
9709 194160 742718.050438 762325.380719
9710 194180 742818.403372 762426.576237
9711 194200 742918.757534 762527.772942
9712 194220 743019.112922 762628.970833
9713 194240 743119.469538 762730.169912
9714 194260 743219.827382 762831.370177
9715 194280 743320.186452 762932.571629
9716 194300 743420.546749 763033.774267
9717 194320 743520.908272 763134.978091
9718 194340 743621.271022 763236.183101
9719 194360 743721.634999 763337.389297
9720 194380 743822.000201 763438.596679
9721 194400 743922.366630 763539.805247
9722 194420 744022.734284 763641.015000
9723 194440 744123.103165 763742.225938
9724 194460 744223.473271 763843.438062
9725 194480 744323.844603 763944.651371
9726 194500 744424.217160 764045.865865
9727 194520 744524.590942 764147.081543
9728 194540 744624.965949 764248.298407
9729 194560 744725.342182 764349.516454
9730 194580 744825.719639 764450.735687
9731 194600 744926.098321 764551.956103
9732 194620 745026.478227 764653.177704
9733 194640 745126.859358 764754.400489
9734 194660 745227.241713 764855.624458
9735 194680 745327.625292 764956.849610
9736 194700 745428.010095 765058.075946
9737 194720 745528.396122 765159.303466
9738 194740 745628.783373 765260.532169
9739 194760 745729.171847 765361.762055
9740 194780 745829.561544 765462.993124
9741 194800 745929.952465 765564.225377
9742 194820 746030.344609 765665.458812
9743 194840 746130.737976 765766.693429
9744 194860 746231.132566 765867.929230
9745 194880 746331.528379 765969.166213
9746 194900 746431.925414 766070.404378
9747 194920 746532.323671 766171.643725
9748 194940 746632.723151 766272.884254
9749 194960 746733.123853 766374.125965
9750 194980 746833.525777 766475.368858
9751 195000 746933.928923 766576.612932
9752 195020 747034.333291 766677.858188
9753 195040 747134.738880 766779.104625
9754 195060 747235.145690 766880.352244
9755 195080 747335.553722 766981.601044
9756 195100 747435.962975 767082.851024
9757 195120 747536.373450 767184.102185
9758 195140 747636.785145 767285.354527
9759 195160 747737.198060 767386.608050
9760 195180 747837.612197 767487.862753
9761 195200 747938.027554 767589.118636
9762 195220 748038.444131 767690.375700
9763 195240 748138.861928 767791.633943
9764 195260 748239.280946 767892.893366
9765 195280 748339.701183 767994.153969
9766 195300 748440.122640 768095.415752
9767 195320 748540.545317 768196.678714
9768 195340 748640.969213 768297.942856
9769 195360 748741.394329 768399.208176
9770 195380 748841.820664 768500.474676
9771 195400 748942.248218 768601.742355
9772 195420 749042.676991 768703.011212
9773 195440 749143.106982 768804.281249
9774 195460 749243.538192 768905.552463
9775 195480 749343.970621 769006.824856
9776 195500 749444.404268 769108.098428
9777 195520 749544.839134 769209.373177
9778 195540 749645.275217 769310.649105
9779 195560 749745.712519 769411.926211
9780 195580 749846.151038 769513.204494
9781 195600 749946.590775 769614.483955
9782 195620 750047.031730 769715.764593
9783 195640 750147.473902 769817.046409
9784 195660 750247.917291 769918.329401
9785 195680 750348.361897 770019.613571
9786 195700 750448.807721 770120.898918
9787 195720 750549.254761 770222.185442
9788 195740 750649.703018 770323.473142
9789 195760 750750.152491 770424.762019
9790 195780 750850.603181 770526.052073
9791 195800 750951.055087 770627.343302
9792 195820 751051.508209 770728.635708
9793 195840 751151.962548 770829.929290
9794 195860 751252.418102 770931.224048
9795 195880 751352.874872 771032.519981
9796 195900 751453.332858 771133.817090
9797 195920 751553.792059 771235.115375
9798 195940 751654.252475 771336.414835
9799 195960 751754.714106 771437.715470
9800 195980 751855.176953 771539.017280
9801 196000 751955.641014 771640.320266
9802 196020 752056.106291 771741.624426
9803 196040 752156.572782 771842.929760
9804 196060 752257.040487 771944.236270
9805 196080 752357.509407 772045.543953
9806 196100 752457.979541 772146.852811
9807 196120 752558.450889 772248.162844
9808 196140 752658.923450 772349.474050
9809 196160 752759.397226 772450.786430
9810 196180 752859.872216 772552.099984
9811 196200 752960.348419 772653.414711
9812 196220 753060.825835 772754.730612
9813 196240 753161.304464 772856.047687
9814 196260 753261.784307 772957.365934
9815 196280 753362.265363 773058.685355
9816 196300 753462.747631 773160.005949
9817 196320 753563.231112 773261.327716
9818 196340 753663.715806 773362.650655
9819 196360 753764.201712 773463.974767
9820 196380 753864.688831 773565.300051
9821 196400 753965.177161 773666.626508
9822 196420 754065.666704 773767.954136
9823 196440 754166.157458 773869.282937
9824 196460 754266.649424 773970.612910
9825 196480 754367.142602 774071.944055
9826 196500 754467.636991 774173.276371
9827 196520 754568.132592 774274.609859
9828 196540 754668.629403 774375.944518
9829 196560 754769.127426 774477.280348
9830 196580 754869.626660 774578.617350
9831 196600 754970.127104 774679.955522
9832 196620 755070.628759 774781.294866
9833 196640 755171.131625 774882.635380
9834 196660 755271.635701 774983.977065
9835 196680 755372.140987 775085.319920
9836 196700 755472.647483 775186.663946
9837 196720 755573.155189 775288.009141
9838 196740 755673.664105 775389.355507
9839 196760 755774.174230 775490.703043
9840 196780 755874.685565 775592.051749
9841 196800 755975.198110 775693.401624
9842 196820 756075.711864 775794.752669
9843 196840 756176.226827 775896.104883
9844 196860 756276.742998 775997.458267
9845 196880 756377.260379 776098.812820
9846 196900 756477.778968 776200.168541
9847 196920 756578.298766 776301.525432
9848 196940 756678.819773 776402.883491
9849 196960 756779.341987 776504.242720
9850 196980 756879.865410 776605.603116
9851 197000 756980.390041 776706.964681
9852 197020 757080.915880 776808.327414
9853 197040 757181.442926 776909.691316
9854 197060 757281.971180 777011.056385
9855 197080 757382.500642 777112.422622
9856 197100 757483.031311 777213.790027
9857 197120 757583.563187 777315.158599
9858 197140 757684.096270 777416.528339
9859 197160 757784.630560 777517.899247
9860 197180 757885.166057 777619.271321
9861 197200 757985.702761 777720.644563
9862 197220 758086.240671 777822.018971
9863 197240 758186.779787 777923.394546
9864 197260 758287.320110 778024.771288
9865 197280 758387.861639 778126.149197
9866 197300 758488.404373 778227.528272
9867 197320 758588.948314 778328.908513
9868 197340 758689.493460 778430.289920
9869 197360 758790.039812 778531.672493
9870 197380 758890.587369 778633.056233
9871 197400 758991.136132 778734.441138
9872 197420 759091.686099 778835.827208
9873 197440 759192.237272 778937.214444
9874 197460 759292.789650 779038.602846
9875 197480 759393.343232 779139.992413
9876 197500 759493.898019 779241.383145
9877 197520 759594.454010 779342.775041
9878 197540 759695.011206 779444.168103
9879 197560 759795.569606 779545.562329
9880 197580 759896.129210 779646.957720
9881 197600 759996.690018 779748.354276
9882 197620 760097.252029 779849.751996
9883 197640 760197.815245 779951.150880
9884 197660 760298.379664 780052.550928
9885 197680 760398.945286 780153.952140
9886 197700 760499.512111 780255.354515
9887 197720 760600.080140 780356.758055
9888 197740 760700.649371 780458.162758
9889 197760 760801.219806 780559.568624
9890 197780 760901.791443 780660.975654
9891 197800 761002.364283 780762.383847
9892 197820 761102.938325 780863.793203
9893 197840 761203.513569 780965.203722
9894 197860 761304.090016 781066.615403
9895 197880 761404.667664 781168.028248
9896 197900 761505.246515 781269.442254
9897 197920 761605.826567 781370.857423
9898 197940 761706.407821 781472.273755
9899 197960 761806.990276 781573.691248
9900 197980 761907.573933 781675.109904
9901 198000 762008.158791 781776.529721
9902 198020 762108.744849 781877.950700
9903 198040 762209.332109 781979.372841
9904 198060 762309.920570 782080.796143
9905 198080 762410.510231 782182.220607
9906 198100 762511.101093 782283.646231
9907 198120 762611.693155 782385.073017
9908 198140 762712.286418 782486.500964
9909 198160 762812.880881 782587.930071
9910 198180 762913.476543 782689.360340
9911 198200 763014.073406 782790.791769
9912 198220 763114.671468 782892.224358
9913 198240 763215.270730 782993.658107
9914 198260 763315.871191 783095.093017
9915 198280 763416.472852 783196.529087
9916 198300 763517.075711 783297.966317
9917 198320 763617.679770 783399.404706
9918 198340 763718.285028 783500.844256
9919 198360 763818.891485 783602.284964
9920 198380 763919.499140 783703.726833
9921 198400 764020.107993 783805.169860
9922 198420 764120.718045 783906.614047
9923 198440 764221.329296 784008.059392
9924 198460 764321.941744 784109.505897
9925 198480 764422.555390 784210.953560
9926 198500 764523.170234 784312.402382
9927 198520 764623.786276 784413.852362
9928 198540 764724.403516 784515.303501
9929 198560 764825.021952 784616.755798
9930 198580 764925.641587 784718.209253
9931 198600 765026.262418 784819.663866
9932 198620 765126.884446 784921.119637
9933 198640 765227.507671 785022.576566
9934 198660 765328.132093 785124.034652
9935 198680 765428.757712 785225.493896
9936 198700 765529.384527 785326.954297
9937 198720 765630.012538 785428.415856
9938 198740 765730.641746 785529.878571
9939 198760 765831.272150 785631.342444
9940 198780 765931.903750 785732.807473
9941 198800 766032.536545 785834.273659
9942 198820 766133.170536 785935.741001
9943 198840 766233.805723 786037.209500
9944 198860 766334.442105 786138.679156
9945 198880 766435.079682 786240.149967
9946 198900 766535.718455 786341.621935
9947 198920 766636.358423 786443.095058
9948 198940 766736.999585 786544.569337
9949 198960 766837.641942 786646.044772
9950 198980 766938.285494 786747.521363
9951 199000 767038.930240 786848.999109
9952 199020 767139.576181 786950.478010
9953 199040 767240.223316 787051.958067
9954 199060 767340.871645 787153.439278
9955 199080 767441.521167 787254.921644
9956 199100 767542.171884 787356.405165
9957 199120 767642.823794 787457.889841
9958 199140 767743.476898 787559.375672
9959 199160 767844.131195 787660.862656
9960 199180 767944.786686 787762.350795
9961 199200 768045.443370 787863.840088
9962 199220 768146.101246 787965.330536
9963 199240 768246.760316 788066.822137
9964 199260 768347.420578 788168.314891
9965 199280 768448.082033 788269.808800
9966 199300 768548.744680 788371.303862
9967 199320 768649.408520 788472.800077
9968 199340 768750.073552 788574.297445
9969 199360 768850.739776 788675.795967
9970 199380 768951.407191 788777.295642
9971 199400 769052.075799 788878.796469
9972 199420 769152.745598 788980.298450
9973 199440 769253.416589 789081.801582
9974 199460 769354.088771 789183.305868
9975 199480 769454.762145 789284.811305
9976 199500 769555.436710 789386.317895
9977 199520 769656.112465 789487.825637
9978 199540 769756.789412 789589.334531
9979 199560 769857.467549 789690.844577
9980 199580 769958.146877 789792.355775
9981 199600 770058.827395 789893.868124
9982 199620 770159.509104 789995.381625
9983 199640 770260.192003 790096.896277
9984 199660 770360.876092 790198.412080
9985 199680 770461.561371 790299.929034
9986 199700 770562.247839 790401.447139
9987 199720 770662.935498 790502.966395
9988 199740 770763.624345 790604.486802
9989 199760 770864.314383 790706.008359
9990 199780 770965.005609 790807.531067
9991 199800 771065.698025 790909.054925
9992 199820 771166.391630 791010.579933
9993 199840 771267.086423 791112.106092
9994 199860 771367.782406 791213.633400
9995 199880 771468.479577 791315.161858
9996 199900 771569.177936 791416.691466
9997 199920 771669.877484 791518.222223
9998 199940 771770.578220 791619.754130
9999 199960 771871.280144 791721.287186
10000 199980 771971.983256 791822.821391
10001 200000 772072.687555 791924.356746

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