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 2 of 40 · 500/page · 20000 rows next →
# nmargin_tracedmargin_measured
502 10020 -74527.524653 -67313.468707
503 10040 -74480.371331 -67262.619469
504 10060 -74433.154179 -67211.710448
505 10080 -74385.873403 -67160.741838
506 10100 -74338.529208 -67109.713829
507 10120 -74291.121795 -67058.626609
508 10140 -74243.651366 -67007.480369
509 10160 -74196.118123 -66956.275296
510 10180 -74148.522264 -66905.011577
511 10200 -74100.863988 -66853.689397
512 10220 -74053.143493 -66802.308941
513 10240 -74005.360975 -66750.870392
514 10260 -73957.516628 -66699.373934
515 10280 -73909.610649 -66647.819748
516 10300 -73861.643228 -66596.208016
517 10320 -73813.614560 -66544.538916
518 10340 -73765.524834 -66492.812627
519 10360 -73717.374242 -66441.029328
520 10380 -73669.162972 -66389.189196
521 10400 -73620.891212 -66337.292407
522 10420 -73572.559150 -66285.339135
523 10440 -73524.166972 -66233.329555
524 10460 -73475.714863 -66181.263841
525 10480 -73427.203008 -66129.142164
526 10500 -73378.631589 -66076.964697
527 10520 -73330.000789 -66024.731609
528 10540 -73281.310790 -65972.443071
529 10560 -73232.561771 -65920.099251
530 10580 -73183.753913 -65867.700317
531 10600 -73134.887394 -65815.246437
532 10620 -73085.962392 -65762.737776
533 10640 -73036.979083 -65710.174500
534 10660 -72987.937643 -65657.556774
535 10680 -72938.838248 -65604.884761
536 10700 -72889.681071 -65552.158624
537 10720 -72840.466286 -65499.378525
538 10740 -72791.194065 -65446.544626
539 10760 -72741.864579 -65393.657086
540 10780 -72692.477999 -65340.716065
541 10800 -72643.034494 -65287.721722
542 10820 -72593.534234 -65234.674215
543 10840 -72543.977387 -65181.573701
544 10860 -72494.364119 -65128.420336
545 10880 -72444.694597 -65075.214277
546 10900 -72394.968986 -65021.955677
547 10920 -72345.187451 -64968.644691
548 10940 -72295.350156 -64915.281472
549 10960 -72245.457264 -64861.866172
550 10980 -72195.508937 -64808.398944
551 11000 -72145.505337 -64754.879938
552 11020 -72095.446623 -64701.309305
553 11040 -72045.332957 -64647.687193
554 11060 -71995.164496 -64594.013752
555 11080 -71944.941400 -64540.289130
556 11100 -71894.663825 -64486.513474
557 11120 -71844.331928 -64432.686930
558 11140 -71793.945866 -64378.809645
559 11160 -71743.505792 -64324.881763
560 11180 -71693.011863 -64270.903430
561 11200 -71642.464231 -64216.874788
562 11220 -71591.863050 -64162.795980
563 11240 -71541.208471 -64108.667150
564 11260 -71490.500645 -64054.488438
565 11280 -71439.739725 -64000.259986
566 11300 -71388.925859 -63945.981934
567 11320 -71338.059197 -63891.654421
568 11340 -71287.139887 -63837.277587
569 11360 -71236.168077 -63782.851569
570 11380 -71185.143915 -63728.376506
571 11400 -71134.067547 -63673.852534
572 11420 -71082.939117 -63619.279789
573 11440 -71031.758773 -63564.658408
574 11460 -70980.526657 -63509.988524
575 11480 -70929.242913 -63455.270273
576 11500 -70877.907685 -63400.503789
577 11520 -70826.521115 -63345.689203
578 11540 -70775.083344 -63290.826649
579 11560 -70723.594513 -63235.916259
580 11580 -70672.054763 -63180.958164
581 11600 -70620.464233 -63125.952494
582 11620 -70568.823062 -63070.899379
583 11640 -70517.131388 -63015.798949
584 11660 -70465.389350 -62960.651333
585 11680 -70413.597084 -62905.456658
586 11700 -70361.754727 -62850.215052
587 11720 -70309.862414 -62794.926643
588 11740 -70257.920281 -62739.591556
589 11760 -70205.928462 -62684.209918
590 11780 -70153.887091 -62628.781853
591 11800 -70101.796301 -62573.307487
592 11820 -70049.656225 -62517.786943
593 11840 -69997.466995 -62462.220345
594 11860 -69945.228743 -62406.607816
595 11880 -69892.941599 -62350.949478
596 11900 -69840.605694 -62295.245453
597 11920 -69788.221157 -62239.495862
598 11940 -69735.788117 -62183.700827
599 11960 -69683.306704 -62127.860467
600 11980 -69630.777044 -62071.974901
601 12000 -69578.199265 -62016.044249
602 12020 -69525.573495 -61960.068630
603 12040 -69472.899858 -61904.048161
604 12060 -69420.178481 -61847.982961
605 12080 -69367.409489 -61791.873145
606 12100 -69314.593006 -61735.718830
607 12120 -69261.729157 -61679.520133
608 12140 -69208.818063 -61623.277168
609 12160 -69155.859850 -61566.990050
610 12180 -69102.854638 -61510.658895
611 12200 -69049.802550 -61454.283815
612 12220 -68996.703706 -61397.864923
613 12240 -68943.558228 -61341.402334
614 12260 -68890.366235 -61284.896159
615 12280 -68837.127847 -61228.346510
616 12300 -68783.843183 -61171.753499
617 12320 -68730.512363 -61115.117235
618 12340 -68677.135502 -61058.437830
619 12360 -68623.712721 -61001.715394
620 12380 -68570.244134 -60944.950035
621 12400 -68516.729859 -60888.141864
622 12420 -68463.170012 -60831.290987
623 12440 -68409.564709 -60774.397514
624 12460 -68355.914063 -60717.461552
625 12480 -68302.218190 -60660.483207
626 12500 -68248.477204 -60603.462587
627 12520 -68194.691218 -60546.399798
628 12540 -68140.860346 -60489.294944
629 12560 -68086.984699 -60432.148132
630 12580 -68033.064389 -60374.959466
631 12600 -67979.099529 -60317.729051
632 12620 -67925.090230 -60260.456990
633 12640 -67871.036601 -60203.143386
634 12660 -67816.938753 -60145.788343
635 12680 -67762.796796 -60088.391964
636 12700 -67708.610839 -60030.954349
637 12720 -67654.380991 -59973.475602
638 12740 -67600.107359 -59915.955823
639 12760 -67545.790053 -59858.395112
640 12780 -67491.429178 -59800.793572
641 12800 -67437.024842 -59743.151300
642 12820 -67382.577151 -59685.468397
643 12840 -67328.086212 -59627.744962
644 12860 -67273.552130 -59569.981094
645 12880 -67218.975010 -59512.176891
646 12900 -67164.354957 -59454.332450
647 12920 -67109.692074 -59396.447869
648 12940 -67054.986466 -59338.523246
649 12960 -67000.238237 -59280.558677
650 12980 -66945.447488 -59222.554257
651 13000 -66890.614323 -59164.510084
652 13020 -66835.738843 -59106.426252
653 13040 -66780.821151 -59048.302857
654 13060 -66725.861347 -58990.139992
655 13080 -66670.859532 -58931.937753
656 13100 -66615.815807 -58873.696233
657 13120 -66560.730272 -58815.415526
658 13140 -66505.603026 -58757.095725
659 13160 -66450.434169 -58698.736922
660 13180 -66395.223799 -58640.339210
661 13200 -66339.972014 -58581.902681
662 13220 -66284.678913 -58523.427426
663 13240 -66229.344593 -58464.913537
664 13260 -66173.969151 -58406.361104
665 13280 -66118.552684 -58347.770218
666 13300 -66063.095289 -58289.140970
667 13320 -66007.597061 -58230.473448
668 13340 -65952.058096 -58171.767742
669 13360 -65896.478490 -58113.023941
670 13380 -65840.858336 -58054.242134
671 13400 -65785.197730 -57995.422410
672 13420 -65729.496765 -57936.564855
673 13440 -65673.755536 -57877.669558
674 13460 -65617.974135 -57818.736606
675 13480 -65562.152656 -57759.766086
676 13500 -65506.291191 -57700.758084
677 13520 -65450.389832 -57641.712687
678 13540 -65394.448671 -57582.629980
679 13560 -65338.467800 -57523.510049
680 13580 -65282.447309 -57464.352979
681 13600 -65226.387290 -57405.158855
682 13620 -65170.287833 -57345.927761
683 13640 -65114.149028 -57286.659782
684 13660 -65057.970964 -57227.355002
685 13680 -65001.753732 -57168.013503
686 13700 -64945.497419 -57108.635370
687 13720 -64889.202114 -57049.220684
688 13740 -64832.867907 -56989.769530
689 13760 -64776.494884 -56930.281987
690 13780 -64720.083133 -56870.758140
691 13800 -64663.632742 -56811.198068
692 13820 -64607.143798 -56751.601855
693 13840 -64550.616387 -56691.969579
694 13860 -64494.050594 -56632.301322
695 13880 -64437.446507 -56572.597165
696 13900 -64380.804211 -56512.857187
697 13920 -64324.123791 -56453.081468
698 13940 -64267.405332 -56393.270087
699 13960 -64210.648918 -56333.423124
700 13980 -64153.854634 -56273.540657
701 14000 -64097.022563 -56213.622764
702 14020 -64040.152790 -56153.669525
703 14040 -63983.245398 -56093.681015
704 14060 -63926.300469 -56033.657315
705 14080 -63869.318086 -55973.598499
706 14100 -63812.298332 -55913.504647
707 14120 -63755.241289 -55853.375833
708 14140 -63698.147038 -55793.212135
709 14160 -63641.015660 -55733.013629
710 14180 -63583.847238 -55672.780390
711 14200 -63526.641851 -55612.512494
712 14220 -63469.399580 -55552.210016
713 14240 -63412.120506 -55491.873032
714 14260 -63354.804707 -55431.501615
715 14280 -63297.452265 -55371.095841
716 14300 -63240.063258 -55310.655783
717 14320 -63182.637764 -55250.181516
718 14340 -63125.175864 -55189.673112
719 14360 -63067.677635 -55129.130645
720 14380 -63010.143154 -55068.554188
721 14400 -62952.572501 -55007.943814
722 14420 -62894.965753 -54947.299596
723 14440 -62837.322987 -54886.621605
724 14460 -62779.644279 -54825.909913
725 14480 -62721.929707 -54765.164593
726 14500 -62664.179346 -54704.385715
727 14520 -62606.393274 -54643.573350
728 14540 -62548.571565 -54582.727571
729 14560 -62490.714296 -54521.848446
730 14580 -62432.821541 -54460.936047
731 14600 -62374.893376 -54399.990443
732 14620 -62316.929875 -54339.011705
733 14640 -62258.931112 -54277.999902
734 14660 -62200.897163 -54216.955103
735 14680 -62142.828101 -54155.877377
736 14700 -62084.723999 -54094.766794
737 14720 -62026.584930 -54033.623422
738 14740 -61968.410969 -53972.447328
739 14760 -61910.202187 -53911.238582
740 14780 -61851.958658 -53849.997251
741 14800 -61793.680453 -53788.723403
742 14820 -61735.367645 -53727.417104
743 14840 -61677.020305 -53666.078423
744 14860 -61618.638505 -53604.707425
745 14880 -61560.222316 -53543.304178
746 14900 -61501.771809 -53481.868748
747 14920 -61443.287056 -53420.401201
748 14940 -61384.768125 -53358.901602
749 14960 -61326.215089 -53297.370019
750 14980 -61267.628016 -53235.806515
751 15000 -61209.006977 -53174.211157
752 15020 -61150.352040 -53112.584009
753 15040 -61091.663276 -53050.925135
754 15060 -61032.940753 -52989.234601
755 15080 -60974.184540 -52927.512471
756 15100 -60915.394706 -52865.758809
757 15120 -60856.571318 -52803.973678
758 15140 -60797.714445 -52742.157142
759 15160 -60738.824155 -52680.309265
760 15180 -60679.900514 -52618.430109
761 15200 -60620.943592 -52556.519738
762 15220 -60561.953453 -52494.578214
763 15240 -60502.930167 -52432.605600
764 15260 -60443.873798 -52370.601958
765 15280 -60384.784413 -52308.567350
766 15300 -60325.662080 -52246.501838
767 15320 -60266.506862 -52184.405484
768 15340 -60207.318827 -52122.278348
769 15360 -60148.098039 -52060.120493
770 15380 -60088.844565 -51997.931978
771 15400 -60029.558468 -51935.712866
772 15420 -59970.239814 -51873.463216
773 15440 -59910.888667 -51811.183088
774 15460 -59851.505091 -51748.872544
775 15480 -59792.089151 -51686.531642
776 15500 -59732.640911 -51624.160443
777 15520 -59673.160434 -51561.759005
778 15540 -59613.647784 -51499.327390
779 15560 -59554.103023 -51436.865654
780 15580 -59494.526215 -51374.373858
781 15600 -59434.917424 -51311.852060
782 15620 -59375.276710 -51249.300318
783 15640 -59315.604137 -51186.718692
784 15660 -59255.899767 -51124.107238
785 15680 -59196.163662 -51061.466015
786 15700 -59136.395884 -50998.795081
787 15720 -59076.596493 -50936.094493
788 15740 -59016.765552 -50873.364308
789 15760 -58956.903122 -50810.604584
790 15780 -58897.009263 -50747.815377
791 15800 -58837.084036 -50684.996744
792 15820 -58777.127501 -50622.148742
793 15840 -58717.139720 -50559.271427
794 15860 -58657.120752 -50496.364856
795 15880 -58597.070656 -50433.429083
796 15900 -58536.989494 -50370.464166
797 15920 -58476.877323 -50307.470159
798 15940 -58416.734204 -50244.447119
799 15960 -58356.560195 -50181.395100
800 15980 -58296.355356 -50118.314157
801 16000 -58236.119745 -50055.204345
802 16020 -58175.853420 -49992.065719
803 16040 -58115.556441 -49928.898334
804 16060 -58055.228864 -49865.702243
805 16080 -57994.870749 -49802.477501
806 16100 -57934.482152 -49739.224162
807 16120 -57874.063132 -49675.942280
808 16140 -57813.613745 -49612.631908
809 16160 -57753.134049 -49549.293099
810 16180 -57692.624100 -49485.925907
811 16200 -57632.083956 -49422.530386
812 16220 -57571.513674 -49359.106587
813 16240 -57510.913309 -49295.654564
814 16260 -57450.282918 -49232.174369
815 16280 -57389.622556 -49168.666054
816 16300 -57328.932281 -49105.129673
817 16320 -57268.212146 -49041.565276
818 16340 -57207.462209 -48977.972916
819 16360 -57146.682524 -48914.352644
820 16380 -57085.873147 -48850.704513
821 16400 -57025.034131 -48787.028572
822 16420 -56964.165533 -48723.324875
823 16440 -56903.267407 -48659.593470
824 16460 -56842.339807 -48595.834411
825 16480 -56781.382788 -48532.047746
826 16500 -56720.396403 -48468.233527
827 16520 -56659.380706 -48404.391804
828 16540 -56598.335752 -48340.522628
829 16560 -56537.261593 -48276.626048
830 16580 -56476.158284 -48212.702114
831 16600 -56415.025877 -48148.750877
832 16620 -56353.864425 -48084.772385
833 16640 -56292.673982 -48020.766688
834 16660 -56231.454600 -47956.733835
835 16680 -56170.206331 -47892.673876
836 16700 -56108.929228 -47828.586859
837 16720 -56047.623344 -47764.472834
838 16740 -55986.288730 -47700.331848
839 16760 -55924.925437 -47636.163951
840 16780 -55863.533519 -47571.969190
841 16800 -55802.113026 -47507.747614
842 16820 -55740.664010 -47443.499271
843 16840 -55679.186522 -47379.224209
844 16860 -55617.680613 -47314.922475
845 16880 -55556.146334 -47250.594118
846 16900 -55494.583735 -47186.239183
847 16920 -55432.992868 -47121.857720
848 16940 -55371.373783 -47057.449775
849 16960 -55309.726530 -46993.015394
850 16980 -55248.051159 -46928.554626
851 17000 -55186.347720 -46864.067516
852 17020 -55124.616263 -46799.554110
853 17040 -55062.856838 -46735.014457
854 17060 -55001.069494 -46670.448601
855 17080 -54939.254280 -46605.856588
856 17100 -54877.411245 -46541.238466
857 17120 -54815.540440 -46476.594279
858 17140 -54753.641912 -46411.924073
859 17160 -54691.715710 -46347.227894
860 17180 -54629.761883 -46282.505788
861 17200 -54567.780479 -46217.757799
862 17220 -54505.771547 -46152.983972
863 17240 -54443.735134 -46088.184353
864 17260 -54381.671290 -46023.358987
865 17280 -54319.580060 -45958.507918
866 17300 -54257.461494 -45893.631191
867 17320 -54195.315639 -45828.728849
868 17340 -54133.142542 -45763.800939
869 17360 -54070.942251 -45698.847503
870 17380 -54008.714812 -45633.868586
871 17400 -53946.460272 -45568.864231
872 17420 -53884.178679 -45503.834483
873 17440 -53821.870079 -45438.779385
874 17460 -53759.534519 -45373.698980
875 17480 -53697.172044 -45308.593312
876 17500 -53634.782702 -45243.462425
877 17520 -53572.366538 -45178.306360
878 17540 -53509.923599 -45113.125162
879 17560 -53447.453930 -45047.918873
880 17580 -53384.957577 -44982.687536
881 17600 -53322.434585 -44917.431193
882 17620 -53259.885001 -44852.149887
883 17640 -53197.308868 -44786.843661
884 17660 -53134.706233 -44721.512555
885 17680 -53072.077141 -44656.156613
886 17700 -53009.421636 -44590.775877
887 17720 -52946.739762 -44525.370388
888 17740 -52884.031566 -44459.940188
889 17760 -52821.297091 -44394.485318
890 17780 -52758.536382 -44329.005821
891 17800 -52695.749482 -44263.501737
892 17820 -52632.936436 -44197.973107
893 17840 -52570.097288 -44132.419974
894 17860 -52507.232082 -44066.842376
895 17880 -52444.340861 -44001.240357
896 17900 -52381.423670 -43935.613956
897 17920 -52318.480550 -43869.963213
898 17940 -52255.511547 -43804.288170
899 17960 -52192.516702 -43738.588866
900 17980 -52129.496060 -43672.865343
901 18000 -52066.449663 -43607.117640
902 18020 -52003.377553 -43541.345796
903 18040 -51940.279774 -43475.549853
904 18060 -51877.156368 -43409.729850
905 18080 -51814.007378 -43343.885826
906 18100 -51750.832845 -43278.017821
907 18120 -51687.632812 -43212.125874
908 18140 -51624.407322 -43146.210026
909 18160 -51561.156415 -43080.270314
910 18180 -51497.880135 -43014.306779
911 18200 -51434.578522 -42948.319458
912 18220 -51371.251618 -42882.308391
913 18240 -51307.899465 -42816.273618
914 18260 -51244.522104 -42750.215175
915 18280 -51181.119576 -42684.133102
916 18300 -51117.691922 -42618.027438
917 18320 -51054.239184 -42551.898220
918 18340 -50990.761401 -42485.745487
919 18360 -50927.258616 -42419.569277
920 18380 -50863.730869 -42353.369628
921 18400 -50800.178199 -42287.146577
922 18420 -50736.600648 -42220.900163
923 18440 -50672.998256 -42154.630423
924 18460 -50609.371063 -42088.337395
925 18480 -50545.719109 -42022.021116
926 18500 -50482.042433 -41955.681623
927 18520 -50418.341077 -41889.318954
928 18540 -50354.615079 -41822.933146
929 18560 -50290.864479 -41756.524236
930 18580 -50227.089317 -41690.092261
931 18600 -50163.289632 -41623.637257
932 18620 -50099.465463 -41557.159261
933 18640 -50035.616850 -41490.658310
934 18660 -49971.743831 -41424.134441
935 18680 -49907.846445 -41357.587689
936 18700 -49843.924732 -41291.018092
937 18720 -49779.978730 -41224.425684
938 18740 -49716.008477 -41157.810503
939 18760 -49652.014013 -41091.172584
940 18780 -49587.995375 -41024.511964
941 18800 -49523.952602 -40957.828677
942 18820 -49459.885731 -40891.122761
943 18840 -49395.794802 -40824.394249
944 18860 -49331.679852 -40757.643178
945 18880 -49267.540918 -40690.869583
946 18900 -49203.378040 -40624.073500
947 18920 -49139.191253 -40557.254963
948 18940 -49074.980596 -40490.414008
949 18960 -49010.746107 -40423.550670
950 18980 -48946.487822 -40356.664983
951 19000 -48882.205779 -40289.756982
952 19020 -48817.900015 -40222.826703
953 19040 -48753.570567 -40155.874179
954 19060 -48689.217472 -40088.899446
955 19080 -48624.840767 -40021.902537
956 19100 -48560.440488 -39954.883487
957 19120 -48496.016672 -39887.842331
958 19140 -48431.569356 -39820.779101
959 19160 -48367.098576 -39753.693833
960 19180 -48302.604368 -39686.586561
961 19200 -48238.086769 -39619.457317
962 19220 -48173.545814 -39552.306136
963 19240 -48108.981540 -39485.133053
964 19260 -48044.393983 -39417.938099
965 19280 -47979.783179 -39350.721309
966 19300 -47915.149162 -39283.482716
967 19320 -47850.491970 -39216.222353
968 19340 -47785.811637 -39148.940254
969 19360 -47721.108199 -39081.636451
970 19380 -47656.381691 -39014.310979
971 19400 -47591.632148 -38946.963869
972 19420 -47526.859606 -38879.595154
973 19440 -47462.064100 -38812.204868
974 19460 -47397.245664 -38744.793043
975 19480 -47332.404334 -38677.359712
976 19500 -47267.540144 -38609.904906
977 19520 -47202.653129 -38542.428660
978 19540 -47137.743323 -38474.931004
979 19560 -47072.810762 -38407.411971
980 19580 -47007.855479 -38339.871593
981 19600 -46942.877509 -38272.309902
982 19620 -46877.876886 -38204.726931
983 19640 -46812.853643 -38137.122711
984 19660 -46747.807816 -38069.497275
985 19680 -46682.739438 -38001.850652
986 19700 -46617.648544 -37934.182877
987 19720 -46552.535165 -37866.493979
988 19740 -46487.399337 -37798.783990
989 19760 -46422.241094 -37731.052943
990 19780 -46357.060467 -37663.300867
991 19800 -46291.857492 -37595.527795
992 19820 -46226.632200 -37527.733757
993 19840 -46161.384627 -37459.918785
994 19860 -46096.114803 -37392.082910
995 19880 -46030.822764 -37324.226162
996 19900 -45965.508541 -37256.348572
997 19920 -45900.172167 -37188.450171
998 19940 -45834.813676 -37120.530990
999 19960 -45769.433100 -37052.591059
1000 19980 -45704.030471 -36984.630408
1001 20000 -45638.605823 -36916.649069

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