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 10 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
4502 90020 241697.762551 256140.779349
4503 90040 241788.434748 256232.662592
4504 90060 241879.109930 256324.548692
4505 90080 241969.788096 256416.437646
4506 90100 242060.469246 256508.329456
4507 90120 242151.153377 256600.224119
4508 90140 242241.840490 256692.121635
4509 90160 242332.530584 256784.022004
4510 90180 242423.223658 256875.925224
4511 90200 242513.919711 256967.831295
4512 90220 242604.618743 257059.740216
4513 90240 242695.320752 257151.651986
4514 90260 242786.025737 257243.566606
4515 90280 242876.733699 257335.484072
4516 90300 242967.444636 257427.404387
4517 90320 243058.158547 257519.327547
4518 90340 243148.875432 257611.253553
4519 90360 243239.595290 257703.182404
4520 90380 243330.318120 257795.114099
4521 90400 243421.043921 257887.048637
4522 90420 243511.772693 257978.986018
4523 90440 243602.504434 258070.926241
4524 90460 243693.239144 258162.869305
4525 90480 243783.976822 258254.815209
4526 90500 243874.717468 258346.763953
4527 90520 243965.461080 258438.715536
4528 90540 244056.207657 258530.669957
4529 90560 244146.957200 258622.627215
4530 90580 244237.709707 258714.587311
4531 90600 244328.465177 258806.550242
4532 90620 244419.223610 258898.516008
4533 90640 244509.985005 258990.484608
4534 90660 244600.749360 259082.456043
4535 90680 244691.516676 259174.430310
4536 90700 244782.286952 259266.407409
4537 90720 244873.060186 259358.387340
4538 90740 244963.836378 259450.370102
4539 90760 245054.615527 259542.355694
4540 90780 245145.397632 259634.344115
4541 90800 245236.182693 259726.335364
4542 90820 245326.970708 259818.329441
4543 90840 245417.761678 259910.326346
4544 90860 245508.555601 260002.326076
4545 90880 245599.352476 260094.328632
4546 90900 245690.152303 260186.334013
4547 90920 245780.955081 260278.342218
4548 90940 245871.760809 260370.353247
4549 90960 245962.569486 260462.367098
4550 90980 246053.381112 260554.383771
4551 91000 246144.195685 260646.403265
4552 91020 246235.013205 260738.425579
4553 91040 246325.833672 260830.450713
4554 91060 246416.657084 260922.478666
4555 91080 246507.483441 261014.509438
4556 91100 246598.312741 261106.543026
4557 91120 246689.144985 261198.579432
4558 91140 246779.980171 261290.618653
4559 91160 246870.818298 261382.660690
4560 91180 246961.659367 261474.705541
4561 91200 247052.503375 261566.753206
4562 91220 247143.350322 261658.803685
4563 91240 247234.200208 261750.856975
4564 91260 247325.053032 261842.913077
4565 91280 247415.908792 261934.971990
4566 91300 247506.767489 262027.033714
4567 91320 247597.629121 262119.098246
4568 91340 247688.493688 262211.165588
4569 91360 247779.361188 262303.235737
4570 91380 247870.231622 262395.308694
4571 91400 247961.104987 262487.384457
4572 91420 248051.981285 262579.463026
4573 91440 248142.860513 262671.544399
4574 91460 248233.742671 262763.628578
4575 91480 248324.627758 262855.715560
4576 91500 248415.515774 262947.805344
4577 91520 248506.406717 263039.897931
4578 91540 248597.300587 263131.993320
4579 91560 248688.197384 263224.091509
4580 91580 248779.097105 263316.192498
4581 91600 248869.999752 263408.296286
4582 91620 248960.905322 263500.402873
4583 91640 249051.813815 263592.512258
4584 91660 249142.725231 263684.624439
4585 91680 249233.639568 263776.739417
4586 91700 249324.556826 263868.857191
4587 91720 249415.477004 263960.977760
4588 91740 249506.400101 264053.101123
4589 91760 249597.326117 264145.227279
4590 91780 249688.255050 264237.356228
4591 91800 249779.186901 264329.487969
4592 91820 249870.121667 264421.622502
4593 91840 249961.059350 264513.759825
4594 91860 250051.999946 264605.899938
4595 91880 250142.943457 264698.042840
4596 91900 250233.889881 264790.188530
4597 91920 250324.839217 264882.337008
4598 91940 250415.791465 264974.488274
4599 91960 250506.746624 265066.642325
4600 91980 250597.704693 265158.799162
4601 92000 250688.665671 265250.958784
4602 92020 250779.629558 265343.121190
4603 92040 250870.596353 265435.286380
4604 92060 250961.566055 265527.454352
4605 92080 251052.538663 265619.625106
4606 92100 251143.514177 265711.798642
4607 92120 251234.492596 265803.974958
4608 92140 251325.473918 265896.154053
4609 92160 251416.458145 265988.335928
4610 92180 251507.445273 266080.520582
4611 92200 251598.435304 266172.708013
4612 92220 251689.428235 266264.898221
4613 92240 251780.424067 266357.091205
4614 92260 251871.422798 266449.286965
4615 92280 251962.424429 266541.485500
4616 92300 252053.428957 266633.686809
4617 92320 252144.436382 266725.890891
4618 92340 252235.446704 266818.097746
4619 92360 252326.459922 266910.307373
4620 92380 252417.476035 267002.519771
4621 92400 252508.495042 267094.734940
4622 92420 252599.516943 267186.952879
4623 92440 252690.541737 267279.173587
4624 92460 252781.569422 267371.397063
4625 92480 252872.599999 267463.623307
4626 92500 252963.633466 267555.852319
4627 92520 253054.669824 267648.084096
4628 92540 253145.709070 267740.318639
4629 92560 253236.751204 267832.555947
4630 92580 253327.796227 267924.796019
4631 92600 253418.844136 268017.038855
4632 92620 253509.894930 268109.284454
4633 92640 253600.948611 268201.532814
4634 92660 253692.005176 268293.783936
4635 92680 253783.064624 268386.037819
4636 92700 253874.126956 268478.294462
4637 92720 253965.192170 268570.553864
4638 92740 254056.260266 268662.816024
4639 92760 254147.331242 268755.080943
4640 92780 254238.405099 268847.348618
4641 92800 254329.481835 268939.619050
4642 92820 254420.561450 269031.892238
4643 92840 254511.643942 269124.168181
4644 92860 254602.729312 269216.446878
4645 92880 254693.817558 269308.728329
4646 92900 254784.908679 269401.012533
4647 92920 254876.002676 269493.299489
4648 92940 254967.099547 269585.589196
4649 92960 255058.199291 269677.881655
4650 92980 255149.301908 269770.176863
4651 93000 255240.407397 269862.474821
4652 93020 255331.515757 269954.775528
4653 93040 255422.626987 270047.078983
4654 93060 255513.741087 270139.385185
4655 93080 255604.858057 270231.694134
4656 93100 255695.977894 270324.005828
4657 93120 255787.100599 270416.320268
4658 93140 255878.226171 270508.637453
4659 93160 255969.354609 270600.957381
4660 93180 256060.485912 270693.280053
4661 93200 256151.620079 270785.605467
4662 93220 256242.757111 270877.933623
4663 93240 256333.897006 270970.264520
4664 93260 256425.039763 271062.598157
4665 93280 256516.185381 271154.934534
4666 93300 256607.333861 271247.273650
4667 93320 256698.485201 271339.615505
4668 93340 256789.639400 271431.960097
4669 93360 256880.796458 271524.307426
4670 93380 256971.956373 271616.657491
4671 93400 257063.119146 271709.010291
4672 93420 257154.284776 271801.365827
4673 93440 257245.453261 271893.724096
4674 93460 257336.624602 271986.085099
4675 93480 257427.798796 272078.448835
4676 93500 257518.975845 272170.815303
4677 93520 257610.155746 272263.184502
4678 93540 257701.338499 272355.556432
4679 93560 257792.524104 272447.931092
4680 93580 257883.712559 272540.308482
4681 93600 257974.903864 272632.688599
4682 93620 258066.098019 272725.071445
4683 93640 258157.295022 272817.457018
4684 93660 258248.494872 272909.845318
4685 93680 258339.697570 273002.236344
4686 93700 258430.903114 273094.630094
4687 93720 258522.111504 273187.026569
4688 93740 258613.322738 273279.425768
4689 93760 258704.536817 273371.827691
4690 93780 258795.753739 273464.232335
4691 93800 258886.973503 273556.639701
4692 93820 258978.196110 273649.049789
4693 93840 259069.421558 273741.462597
4694 93860 259160.649846 273833.878124
4695 93880 259251.880974 273926.296370
4696 93900 259343.114941 274018.717335
4697 93920 259434.351747 274111.141018
4698 93940 259525.591390 274203.567417
4699 93960 259616.833870 274295.996533
4700 93980 259708.079186 274388.428364
4701 94000 259799.327337 274480.862910
4702 94020 259890.578323 274573.300170
4703 94040 259981.832144 274665.740144
4704 94060 260073.088797 274758.182831
4705 94080 260164.348283 274850.628230
4706 94100 260255.610601 274943.076341
4707 94120 260346.875750 275035.527162
4708 94140 260438.143730 275127.980693
4709 94160 260529.414539 275220.436934
4710 94180 260620.688177 275312.895884
4711 94200 260711.964643 275405.357542
4712 94220 260803.243937 275497.821907
4713 94240 260894.526058 275590.288979
4714 94260 260985.811005 275682.758757
4715 94280 261077.098777 275775.231240
4716 94300 261168.389374 275867.706429
4717 94320 261259.682794 275960.184321
4718 94340 261350.979038 276052.664916
4719 94360 261442.278105 276145.148214
4720 94380 261533.579993 276237.634215
4721 94400 261624.884703 276330.122916
4722 94420 261716.192232 276422.614318
4723 94440 261807.502582 276515.108421
4724 94460 261898.815750 276607.605222
4725 94480 261990.131737 276700.104722
4726 94500 262081.450541 276792.606920
4727 94520 262172.772162 276885.111816
4728 94540 262264.096599 276977.619408
4729 94560 262355.423851 277070.129695
4730 94580 262446.753918 277162.642679
4731 94600 262538.086800 277255.158356
4732 94620 262629.422494 277347.676728
4733 94640 262720.761001 277440.197793
4734 94660 262812.102319 277532.721550
4735 94680 262903.446449 277625.248000
4736 94700 262994.793389 277717.777140
4737 94720 263086.143139 277810.308971
4738 94740 263177.495698 277902.843492
4739 94760 263268.851065 277995.380703
4740 94780 263360.209240 278087.920601
4741 94800 263451.570222 278180.463188
4742 94820 263542.934010 278273.008462
4743 94840 263634.300603 278365.556422
4744 94860 263725.670001 278458.107069
4745 94880 263817.042203 278550.660400
4746 94900 263908.417208 278643.216416
4747 94920 263999.795017 278735.775116
4748 94940 264091.175626 278828.336499
4749 94960 264182.559038 278920.900565
4750 94980 264273.945249 279013.467313
4751 95000 264365.334261 279106.036741
4752 95020 264456.726072 279198.608851
4753 95040 264548.120681 279291.183640
4754 95060 264639.518087 279383.761108
4755 95080 264730.918291 279476.341255
4756 95100 264822.321291 279568.924080
4757 95120 264913.727087 279661.509582
4758 95140 265005.135678 279754.097760
4759 95160 265096.547062 279846.688615
4760 95180 265187.961241 279939.282144
4761 95200 265279.378212 280031.878349
4762 95220 265370.797975 280124.477227
4763 95240 265462.220529 280217.078778
4764 95260 265553.645875 280309.683002
4765 95280 265645.074010 280402.289898
4766 95300 265736.504935 280494.899465
4767 95320 265827.938648 280587.511702
4768 95340 265919.375149 280680.126610
4769 95360 266010.814437 280772.744187
4770 95380 266102.256512 280865.364432
4771 95400 266193.701372 280957.987346
4772 95420 266285.149018 281050.612926
4773 95440 266376.599448 281143.241174
4774 95460 266468.052662 281235.872087
4775 95480 266559.508659 281328.505666
4776 95500 266650.967438 281421.141909
4777 95520 266742.428999 281513.780816
4778 95540 266833.893340 281606.422387
4779 95560 266925.360462 281699.066620
4780 95580 267016.830364 281791.713515
4781 95600 267108.303044 281884.363072
4782 95620 267199.778503 281977.015289
4783 95640 267291.256739 282069.670167
4784 95660 267382.737752 282162.327704
4785 95680 267474.221541 282254.987899
4786 95700 267565.708105 282347.650753
4787 95720 267657.197444 282440.316264
4788 95740 267748.689557 282532.984432
4789 95760 267840.184443 282625.655256
4790 95780 267931.682102 282718.328735
4791 95800 268023.182533 282811.004870
4792 95820 268114.685735 282903.683658
4793 95840 268206.191707 282996.365100
4794 95860 268297.700450 283089.049195
4795 95880 268389.211962 283181.735942
4796 95900 268480.726242 283274.425340
4797 95920 268572.243290 283367.117390
4798 95940 268663.763105 283459.812090
4799 95960 268755.285686 283552.509439
4800 95980 268846.811034 283645.209437
4801 96000 268938.339146 283737.912084
4802 96020 269029.870022 283830.617378
4803 96040 269121.403663 283923.325319
4804 96060 269212.940066 284016.035907
4805 96080 269304.479232 284108.749140
4806 96100 269396.021159 284201.465019
4807 96120 269487.565847 284294.183541
4808 96140 269579.113295 284386.904708
4809 96160 269670.663503 284479.628518
4810 96180 269762.216470 284572.354970
4811 96200 269853.772195 284665.084064
4812 96220 269945.330678 284757.815799
4813 96240 270036.891917 284850.550174
4814 96260 270128.455912 284943.287190
4815 96280 270220.022663 285036.026844
4816 96300 270311.592169 285128.769138
4817 96320 270403.164429 285221.514069
4818 96340 270494.739442 285314.261637
4819 96360 270586.317208 285407.011842
4820 96380 270677.897726 285499.764683
4821 96400 270769.480996 285592.520160
4822 96420 270861.067016 285685.278271
4823 96440 270952.655786 285778.039016
4824 96460 271044.247305 285870.802395
4825 96480 271135.841573 285963.568407
4826 96500 271227.438589 286056.337050
4827 96520 271319.038353 286149.108325
4828 96540 271410.640863 286241.882231
4829 96560 271502.246119 286334.658768
4830 96580 271593.854120 286427.437933
4831 96600 271685.464865 286520.219728
4832 96620 271777.078355 286613.004151
4833 96640 271868.694588 286705.791202
4834 96660 271960.313563 286798.580879
4835 96680 272051.935281 286891.373183
4836 96700 272143.559739 286984.168113
4837 96720 272235.186938 287076.965668
4838 96740 272326.816877 287169.765847
4839 96760 272418.449555 287262.568650
4840 96780 272510.084972 287355.374077
4841 96800 272601.723126 287448.182125
4842 96820 272693.364018 287540.992796
4843 96840 272785.007646 287633.806088
4844 96860 272876.654009 287726.622001
4845 96880 272968.303108 287819.440533
4846 96900 273059.954942 287912.261685
4847 96920 273151.609509 288005.085456
4848 96940 273243.266809 288097.911845
4849 96960 273334.926842 288190.740851
4850 96980 273426.589606 288283.572474
4851 97000 273518.255102 288376.406713
4852 97020 273609.923328 288469.243568
4853 97040 273701.594284 288562.083037
4854 97060 273793.267969 288654.925121
4855 97080 273884.944382 288747.769819
4856 97100 273976.623523 288840.617129
4857 97120 274068.305391 288933.467052
4858 97140 274159.989986 289026.319587
4859 97160 274251.677306 289119.174732
4860 97180 274343.367351 289212.032488
4861 97200 274435.060121 289304.892854
4862 97220 274526.755615 289397.755830
4863 97240 274618.453831 289490.621413
4864 97260 274710.154770 289583.489605
4865 97280 274801.858431 289676.360404
4866 97300 274893.564813 289769.233810
4867 97320 274985.273915 289862.109822
4868 97340 275076.985737 289954.988439
4869 97360 275168.700278 290047.869661
4870 97380 275260.417538 290140.753487
4871 97400 275352.137515 290233.639917
4872 97420 275443.860209 290326.528949
4873 97440 275535.585620 290419.420584
4874 97460 275627.313746 290512.314821
4875 97480 275719.044588 290605.211658
4876 97500 275810.778144 290698.111096
4877 97520 275902.514413 290791.013134
4878 97540 275994.253396 290883.917770
4879 97560 276085.995092 290976.825006
4880 97580 276177.739499 291069.734839
4881 97600 276269.486617 291162.647269
4882 97620 276361.236445 291255.562296
4883 97640 276452.988984 291348.479919
4884 97660 276544.744231 291441.400138
4885 97680 276636.502187 291534.322951
4886 97700 276728.262851 291627.248358
4887 97720 276820.026222 291720.176359
4888 97740 276911.792300 291813.106953
4889 97760 277003.561083 291906.040139
4890 97780 277095.332571 291998.975917
4891 97800 277187.106764 292091.914285
4892 97820 277278.883661 292184.855244
4893 97840 277370.663261 292277.798793
4894 97860 277462.445564 292370.744931
4895 97880 277554.230568 292463.693657
4896 97900 277646.018274 292556.644972
4897 97920 277737.808681 292649.598873
4898 97940 277829.601787 292742.555361
4899 97960 277921.397592 292835.514436
4900 97980 278013.196096 292928.476095
4901 98000 278104.997298 293021.440340
4902 98020 278196.801198 293114.407169
4903 98040 278288.607794 293207.376581
4904 98060 278380.417086 293300.348576
4905 98080 278472.229073 293393.323153
4906 98100 278564.043755 293486.300313
4907 98120 278655.861131 293579.280053
4908 98140 278747.681200 293672.262373
4909 98160 278839.503962 293765.247274
4910 98180 278931.329416 293858.234754
4911 98200 279023.157562 293951.224812
4912 98220 279114.988398 294044.217448
4913 98240 279206.821925 294137.212662
4914 98260 279298.658141 294230.210453
4915 98280 279390.497045 294323.210819
4916 98300 279482.338638 294416.213761
4917 98320 279574.182918 294509.219278
4918 98340 279666.029886 294602.227370
4919 98360 279757.879539 294695.238035
4920 98380 279849.731878 294788.251273
4921 98400 279941.586902 294881.267083
4922 98420 280033.444610 294974.285465
4923 98440 280125.305002 295067.306419
4924 98460 280217.168076 295160.329943
4925 98480 280309.033833 295253.356037
4926 98500 280400.902272 295346.384701
4927 98520 280492.773392 295439.415933
4928 98540 280584.647192 295532.449733
4929 98560 280676.523672 295625.486101
4930 98580 280768.402830 295718.525036
4931 98600 280860.284668 295811.566537
4932 98620 280952.169183 295904.610604
4933 98640 281044.056375 295997.657236
4934 98660 281135.946244 296090.706433
4935 98680 281227.838788 296183.758193
4936 98700 281319.734008 296276.812516
4937 98720 281411.631902 296369.869403
4938 98740 281503.532470 296462.928851
4939 98760 281595.435712 296555.990861
4940 98780 281687.341626 296649.055431
4941 98800 281779.250212 296742.122562
4942 98820 281871.161470 296835.192252
4943 98840 281963.075398 296928.264501
4944 98860 282054.991996 297021.339309
4945 98880 282146.911264 297114.416675
4946 98900 282238.833200 297207.496597
4947 98920 282330.757804 297300.579076
4948 98940 282422.685076 297393.664112
4949 98960 282514.615015 297486.751702
4950 98980 282606.547620 297579.841847
4951 99000 282698.482891 297672.934547
4952 99020 282790.420826 297766.029800
4953 99040 282882.361426 297859.127605
4954 99060 282974.304690 297952.227963
4955 99080 283066.250616 298045.330873
4956 99100 283158.199205 298138.436334
4957 99120 283250.150456 298231.544346
4958 99140 283342.104367 298324.654907
4959 99160 283434.060940 298417.768018
4960 99180 283526.020171 298510.883677
4961 99200 283617.982063 298604.001885
4962 99220 283709.946612 298697.122640
4963 99240 283801.913820 298790.245941
4964 99260 283893.883685 298883.371789
4965 99280 283985.856206 298976.500183
4966 99300 284077.831384 299069.631122
4967 99320 284169.809217 299162.764605
4968 99340 284261.789704 299255.900633
4969 99360 284353.772846 299349.039203
4970 99380 284445.758641 299442.180316
4971 99400 284537.747089 299535.323971
4972 99420 284629.738189 299628.470168
4973 99440 284721.731941 299721.618905
4974 99460 284813.728343 299814.770183
4975 99480 284905.727396 299907.924001
4976 99500 284997.729099 300001.080357
4977 99520 285089.733450 300094.239252
4978 99540 285181.740450 300187.400685
4979 99560 285273.750098 300280.564655
4980 99580 285365.762393 300373.731162
4981 99600 285457.777334 300466.900205
4982 99620 285549.794922 300560.071784
4983 99640 285641.815154 300653.245897
4984 99660 285733.838031 300746.422544
4985 99680 285825.863552 300839.601726
4986 99700 285917.891717 300932.783440
4987 99720 286009.922524 301025.967687
4988 99740 286101.955973 301119.154466
4989 99760 286193.992063 301212.343776
4990 99780 286286.030795 301305.535616
4991 99800 286378.072167 301398.729987
4992 99820 286470.116178 301491.926888
4993 99840 286562.162828 301585.126317
4994 99860 286654.212116 301678.328275
4995 99880 286746.264043 301771.532760
4996 99900 286838.318606 301864.739772
4997 99920 286930.375805 301957.949311
4998 99940 287022.435641 302051.161376
4999 99960 287114.498111 302144.375967
5000 99980 287206.563217 302237.593082
5001 100000 287298.630956 302330.812721

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