
// Make newform 245.4.e.l in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_245_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_245_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_245_4_e_l();".
// 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_245_4_e_l();".
// 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_245_e();"
function MakeCharacter_245_e()
    N := 245;
    order := 3;
    char_gens := [197, 101];
    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_245_e_Hecke();"
function MakeCharacter_245_e_Hecke(Kf)
    N := 245;
    order := 3;
    char_gens := [197, 101];
    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 := 4;
    raw_aps := [[0, 1, -3, 1], [-1, -3, -1, 0], [0, 0, -5, 0], [0, 0, 0, 0], [-14, -10, -14, 0], [18, 0, 0, 10], [-38, 54, -38, 0], [0, -26, -80, -26], [0, -117, -11, -117], [-125, 0, 0, 60], [-66, -100, -66, 0], [0, 136, -208, 136], [53, 0, 0, 30], [-333, 0, 0, -5], [0, 64, 98, 64], [476, -142, 476, 0], [420, -266, 420, 0], [0, -570, -49, -570], [643, -69, 643, 0], [532, 0, 0, -350], [-86, -118, -86, 0], [0, -214, -620, -214], [953, 0, 0, -5], [0, 324, -325, 324], [314, 0, 0, -500], [709, 180, 709, 0], [0, 387, 1251, 387], [0, -239, 617, -239], [-535, -174, -535, 0], [332, 0, 0, 650], [-194, 0, 0, -1240], [0, 960, 296, 960], [868, -404, 868, 0], [-450, 0, 0, -150], [0, 1246, 975, 1246], [2206, 960, 2206, 0], [1102, 104, 1102, 0], [0, -692, -986, -692], [-991, 0, 0, -1365], [0, -448, 3336, -448], [1270, 116, 1270, 0], [-1297, 0, 0, 1000], [0, -1590, -1246, -1590], [-814, 1728, -814, 0], [326, 0, 0, -260], [-700, -2196, -700, 0], [292, 0, 0, 650], [-1082, 0, 0, -2180], [382, -1396, 382, 0], [0, 2924, -210, 2924], [0, -102, 2314, -102], [690, 0, 0, -260], [-1306, 2620, -1306, 0], [-62, 0, 0, -2740], [0, -2516, 68, -2516], [-3169, -2797, -3169, 0], [1815, -1586, 1815, 0], [0, -1870, -5544, -1870], [-562, 3346, -562, 0], [1822, 0, 0, 380], [954, 2712, 954, 0], [-3872, 0, 0, 2200], [3519, 0, 0, 1025], [-1456, 2530, -1456, 0], [0, 732, -2564, 732], [0, 566, 4342, 566], [0, 560, -6566, 560], [-1404, 0, 0, -650], [6623, 831, 6623, 0], [945, 0, 0, 200], [4304, 1642, 4304, 0], [0, 7996, -910, 7996], [7947, -4031, 7947, 0], [0, 2028, -2676, 2028], [-2600, 0, 0, 1390], [0, 1557, -3589, 1557], [-3050, -1084, -3050, 0], [0, 384, 8418, 384], [0, 8040, 259, 8040], [-3195, 5314, -3195, 0], [-8220, 0, 0, -280], [6817, 0, 0, -90], [-4714, 1030, -4714, 0], [1428, 0, 0, -10910], [0, 1504, -10080, 1504], [0, -3867, -4241, -3867], [-6455, 0, 0, 3750], [0, -5644, 10732, -5644], [3098, 0, 0, 40], [4737, 0, 0, -485], [0, 7739, -5397, 7739], [1830, -496, 1830, 0], [1818, -4684, 1818, 0], [-13838, 0, 0, -980], [0, 1706, -14210, 1706], [7303, 0, 0, 7215], [0, 1024, -10935, 1024], [-17666, -1900, -17666, 0], [0, -1948, -6474, -1948], [0, -160, -471, -160], [-10359, 0, 0, -1645], [4238, -12604, 4238, 0], [12539, 3307, 12539, 0], [0, -13924, 1250, -13924], [8956, 1290, 8956, 0], [-14298, 944, -14298, 0], [17794, 0, 0, 1140], [0, -5518, 7056, -5518], [3340, -5084, 3340, 0], [-6702, 0, 0, -3460], [0, -8421, 11373, -8421], [-3674, -7822, -3674, 0], [5136, 0, 0, -3160], [-9010, 294, -9010, 0], [6622, 0, 0, 15150], [-6959, 11490, -6959, 0], [3078, 0, 0, 860], [6067, 16589, 6067, 0], [0, -3282, 9834, -3282], [-5560, 0, 0, 4370], [7449, 950, 7449, 0], [7032, 0, 0, -16500], [0, 4454, 11118, 4454], [-319, -6017, -319, 0], [0, -3590, -5554, -3590], [4877, 0, 0, -17850], [0, -6364, 13415, -6364], [0, -15306, 4510, -15306], [8469, 0, 0, 3675], [0, 7272, -14754, 7272], [-390, 2026, -390, 0], [437, 0, 0, -6225], [0, 5380, -2556, 5380], [-14374, 0, 0, 16550], [0, -14120, 12466, -14120], [-790, 0, 0, 16560], [5114, 1702, 5114, 0], [-20603, 3169, -20603, 0], [12424, 0, 0, -14000], [11115, 9496, 11115, 0], [-28542, 0, 0, -6900], [0, 5000, 11014, 5000], [-12969, 3913, -12969, 0], [7261, 0, 0, -13595], [-27670, 5864, -27670, 0], [12410, 0, 0, 5490], [-28932, 0, 0, 2570], [19702, -13836, 19702, 0], [0, -1456, 12440, -1456], [0, 17593, 469, 17593], [0, -6174, -13788, -6174], [-407, 0, 0, -9070], [-29918, 0, 0, 11380], [0, -3441, 32953, -3441], [14703, 11141, 14703, 0], [-9498, 0, 0, 12270], [0, -15214, -16580, -15214], [0, -18406, -10325, -18406], [2964, 0, 0, 28880], [29094, 17700, 29094, 0], [0, -11839, -21603, -11839], [-17048, 0, 0, -1380], [-12889, 0, 0, -14005], [0, -16160, -16664, -16160], [18358, 14496, 18358, 0], [8149, -6123, 8149, 0], [7486, 20320, 7486, 0], [-29248, 5934, -29248, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_245_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_245_4_e_l();".
// 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_245_4_e_l(:prec:=4)
    chi := MakeCharacter_245_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_245_4_e_l();".
// 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_245_4_e_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_245_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![49, -42, 29, -6, 1]>,<3,R![289, -34, 21, 2, 1]>],Snew);
    return Vf;
end function;