
// Make newform 5610.2.a.d in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5610_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_5610_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_5610_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_5610_a();"
function MakeCharacter_5610_a()
    N := 5610;
    order := 1;
    char_gens := [1871, 3367, 1531, 3301];
    v := [1, 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_5610_a_Hecke(Kf)
    return MakeCharacter_5610_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], [-1], [1], [-1], [0], [1], [4], [5], [9], [-7], [-2], [0], [-5], [8], [12], [-6], [-14], [8], [-6], [-14], [8], [16], [0], [7], [10], [-1], [-5], [-4], [6], [-18], [-8], [9], [-1], [-8], [20], [10], [5], [18], [-18], [2], [-7], [-3], [22], [-8], [0], [17], [-9], [-29], [4], [-15], [-2], [1], [-8], [-17], [7], [30], [27], [14], [-3], [22], [33], [-28], [20], [25], [27], [-19], [22], [28], [2], [-7], [18], [18], [14], [20], [8], [-22], [22], [3], [0], [-23], [12], [39], [-4], [-38], [13], [-15], [22], [-18], [-8], [36], [-39], [2], [40], [28], [24], [-30], [-18], [19], [8], [38], [23], [24], [-2], [-4], [-18], [35], [42], [-13], [-42], [-16], [-6], [22], [42], [14], [-27], [-13], [-2], [-37], [15], [36], [-8], [-6], [-8], [12], [42], [-26], [-14], [23], [-36], [26], [8], [-17], [-45], [-37], [22], [36], [-28], [8], [26], [24], [27], [-22], [-11], [-50], [30], [49], [-9], [3], [-8], [9], [-29], [56], [-44], [-5], [12], [-11], [-47], [30], [30], [-6], [-38], [42], [28], [18], [-19], [3], [-21], [-41], [-5], [9], [4], [36], [-3], [20], [18], [28], [-18], [28], [-17], [-17], [-16], [-38], [2], [56], [-18], [-63], [4], [-19], [8], [35], [44], [-25], [57], [-33], [24], [-58], [-46], [-23], [-47], [30], [-2], [23], [-48], [-30], [-40], [-54], [4], [-44], [-63], [-44], [-17], [-56], [1], [24], [0], [-26], [43], [48], [-58], [-17], [-44], [36], [-62], [48], [9], [-62], [-38], [38], [22], [-37], [57], [-1], [5], [16], [-59], [-20], [-24], [31], [72], [-28], [10], [-29], [62], [31], [-71], [-30], [75], [42], [-33], [52], [60], [12], [-46], [-5], [-39], [22], [-26], [-33], [-18], [-44], [78], [54], [-49], [-28], [-3], [-71], [-4], [78], [-5], [43], [-54], [46], [27], [29], [-12], [-12], [73], [30], [44], [79], [72], [-12], [33], [-54], [64], [-74], [26], [-46], [33], [-62], [12], [-24], [38], [-43], [14], [-42], [48], [-46], [-11], [17], [-31], [18], [64], [-24], [-58], [-67], [34], [-7], [38], [70], [-22], [-64], [-44], [79], [44], [-40], [16], [22], [-45], [3], [25], [60], [13], [-46], [-33], [4], [-59], [-12], [72], [34], [-69], [38], [48], [60], [-40], [56], [-69], [-72], [-86], [74], [26], [59], [-10], [-24], [18], [18], [-64], [9], [55], [56], [2], [-87], [36], [-22], [46], [-39], [80], [-27], [54], [29], [-34], [24], [66], [56], [4], [14], [18], [-74], [34], [-20], [-66], [-86], [9], [-56], [-52], [-15], [-71], [-66], [22], [60], [39], [88], [-59], [33], [-63], [10], [-25], [18], [28], [84], [-59], [-14], [-46], [-80], [68], [60], [78], [-54], [-8], [-4], [2], [-104], [48], [10], [54], [62], [-14], [34], [13], [94], [34], [84], [-1], [57], [-6], [-24], [-104], [72], [40], [-6], [-38], [-48], [-20], [-70], [-18], [80], [-46], [-11], [-81]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5610_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5610_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_5610_2_a_d(:prec:=1)
    chi := MakeCharacter_5610_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_5610_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_5610_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5610_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-1, 1]>,<13,R![0, 1]>,<19,R![-4, 1]>,<23,R![-5, 1]>],Snew);
    return Vf;
end function;