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