
// Make newform 5586.2.a.k in Magma, downloaded from the LMFDB on 20 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_k();".
// 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_k();".
// 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], [3], [0], [3], [4], [-6], [-1], [-6], [-3], [1], [2], [6], [8], [6], [-3], [3], [-2], [2], [12], [-2], [-1], [9], [12], [-17], [-6], [16], [15], [8], [-6], [17], [21], [-18], [16], [6], [-1], [4], [-4], [-12], [6], [-24], [16], [-12], [11], [-6], [-8], [20], [19], [-15], [28], [-24], [-24], [-5], [21], [18], [-12], [27], [7], [-22], [-18], [16], [15], [-26], [-18], [-17], [9], [26], [-13], [0], [16], [-6], [-18], [37], [-4], [8], [-6], [-18], [4], [24], [19], [-12], [-10], [6], [-2], [-35], [15], [30], [-25], [42], [32], [-12], [-18], [-43], [-33], [26], [-30], [3], [-18], [-2], [20], [38], [45], [39], [-24], [-4], [13], [3], [-12], [-12], [43], [-41], [38], [12], [46], [-13], [0], [-44], [12], [-27], [0], [4], [-1], [-3], [39], [22], [51], [-40], [30], [19], [16], [20], [6], [-31], [-40], [12], [31], [-42], [46], [-39], [-54], [16], [27], [32], [33], [-20], [42], [-8], [-18], [-26], [-48], [-10], [-30], [2], [18], [-34], [-30], [-40], [-24], [-47], [45], [-12], [-30], [-7], [-3], [-54], [-12], [11], [-62], [41], [-45], [-21], [-32], [-24], [-19], [-29], [-36], [-34], [-3], [40], [-20], [17], [-39], [-46], [-36], [18], [-57], [38], [-44], [53], [12], [25], [-39], [32], [-42], [3], [36], [50], [-22], [-6], [36], [51], [52], [28], [34], [60], [-27], [16], [51], [30], [-8], [2], [-69], [5], [9], [-36], [-14], [23], [0], [-6], [54], [-34], [-32], [42], [29], [-27], [50], [48], [18], [-35], [0], [50], [-20], [47], [0], [-74], [0], [-23], [-3], [36], [30], [-24], [-20], [13], [26], [-30], [-66], [-5], [-36], [50], [-36], [-4], [30], [-48], [70], [45], [-21], [-64], [46], [-33], [-29], [8], [-3], [16], [-26], [-12], [64], [-21], [-78], [50], [-3], [-32], [-34], [31], [-37], [49], [28], [33], [26], [-22], [12], [-42], [-7], [18], [-14], [22], [-48], [-37], [3], [1], [6], [30], [-9], [-78], [-12], [-52], [27], [-56], [-18], [36], [-68], [58], [27], [-25], [9], [-16], [2], [-63], [-32], [-48], [-16], [6], [54], [0], [-16], [42], [-62], [-51], [-12], [55], [24], [10], [86], [39], [20], [54], [19], [-52], [16], [-54], [-3], [26], [51], [64], [48], [-64], [60], [-64], [-6], [-5], [-23], [-34], [48], [-33], [53], [-3], [39], [64], [26], [54], [45], [28], [-55], [18], [19], [-28], [0], [-66], [93], [-66], [-42], [44], [-54], [-12], [12], [-32], [17], [18], [-64], [-13], [9], [-44], [18], [-15], [4], [-88], [60], [36], [-11], [-36], [-98], [-30], [-66], [56], [-36], [-56], [-72], [-16], [-32], [-88], [-54], [35], [-42], [87], [-8], [-60], [77], [67], [-54], [-4], [45], [76], [18], [-40], [24], [-9], [61], [-16], [54], [-80], [-15], [-17], [-15], [3], [74], [26], [-27], [-36], [64], [90], [-12], [39], [-92], [18], [-21], [-38], [-6], [-9], [24], [-56], [-30]];
    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_k();".
// 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_k(: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_k();".
// 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_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5586_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-3, 1]>,<11,R![-3, 1]>,<13,R![-4, 1]>,<17,R![6, 1]>],Snew);
    return Vf;
end function;