
// Make newform 5054.2.a.d in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5054_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_5054_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_5054_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
        poly := [-1, -1, 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_5054_a();"
function MakeCharacter_5054_a()
    N := 5054;
    order := 1;
    char_gens := [1445, 1807];
    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_5054_a_Hecke(Kf)
    return MakeCharacter_5054_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], [0, -1], [1, -3], [-1, 0], [0, 1], [-2, 0], [0, -2], [0, 0], [0, 0], [-7, 5], [-2, -4], [-1, -5], [7, -3], [3, -1], [10, -1], [2, 7], [-11, 1], [-2, 9], [-6, 2], [-2, 9], [0, 8], [-3, -5], [4, 6], [0, -7], [-4, -1], [10, 0], [-8, 4], [-10, 6], [10, -1], [-10, 16], [-6, 13], [-14, -4], [-11, 15], [10, -8], [-2, -2], [-16, 8], [-10, 5], [4, 5], [-6, -4], [-6, 8], [12, -10], [0, -16], [-2, 12], [12, 4], [-10, 18], [1, -1], [6, -18], [14, -14], [-8, 20], [13, -15], [12, -11], [6, 10], [-23, 3], [10, -20], [7, -23], [14, -14], [-4, 0], [-13, 9], [-2, 4], [-4, 4], [-8, -12], [-8, 4], [23, -5], [14, -5], [-2, -4], [-2, -15], [10, 2], [-24, 2], [-12, 16], [10, 16], [14, -20], [-28, 14], [2, -13], [17, -7], [-12, 18], [24, -14], [12, 8], [10, 9], [-22, 12], [-16, 15], [-26, 14], [10, 4], [22, 3], [1, 3], [22, 6], [-11, -7], [-14, -14], [-4, 5], [-14, 1], [-8, -20], [28, 0], [27, 1], [9, -17], [24, -12], [-35, 1], [11, -15], [-14, 22], [10, 4], [16, 0], [-26, 26], [-14, 10], [-6, -14], [24, -15], [-4, -14], [4, -17], [-8, -14], [14, -4], [-38, 12], [-10, -3], [-18, -12], [-4, -8], [-18, 22], [11, -27], [-4, 0], [-32, 4], [28, -12], [34, -24], [9, -9], [-20, 20], [-14, -2], [-8, -4], [-12, 0], [-32, -2], [24, 14], [-8, 10], [10, -30], [-8, 10], [16, 12], [-2, -21], [-19, 29], [15, 11], [31, 1], [-5, -19], [-24, 12], [2, -20], [18, -6], [36, -2], [27, -1], [38, 8], [-19, 11], [-12, 13], [0, -6], [24, -2], [32, -14], [18, 2], [-18, 12], [-1, 19], [2, -12], [6, 16], [23, -7], [-22, -5], [22, -6], [-8, 33], [-36, 20], [4, -8], [27, 1], [-26, 42], [2, -2], [28, 10], [24, -34], [8, 7], [-8, 6], [-24, 40], [44, -17], [22, -14], [6, -34], [-50, -7], [-17, -9], [30, 2], [22, -20], [26, -6], [-22, 22], [16, -10], [9, -21], [8, -16], [-32, 26], [-3, -19], [40, 10], [-44, 14], [-27, 45], [2, 36], [12, -29], [30, -37], [-44, -11], [12, -6], [-22, 5], [-23, 1], [13, 25], [42, -26], [14, 25], [-34, 20], [-29, 27], [38, -12], [32, -24], [32, -19], [23, 1], [-32, -5], [6, 10], [-20, 22], [-45, 13], [18, 16], [-38, 22], [56, -18], [36, -5], [26, -16], [30, -29], [-42, -5], [-18, 54], [-14, -12], [36, -43], [35, -19], [2, 25], [-2, 12], [-35, 25], [-24, 20], [-17, 33], [16, -14], [21, -21], [33, -5], [58, 8], [-38, -5], [30, -40], [-44, 26], [22, -14], [-4, -3], [30, -54], [-57, -11], [28, -32], [20, -22], [-15, -11], [12, -18], [-28, 15], [-18, 43], [12, -12], [42, -28], [-41, 41], [26, -38], [-38, 18], [-57, 11], [14, 32], [2, -36], [18, -28], [32, -12], [-50, -5], [-14, 16], [-22, 13], [-7, -33], [16, 28], [-34, -12], [-18, 24], [-18, 0], [30, -30], [-30, -10], [-38, 36], [-10, -12], [61, -19], [30, 20], [-4, 17], [6, 20], [-12, 20], [-33, 13], [30, -28], [-54, 13], [50, -2], [-16, -24], [-16, 17], [22, -44], [8, 10], [16, -28], [18, -42], [-52, -10], [-14, 28], [0, -22], [36, -34], [-7, -37], [53, -25], [16, -12], [-1, -21], [-20, 24], [0, 40], [-6, -22], [44, 20], [2, -27], [-38, 64], [14, 26], [8, -42], [4, -39], [43, -17], [-38, 43], [-10, 4], [-34, 14], [-42, 20], [62, 4], [-51, 5], [-26, -24], [-53, -1], [-52, -2], [27, 3], [12, -51], [56, -20], [52, 12], [-54, -20], [-12, 26], [-76, 10], [-25, 35], [12, 12], [-30, -30], [-20, 10], [-32, -20], [-34, -26], [57, -41], [-22, -14], [-20, 48], [66, 6], [66, -2], [-51, 43], [8, 34], [-5, -35], [-44, 28], [-8, 11], [-32, 3], [-39, -21], [-28, -4], [70, -3], [-62, -4], [-15, 15], [-36, -8], [-61, 35], [-2, 29], [-56, -6], [4, -32], [55, -45], [-26, 37], [0, -2], [41, -7], [-42, 0], [-6, -5], [-20, -15], [26, -26], [-50, 33], [82, 1], [52, -51], [6, 26], [-40, 28], [21, -67], [-18, -4], [70, -41], [-34, -30], [58, -6], [15, -13], [-27, 29], [-14, 32], [68, 4], [54, -28], [21, 17], [-32, -29], [-30, -25], [64, 14], [-16, 63], [14, 29], [-24, -28], [-74, 12], [4, -24], [-36, 52], [-12, -22], [20, -52], [9, -27], [31, -15], [-48, 42], [31, -41], [-49, 23], [-8, 66], [38, 0], [0, -40], [-66, -2], [64, -45], [36, -64], [-28, -28], [14, 8], [17, 7], [4, 22], [40, -15], [-48, -18], [-1, 17], [-16, -43], [74, -26], [64, -50], [19, 27], [-72, 12], [-42, 56], [24, 32], [48, -38], [9, -15], [-34, -10], [-23, -41], [0, 50], [18, -8], [-52, 7], [2, 30], [42, -14], [-52, -16], [-20, -31], [2, -25], [28, -23], [-4, 64], [-25, -23], [-20, -30], [-6, -14], [-32, -4], [-48, 72], [-12, 51], [28, -18], [28, -6], [12, 17], [-13, -15], [-46, 29], [-4, 48], [34, 12], [-24, -6], [48, 32], [-58, 23], [-28, 48], [-40, 10], [8, 4], [-56, 53], [44, -74], [-14, -52], [37, -29], [-18, -20], [-8, -32]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5054_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5054_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_5054_2_a_d(:prec:=2)
    chi := MakeCharacter_5054_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_5054_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_5054_2_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5054_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1, 1]>,<5,R![-11, 1, 1]>,<13,R![2, 1]>],Snew);
    return Vf;
end function;