
// Make newform 4598.2.a.be in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4598_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_4598_2_a_be();".
// 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_4598_2_a_be();".
// 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 := [-1, -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_4598_a();"
function MakeCharacter_4598_a()
    N := 4598;
    order := 1;
    char_gens := [3269, 3631];
    v := [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_4598_a_Hecke(Kf)
    return MakeCharacter_4598_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], [0, -2], [-1, 3], [1, -3], [0, 0], [-4, 0], [0, 1], [1, 0], [-4, 3], [4, -6], [-6, 2], [0, -2], [-6, 4], [-6, -1], [4, -3], [8, 2], [-6, 8], [-6, 3], [-6, -2], [-6, 2], [2, -8], [-6, -2], [7, -3], [2, -10], [4, 8], [-11, 9], [6, -8], [4, -4], [-12, 8], [-14, 4], [-12, 6], [9, 3], [5, -9], [6, 5], [-10, 4], [-14, 2], [-11, -7], [-7, 15], [16, -6], [4, 0], [8, -18], [14, -12], [-15, 1], [-12, -4], [2, -8], [25, 1], [2, 8], [12, -14], [-10, -10], [-6, -3], [11, -7], [-4, -7], [-26, 4], [-14, 1], [-2, -10], [-16, 0], [10, 6], [20, 5], [-14, -4], [-4, 0], [-5, -3], [-24, 0], [-8, 4], [20, -9], [12, -17], [-2, -4], [-4, -4], [10, 10], [-14, 11], [21, -7], [13, -29], [-20, 11], [1, 5], [-12, -4], [-18, 16], [4, -14], [-22, 15], [2, 9], [-16, -2], [36, -4], [2, -19], [18, 4], [30, -12], [-14, 16], [-14, 20], [3, -15], [6, 6], [3, 5], [13, -27], [25, -23], [-15, 15], [28, -5], [2, 12], [7, -19], [1, -13], [12, 8], [20, 2], [-4, -12], [-10, -12], [10, 3], [2, -28], [33, -3], [20, 8], [-2, 10], [-23, 23], [-6, 28], [40, -4], [-20, 19], [10, 12], [-16, 28], [-6, -2], [22, -15], [-30, 20], [10, 15], [-20, 4], [28, -16], [18, 7], [12, -28], [26, 1], [10, 12], [0, -12], [-2, -14], [2, -12], [0, -6], [-23, 23], [34, 5], [25, 13], [-33, 15], [-16, 31], [3, 11], [-9, -23], [-16, 12], [-12, -16], [-2, 20], [-38, 4], [11, -15], [-18, 20], [-2, 12], [34, 0], [27, 5], [-16, 2], [46, 1], [29, -19], [30, -26], [10, 20], [-32, 34], [2, 3], [-12, 24], [-14, 13], [-22, 2], [10, 14], [-14, 12], [7, 13], [10, -28], [40, -14], [-12, 22], [-17, 35], [-48, -1], [-36, 17], [24, -2], [-29, 17], [-32, 34], [-44, -3], [-20, -8], [-32, 24], [20, -12], [0, 8], [-7, 29], [-14, 0], [42, -19], [2, -30], [-12, 40], [25, -19], [-32, 19], [-16, 10], [11, -27], [-2, -37], [-39, 29], [-22, -8], [-41, 3], [21, -11], [30, -12], [-22, -12], [-20, -12], [21, -11], [29, -43], [-8, -28], [-32, 2], [4, 10], [9, 13], [-2, -16], [10, 23], [18, -26], [36, -52], [-6, -27], [16, 16], [13, -41], [30, -9], [-8, 15], [-32, -9], [-4, 22], [10, 14], [22, -28], [-28, 30], [21, 15], [-10, 44], [25, -7], [-36, 20], [-47, 11], [40, -36], [-17, -7], [-37, -17], [52, -23], [0, -36], [4, 10], [-10, 36], [36, -21], [-38, -10], [-2, 18], [-14, 39], [-38, 8], [32, 4], [-22, -2], [-40, 7], [38, 0], [35, -53], [18, -30], [4, 0], [36, -6], [-35, 19], [18, -33], [14, 26], [-36, 14], [-6, -14], [62, 7], [-28, -8], [-8, 25], [46, -5], [-26, 37], [48, 4], [32, 4], [-5, -15], [-51, 21], [40, -16], [-20, -6], [-12, 40], [-40, -8], [4, 36], [-12, -18], [13, 33], [11, 11], [-22, -4], [-47, 13], [-18, -2], [-38, 39], [-38, 41], [26, 9], [14, -22], [4, -10], [-60, 9], [14, 34], [10, -10], [-6, -9], [2, 12], [-2, -40], [-8, 42], [-34, 16], [-52, 23], [-6, 28], [-49, 23], [-2, 22], [26, -16], [29, 11], [3, 19], [26, -38], [-8, 27], [2, -1], [66, -10], [-18, 36], [1, -5], [4, 36], [4, 1], [-24, -19], [-20, 26], [-12, 24], [-25, -37], [7, -19], [-40, 0], [-56, 7], [-12, 30], [13, -35], [-36, 68], [-46, -4], [-22, 46], [-36, 62], [-14, -13], [-30, 14], [11, -5], [18, 0], [-28, 40], [-16, 25], [28, -2], [20, -31], [34, -2], [-46, -1], [-40, 24], [2, 0], [46, -60], [-64, 15], [-25, 15], [27, -1], [42, -25], [-4, 38], [-10, 2], [-17, -21], [50, -22], [6, -13], [-34, 54], [-45, 1], [-6, 40], [-6, -38], [8, 13], [40, 30], [-58, 46], [-55, 27], [-30, 26], [-68, 22], [-24, -22], [-48, -12], [5, 41], [-42, -9], [-28, -26], [-12, -13], [-5, 25], [33, -21], [39, -19], [24, -4], [60, -24], [47, -63], [-9, 19], [-46, 62], [6, 12], [24, 6], [58, -8], [-8, 10], [4, 28], [47, -41], [56, 14], [-58, -18], [-6, -15], [-22, 70], [-20, -24], [22, -64], [-30, 60], [-78, -4], [36, -46], [28, -12], [-28, -40], [-55, 43], [24, -40], [18, 33], [49, -33], [-58, 50], [-52, -18], [-4, -16], [-28, 38], [37, -3], [16, -44], [26, -10], [11, -7], [74, -20], [0, -32], [-68, 36], [-12, -52], [9, -55], [-28, -4], [16, -53], [55, 21], [-8, 31], [16, -6], [8, -38], [35, -27], [-27, 29], [-52, 59], [-18, -16], [-22, -22], [88, -3], [2, -11], [32, -56], [-32, 40], [-52, -16], [20, -66], [60, -24], [-23, 15], [34, -28], [-48, 12], [74, -18], [18, -56], [-58, 40], [70, -29], [-42, -18], [12, -9], [10, 10], [46, 16], [-44, 50], [-60, 45], [50, -3], [28, -50], [-20, 18], [34, 13], [-58, 30], [14, 23], [-38, 72], [20, 20], [62, -52], [-10, 21], [-6, 32], [-46, -8], [-71, 43], [12, 54], [40, -70], [-72, 48], [-52, 88], [44, -42], [60, -12], [26, -18], [46, -48], [28, 3], [29, 23], [-92, -4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4598_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4598_2_a_be();".
// 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_4598_2_a_be(:prec:=2)
    chi := MakeCharacter_4598_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_4598_2_a_be();".
// 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_4598_2_a_be( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4598_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-4, 2, 1]>,<5,R![-11, -1, 1]>,<7,R![-11, 1, 1]>,<13,R![4, 1]>],Snew);
    return Vf;
end function;