
// Make newform 5586.2.a.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5586_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_5586_2_a_e();".
// 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_5586_2_a_e();".
// 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_5586_a();"
function MakeCharacter_5586_a()
    N := 5586;
    order := 1;
    char_gens := [3725, 4903, 4999];
    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_5586_a_Hecke(Kf)
    return MakeCharacter_5586_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], [0], [-1], [-4], [2], [1], [2], [5], [5], [-6], [-2], [8], [-10], [13], [3], [-10], [10], [-4], [14], [-5], [-3], [4], [-13], [-6], [-8], [15], [-16], [2], [-3], [-15], [22], [-8], [6], [-5], [4], [4], [12], [6], [24], [-16], [-12], [-1], [-6], [-8], [4], [15], [-15], [12], [-16], [-16], [-17], [-7], [18], [-12], [3], [-17], [2], [30], [-16], [-9], [-2], [-18], [-1], [-15], [-22], [-33], [24], [-8], [26], [-10], [-35], [12], [-8], [-22], [-18], [20], [8], [-17], [-36], [38], [-2], [14], [25], [-21], [-34], [-33], [-6], [-16], [-36], [14], [-7], [19], [-38], [-22], [-13], [-10], [-10], [20], [-2], [17], [15], [0], [-20], [-43], [-17], [-36], [12], [-25], [-13], [-10], [12], [-10], [-5], [0], [44], [12], [9], [0], [20], [-29], [-35], [-1], [14], [39], [16], [-26], [3], [0], [-4], [6], [-19], [-8], [36], [47], [-42], [14], [41], [-22], [8], [-25], [16], [9], [-28], [-54], [32], [-10], [-2], [8], [38], [10], [-38], [-14], [-10], [-22], [8], [40], [33], [45], [12], [-54], [-7], [-3], [18], [36], [-41], [-30], [61], [39], [-29], [-16], [-32], [-59], [-33], [-4], [38], [-55], [-48], [44], [-7], [-47], [10], [36], [-38], [-13], [22], [28], [49], [36], [53], [61], [56], [-42], [63], [12], [-14], [2], [10], [20], [51], [-4], [-12], [50], [68], [49], [64], [-53], [-10], [32], [-30], [23], [-3], [57], [-12], [-46], [7], [-24], [-54], [6], [30], [-24], [66], [37], [45], [-70], [56], [-38], [73], [-72], [-54], [-4], [19], [40], [-58], [40], [1], [-55], [-36], [14], [72], [44], [-51], [34], [18], [-50], [67], [-36], [50], [-68], [-12], [46], [-72], [54], [41], [-57], [64], [-34], [55], [-37], [-16], [-59], [40], [-58], [36], [8], [-53], [26], [-30], [-71], [0], [6], [-81], [-69], [61], [-20], [-3], [-14], [-54], [-60], [-50], [-39], [-6], [2], [-58], [72], [27], [83], [17], [14], [-18], [27], [-22], [-60], [4], [-65], [16], [30], [36], [-36], [26], [83], [39], [41], [-16], [34], [-47], [40], [-8], [40], [-50], [-74], [-56], [32], [18], [34], [-39], [44], [79], [-56], [-6], [22], [15], [44], [6], [31], [12], [56], [10], [33], [82], [-61], [-80], [24], [40], [-12], [-56], [82], [-77], [-23], [-50], [-72], [-17], [1], [-11], [23], [-40], [-46], [-90], [73], [-76], [53], [2], [-57], [-76], [0], [-50], [17], [14], [-26], [44], [10], [60], [-36], [-40], [-27], [18], [72], [63], [37], [-28], [-22], [-79], [20], [-64], [12], [-20], [29], [-76], [14], [2], [-18], [-72], [68], [-8], [24], [-32], [0], [-48], [-54], [-49], [6], [11], [24], [-20], [-95], [-25], [-54], [-44], [25], [-76], [-22], [-32], [0], [-49], [-67], [24], [-2], [32], [-35], [-69], [69], [-69], [98], [-38], [25], [76], [24], [-62], [-12], [-57], [-52], [-78], [27], [-38], [10], [-105], [32], [-64], [-94]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5586_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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