
// Make newform 4998.2.a.f in Magma, downloaded from the LMFDB on 17 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_f();".
// 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_f();".
// 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], [1], [0], [-1], [-5], [-1], [-8], [-6], [-4], [2], [3], [-6], [13], [-2], [5], [4], [2], [-5], [14], [11], [1], [15], [5], [-1], [10], [-11], [-8], [10], [19], [14], [-18], [14], [9], [17], [-14], [-14], [-24], [5], [14], [12], [2], [15], [14], [18], [-22], [-4], [-16], [8], [1], [-13], [-27], [-10], [-11], [23], [11], [-2], [-29], [-2], [18], [17], [-12], [-16], [-7], [-30], [-6], [-25], [14], [9], [33], [-27], [4], [32], [34], [12], [-6], [-6], [-8], [-6], [0], [16], [0], [10], [-14], [-18], [30], [-33], [11], [-30], [8], [32], [15], [7], [0], [6], [-28], [20], [2], [16], [-29], [-26], [30], [11], [-6], [-6], [2], [-5], [30], [-24], [34], [18], [2], [7], [35], [42], [-41], [-27], [-6], [14], [16], [-38], [2], [9], [-20], [-37], [7], [19], [-36], [1], [14], [4], [-6], [-24], [-26], [-5], [16], [36], [-1], [6], [-18], [37], [44], [-47], [-9], [-1], [-21], [4], [12], [-6], [3], [33], [-2], [-44], [39], [40], [-20], [12], [60], [12], [15], [32], [0], [-50], [-41], [38], [16], [-43], [46], [58], [45], [-54], [-27], [6], [-27], [32], [-28], [-52], [18], [64], [-34], [-12], [14], [54], [6], [-49], [38], [-25], [-24], [-62], [-25], [-5], [37], [-25], [36], [-5], [-2], [22], [61], [-68], [-54], [44], [40], [-29], [-14], [15], [-42], [-19], [0], [10], [4], [30], [60], [-11], [-3], [31], [58], [-14], [-45], [-18], [-55], [-20], [36], [42], [5], [33], [-4], [10], [4], [45], [-36], [24], [68], [-32], [26], [18], [58], [77], [-38], [52], [16], [73], [-28], [-41], [20], [36], [57], [56], [54], [31], [37], [36], [28], [-36], [59], [-36], [14], [-43], [32], [45], [-26], [35], [-38], [-32], [-30], [-42], [28], [-6], [-51], [38], [43], [-48], [-11], [-14], [34], [22], [49], [-46], [22], [49], [9], [-64], [-1], [83], [19], [59], [-75], [44], [-18], [-4], [15], [42], [-62], [60], [70], [21], [23], [-1], [16], [-24], [44], [-46], [87], [73], [-21], [-72], [32], [38], [-2], [-72], [-86], [-72], [18], [-69], [-16], [-17], [34], [-20], [-40], [-34], [-50], [25], [-52], [-24], [-44], [26], [48], [-18], [51], [78], [-22], [-67], [78], [42], [11], [2], [62], [4], [-58], [79], [15], [44], [-42], [-49], [48], [47], [-3], [24], [43], [75], [-63], [48], [-4], [47], [64], [38], [-5], [42], [70], [34], [-7], [-7], [24], [69], [13], [38], [3], [-3], [-46], [-76], [-30], [-56], [-34], [-43], [-40], [82], [-98], [40], [-74], [61], [44], [87], [66], [93], [95], [-101], [-9], [6], [42], [-64], [12], [8], [71], [56], [8], [-53], [10], [-63], [54], [-82], [81], [-20], [-26], [50], [62], [-90], [34], [4], [12], [-56], [-18], [31], [75], [-94], [-2], [-90], [-69], [-24], [98], [-22], [-78], [-7], [14], [-48], [81], [-55], [-52], [15], [54], [-16], [-56]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4998_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<11,R![1, 1]>,<13,R![5, 1]>,<23,R![6, 1]>],Snew);
    return Vf;
end function;