
// Make newform 245.4.e.h in Magma, downloaded from the LMFDB on 25 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_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_245_4_e_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 := [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, 4, -1], [-1, -4, -1, 0], [0, 0, -5, 0], [0, 0, 0, 0], [7, -32, 7, 0], [25, 0, 0, 4], [25, 44, 25, 0], [0, -44, 18, -44], [0, 68, 122, 68], [-13, 0, 0, 24], [60, -180, 60, 0], [0, 60, 282, 60], [-164, 0, 0, 124], [-130, 0, 0, 68], [0, 132, -175, 132], [28, 128, 28, 0], [616, 0, 616, 0], [0, 108, 168, 108], [76, -64, 76, 0], [-952, 0, 0, 0], [-338, -344, -338, 0], [0, -248, 507, -248], [-188, 0, 0, 600], [0, -44, -108, -44], [1371, 0, 0, 220], [338, -68, 338, 0], [0, -716, -667, -716], [0, 456, 862, 456], [-241, 1056, -241, 0], [-242, 0, 0, -1140], [1304, 0, 0, 64], [0, -1324, -110, -1324], [-644, -1108, -644, 0], [166, 0, 0, -256], [0, -392, 226, -392], [645, -1184, 645, 0], [-774, 176, -774, 0], [0, 832, -174, 832], [-795, 0, 0, -308], [0, 2044, 403, 2044], [-2300, -480, -2300, 0], [2028, 0, 0, 400], [0, 512, -1099, 512], [404, -804, 404, 0], [-696, 0, 0, 988], [-3080, 152, -3080, 0], [4863, 0, 0, -552], [-2811, 0, 0, 2516], [-325, 948, -325, 0], [0, 3796, -1260, 3796], [0, 368, 88, 368], [263, 0, 0, 3760], [3146, -2228, 3146, 0], [-118, 0, 0, 304], [0, 2152, -4818, 2152], [-2014, 2272, -2014, 0], [-2266, -696, -2266, 0], [0, -1980, 1904, -1980], [-4286, 3304, -4286, 0], [-1853, 0, 0, 1048], [205, -3972, 205, 0], [3863, 0, 0, 2532], [579, 0, 0, -948], [-4466, 2300, -4466, 0], [0, -3532, 3561, -3532], [0, 5048, -642, 5048], [0, -1680, -7308, -1680], [-3546, 0, 0, -2528], [-846, 6148, -846, 0], [-4494, 0, 0, 4084], [-4299, 1060, -4299, 0], [0, -1920, -448, -1920], [2095, -3732, 2095, 0], [0, -3204, 1916, -3204], [-2460, 0, 0, -1264], [0, 2776, -2756, 2776], [3747, -1856, 3747, 0], [0, -2204, 1131, -2204], [0, 1648, -6503, 1648], [6010, 4024, 6010, 0], [-5028, 0, 0, -5488], [3401, 0, 0, -7176], [5807, 440, 5807, 0], [1890, 0, 0, -4336], [0, -48, -5502, -48], [0, 2124, -8490, 2124], [-12041, 0, 0, -3400], [0, -6452, 6224, -6452], [10056, 0, 0, 2816], [-13792, 0, 0, 3628], [0, -4876, 119, -4876], [-10266, 5564, -10266, 0], [-11062, 3872, -11062, 0], [379, 0, 0, -112], [0, -1888, 3269, -1888], [-3589, 0, 0, -5668], [0, 6928, 7930, 6928], [-7390, -900, -7390, 0], [0, 3152, -12228, 3152], [0, 4360, 15475, 4360], [9192, 0, 0, 1204], [-8712, -11728, -8712, 0], [-18716, 2160, -18716, 0], [0, 848, -11182, 848], [8676, 2056, 8676, 0], [-2069, 1884, -2069, 0], [-12096, 0, 0, 2584], [0, -6844, 9261, -6844], [-1329, 15080, -1329, 0], [-15970, 0, 0, 16], [0, 14196, -5917, 14196], [1982, 1872, 1982, 0], [-2382, 0, 0, 14696], [-610, -8316, -610, 0], [9681, 0, 0, -3040], [17758, -1200, 17758, 0], [-9599, 0, 0, -3604], [-8360, -14888, -8360, 0], [0, 8980, 314, 8980], [10701, 0, 0, -9032], [4096, 3656, 4096, 0], [564, 0, 0, 18124], [0, -6428, -13319, -6428], [-9384, 12828, -9384, 0], [0, 14776, -12428, 14776], [-8633, 0, 0, -17416], [0, 2192, 16803, 2192], [0, -1060, -9574, -1060], [13992, 0, 0, -12488], [0, 11292, 989, 11292], [2543, 6416, 2543, 0], [20940, 0, 0, 3544], [0, 14024, 5739, 14024], [1824, 0, 0, -3096], [0, -11108, -13490, -11108], [-314, 0, 0, -15664], [7935, 12452, 7935, 0], [29083, 5756, 29083, 0], [-8429, 0, 0, 22596], [10079, -5744, 10079, 0], [-18406, 0, 0, 11240], [0, -11160, 2117, -11160], [3740, 12572, 3740, 0], [25174, 0, 0, -7104], [-26676, -3316, -26676, 0], [-15282, 0, 0, 9196], [11374, 0, 0, 1392], [4638, 16496, 4638, 0], [0, -4256, 12300, -4256], [0, -15164, 16688, -15164], [0, -3724, -24974, -3724], [15392, 0, 0, -20848], [4004, 0, 0, -628], [0, -13072, 20248, -13072], [-21466, -1572, -21466, 0], [10368, 0, 0, 17144], [0, -4064, 20079, -4064], [0, -984, 18060, -984], [32427, 0, 0, 1444], [-6480, 5428, -6480, 0], [0, 14100, -18152, 14100], [6808, 0, 0, -1252], [-21982, 0, 0, 23672], [0, -108, 22200, -108], [-14374, -20, -14374, 0], [-21139, 9420, -21139, 0], [5344, -21104, 5344, 0], [50713, -12, 50713, 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_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_245_4_e_h(: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_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_245_4_e_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_245_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![196, 112, 50, 8, 1]>,<3,R![961, -62, 35, 2, 1]>],Snew);
    return Vf;
end function;