
// Make newform 5054.2.a.s in Magma, downloaded from the LMFDB on 19 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_s();".
// 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_s();".
// 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, -3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0], [-1, 0, 1], [1, 0, 0], [-1, -1, 0], [0, 1, -1], [0, 0, -3], [0, 0, 0], [-2, 4, -2], [6, 1, 1], [-1, 3, 0], [-3, 3, 2], [-3, 1, 5], [-5, 2, 2], [3, 2, -1], [4, 3, -4], [-1, 0, -5], [-6, 0, -2], [-1, 0, -7], [-3, -3, -3], [-7, -4, 3], [2, -5, 5], [5, -3, 3], [5, 10, -6], [-4, 0, -4], [1, 4, 2], [2, -9, 8], [4, 3, 1], [-1, -12, 4], [-1, 2, -4], [2, -3, 9], [1, -6, 1], [6, -7, 5], [-9, 6, -9], [2, -4, 5], [5, 4, -5], [-7, -5, 5], [-18, 1, -1], [-7, 1, -4], [-3, -16, 10], [-4, -14, 12], [-6, -4, 1], [-10, 2, -1], [15, 2, -4], [-11, 1, 6], [-12, 2, 6], [-5, 3, -7], [-5, -12, 8], [4, -2, 11], [-17, -1, 6], [9, -4, 13], [6, 15, -10], [2, 5, 3], [-16, 6, 5], [-5, -15, 9], [-5, -18, 16], [6, 0, -4], [7, 8, 5], [-18, 0, -7], [-14, -2, -2], [-5, 15, -4], [-8, 13, -10], [1, -5, 9], [2, -12, 8], [4, 2, -13], [16, 4, -6], [1, 17, -9], [-12, 5, 9], [-18, -13, 5], [-6, -12, -4], [3, -12, -3], [-7, 13, -17], [5, -13, 22], [3, -13, 10], [18, -1, 8], [29, 4, -6], [21, 9, -9], [11, -9, -7], [4, -10, 17], [-1, 4, -4], [14, -4, 14], [-1, 3, -21], [-12, -6, 0], [-24, 7, -12], [7, 1, -6], [2, 7, 7], [-5, 0, 2], [-5, -10, -6], [3, 6, -11], [1, -18, 24], [-11, 15, -5], [-16, 0, -16], [18, -2, -3], [8, 6, -18], [-34, -3, 5], [-37, 1, -4], [-4, -4, -9], [-5, -4, 16], [24, 1, 0], [2, 7, -12], [-6, 11, 1], [-7, -1, -3], [-2, 3, -2], [-7, -6, 9], [-19, 11, -23], [0, -12, 1], [32, 5, 1], [-25, -9, 5], [-9, 2, -2], [-14, 3, -20], [5, -5, -17], [-6, 18, -9], [12, -6, -10], [-5, 10, -11], [4, -10, 11], [-10, 0, 8], [6, 24, -12], [29, -1, -2], [8, -6, 23], [-13, -6, 1], [2, 1, 5], [25, 13, -8], [9, -19, 3], [-24, 6, 0], [33, 3, -10], [11, -15, -19], [-16, -2, -8], [7, 29, -14], [-11, 14, -13], [18, 31, -18], [-19, 3, 10], [-21, 12, -1], [-21, 7, -20], [-13, -10, 18], [21, 5, 11], [7, 10, 5], [10, -7, -1], [9, 14, 3], [-29, 12, -4], [-26, 3, -17], [12, -16, 29], [-16, -3, -19], [-1, 11, 8], [11, -7, 34], [2, 16, -22], [-16, 10, -27], [21, 9, 7], [-5, 33, -3], [5, -8, 11], [24, -8, 2], [7, -3, -21], [-4, -3, 8], [-5, -21, 9], [-11, 22, -19], [-24, -5, -12], [21, 7, 1], [1, 6, 22], [11, -21, 35], [-9, -9, 2], [25, -9, 24], [-11, -20, 29], [-34, 2, 6], [3, 19, -5], [-2, 5, -14], [-15, -11, 6], [41, -2, 9], [0, -4, 4], [5, -4, 20], [-33, -2, -9], [11, -3, -8], [4, -8, -19], [5, 29, -21], [7, 1, -22], [5, -23, -9], [-9, -3, -9], [-26, 1, 17], [0, -22, 2], [27, -22, 5], [27, 8, 3], [12, 7, 19], [8, -30, -5], [1, -11, -8], [-2, 1, 1], [-17, -1, 4], [-16, -9, -5], [16, 27, -26], [20, 19, -11], [3, 21, -9], [12, 1, 9], [-20, -38, 16], [-9, -10, 32], [32, -2, -11], [-11, -8, 4], [-27, 6, 4], [14, -17, 0], [6, 10, -24], [-19, -3, -17], [9, -15, -23], [2, 12, 16], [31, 16, -8], [19, -4, 13], [-28, -24, 12], [-33, -9, -1], [-2, -17, 21], [3, 4, 13], [24, 6, -13], [-7, -29, -3], [30, 16, -2], [1, -13, 24], [36, -3, 7], [-15, 11, -29], [5, -11, -4], [19, 40, -35], [-17, -39, 28], [-8, -14, 2], [13, 29, -18], [-16, 1, 21], [-5, -7, -30], [11, -3, -7], [18, -13, 19], [23, -4, 11], [-9, -27, 26], [-34, -9, 2], [-5, -10, 25], [11, 8, 1], [-3, -6, 4], [-16, 3, -31], [13, 17, -4], [41, 17, -16], [-52, -3, 11], [0, -14, 18], [29, -4, -6], [16, -12, -16], [21, -28, 33], [22, 4, -8], [-7, -18, 4], [3, -18, 20], [-17, 1, 5], [-10, 33, -14], [-3, 32, -35], [21, -24, 27], [0, -8, 32], [22, -5, -4], [-12, 19, -6], [20, -5, 22], [32, 8, 15], [-5, 0, -33], [30, -20, -20], [-24, 27, -23], [7, -24, 1], [-32, 21, -16], [43, 10, 3], [-1, -31, 29], [-34, 2, 18], [-24, 17, -19], [-11, 35, -3], [-34, -38, 27], [-9, 8, -17], [11, -3, 22], [8, 13, -23], [-39, 3, -4], [14, -7, 24], [-44, 3, -5], [4, 15, -2], [-40, 18, -23], [6, 41, -23], [-1, 27, -16], [-1, -22, -10], [40, 5, -10], [8, 50, -35], [-6, -41, 23], [27, -32, 7], [-1, -21, -14], [-52, -1, 11], [1, 1, -27], [-22, -7, -27], [-15, 3, 5], [-51, -9, -5], [-23, -51, 25], [1, 29, -9], [-12, -1, -1], [-39, -5, -7], [0, -22, 17], [23, -27, -4], [22, 17, 15], [-18, 22, -28], [24, 46, -26], [11, 25, -16], [-14, 5, 10], [10, -4, -3], [-7, -23, 12], [-13, 4, -8], [12, -39, 36], [33, 0, 6], [-34, 10, 22], [-31, 10, 13], [23, 7, 17], [16, 26, -16], [1, 26, 11], [28, -9, -8], [31, -31, -21], [-7, 21, 20], [13, 2, 26], [-6, 21, -9], [-20, 12, -5], [26, 3, 7], [9, 15, -41], [16, 5, 26], [-1, 3, 18], [-20, -31, 28], [44, -12, 38], [-50, -39, 25], [-15, 21, -20], [-32, 32, 22], [25, -11, 35], [-45, 12, -41], [8, -13, 11], [28, -40, -6], [42, -6, 38], [1, 10, -3], [7, 33, -6], [7, -1, 17], [-23, -9, 1], [-56, 14, -31], [30, 19, 2], [-13, 18, 11], [-47, 13, -15], [39, 5, -22], [-14, 1, 36], [23, 23, 6], [-50, -2, -8], [47, -11, 9], [0, -9, -5], [-25, -5, -24], [-25, -7, 20], [-15, 9, -1], [-4, -41, 5], [-70, 1, 9], [-30, 16, 26], [8, -24, 2], [2, -2, 31], [-14, 24, -30], [-9, 24, -15], [24, -21, 29], [42, 22, -17], [-53, 11, -12], [-25, -58, 33], [1, 9, 36], [43, -11, 14], [-20, -13, 9], [5, -18, 0], [0, 29, -11], [53, 16, -8], [26, -14, 36], [14, -21, -15], [-25, 27, -16], [-23, -41, 14], [10, 2, -21], [-67, 9, 2], [11, 1, -12], [-15, 14, 29], [0, -27, 20], [16, 38, -18], [-32, -30, 22], [67, -19, 5], [2, 43, -12], [16, 11, -24], [-41, 13, -34], [-40, 18, 12], [-11, 18, -23], [-3, -23, 28], [30, 9, -3], [-12, 37, -35], [-20, -12, 42], [2, -11, -29], [-28, -41, 7], [-28, -4, -11], [16, 39, -2], [52, -32, -10], [-43, 25, -23], [-4, 11, -32], [-10, 12, 0], [-58, -13, 4], [-11, 24, -18], [19, 6, -39], [49, 8, -12], [13, -46, 24], [13, 25, -41], [4, 58, -18], [9, 15, 6], [37, -8, 4], [-25, -1, 7], [19, 17, -22], [22, 9, -22], [45, 47, -25], [21, -1, 2], [-9, 7, 35], [11, -27, 59], [26, -33, 30], [2, 5, -20], [51, 10, 8], [26, -5, -6], [-58, 3, 9], [-45, 27, -9], [-1, 26, 19], [28, 8, -4], [21, -12, -20], [-45, -3, -21], [15, 42, -32], [-32, 21, -35], [-51, 1, -9], [-14, 21, -15], [9, -2, 20], [9, -6, -28], [-16, -32, 8], [42, -23, -4], [17, -48, 23], [-37, 8, -19], [16, 24, -4], [17, -10, -9], [1, 52, -22], [-29, -9, 15], [31, 16, -7], [51, -35, -24], [2, -16, 27], [69, 22, -2], [24, -22, 16], [-30, 5, 23], [44, -2, 34], [-11, 33, -9]];
    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_s();".
// 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_s(:prec:=3)
    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_s();".
// 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_s( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5054_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, 0, -3, 1]>,<5,R![-1, 0, 3, 1]>,<13,R![1, -3, 0, 1]>],Snew);
    return Vf;
end function;