
// Make newform 5054.2.a.h in Magma, downloaded from the LMFDB on 30 December 2025.
// 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_h();".
// 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_h();".
// 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 := [-4, -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 := 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]];
    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], [-1, 0], [-4, -1], [-3, 1], [2, 0], [0, 0], [1, 1], [0, -4], [-2, 0], [4, -2], [0, -3], [-2, -2], [-2, -2], [4, 2], [11, 0], [-13, 1], [4, -3], [-7, 5], [2, 1], [8, -4], [-1, 0], [0, 6], [6, -1], [2, -6], [-8, 2], [2, -2], [4, -4], [-17, 0], [9, -3], [1, 4], [7, -6], [-12, -1], [0, 6], [-1, -1], [-15, 3], [-14, 3], [18, -2], [13, -3], [2, 7], [5, 5], [13, -5], [9, -3], [0, -10], [-10, 0], [4, -8], [2, 8], [-5, 12], [9, -5], [-19, -2], [11, 5], [-14, 7], [3, 6], [0, -1], [13, -9], [10, 2], [-18, 6], [2, 2], [-6, -3], [3, -2], [15, -7], [-3, 0], [-2, -8], [-4, -11], [6, -8], [-8, 5], [-15, 10], [10, -3], [-10, 6], [4, 3], [0, 6], [8, 6], [-30, 2], [14, 6], [-32, 0], [8, -12], [-18, 12], [-9, 6], [4, -3], [12, 8], [-20, 6], [-16, -6], [-4, -10], [-2, 12], [2, -9], [-3, 12], [17, 4], [21, 5], [19, -1], [0, 5], [-22, 2], [-4, -14], [20, -12], [10, -11], [-18, 4], [9, 3], [-10, 7], [8, 4], [-24, 4], [-6, 18], [38, 2], [31, 0], [-25, -1], [22, -13], [-16, -9], [12, 0], [0, 7], [13, -3], [-26, 9], [12, -12], [18, -8], [-23, -6], [13, -5], [-16, -8], [-29, -2], [-11, 4], [6, 12], [-26, 6], [14, -2], [19, 5], [-1, 3], [22, -8], [20, -8], [9, -9], [-30, 2], [0, -6], [2, 4], [-8, -16], [-5, -9], [-20, 15], [29, -15], [-16, -10], [-22, 20], [-28, 1], [-6, 8], [-5, 21], [-21, -6], [29, 9], [5, -16], [-4, 12], [24, 2], [9, -7], [-4, -3], [-41, 1], [10, -2], [-18, 20], [-18, -5], [-48, 5], [-20, 2], [12, -12], [26, 1], [42, -5], [22, -6], [38, -13], [25, -3], [-13, 7], [-48, 1], [-22, 13], [15, 11], [2, 2], [-42, -1], [7, 3], [25, -8], [-5, -10], [38, -6], [17, -13], [35, -7], [-14, -4], [-38, 8], [20, -19], [38, 4], [-8, 10], [-10, -3], [-24, 2], [-30, 1], [8, -17], [34, -6], [-20, 0], [23, -13], [1, -3], [-29, -3], [-30, -2], [-18, -3], [-13, 25], [18, 2], [-14, -14], [-5, 1], [15, 2], [8, 18], [30, -15], [-14, 10], [52, -1], [-7, 11], [-4, 5], [36, -2], [41, -6], [36, -16], [0, -1], [6, 12], [-36, -2], [38, -6], [35, 3], [8, -17], [40, 5], [27, 15], [32, -6], [18, -15], [46, 1], [-8, -7], [-13, -2], [-1, 23], [8, 12], [-17, -10], [-2, 8], [-18, 12], [-24, 2], [-12, 18], [17, 5], [-20, -2], [-18, 0], [14, -16], [9, -12], [43, 5], [5, 14], [-64, 2], [-20, 15], [-29, 17], [4, -16], [0, -20], [-2, -2], [-23, 0], [11, 3], [-17, 12], [-8, 7], [32, -14], [38, -17], [42, -12], [-40, -3], [8, 14], [10, -7], [-9, -10], [10, 2], [24, 2], [-52, -2], [-26, 28], [8, -28], [-32, 5], [-40, 13], [-45, 5], [6, 2], [14, -8], [8, 22], [6, 1], [14, 24], [16, -4], [-20, -14], [13, 10], [18, -24], [-16, -14], [-9, 15], [0, -25], [-57, -3], [-6, 14], [-28, 5], [-40, 7], [20, -4], [-54, 13], [16, 12], [-28, -2], [-26, 4], [-44, 0], [-2, 5], [-40, -4], [16, -10], [-28, 2], [-20, 9], [18, 6], [-21, 0], [-41, 1], [-2, -28], [-11, 1], [-6, -30], [18, -36], [-35, 11], [-11, 11], [-27, 0], [-14, 22], [2, -10], [8, 22], [-20, 18], [-33, 17], [-7, -6], [-3, -5], [6, 2], [-3, 13], [-20, 14], [25, 7], [-71, 7], [20, -39], [18, -17], [10, 28], [-11, 23], [58, -7], [-48, 3], [-77, 6], [2, 29], [26, 18], [-12, -4], [34, 10], [-24, 6], [-8, -22], [-53, 10], [-14, 1], [-7, 21], [6, 20], [-25, -2], [15, 11], [-16, -21], [-31, 17], [73, -8], [70, 3], [2, -36], [-13, -3], [-23, 5], [62, -4], [-30, 35], [24, 17], [53, -11], [-20, 34], [-46, -12], [-68, 4], [6, 14], [65, -1], [-16, 17], [-33, 25], [50, -14], [-36, -13], [58, -12], [50, 8], [-52, 4], [42, -1], [-23, -3], [-40, -4], [-56, 14], [42, -25], [34, -18], [22, -2], [-10, -12], [-61, 11], [31, 4], [-58, 11], [2, -16], [-16, 10], [-68, 16], [4, -13], [-4, -8], [-24, 29], [-2, -33], [51, 11], [16, 26], [-38, 17], [-35, -1], [-40, -7], [-76, 12], [-19, 29], [21, 11], [16, -2], [13, 16], [22, -3], [19, 13], [35, 13], [32, 14], [-14, 0], [-48, -16], [25, -25], [-56, 24], [-58, 9], [-12, -7], [-2, -13], [-2, -6], [26, -8], [64, 10], [-37, 0], [-39, 14], [-24, 16], [-25, -15], [-61, 11], [-44, 9], [-6, -12], [-2, 1], [10, 12], [16, -5], [-25, -14], [57, -5], [43, -18], [-80, 0], [12, -15], [-48, 19], [52, -18], [34, -8], [30, -37], [-53, 1], [-42, -4], [1, 3], [-46, -8], [-2, 10], [-42, 35], [25, -17], [23, -22], [-44, -5], [0, 12], [22, 1], [-64, 9], [5, 2], [-51, 5], [29, 11], [16, -12], [-40, -14], [0, -28], [-6, 8], [51, -11], [-15, -7], [3, -10], [56, 18], [15, -27], [-6, -25], [49, -7], [17, -1], [12, 2]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5054_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5054_2_a_h();".
// 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_h(: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_h();".
// 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_h( : 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, -1, 1]>,<5,R![-4, 1, 1]>,<13,R![2, 5, 1]>],Snew);
    return Vf;
end function;