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