
// Make newform 147.6.e.n in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_147_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_147_e_Hecke();"
// 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_e_n();".
// 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_e_n();".
// 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 := [2304, 48, 49, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [2304, -49, 49, -1], [97, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2352, 49];
    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_147_e();"
function MakeCharacter_147_e()
    N := 147;
    order := 3;
    char_gens := [50, 52];
    v := [3, 2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_147_e_Hecke();"
function MakeCharacter_147_e_Hecke(Kf)
    N := 147;
    order := 3;
    char_gens := [50, 52];
    char_values := [[1, 0, 0, 0], [0, 0, -1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := [[0, 1, 1, 0], [9, 0, -9, 0], [0, 0, -36, 0], [0, 0, 0, 0], [-244, 8, 236, 8], [684, 0, 0, -72], [-360, -216, 576, -216], [0, 288, -36, 0], [0, 56, 224, 0], [3506, 0, 0, -640], [-4680, -576, 5256, -576], [0, 1056, -6090, 0], [10584, 0, 0, -216], [-4332, 0, 0, 2400], [0, -3456, -2232, 0], [-518, -1184, 1702, -1184], [-15300, 864, 14436, 864], [0, -4680, 10980, 0], [7100, 6480, -13580, 6480], [-44864, 0, 0, -2552], [-27360, -1872, 29232, -1872], [0, 6480, 24808, 0], [-30924, 0, 0, -9504], [0, 3672, -63432, 0], [27720, 0, 0, -19584], [64620, -11664, -52956, -11664], [0, 1440, -126648, 0], [0, 2488, -101588, 0], [-8226, 5760, 2466, 5760], [-12526, 0, 0, -1888], [26944, 0, 0, -22032], [0, -16416, -51732, 0], [-393302, -3968, 397270, -3968], [-121428, 0, 0, 14976], [0, 41056, -240806, 0], [209544, -2208, -207336, -2208], [-369468, 14040, 355428, 14040], [0, -45360, 246692, 0], [270216, 0, 0, -6912], [0, 16416, -484380, 0], [194060, 3128, -197188, 3128], [14940, 0, 0, 23256], [0, -568, -177184, 0], [-572418, 61056, 511362, 61056], [-270410, 0, 0, 66496], [-566568, -23328, 589896, -23328], [-545772, 0, 0, -21504], [-453168, 0, 0, 66528], [-948276, 27648, 920628, 27648], [0, 90072, -360612, 0], [0, -118688, -252758, 0], [-6608, 0, 0, -14600], [710424, 26784, -737208, 26784], [-874548, 0, 0, 74304], [0, -110376, -155016, 0], [774928, -72728, -702200, -72728], [105012, 184464, -289476, 184464], [0, -143136, -1100448, 0], [431194, 106272, -537466, 106272], [694150, 0, 0, -69344], [179100, 122400, -301500, 122400], [169380, 0, 0, 50544], [-1683756, 0, 0, -215712], [1639008, -126144, -1512864, -126144], [0, 176976, 517608, 0], [0, -88480, 68882, 0], [0, 88704, -837828, 0], [-2355954, 0, 0, 107328], [1183820, -192328, -991492, -192328], [2298636, 0, 0, -58680], [-312480, -195912, 508392, -195912], [0, -522904, 1104848, 0], [-1388880, -239328, 1628208, -239328], [0, 5184, 2345210, 0], [1682772, 0, 0, -305472], [0, -103680, 2929320, 0], [3007094, -102016, -2905078, -102016], [0, -614232, 627228, 0], [0, 430400, -3229870, 0], [2095128, 195840, -2290968, 195840], [-1183716, 0, 0, 429408], [-206186, 0, 0, -764640], [-417728, 567832, -150104, 567832], [4024296, 0, 0, 345168], [0, -2304, 4535424, 0], [0, 573544, -1418180, 0], [1743938, 0, 0, 565664], [0, -377664, 163830, 0], [1510668, 0, 0, 403488], [-3920392, 0, 0, -219024], [0, -803520, 2773836, 0], [4286232, 305856, -4592088, 305856], [321576, -669504, 347928, -669504], [2265884, 0, 0, -427480], [0, -220608, -1735524, 0], [-4435920, 0, 0, 93312], [0, 1225584, -2401308, 0], [5423256, 154440, -5577696, 154440], [0, 1247040, -326628, 0], [0, 142560, 8985202, 0], [-5231092, 0, 0, 1227312], [516002, 1486688, -2002690, 1486688], [-6524604, 619488, 5905116, 619488], [0, 60832, -7776038, 0], [7205372, 34992, -7240364, 34992], [6309648, 77184, -6386832, 77184], [-8087364, 0, 0, -110592], [0, 207144, -11007720, 0], [365008, -1265384, 900376, -1265384], [3089376, 0, 0, 581184], [0, -838368, 373824, 0], [-4980202, -176256, 5156458, -176256], [-1087946, 0, 0, -1168832], [2545164, 1440000, -3985164, 1440000], [-2854352, 0, 0, 975888], [-3214654, -660160, 3874814, -660160], [-6322788, 0, 0, 428832], [-7431336, -810432, 8241768, -810432], [0, 2363744, 759998, 0], [3530396, 0, 0, -416008], [-8252676, 1112328, 7140348, 1112328], [-15292770, 0, 0, 1152192], [0, -1301184, -3928428, 0], [7058404, 1467256, -8525660, 1467256], [0, 281664, -693684, 0], [1391282, 0, 0, 1114016], [0, 128640, 10356438, 0], [0, 2025216, 12297816, 0], [-17656488, 0, 0, -1208736], [0, 1670472, -16317468, 0], [-5921516, -2152656, 8074172, -2152656], [11575256, 0, 0, -779128], [0, -1690464, -2519616, 0], [8627418, 0, 0, 437184], [0, -556632, -21282192, 0], [6337656, 0, 0, -1404432], [-6371892, -848016, 7219908, -848016], [10727244, -2669184, -8058060, -2669184], [6616332, 0, 0, -2368224], [1308374, 3991136, -5299510, 3991136], [-11268684, 0, 0, -2441088], [0, -1346336, -3445670, 0], [-4406624, -1310256, 5716880, -1310256], [20523740, 0, 0, 1245848], [17072316, 383832, -17456148, 383832], [-13637736, 0, 0, -694656], [4903884, 0, 0, 2107656], [5860296, 2537784, -8398080, 2537784], [0, -3465504, 16767108, 0], [0, -1680808, 28501424, 0], [0, -1342656, -16747586, 0], [-5316912, 0, 0, 169128], [11004468, 0, 0, 936960], [0, -1729728, 4163688, 0], [22767204, -133152, -22634052, -133152], [-11560496, 0, 0, 833896], [0, 330720, 28917744, 0], [0, 1083240, 6242832, 0], [15015456, 0, 0, -2032992], [11982060, -3253824, -8728236, -3253824], [0, -1440296, -5572724, 0], [-27057638, 0, 0, 506560], [2462696, 0, 0, 2694384], [0, 243648, -18375012, 0], [-3611566, 613856, 2997710, 613856], [-6428664, -6148224, 12576888, -6148224], [-10471944, 3774048, 6697896, 3774048], [-23829444, -2237112, 26066556, -2237112]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_6_e_n();".
// 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_e_n(:prec:=4)
    chi := MakeCharacter_147_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_e_n();".
// 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_e_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![2116, 138, 55, -3, 1]>,<5,R![1296, 36, 1]>],Snew);
    return Vf;
end function;