
// Make newform 4998.2.a.cb 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_cb();".
// 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_cb();".
// 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 := [-2, 0, 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, 0], [0, 0], [-3, 1], [-1, 2], [1, 0], [2, -3], [-6, -2], [-4, -1], [4, -3], [-5, -4], [2, 3], [-5, -1], [-8, -3], [1, 2], [-4, 0], [6, 4], [-5, 7], [4, 9], [7, 4], [-5, 1], [-7, 1], [-11, 4], [-3, -2], [-6, -5], [-3, -1], [-2, -12], [8, 2], [-1, 4], [-14, -2], [10, 2], [-2, -15], [13, -3], [3, 0], [-12, 5], [0, -6], [-6, 11], [3, 11], [-4, 0], [2, 5], [2, 11], [-17, 1], [2, 5], [-2, 7], [-4, -15], [6, -15], [-2, 12], [0, 0], [11, -6], [-15, -4], [3, 5], [4, -6], [-11, 5], [7, -2], [7, 7], [8, -12], [-7, 13], [16, -6], [6, -4], [-15, 3], [8, 4], [-12, 0], [1, -23], [12, -2], [6, 8], [-17, 11], [10, -8], [-25, -3], [5, 6], [-19, 6], [8, -10], [-32, 0], [6, -7], [-2, 15], [-16, 13], [16, 4], [0, -8], [-4, -2], [20, 9], [-14, -7], [4, -13], [-4, 19], [6, 13], [28, 5], [0, -17], [-11, 12], [5, 4], [-2, -20], [4, -8], [-6, 12], [-1, 3], [21, 3], [-10, -1], [4, -7], [-8, 6], [-4, -24], [-2, 5], [0, -12], [3, 18], [2, -26], [20, -8], [23, -3], [22, 8], [28, -7], [2, -4], [-15, 7], [-12, -4], [-6, -20], [-14, -24], [32, 1], [-14, 15], [5, 26], [5, 3], [20, 9], [-29, -6], [19, 13], [-8, -27], [-6, -24], [16, 12], [-32, 0], [-2, -1], [1, 24], [32, -2], [5, -5], [3, 18], [11, -12], [-8, 6], [5, 21], [34, -8], [-26, 0], [-26, -2], [20, 18], [-10, -19], [-19, -12], [36, 7], [36, 4], [21, 17], [-26, 0], [-28, -6], [35, -3], [-12, -19], [27, 1], [1, -13], [3, 20], [-9, -3], [-24, -15], [0, 20], [34, -2], [-7, 1], [25, 0], [10, -28], [12, 8], [-1, -21], [0, 4], [-28, -4], [0, -28], [28, -12], [40, 12], [-1, 34], [-20, 10], [-12, 27], [12, -27], [23, -9], [14, -29], [-10, -28], [-21, -13], [6, 11], [14, 17], [43, 2], [6, 6], [-9, -22], [4, -5], [11, -6], [36, 10], [8, -3], [14, 27], [-18, 16], [-24, 4], [-16, -14], [-40, 4], [-14, 14], [-42, 8], [4, -26], [-11, 21], [-6, 23], [33, 10], [-2, 11], [14, 4], [-9, 27], [-31, -8], [-5, -3], [-19, -25], [-12, 9], [-1, -21], [-30, 19], [-18, 13], [45, -12], [24, 5], [-30, 6], [8, 20], [-6, 7], [1, -36], [-6, 20], [-53, 5], [-2, 32], [45, -13], [16, -16], [-28, -22], [-10, -43], [26, -21], [-52, -1], [-11, -27], [11, -29], [5, 3], [-18, 8], [-50, 10], [37, 18], [-22, 30], [-13, -4], [-40, 4], [34, -9], [-26, -24], [-15, 7], [-41, 23], [8, 44], [-2, -32], [-8, 12], [9, 5], [24, -10], [44, 4], [14, 8], [-22, 5], [34, -5], [-6, 2], [-46, -14], [33, -24], [22, 9], [-28, 4], [2, 3], [9, -21], [36, 4], [-1, 1], [40, -7], [-40, -15], [21, -1], [24, -24], [-20, -33], [-11, 39], [15, 3], [20, 1], [-44, 12], [-56, -2], [7, -10], [36, 0], [18, 2], [27, -20], [20, -18], [-21, 0], [30, -17], [-1, -39], [-26, 22], [-12, 44], [34, -16], [18, 8], [-38, 15], [8, -30], [13, -38], [-32, -1], [-49, -8], [32, 4], [-13, 11], [10, -1], [12, 23], [-42, -20], [1, 35], [-36, 13], [-58, 9], [31, 14], [-31, 33], [-30, 21], [-41, 11], [49, 7], [55, 16], [-3, 27], [11, -39], [-16, 9], [-30, 17], [16, 30], [7, 10], [-10, -32], [-30, -18], [-60, 16], [72, -5], [35, 12], [-25, -36], [-23, 11], [-16, 36], [-38, 13], [56, 4], [22, 3], [-13, 34], [-49, -25], [15, 51], [0, -28], [-12, -4], [-10, -42], [-30, 7], [-24, 0], [22, -31], [0, 28], [10, -4], [-1, 42], [-52, 26], [-33, 5], [-14, 13], [28, 0], [-30, -25], [-70, 0], [-2, -9], [-55, -21], [-52, -17], [-36, -24], [-6, 35], [34, -16], [16, 20], [-26, 30], [25, 9], [-86, 2], [30, 4], [11, 16], [-34, 28], [14, 6], [15, -39], [42, 2], [-18, -26], [20, -16], [38, -7], [11, -40], [-15, -21], [0, -39], [-26, -27], [25, 10], [62, -9], [75, 6], [-35, -5], [24, 9], [-45, -9], [-37, -29], [1, 28], [42, 39], [-24, -25], [45, 26], [-32, 4], [30, -24], [21, 12], [12, 35], [0, 1], [-26, 40], [-13, 31], [9, 14], [64, 8], [1, -3], [-9, 47], [36, -17], [-29, 20], [29, -16], [44, -19], [12, 5], [-48, -3], [-2, -12], [-30, 42], [-31, 28], [16, 24], [30, -32], [60, -3], [46, -25], [-74, 0], [11, -24], [-96, 0], [-25, -42], [-54, 7], [-67, 1], [-21, -24], [45, 11], [-11, 15], [-50, -10], [32, -28], [-28, -8], [-30, 31], [-80, 0], [-7, 18], [-44, -4], [-46, 21], [27, 29], [-6, 52], [-47, -33], [80, -4], [-66, -6], [3, -12], [-32, 15], [-54, -21], [4, 1], [62, 5], [-58, -15], [-54, -10], [-36, 12], [64, -19], [0, -28], [84, -13], [-35, -28], [41, 46], [18, -6], [-38, 18], [-60, -14], [5, 58], [22, -59], [10, -26], [-22, 4], [-8, -13], [-21, 6], [30, -19], [-32, -28], [17, 17], [11, 50], [8, 3], [-25, 21], [-6, -8], [44, 12], [24, -48]];
    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_cb();".
// 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_cb(: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_cb();".
// 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_cb( : 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![7, 6, 1]>,<13,R![-7, 2, 1]>,<23,R![28, 12, 1]>],Snew);
    return Vf;
end function;