
// Make newform 4598.2.a.d 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_d();".
// 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_d();".
// 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_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], [-1], [4], [0], [5], [-3], [-1], [4], [-3], [0], [11], [-3], [8], [4], [-9], [12], [2], [-8], [12], [-10], [12], [-12], [-1], [7], [-6], [-4], [0], [-7], [3], [-4], [-4], [10], [20], [9], [12], [-2], [24], [-8], [-14], [20], [-25], [12], [9], [-15], [0], [0], [8], [-8], [-25], [21], [24], [-2], [-24], [11], [-24], [23], [24], [-11], [-26], [20], [29], [8], [32], [15], [-18], [24], [5], [12], [5], [31], [-16], [8], [10], [20], [0], [3], [23], [-25], [37], [12], [-1], [20], [-41], [-12], [24], [15], [-31], [25], [-4], [-4], [-8], [36], [-28], [24], [-16], [30], [-22], [-4], [2], [8], [2], [-4], [-26], [-12], [-41], [16], [33], [-24], [-23], [8], [-19], [27], [32], [-8], [39], [12], [16], [-18], [-44], [-25], [-50], [21], [16], [8], [17], [38], [-20], [-16], [45], [-16], [-24], [-24], [27], [17], [21], [-26], [-20], [-2], [6], [4], [10], [4], [-24], [-29], [-36], [-23], [-10], [28], [48], [5], [15], [36], [-24], [-32], [-56], [-32], [35], [-51], [-35], [-12], [61], [-4], [-12], [23], [-32], [-56], [33], [-7], [54], [-36], [-34], [16], [-19], [-44], [11], [-40], [-17], [-20], [-39], [20], [44], [-26], [1], [-56], [6], [29], [-32], [-27], [4], [18], [16], [-8], [-37], [0], [-49], [-39], [46], [-63], [-8], [34], [-32], [19], [-35], [-24], [-18], [-40], [-4], [45], [4], [13], [15], [0], [-8], [-44], [43], [-56], [-18], [4], [-65], [29], [-64], [-17], [60], [-72], [41], [-57], [36], [-48], [-48], [-65], [16], [-28], [65], [-20], [8], [-30], [-43], [12], [-12], [-36], [0], [-8], [39], [14], [40], [-48], [40], [20], [-60], [-14], [-55], [56], [55], [-63], [24], [7], [4], [51], [22], [-32], [36], [73], [2], [-53], [-60], [30], [26], [36], [-6], [7], [16], [19], [48], [-18], [-32], [80], [73], [6], [-72], [-16], [20], [4], [65], [28], [-48], [-78], [74], [60], [-51], [55], [24], [-47], [-48], [-46], [66], [4], [-17], [16], [56], [70], [-63], [80], [-36], [64], [34], [76], [-1], [80], [29], [84], [-1], [41], [16], [-64], [-55], [56], [-60], [82], [-55], [44], [7], [-15], [68], [-31], [34], [64], [40], [-60], [-51], [-27], [62], [64], [60], [60], [-52], [-5], [62], [-45], [-76], [-45], [-6], [29], [-20], [-34], [-36], [-53], [28], [12], [-45], [-68], [-54], [-85], [-36], [41], [9], [-44], [-60], [-27], [64], [-38], [-58], [-48], [-32], [-4], [-57], [81], [-20], [-59], [84], [92], [4], [-39], [-76], [27], [-60], [84], [49], [69], [-42], [-66], [7], [-88], [78], [0], [68], [-64], [-21], [36], [-28], [-62], [-42], [-4], [44], [-12], [-90], [16], [59], [-44], [-51], [-67], [83], [12], [90], [42], [-28], [-61], [-15], [-60], [-48], [29], [93], [44], [100], [-59], [-45], [-60], [4], [34], [36], [3], [-102], [64], [-24], [-21], [19], [0], [-26], [-100], [-8]];
    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_d();".
// 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_d(:prec:=1)
    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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4598_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![1, 1]>,<7,R![-4, 1]>,<13,R![-5, 1]>],Snew);
    return Vf;
end function;