
// Make newform 147.6.a.j in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_147_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_147_6_a_j();".
// 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_147_6_a_j();".
// 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 := [-48, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_147_a();"
function MakeCharacter_147_a()
    N := 147;
    order := 1;
    char_gens := [50, 52];
    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_147_a_Hecke(Kf)
    return MakeCharacter_147_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 := [[-1, -1], [9, 0], [-36, 0], [0, 0], [236, 8], [-612, -72], [-576, 216], [-36, 288], [-224, -56], [2866, 640], [-5256, 576], [6090, -1056], [-10368, -216], [-1932, -2400], [-2232, -3456], [1702, -1184], [-14436, -864], [10980, -4680], [-13580, 6480], [-47416, 2552], [-29232, 1872], [-24808, -6480], [40428, -9504], [-63432, 3672], [-8136, -19584], [52956, 11664], [-126648, 1440], [101588, -2488], [2466, 5760], [-14414, 1888], [4912, 22032], [-51732, -16416], [397270, -3968], [106452, 14976], [240806, -41056], [-207336, -2208], [-355428, -14040], [-246692, 45360], [-263304, -6912], [-484380, 16416], [-197188, 3128], [-38196, 23256], [177184, 568], [511362, 61056], [-203914, -66496], [-589896, 23328], [-567276, 21504], [386640, 66528], [-920628, -27648], [-360612, 90072], [252758, 118688], [-21208, 14600], [737208, -26784], [800244, 74304], [-155016, -110376], [-702200, -72728], [289476, -184464], [-1100448, -143136], [-537466, 106272], [624806, 69344], [301500, -122400], [-219924, 50544], [1899468, -215712], [1512864, 126144], [517608, 176976], [-68882, 88480], [837828, -88704], [-2248626, -107328], [-991492, -192328], [-2239956, -58680], [-508392, 195912], [-1104848, 522904], [-1628208, 239328], [-2345210, -5184], [1377300, 305472], [2929320, -103680], [-2905078, -102016], [627228, -614232], [3229870, -430400], [2290968, -195840], [754308, 429408], [-970826, 764640], [-150104, 567832], [-4369464, 345168], [4535424, -2304], [1418180, -573544], [2309602, -565664], [-163830, 377664], [-1914156, 403488], [-4139416, 219024], [2773836, -803520], [4592088, -305856], [347928, -669504], [1838404, 427480], [1735524, 220608], [4342608, 93312], [-2401308, 1225584], [5577696, -154440], [-326628, 1247040], [-8985202, -142560], [-4003780, -1227312], [-2002690, 1486688], [-5905116, -619488], [7776038, -60832], [-7240364, 34992], [6386832, -77184], [8197956, -110592], [-11007720, 207144], [900376, -1265384], [-3670560, 581184], [373824, -838368], [5156458, -176256], [-2256778, 1168832], [3985164, -1440000], [-1878464, -975888], [3874814, -660160], [5893956, 428832], [-8241768, 810432], [-759998, -2363744], [3114388, 416008], [-7140348, -1112328], [-14140578, -1152192], [-3928428, -1301184], [-8525660, 1467256], [-693684, 281664], [2505298, -1114016], [-10356438, -128640], [12297816, 2025216], [18865224, -1208736], [-16317468, 1670472], [8074172, -2152656], [10796128, 779128], [2519616, 1690464], [9064602, -437184], [-21282192, -556632], [-4933224, -1404432], [-7219908, 848016], [8058060, 2669184], [-4248108, -2368224], [-5299510, 3991136], [13709772, -2441088], [3445670, 1346336], [5716880, -1310256], [21769588, -1245848], [17456148, -383832], [14332392, -694656], [-7011540, 2107656], [8398080, -2537784], [16767108, -3465504], [-28501424, 1680808], [16747586, 1342656], [5147784, 169128], [11941428, -936960], [4163688, -1729728], [-22634052, -133152], [-10726600, -833896], [-28917744, -330720], [6242832, 1083240], [-12982464, -2032992], [8728236, 3253824], [5572724, 1440296], [-26551078, -506560], [5157080, -2694384], [-18375012, 243648], [2997710, 613856], [-12576888, 6148224], [6697896, 3774048], [-26066556, 2237112], [14804786, -3276288], [2764692, 5674752], [-9042188, -2003480], [-920916, 2915784], [-31854952, 2923544], [-26552938, 4453056], [5242104, 2507328], [-49562568, 3089448], [4744508, 5050512], [28774922, -4397728], [12448368, 1514880], [22505652, -4840488], [26360320, -2536272], [53597268, 2064960], [-9158966, -4320864], [14990040, 5654664], [45073240, 2709160], [-5335596, -6283008], [49009134, -1678272], [353340, -76608], [-18133514, 259200], [30409704, -2505600], [-27903456, -6194160], [21380708, 1075976], [10544004, 1361952], [28649988, -1530144], [35489900, -4109368], [-853992, 2995272], [3138798, 7994496], [18259474, 4574880], [28360152, 5873688], [-63370944, 114048], [2596382, 7008896], [2926728, -7167168], [11656836, 5105448], [82358640, 2168208], [-17489412, -10530432], [-8292564, -2665872], [60450984, 1243008], [-30644428, -2209912], [-17268694, -938368], [29458980, 7782624], [-51080146, 6023808], [35325036, -5407776], [-28328040, 12979584], [-6103044, 12336192], [9315072, 1620864], [13885272, 7607664], [5375688, -7733856], [22840128, -3927528], [-12180376, -14226232], [-38362190, -8358464], [-22609398, -8354496], [2234880, -8867520], [-40973086, 5343392], [-14845344, 1480224], [-103449924, -1652832], [1586682, -3000672], [12416760, -17392104], [42202744, -4028792], [18592848, -679104], [-20091764, 14621752], [34648002, -151488], [-55920780, -2824416], [-60683544, -4418784], [-80586230, 7875232], [-74374740, 410400], [-105726816, -8138880], [84589704, -6026400], [20503622, -7014208], [94177172, -3124456], [-17920080, 4138560], [107841892, 5179096], [-78458436, -4658688], [-40878864, 14264640], [-12393762, -4890336], [177102648, 40824], [-59468904, 594432], [29248416, 7133472], [-32526180, -136512], [48779956, -11696400], [110508352, 10806376], [39825582, -15251520], [576216, 19831176], [6979976, 17664824], [53471952, 930384], [-78925572, 17076528], [-112718180, -10673240], [20435834, 12423456], [17105508, 3842784], [26581788, 14285808], [-106636536, 6112656], [43567024, -5716656], [45660772, 21637432], [-28375884, -6100344], [65126484, 7285464], [69152400, -7272936], [-143795196, 7447680], [-41467778, 11426368], [130409352, -2212920], [5495500, -2196720], [-103045270, 7594400], [201174444, 2149992], [73376508, -22653024], [35066880, 11493936], [-28739040, 13790784], [164598768, 5041296], [51975792, 4778496], [-62638724, -4678616], [-63827826, -25041120], [-110792406, 884544], [-56576196, -2937600], [117350928, -16222464], [-43185808, 21921840], [-124433496, -3011904], [21853620, -17626536], [-155479284, 6772032], [-147610648, -15708136], [30825234, -14182848], [-110190746, 726208], [-54564984, 10482336], [124511688, -13110120], [-171740962, -8592256], [91862748, 30152736], [163153190, -15816256], [-133927524, -16444512], [-174413506, 10922688], [-244274508, 5699808], [-8472672, 4279968], [-56907900, 21116592], [104047236, 361152], [-166381740, -16042176], [65264040, -1050768], [144098818, 5545984], [-194237912, -8251632], [38585252, -23171992], [149083748, 5415984], [108992094, -17807808], [128846396, -10127944], [-113838300, 35496], [188355664, -21698408], [-232999434, 9870048], [-79545168, 314496], [-131988326, -100832], [200281918, -962720], [-192633252, -13992000], [125030840, 24167624], [279595152, 7317648], [-64955772, 6417792], [49911584, -43213336], [56257200, 11165040], [-92303378, -25003136], [68873652, -40591584], [-20061882, -24286464], [-255838932, -14361840], [30590280, -6879456], [-29699446, -28731808], [116450424, -26915904], [-223735444, -16530480], [-14878980, -22418784], [-80727832, 19155032], [-107940310, 494240], [-166326018, -27259584], [-57834254, -19342496], [-39426840, 22803840], [-4104540, 19611936], [4793564, 39083472], [202076892, -31269024], [-72398446, -3452544], [109624104, -34456104], [435255552, 5710608], [83521224, -20683008], [285791558, 14852160], [-19826138, 47596384], [51436836, 42813360], [121474176, -4785888], [73021490, 28386944], [209954756, -32656504], [-100633572, 35022744], [84779748, 31111200], [-37006128, -40277952], [61152444, -1257552], [202733028, 31259808], [-3767830, -16837632], [171175582, -30986048], [128500488, 43715520], [-167677082, -1368576], [-35500680, 3279528], [-45741960, 61622208], [-126018324, -47412864], [90059794, -60198752], [352335904, -23881544], [368235094, 5598720], [-228366504, 20836440], [135272312, 34020824], [-12293276, 15734728], [-24305796, 16795008], [22666806, 42502464], [-304660044, -39269232], [-453747944, 26512272], [-186361830, 56172096], [127339580, -35423848], [15726708, 17936928], [89201728, -70281416], [389185414, 22101760], [-16814880, -62474688], [-28517362, -34486560], [-499116708, -1113696], [337029224, -20831800], [104773536, -55279008], [-112175856, 49433976], [-99025416, -27862272], [-296061948, 15189120], [-105143830, -23607520], [261016800, 20771808], [-625322270, -7217216], [208984788, 8694144], [-18741312, -38560320], [373222536, -36591744], [-496937628, 5089032], [361118540, 16450128], [-14380920, -32277312], [-44513698, 26879040], [120349260, 17995392], [-103829612, 6306664], [84649716, -11067552], [424904096, -1432936], [242173446, 42516096], [-118596168, 32101632], [-54042624, -5845608], [-338665652, 12634704], [-373715942, 17145664], [-441320940, 29364984], [-22360798, 45387104], [167764744, -4822416], [-516911040, -17109144], [-512560908, 5511024], [-228827448, 61405920], [112689310, 57425760], [-446970254, 21808576], [392193252, 43481664], [-482953500, 11573280], [-577848384, -20854368], [-328072198, 18960704], [396776348, 44556728], [214667460, 23376672], [-151536970, -58221504], [174490452, -886032], [152586848, 57330824], [-34869240, -24592608], [-20833200, 64779480], [87979536, -6931008], [-249230414, -56825216], [-119075004, 83828376], [-417071216, 55771192], [345792852, -68714784], [603199944, 10758384], [-38832372, -16212528], [-251785868, 2323864], [222677414, -17871520], [-4663404, -87660864], [131715288, 70372800]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_a_j();".
// 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_147_6_a_j(:prec:=2)
    chi := MakeCharacter_147_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_147_6_a_j();".
// 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_147_6_a_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![-46, 3, 1]>,<5,R![36, 1]>],Snew);
    return Vf;
end function;