
// Make newform 4998.2.a.bx in Magma, downloaded from the LMFDB on 25 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_bx();".
// 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_bx();".
// 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, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_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, 0], [-1, 0], [-1, -1], [0, 0], [1, -1], [-3, 1], [-1, 0], [2, 2], [-2, 2], [2, -4], [-4, 4], [1, 1], [0, -2], [-9, 1], [4, -4], [-9, 3], [4, 0], [-2, 4], [5, 3], [-8, 0], [-1, -5], [1, 3], [-7, 7], [5, -3], [-13, -1], [4, -2], [1, -5], [0, -4], [6, -8], [-13, 3], [-2, -6], [6, -2], [-12, -2], [7, -7], [11, -1], [4, 4], [-18, 4], [-18, -2], [-3, -1], [14, 4], [-10, 6], [-16, 2], [-1, -3], [12, -2], [-8, 6], [-12, 4], [2, 10], [0, 0], [4, -8], [3, -5], [-1, -1], [-15, 3], [-18, 0], [-17, 1], [11, 7], [7, -3], [-18, 4], [15, -3], [26, -4], [2, -8], [-5, 5], [-8, -6], [4, 8], [-19, -1], [-6, -4], [6, 8], [1, -1], [18, 0], [-13, 5], [23, -1], [-23, 1], [0, 8], [-4, -12], [-20, 2], [-14, 2], [12, 4], [26, -4], [-20, -2], [-6, -8], [-6, -4], [-14, 2], [26, -4], [20, -4], [-8, -2], [4, 4], [-8, -12], [11, -5], [19, -5], [-14, 0], [-16, 0], [8, -12], [-5, 1], [7, 13], [22, -2], [-32, 4], [-8, 0], [32, -6], [-12, -6], [8, -4], [13, -3], [-6, 2], [14, 0], [5, 11], [14, -4], [-12, 8], [22, 8], [-7, 15], [2, -4], [0, -8], [10, -20], [-28, -4], [12, -14], [15, -17], [13, 11], [-20, -4], [-5, 11], [27, -3], [0, -8], [10, 4], [-8, 12], [42, 0], [4, 6], [23, 7], [-32, -4], [-11, -5], [13, -19], [1, 1], [-16, 16], [-31, 11], [34, 0], [-20, -8], [-2, 18], [28, -12], [-8, 6], [-1, 11], [42, 4], [0, -14], [-3, -13], [10, -8], [-42, 4], [-13, -3], [18, 4], [-23, 3], [-19, -13], [21, -7], [3, 17], [6, 4], [0, 14], [-14, 10], [3, 9], [-21, 7], [26, 4], [-20, 8], [-13, -15], [32, -4], [24, 8], [44, -4], [12, 10], [-24, -10], [13, 1], [28, -8], [-30, -8], [-8, -16], [-3, -5], [-28, -10], [8, -16], [49, 3], [0, 2], [0, 10], [3, -5], [10, 10], [7, -17], [-28, 4], [25, 13], [-20, -12], [10, 12], [34, -14], [14, -24], [8, -16], [2, -16], [28, -12], [-38, -6], [-6, -20], [6, -16], [-1, 13], [0, 2], [-27, 5], [-6, -6], [-34, -4], [-5, 1], [-5, 23], [-9, 1], [1, 7], [-14, 16], [-31, -9], [-4, 18], [4, -10], [47, 3], [34, 12], [-2, 18], [4, 18], [-22, 30], [-19, 9], [-10, 8], [25, -17], [-10, -4], [17, -13], [0, -12], [2, 8], [-6, -14], [-12, 6], [30, -4], [-27, -9], [-29, -3], [-37, -7], [-54, -4], [58, -2], [35, -1], [-14, -2], [19, -33], [-20, 18], [54, -6], [2, 0], [-15, 19], [23, 1], [8, -10], [-14, 4], [-56, 8], [-19, 31], [-16, -20], [-48, 6], [-20, 32], [34, 6], [-28, 14], [10, 2], [-58, 2], [17, -23], [-16, -2], [44, -16], [-58, 10], [7, -7], [-44, 16], [19, -15], [10, -12], [18, 12], [-31, -5], [-4, 4], [32, 12], [-23, 23], [-11, -17], [22, 8], [-16, -10], [16, -8], [-37, 15], [40, 2], [-18, 14], [11, 15], [28, -24], [23, 7], [4, -6], [23, -11], [-14, -22], [-28, 14], [-6, -8], [-10, 8], [10, 10], [6, -24], [-15, 33], [-20, -16], [37, -3], [-4, 14], [7, -7], [-24, 6], [16, -16], [14, 0], [13, 15], [-32, 12], [-36, -6], [-27, -15], [11, -3], [-38, 14], [23, -3], [-9, -3], [-39, -3], [5, -13], [-55, 11], [-6, 24], [-36, 2], [16, -8], [-37, 23], [6, 24], [-18, -26], [56, -6], [-24, -4], [-13, 31], [-23, 5], [-17, -19], [8, -6], [-22, -14], [16, -36], [0, 30], [-19, -3], [-35, -9], [1, 7], [-48, -4], [36, -26], [46, -26], [-44, 22], [56, 0], [16, -34], [16, 16], [-22, 12], [-17, 7], [68, 0], [-17, -11], [16, -18], [-60, 8], [-26, 2], [38, -8], [12, -18], [51, -11], [6, -28], [-32, 26], [-18, 10], [2, 8], [-48, 6], [-2, -18], [1, 23], [-50, -6], [-18, 8], [-13, 31], [-34, 16], [-46, 6], [-35, 3], [-38, 10], [22, -10], [40, -2], [20, 2], [-45, 23], [-41, 21], [-38, 12], [-28, -2], [-7, 13], [-2, 2], [-27, 21], [19, 13], [18, -8], [-19, 3], [39, 21], [-33, 15], [54, 14], [-2, -4], [-7, 33], [-8, 8], [6, 16], [-17, -5], [24, -24], [0, 4], [34, 16], [-47, 11], [31, -5], [-44, -6], [-15, 35], [67, -11], [8, 4], [-1, 31], [35, -29], [12, 12], [6, -28], [68, 0], [-72, 12], [46, 10], [51, -9], [-8, 16], [-34, 16], [20, -8], [-22, 14], [-26, 24], [89, 1], [20, -22], [69, -7], [52, -2], [-3, -1], [31, -9], [53, -21], [43, -23], [-54, 14], [10, 32], [4, -8], [2, 6], [32, 26], [57, 13], [36, -24], [-18, -26], [-29, 33], [-22, 16], [-3, -9], [82, -12], [66, 2], [27, -17], [38, -4], [8, 18], [-60, -4], [0, 14], [4, 30], [50, -2], [-20, -6], [-98, 4], [-44, -8], [-60, 24], [-33, -13], [-35, -11], [-14, -30], [30, -18], [-34, 28], [-35, -15], [-18, 26], [30, -30], [-10, 24], [36, -44], [-17, -13], [20, -34], [20, -36], [-61, -3], [13, 5], [18, 0], [-23, 31], [22, 20], [60, 8], [24, 0]];
    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_bx();".
// 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_bx(:prec:=2)
    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_bx();".
// 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_bx( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4998_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-2, 3, 1]>,<11,R![-4, -1, 1]>,<13,R![2, 5, 1]>,<23,R![-16, 2, 1]>],Snew);
    return Vf;
end function;