
// Make newform 138.6.a.h in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_138_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_138_6_a_h();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_138_6_a_h();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [-1873, -1025, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 2, 0], [-1366, -4, 2]];
    Rf_basisdens := [1, 1, 5];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_138_a();"
function MakeCharacter_138_a()
    N := 138;
    order := 1;
    char_gens := [47, 97];
    v := [1, 1];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

function MakeCharacter_138_a_Hecke(Kf)
    return MakeCharacter_138_a();
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 6;
    raw_aps := [[-4, 0, 0], [9, 0, 0], [-1, 1, 0], [-11, 1, -1], [102, 6, 1], [334, 4, 0], [125, -17, 1], [755, -33, 0], [-529, 0, 0], [-1248, 90, -10], [3830, 30, 10], [9418, -32, -1], [3934, -196, -10], [11231, 27, 70], [10510, 146, -70], [8073, 75, -22], [-4818, 78, -32], [13738, -8, -111], [6841, 493, 130], [-16176, -600, 182], [23914, -152, 152], [4075, 1303, -231], [-19834, -434, 303], [-43269, 333, 41], [7412, -1422, -208], [8418, 204, -152], [-725, -2489, 343], [-72546, -858, -557], [-30840, -1810, 91], [-14089, 1681, 521], [78122, 1074, 978], [-51488, 1292, -1410], [-76017, -543, 1039], [80100, -4936, 16], [-106685, -4195, 200], [36164, -3516, -1172], [-86270, 3856, -861], [-54034, 2706, 2080], [-80800, -5264, -150], [62000, -8582, 118], [-279234, 3702, 114], [-171368, -962, -669], [459014, -1970, 956], [-84290, 7312, -1662], [-21144, 4602, -3340], [-177081, -12181, -631], [229466, 5598, 1884], [521992, 7576, 2212], [167144, -21224, -817], [-324472, -6618, 3507], [115190, -12812, 1664], [58744, 15536, -1308], [-401100, -11494, 1776], [724844, 1156, 5197], [-126414, -13416, 458], [306262, 9038, 1582], [309104, 8986, 648], [-1209042, 4070, -638], [-1097662, 1140, 202], [206667, 4677, 8461], [663241, 7837, -5292], [91237, -21169, -2542], [-37426, -8454, 3924], [343482, -16266, 1876], [-1299418, 8172, 1714], [-2086548, -14994, 5306], [-341906, -25166, 4496], [-1013820, -5446, 8346], [1179098, -9878, -11446], [159990, 17876, 9634], [479936, -9530, -7350], [1198138, -1318, -5904], [1162019, -20481, 17061], [-937446, -34912, 2223], [1992411, -9361, -6160], [2643550, -10282, 6918], [3001205, -23129, 4720], [-1295738, 39784, -16144], [-1223871, 57891, 8057], [-1161950, -36092, -10462], [940766, 17734, -9525], [457912, -48026, 2813], [-809128, -77384, 10342], [1565526, 82940, -18526], [-262788, 32564, 26020], [-2853432, -72324, 7436], [702682, 5096, -25314], [-565336, 110790, 544], [-3236816, -56494, 14096], [2212924, -1100, 21820], [3429568, -42304, 17937], [-5591468, 26876, -2304], [2402448, -89296, -8156], [-8115002, -36610, 6510], [3163550, 42690, 792], [-1843140, -77340, -24026], [-5654744, -4486, -9490], [-4977113, 11729, -30781], [290733, 8969, -11854], [-5433886, -20616, 22582], [-524622, 132614, -25916], [3531713, -72725, -8720], [-1853688, 152328, 34703], [-9273067, -8921, 20101], [4281121, 65821, 1186], [4625338, -76844, 27894], [-1465744, -17564, 842], [6800782, 67724, 33898], [4573288, -53416, -31872], [-1369262, 83908, -32010], [3883904, 114888, 42056], [-1763550, 36416, -24559], [3693439, -107887, -39011], [14995905, -30763, -6460], [-7467389, -163121, 15427], [8030749, 124895, -18883], [13902543, -6733, 734], [-1364630, 51758, -14864], [-5445202, 31384, 14104], [-3612416, 127616, -51723], [8169238, 178324, -12343], [-10837670, -104324, 23862], [832811, -253259, -45404], [6187660, 25760, 59478], [8258158, 93826, -49760], [300947, 146917, -46982], [-12559652, 85186, 8733], [-13840998, 178038, -21028], [-13369919, 57277, -14953], [20141788, -61742, -9287], [-6041072, -203636, 24444], [907070, -163394, 44734], [-11721107, -76391, 56437], [4227648, 377942, -22953], [-3452168, -126874, -61142], [-7258258, 260412, 30974], [8043639, 112761, -62782], [3020911, 283411, -73390], [616253, -275393, 57176], [14179180, 127586, 13402], [-17611194, -44206, -3080], [-8700202, 8080, -56688], [14761846, 40030, 43890], [11146872, -8688, 88841], [8532130, -20360, 139042], [5240642, -106838, -114402], [-11639514, -114256, -130240], [-5762806, 345832, 49216], [-5474676, -304600, -89296], [-10552314, -142758, -117072], [-11978586, 217808, 69794], [-1524301, 455941, -123395], [-4808558, 174478, -150928], [10942294, 192362, -100206], [-4433587, -79599, 56408], [-27167676, -189900, 6042], [-5046701, 465559, -53611], [-9459034, -61124, 137002], [15473758, 43636, 204124], [2701407, 213033, -56444], [3317966, 308902, 110822], [9791001, 421899, 15253], [23834810, -120918, -39918], [27444940, -12652, 42779], [44125987, 122981, -21133], [2149820, 310468, -118682], [1723870, -444482, -86362], [-11832034, -493884, 20522], [11840252, -117318, -96396], [13265650, -636268, 44320], [19941124, -42724, 66021], [14355446, 357888, 5182], [-47801082, 90198, 20174], [3482644, 117370, 84298], [-1195394, 223270, -60218], [-7839191, -412597, 31751], [-14628952, -766428, 145320], [-23775254, 440756, 13190], [-3251429, -219737, -59857], [22480014, 183284, -20109], [-41385170, 377302, -16118], [9701560, -314416, -27167], [20762306, -442476, 69962], [-7435792, 70534, -113974], [27324892, 361004, 111390], [8259537, -776277, 108356], [673870, 398896, 75112], [11791603, -323033, 257398], [-18339398, 92440, -126132], [-50128586, 257522, 19590], [-56208690, 297188, -44604], [9127852, -745456, -221414], [22328419, -322169, 9458], [14735626, 175940, 188414], [2409910, -603394, 57023], [4713545, 319747, -270961], [-40944474, 931196, 116780], [5901000, -721858, -89659], [14557149, -675753, 63823], [-1757868, -532212, 99200], [32865389, 699703, -171764], [-14646230, 1180138, 123638], [31679946, -226948, -89794], [-44449292, -135398, -102286], [57995986, -244558, 119021], [-27288100, 1224718, 96342], [-10969111, 611037, 6887], [35843502, -808410, 20392], [36881202, 520296, 203634], [58343394, 606446, -53220], [23430342, 859292, -222088], [19254206, 59272, -11466], [-14592359, -526547, -182347], [63050458, 397106, -126603], [22702288, -240088, -176086], [-1825834, 986796, -342802], [8004816, -1331896, -262028], [-27156892, 735874, 287266], [54553960, -314752, 168156], [-39129546, 31128, 325250], [3316958, -1459764, -6468], [42390217, 1681117, -187487], [-8914688, 724718, 314482], [-52124189, 1099447, 237087], [-66596512, 742468, -169356], [-16708146, -1493116, 285296], [47840215, 1426889, -292023], [107315018, 161566, -89426], [8450389, 2054953, -39331], [44577608, -1135076, 117696], [70887706, -1527296, -119462], [78195985, 814957, -7700], [29693625, 724277, -111091], [-54664616, 71846, 119652], [38383385, 137421, -281028], [-69562324, 670108, -111434], [89661888, -283282, -6372], [-25251143, 1127507, -327456], [37945388, 1688632, -208542], [-92026854, -69834, -65190], [47181748, 250052, -159009], [38072410, -1546826, -137952], [31001638, -1834322, 49526], [-35694426, 293108, 437496], [-69057650, 415364, 4700], [38293276, 936980, 339956], [-46268302, -623742, -93986], [5853030, 590574, 96647], [66505609, -672659, 517592], [30466884, 668868, 710324], [-89312144, 1100038, -336709], [28712313, -2093373, -508181], [-26651510, -2457430, 314222], [-36862988, -898406, 149478], [32907174, -569880, 435958], [5883712, -2208676, 89904], [113426436, 544922, 54971], [-56752143, 1984685, -2506], [-110423244, 353838, -548212], [15428822, 1371600, 122946], [-23430445, -462537, -883421], [-96527882, -1740058, 228255], [30285258, -2198644, 197234], [-72477646, 211656, -70203], [-57999418, -1282916, 678580], [-91648599, -1315483, -181592], [55991853, -572961, -375732], [-33159755, -1441081, 101699], [58985825, 1091061, -337712], [64657912, 1145714, -540748], [-37028898, -52312, 353898], [-22992505, -364677, -215902], [-19008904, 557622, 482306], [1104857, -644571, -524007], [-15554194, -3104148, 63164], [-59258412, -3187060, 133384], [-61244630, 291386, -714100], [120003702, 678728, 77848], [-18326778, 1634300, 327468], [-10738662, 2769378, 505013], [-84005414, -1626514, 433420], [38265151, 2447515, -801317], [-131585370, 925110, 173520], [60729106, 2289088, -237939], [61218434, 288438, -736544], [137689334, -1054286, 43364], [-26235388, -598422, -17788], [33295073, 2800531, 156504], [-154112344, -942216, 559280], [162512700, 1010250, 463748], [-77466057, -1033095, 773584], [-62959110, -1174038, 775535], [64619252, 869194, 255168], [-200719800, 97632, 141731], [58493214, -2309332, -27710], [-55055201, -2416327, 89528], [235625285, 952713, -260341], [-159598486, -2263532, -146268], [81088812, 1441392, 4892], [196430428, -2222528, -285096], [-78554702, 2885752, -286838], [79840473, -326349, 274140], [-44138135, -1762355, -215953], [84407414, -951098, -463068], [28411719, -2781349, -32596], [-6844378, 3497448, -58010], [227603964, -2150568, -216636], [-53360328, -19906, -1153637], [-6215094, -2172174, -353034], [248962238, 1688964, -310796], [-42589682, 2540066, -740328], [134553425, 342811, -500954], [-150712225, -908447, -840033], [-148170776, 496060, 119492], [47439848, -1378808, -619362], [258396200, -1949874, -217974], [-49838532, 1563024, 442300], [-211509374, 45038, -702606], [-75728284, 733122, 584774], [-2151674, 1970012, 452434], [-154535389, 768855, -216918], [-204513910, -1761144, 316038], [-22859974, 1080676, 986026], [88276796, 1542324, 853240], [248396129, 935875, 485121], [-45018032, -5540546, -35704], [-178892549, 3065887, -104970], [-81676072, -4719540, -86024], [-260024304, 2706216, 433208], [-23546851, -1124297, 1575388], [-31512358, 299364, -1491362], [-130459730, 1293152, -688804], [-268355868, -2757316, 108576], [-113850360, -5744532, 749478], [-4645475, 2395297, -790712], [7870626, 652482, -384798], [6600062, 607500, -688349], [209310771, 1700133, 826373], [33967070, -4166760, -188190], [-320267673, 2609723, -27717], [99218590, -2067692, 184836], [-103626859, -1332545, 2213], [-84488242, -5739128, 408770], [47659201, 770221, -1255525], [276416655, 1606473, 106770], [112980456, 2255988, -925292], [130596206, -536484, 743588], [195587526, 3461546, 137464], [-127442324, 1265708, 803718], [-207254647, 490723, 145198], [-193050264, 1683668, -1123024], [242274342, 1425632, 811010], [125413132, -4034242, 1191812], [55596959, 2730075, 1219425], [-282997158, -280264, 380617], [66570232, -3832162, 1225490], [83506410, -761022, -182498], [100897108, -1929484, -953335], [-11570692, -3624158, -320310], [319682818, -3658642, -287270], [148747278, 1522916, -127907], [250775320, 1471502, 1125756], [78271864, 1978208, 6422], [-2871592, -1122008, -1855061], [-274840744, 1062660, -1364744], [-65847866, -1007468, 803536], [-563718, -689844, 1719806], [389165027, 462903, 963209], [-89646278, 5150416, 23474], [41388854, 5823430, 626334], [237419530, -4880714, -16212], [115710044, 3078868, 488670], [-172808401, -5877815, 942566], [-201839489, 1525339, 1255439], [-111621346, 7169040, 559856], [-156008756, 7961624, 1665730], [237238540, -3479884, 1256542], [-99958914, 5874404, 889488], [160171159, -2331079, 2041755], [-154658586, 1958912, 600758], [-123350171, 5875607, 1257996], [-98366045, 2988389, -1510829], [25553886, 6334478, -1429198], [-87680970, -4121388, -543402], [-633413, -3165017, -2538204], [15534108, 2026908, -2682880], [499482726, -1950922, 344874], [193448474, -2210340, -942406], [-92627379, 7181513, 406266], [-303878764, 1491436, -686638], [-251380668, -271582, 619082], [-48985812, 2814222, -1395688], [153801042, 1836522, 2626220], [-298017746, -1920350, 934696], [-68663876, -1191652, 184158], [459054976, 1553998, 974896], [-184051479, -2518459, -69751], [1290791, -4608815, 1937755], [-126951157, 2662215, 1411802], [-150255678, -1977828, 2039494], [-81662410, -3624312, -1240372], [242300216, -1945754, 2399020], [-384984765, 971999, -2602783], [-315310411, 4003639, 1020329], [12127034, 6070636, -514230], [403033780, 7067332, -1071456], [-83693892, -2079478, -517365], [-181350320, 1254998, -47000], [-350771771, -1445327, -1014260], [-272156838, -8783670, 1221938], [115748530, 5630788, 642754], [4412522, -3473276, -1436910], [320488834, 2941730, 1507215], [280406817, -2689195, 257066], [-493691724, 1136330, -207030], [-247744560, -1960854, 313878], [-71341282, 3557746, -2346630], [149477454, -4492090, 4590], [223389303, -3686007, 96003], [-221460586, -2358338, -728520], [-255971947, 941479, -362768], [393786110, 851436, 304419], [162998564, 4250740, 1494500], [-29849558, 2534498, 2468072], [77749130, -5496672, -101396], [-138668074, -8680256, 149528], [-54205384, -10495280, -958345], [-18163414, -10077344, -110480], [492540270, 3540722, -252152], [243447034, -3096706, 2794862]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_138_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_138_6_a_h();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_138_6_a_h(:prec:=3)
    chi := MakeCharacter_138_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_138_6_a_h();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_138_6_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_138_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![-23184, -4096, 4, 1]>],Snew);
    return Vf;
end function;