
// Make newform 4998.2.a.bk in Magma, downloaded from the LMFDB on 19 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_bk();".
// 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_bk();".
// 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], [-6], [0], [-1], [3], [7], [6], [8], [7], [0], [-9], [6], [2], [11], [6], [9], [-9], [12], [-8], [-4], [-5], [4], [12], [14], [-8], [9], [-6], [20], [10], [-13], [-8], [-18], [8], [-18], [-4], [-19], [11], [1], [-11], [-20], [-6], [-9], [15], [2], [-4], [-14], [20], [18], [-12], [-8], [12], [-27], [14], [-9], [18], [6], [-31], [-32], [32], [5], [21], [10], [3], [-15], [20], [12], [-8], [9], [10], [-17], [34], [16], [34], [-24], [-3], [2], [14], [0], [8], [23], [19], [29], [-5], [16], [-35], [-30], [-28], [-35], [17], [5], [-35], [32], [33], [-26], [-4], [-8], [26], [10], [-12], [-39], [-9], [14], [-42], [-13], [-14], [-30], [-2], [27], [-10], [46], [-10], [14], [-18], [-36], [18], [-39], [15], [-10], [36], [-6], [24], [36], [-16], [5], [-16], [-40], [-16], [-19], [8], [1], [12], [25], [13], [-10], [-26], [12], [18], [-30], [-25], [-4], [-2], [-34], [-49], [-14], [18], [23], [32], [-10], [-24], [-40], [8], [-54], [19], [56], [6], [31], [33], [34], [-9], [16], [-15], [3], [-39], [25], [6], [12], [5], [-47], [-8], [-9], [-62], [-34], [60], [52], [-17], [-34], [-54], [-52], [42], [18], [51], [-46], [-16], [3], [35], [58], [20], [-56], [18], [56], [-10], [-22], [-60], [-36], [-45], [-2], [6], [11], [55], [-32], [-11], [-44], [-14], [14], [36], [10], [28], [-2], [-42], [7], [56], [-24], [-28], [32], [-18], [-28], [-64], [-10], [23], [11], [20], [47], [-40], [-10], [-44], [62], [-16], [32], [10], [-16], [14], [19], [-24], [56], [-54], [-12], [-28], [2], [-52], [60], [-44], [-12], [-3], [65], [26], [61], [6], [-74], [-60], [-8], [-30], [-8], [-23], [-25], [38], [-7], [59], [-59], [-36], [-3], [-61], [-22], [-15], [-42], [34], [20], [-18], [-62], [52], [43], [76], [-53], [48], [-40], [34], [-74], [81], [44], [24], [77], [28], [28], [-26], [-64], [3], [-10], [37], [65], [-80], [39], [60], [-29], [-34], [14], [-2], [-14], [-79], [22], [-10], [61], [-68], [-28], [-42], [32], [-47], [-24], [38], [-49], [-6], [34], [-60], [-90], [39], [-21], [-88], [14], [39], [-22], [76], [-78], [-6], [40], [-75], [-46], [-26], [27], [-45], [5], [71], [11], [-55], [64], [38], [-16], [-66], [-26], [-34], [26], [83], [54], [-2], [36], [22], [17], [31], [20], [69], [47], [-26], [-54], [65], [32], [15], [-26], [10], [-48], [90], [-26], [58], [-46], [18], [-52], [37], [35], [38], [-80], [73], [-20], [33], [-38], [98], [-91], [64], [-40], [53], [1], [-58], [18], [-26], [49], [16], [-26], [-21], [50], [-74], [-32], [32], [32], [54], [45], [60], [10], [-64], [-66], [41], [-34], [60], [14], [-5], [8], [44], [48], [-11], [-2], [23], [28], [66], [-14], [50], [1], [48], [-53], [50], [-81], [87], [62], [4], [-32], [67], [24], [-36], [48], [78], [24], [32], [-92], [-7]];
    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_bk();".
// 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_bk(: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_bk();".
// 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_bk( : 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![6, 1]>,<13,R![0, 1]>,<23,R![-7, 1]>],Snew);
    return Vf;
end function;