
// Make newform 147.4.e.j in Magma, downloaded from the LMFDB on 20 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_4_e_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_4_e_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 := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 2, 2];
    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], [-1, 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 := 4;
    raw_aps := [[-1, 1, -1, 0], [0, 0, 3, 0], [10, -7, 10, 0], [0, 0, 0, 0], [0, -24, -10, -24], [-52, 0, 0, -25], [0, -45, -58, -45], [96, 22, 96, 0], [-14, -28, -14, 0], [148, 0, 0, -62], [0, -50, 52, -50], [124, 48, 124, 0], [-10, 0, 0, 219], [-360, 0, 0, -100], [-48, -250, -48, 0], [0, 360, 134, 360], [0, -226, 308, -226], [8, 3, 8, 0], [0, 524, -72, 524], [494, 0, 0, -232], [0, 401, -52, 401], [472, 236, 472, 0], [-508, 0, 0, -80], [-194, -339, -194, 0], [-244, 0, 0, 599], [0, -323, -914, -323], [1100, 222, 1100, 0], [270, 52, 270, 0], [0, -4, -312, -4], [1398, 0, 0, 424], [-620, 0, 0, -1196], [1180, 944, 1180, 0], [0, -94, 384, -94], [-36, 0, 0, -652], [594, -832, 594, 0], [0, -96, -2676, -96], [0, -981, -304, -981], [-1464, 1064, -1464, 0], [2496, 0, 0, -286], [838, 925, 838, 0], [0, 52, 1130, 52], [-1292, 0, 0, 1201], [1314, 1056, 1314, 0], [0, 2024, 474, 2024], [-3446, 0, 0, 552], [0, 1076, 2864, 1076], [1460, 0, 0, -588], [376, 0, 0, 420], [0, -1030, 3292, -1030], [-3788, 717, -3788, 0], [2072, -766, 2072, 0], [1702, 0, 0, 1796], [0, -2757, 444, -2757], [1316, 0, 0, -3054], [3370, -777, 3370, 0], [0, -2120, 3162, -2120], [0, -63, -5078, -63], [3028, -994, 3028, 0], [0, -2024, -1750, -2024], [344, 0, 0, 1746], [0, 2022, -288, 2022], [-6234, 0, 0, -2057], [2768, 0, 0, 1330], [0, -674, 5464, -674], [4792, -761, 4792, 0], [-1490, -3864, -1490, 0], [9388, 336, 9388, 0], [-984, 0, 0, 3508], [0, -2000, 3998, -2000], [4992, 0, 0, -2959], [0, -6543, -1334, -6543], [102, -6164, 102, 0], [0, -6880, 1432, -6880], [1198, 2776, 1198, 0], [1292, 0, 0, 1976], [1080, -8452, 1080, 0], [0, 1702, 2212, 1702], [-2288, 8411, -2288, 0], [248, 7250, 248, 0], [0, 5905, 3716, 5905], [684, 0, 0, -4018], [-2718, 0, 0, 1424], [0, 4980, -3290, 4980], [-11408, 0, 0, 213], [-11112, -2696, -11112, 0], [-3786, 8700, -3786, 0], [3798, 0, 0, 6904], [1506, 8736, 1506, 0], [954, 0, 0, 569], [-4, 0, 0, -3824], [5908, -1558, 5908, 0], [0, 4514, 4288, 4514], [0, -1328, 7732, -1328], [-3826, 0, 0, -5580], [2192, -4952, 2192, 0], [-16560, 0, 0, 312], [9358, 3139, 9358, 0], [0, 3545, -8950, 3545], [1852, 11036, 1852, 0], [-16782, -1640, -16782, 0], [14548, 0, 0, -612], [0, -4536, -14078, -4536], [0, 8702, -5164, 8702], [4888, -6354, 4888, 0], [0, 332, -3308, 332], [0, 10375, 792, 10375], [-5804, 0, 0, 5798], [6554, -35, 6554, 0], [0, -10560, 2318, -10560], [-10552, 0, 0, -1613], [6216, -1700, 6216, 0], [0, 2200, 8248, 2200], [-10016, 0, 0, 5838], [0, -11724, -13020, -11724], [-10068, 0, 0, -12200], [0, 7670, -11080, 7670], [3832, 0, 0, 1410], [0, 15134, -2808, 15134], [27244, 1146, 27244, 0], [-26170, 0, 0, 2048], [0, -5677, 12008, -5677], [-9156, 0, 0, -6344], [-1434, -10783, -1434, 0], [0, 7924, 6602, 7924], [-1116, -13456, -1116, 0], [4292, 0, 0, 4958], [8336, -5452, 8336, 0], [-17384, 5520, -17384, 0], [2732, 0, 0, -9906], [8612, -10683, 8612, 0], [0, -2696, 4312, -2696], [7294, 0, 0, 9660], [18856, -13684, 18856, 0], [21992, 0, 0, 6340], [-32762, 5153, -32762, 0], [18528, 0, 0, -7443], [0, -5597, 6018, -5597], [0, -1792, -40116, -1792], [-13682, 0, 0, 16363], [0, 2472, 1586, 2472], [-27492, 0, 0, -11044], [-10498, -4576, -10498, 0], [0, -12612, 21832, -12612], [8274, 0, 0, 15460], [0, 19065, 3684, 19065], [8496, 0, 0, 706], [11520, 0, 0, 829], [0, 7303, -11878, 7303], [17504, -1638, 17504, 0], [-38982, -1816, -38982, 0], [-23640, -2968, -23640, 0], [13886, 0, 0, 7989], [-10216, 0, 0, 4360], [9312, -22834, 9312, 0], [0, -6768, -13144, -6768], [32190, 0, 0, -1932], [-4196, -5308, -4196, 0], [-42282, -7647, -42282, 0], [28192, 0, 0, -7657], [0, -12831, -12598, -12831], [10830, -19184, 10830, 0], [-10766, 0, 0, 528], [4828, 0, 0, 16268], [-23276, -3536, -23276, 0], [0, 10282, -28488, 10282], [0, 1408, -28568, 1408], [0, 21636, -14348, 21636], [0, 16907, -5096, 16907]];
    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_4_e_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_4_e_j(:prec:=4)
    chi := MakeCharacter_147_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_4_e_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_4_e_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![1, -2, 5, 2, 1]>,<5,R![4, -40, 398, -20, 1]>],Snew);
    return Vf;
end function;