
// Make newform 4998.2.a.y in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4998_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_4998_2_a_y();".
// 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_4998_2_a_y();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_4998_a();"
function MakeCharacter_4998_a()
    N := 4998;
    order := 1;
    char_gens := [1667, 2551, 4117];
    v := [1, 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_4998_a_Hecke(Kf)
    return MakeCharacter_4998_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 := 2;
    raw_aps := [[1], [-1], [-3], [0], [2], [0], [1], [-3], [-5], [6], [0], [3], [0], [-1], [-6], [-6], [5], [10], [-7], [3], [-4], [16], [-12], [5], [-4], [-12], [-14], [-16], [-11], [-14], [-20], [-10], [11], [16], [-10], [0], [2], [20], [-25], [9], [-7], [-1], [12], [-14], [-21], [21], [-14], [-4], [-18], [-12], [10], [-20], [-8], [-12], [-13], [-26], [-19], [-2], [22], [-15], [-8], [8], [19], [15], [-2], [-1], [9], [12], [12], [16], [-9], [-6], [29], [26], [0], [14], [0], [23], [-6], [-14], [24], [-8], [-37], [37], [-41], [35], [-24], [21], [30], [20], [-21], [27], [17], [-27], [16], [3], [10], [28], [-24], [-38], [-6], [4], [-41], [-17], [14], [-22], [-27], [14], [34], [10], [-47], [6], [-18], [42], [30], [-2], [-12], [-2], [45], [15], [-46], [36], [6], [-32], [-4], [48], [17], [8], [-16], [24], [-3], [-16], [-43], [4], [47], [19], [26], [-30], [44], [-54], [14], [-53], [4], [22], [-14], [-51], [-18], [54], [-15], [-24], [38], [-40], [-8], [0], [-30], [-33], [-32], [-30], [-39], [27], [-30], [-41], [24], [-1], [-21], [19], [-3], [42], [44], [39], [-15], [40], [-45], [-46], [-22], [-4], [44], [59], [42], [38], [-36], [54], [-30], [-43], [-38], [24], [23], [21], [42], [-28], [-48], [-6], [24], [42], [-14], [52], [12], [-17], [-54], [-62], [15], [5], [-32], [27], [44], [30], [10], [20], [50], [4], [22], [-14], [11], [-8], [0], [-20], [40], [-30], [12], [-40], [14], [-43], [-37], [60], [-39], [40], [34], [12], [34], [-40], [-64], [-50], [-56], [30], [-35], [32], [40], [42], [12], [12], [18], [36], [52], [76], [-12], [-25], [-53], [-10], [69], [54], [-58], [-36], [32], [-2], [24], [-15], [35], [2], [-21], [-43], [17], [-4], [31], [-39], [-42], [-17], [-34], [14], [-12], [-2], [30], [-12], [53], [76], [13], [-16], [24], [50], [22], [-81], [-36], [80], [-1], [28], [-44], [-34], [56], [31], [-22], [27], [-67], [-72], [15], [28], [71], [2], [26], [50], [-66], [-41], [-54], [-18], [81], [12], [60], [38], [0], [75], [-32], [-58], [69], [-6], [2], [-12], [-66], [-47], [13], [0], [-46], [-9], [-82], [-28], [-42], [-86], [-24], [-85], [-14], [-86], [23], [7], [-71], [83], [-71], [7], [40], [-46], [48], [10], [50], [-70], [58], [59], [-38], [-26], [68], [78], [-85], [-81], [36], [7], [-55], [-2], [2], [-53], [56], [73], [-6], [6], [40], [-54], [-90], [-82], [10], [26], [52], [13], [-47], [38], [8], [9], [-12], [69], [-6], [-74], [49], [48], [48], [59], [-33], [10], [94], [6], [-75], [-24], [-94], [97], [2], [-102], [-40], [-56], [-8], [-54], [-35], [52], [-78], [72], [2], [71], [30], [52], [-14], [-77], [80], [12], [40], [7], [-58], [63], [4], [-42], [-50], [18], [65], [64], [-13], [14], [-45], [21], [-46], [28], [24], [-31], [-16], [-60], [-8], [42], [104], [-56], [-20], [3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4998_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4998_2_a_y();".
// 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_4998_2_a_y(:prec:=1)
    chi := MakeCharacter_4998_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    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_4998_2_a_y();".
// 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_4998_2_a_y( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4998_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<11,R![-2, 1]>,<13,R![0, 1]>,<23,R![5, 1]>],Snew);
    return Vf;
end function;