
// Make newform 5586.2.a.bm 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_bm();".
// 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_bm();".
// 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 := [-3, 0, 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_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, 0], [1, 0], [-2, 1], [0, 0], [-1, 0], [-2, -2], [-3, -1], [1, 0], [-1, -1], [1, 0], [-1, 2], [1, 3], [-7, 1], [-5, 3], [-8, -2], [5, -2], [0, 1], [-8, 2], [6, -4], [-7, -3], [0, -4], [-1, -6], [3, 0], [-9, 5], [-4, -1], [-2, 4], [-6, 0], [-10, 5], [9, 1], [-7, 1], [-3, 4], [-3, 2], [-2, 8], [-3, -3], [12, -6], [5, 6], [2, -2], [2, 6], [-15, -3], [0, 12], [-6, -2], [2, -2], [10, 2], [12, 9], [-4, 12], [-2, -6], [-9, 1], [7, -10], [-10, -5], [10, 10], [6, -2], [-12, -4], [2, -15], [-11, 8], [-2, 4], [-3, 11], [-3, -10], [-4, -11], [25, -3], [-14, -8], [-3, 17], [-27, -2], [9, 9], [23, 7], [-11, -12], [-15, -6], [-7, 13], [-2, -15], [4, -6], [17, -1], [9, 3], [6, -4], [2, 13], [2, -10], [24, -8], [14, -12], [0, 10], [-2, -10], [10, -6], [2, 5], [0, -18], [8, -14], [9, -17], [16, 0], [1, -2], [7, 0], [-11, -7], [17, 4], [-26, -8], [4, 0], [-10, -4], [-10, -8], [25, 8], [-43, 0], [-14, 4], [33, 5], [-3, 2], [10, 8], [1, 5], [-13, 17], [19, 15], [-2, 1], [24, -7], [-8, -20], [-1, 11], [31, -6], [15, -8], [26, -2], [3, 19], [-4, 15], [-17, 18], [25, -3], [3, -9], [-28, -2], [8, -15], [9, 15], [29, -3], [31, 9], [-30, -7], [-16, -2], [-1, -13], [2, 15], [-13, 20], [-44, 3], [-3, -9], [8, -7], [-9, -7], [44, -2], [2, -1], [-15, 19], [-1, -17], [-22, 12], [17, -6], [-5, -7], [30, -10], [23, -6], [18, 10], [-22, 0], [15, -10], [-21, -11], [-3, 15], [-6, -1], [-40, 4], [8, 15], [24, -8], [-5, 17], [-39, -1], [18, 0], [-16, 18], [3, 7], [19, -7], [30, -12], [37, 3], [-1, -15], [-37, 3], [-12, 2], [-16, -18], [-5, 3], [-17, -6], [-1, 12], [12, -6], [-28, -14], [6, -9], [-2, -3], [14, -8], [-24, -16], [13, 26], [44, 10], [-8, 7], [24, -11], [-24, -11], [1, 13], [24, 4], [-13, 4], [9, 12], [-30, -6], [-22, -12], [-60, -1], [50, 8], [-11, -1], [-26, 11], [-22, -5], [-2, 32], [-37, 9], [0, -18], [-24, 3], [-10, 8], [-13, -11], [32, -7], [-18, -14], [-40, 7], [-1, 6], [-14, -6], [12, -12], [-41, -6], [39, 17], [-10, -2], [-6, -12], [16, -10], [-9, 1], [-39, 2], [8, -2], [-7, 25], [-8, 6], [40, -6], [-56, 5], [40, -4], [-12, -3], [-45, 1], [-29, -15], [-16, 16], [16, 5], [12, 17], [-6, -19], [30, -6], [36, -8], [0, -3], [-6, 2], [-32, 14], [-6, -32], [-27, 15], [-14, 4], [14, 28], [62, 7], [-14, -23], [21, 9], [7, 13], [21, 15], [13, 14], [-36, -2], [24, 10], [-20, -24], [9, -18], [-5, -27], [4, 6], [30, 10], [-45, -10], [-16, 5], [-10, -12], [45, 3], [10, 18], [-16, 24], [2, -15], [56, 6], [-21, -1], [-21, 23], [32, -3], [4, 24], [-7, 13], [-6, -26], [49, 3], [-26, 0], [-1, 1], [-26, 4], [-14, -35], [25, -16], [57, -5], [13, 21], [-27, -20], [-7, -36], [4, -36], [30, 1], [-45, 13], [-15, -29], [27, -9], [-25, 1], [27, 20], [21, 23], [-1, -25], [-60, 11], [0, -40], [4, 26], [3, -14], [32, -15], [-14, 5], [30, 10], [-3, -14], [31, -11], [-44, 8], [12, 38], [0, 22], [-8, -15], [3, 43], [-39, -5], [-4, -10], [12, -36], [9, 6], [-15, 20], [-34, 27], [-47, -3], [12, -10], [9, 10], [-51, -3], [4, -22], [26, 22], [-68, 9], [-8, 32], [-26, -28], [8, -32], [45, -23], [-22, -24], [-55, -4], [-2, -43], [-40, 5], [15, 7], [26, 4], [36, -29], [-5, 31], [3, 33], [17, -9], [-4, -26], [-18, 36], [4, 16], [-18, -30], [21, 1], [-18, -36], [-9, -16], [40, 0], [37, -22], [16, -12], [5, 13], [-18, 30], [33, -18], [8, -10], [-33, 9], [-12, -17], [-29, 5], [-1, -15], [34, -4], [-46, 9], [46, 12], [-27, -24], [-8, -36], [-4, 36], [21, -15], [52, 0], [54, 10], [-18, 28], [31, 26], [-16, 7], [-68, 2], [-6, -14], [-39, 2], [5, 18], [-43, 4], [40, -15], [15, 29], [12, 26], [-2, -48], [-12, -17], [2, -14], [38, 21], [-4, -12], [-3, 8], [-55, 13], [-31, -5], [-32, 2], [75, 0], [-89, 1], [54, -20], [-65, -11], [43, -11], [-15, -35], [68, -4], [46, 10], [-46, 3], [54, 24], [-6, -40], [-38, -1], [-3, 22], [0, 28], [-58, -16], [-11, 14], [-14, 6], [-82, -10], [-2, 38], [-58, 2], [25, 40], [15, -5], [12, 14], [-30, 22], [-4, -10], [88, -6], [-24, -28], [-9, 29], [19, -13], [-32, -4], [2, 22], [10, 14], [11, -7], [-58, -11], [-62, -4], [75, 12], [-61, -17], [-45, -9], [-58, -23], [59, -8], [-8, -46], [-20, 16], [24, -31], [22, -2], [-11, -25], [-24, -2], [-31, 11], [-33, -16], [22, 3], [-5, -47], [14, 24], [17, -41], [-47, -20], [-4, 49], [-12, 17], [-26, 25], [14, 20], [28, -12], [6, 21], [-6, -42], [-38, 0], [-33, -3], [65, -15], [93, 8], [-29, 19], [74, 8], [2, 13], [-28, -22], [0, 8], [38, 7], [-26, -22], [19, -29], [-29, -21]];
    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_bm();".
// 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_bm(:prec:=2)
    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_bm();".
// 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_bm( : 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, 4, 1]>,<11,R![1, 1]>,<13,R![-8, 4, 1]>,<17,R![6, 6, 1]>],Snew);
    return Vf;
end function;