
// Make newform 5202.2.a.bh in Magma, downloaded from the LMFDB on 12 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5202_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_5202_2_a_bh();".
// 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_5202_2_a_bh();".
// 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_5202_a();"
function MakeCharacter_5202_a()
    N := 5202;
    order := 1;
    char_gens := [2891, 4627];
    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_5202_a_Hecke(Kf)
    return MakeCharacter_5202_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], [0, 0, 0], [-2, -1, 0], [1, 0, 0], [-2, 1, -1], [2, 0, 0], [0, 0, 0], [4, 0, 2], [-2, 0, -2], [-4, -2, 3], [0, -2, -3], [-4, -2, 2], [0, 6, 0], [0, -2, -4], [4, -4, 0], [2, -1, 7], [2, 5, -2], [4, 8, -4], [-2, -2, 0], [0, -4, 8], [4, 4, 1], [4, -5, 7], [7, 8, -6], [2, 8, -2], [4, -5, 1], [-2, -9, 4], [-4, 6, -9], [-8, 4, -7], [-2, 2, -4], [-10, 6, -4], [3, 4, -4], [-7, 4, 6], [2, -6, 8], [-2, 4, 4], [6, -7, -7], [-8, -3, 5], [0, -6, 10], [-14, 0, -6], [-10, 0, 8], [-2, -9, 7], [-1, 8, -14], [2, -2, 2], [-6, 8, -10], [8, -9, 4], [-13, 8, -2], [-8, -1, -10], [-4, -2, 8], [-4, -17, 7], [-3, -4, -10], [-4, 10, -14], [-12, -8, 4], [-6, -14, 10], [4, 1, -8], [20, 0, 5], [-6, -8, 10], [-10, 4, 6], [-2, -11, 5], [-4, -2, 7], [-6, 0, 2], [-16, -2, -6], [-18, 4, 0], [-6, 11, -4], [8, 4, -2], [-10, 6, -16], [2, 4, 8], [-5, 12, -2], [-14, -8, 6], [-5, 12, 0], [-14, -5, 2], [2, -10, -4], [2, 0, -16], [-2, -6, 10], [-4, 11, 6], [8, -2, -8], [-10, 18, -2], [-8, -8, 2], [0, -6, 3], [-2, -6, 18], [-4, -16, 16], [-8, 13, 3], [-6, 7, -2], [4, 22, -12], [14, 12, -4], [0, 1, -4], [0, 4, -12], [9, 4, 10], [0, 18, -18], [-4, -24, 9], [0, 2, -1], [-4, 7, -2], [20, 8, 1], [-14, -14, 2], [-20, -3, 6], [18, 5, 2], [-18, 8, 4], [-8, 2, -12], [-8, 2, -15], [6, -12, 12], [2, 10, -20], [-14, 2, 2], [-16, 4, -22], [7, -8, 14], [4, 0, -17], [-4, 10, 0], [-4, -4, 6], [-5, 8, -20], [18, -3, 6], [-32, 8, 0], [-2, -16, 12], [0, -20, 27], [-20, -2, 1], [4, -6, 20], [-12, -8, 4], [4, -22, 26], [20, -2, 1], [2, -6, 2], [-12, 12, -22], [-8, 8, -30], [-6, -16, 20], [2, 15, 5], [4, -24, 8], [14, 4, 8], [1, 12, -14], [-2, 13, 2], [-6, 4, -6], [6, -13, -1], [8, 18, -14], [28, -12, 4], [-12, 1, 2], [-12, 2, 18], [-18, 4, -4], [-2, 14, -12], [-16, 11, 6], [20, 22, -8], [-30, 2, 8], [-4, -3, 12], [14, 7, 3], [-16, -8, 20], [3, -28, 2], [34, 18, -14], [-12, -8, -12], [2, -12, 8], [-8, 9, 9], [2, -31, 10], [14, -14, 4], [8, -10, 34], [28, -2, -8], [2, -14, 18], [-10, -28, 26], [-28, 6, 2], [-8, 16, -20], [12, -6, -18], [-10, -24, 24], [12, 26, -16], [-8, -4, -16], [18, 2, 8], [-13, 12, -12], [14, -28, -8], [-44, -5, 0], [-26, 8, 0], [14, -3, -13], [-2, 0, -26], [-16, 23, 2], [14, -17, -15], [-12, 2, -10], [8, -12, -22], [7, 24, -12], [24, 6, -10], [29, -4, -12], [-14, -17, 23], [0, 4, -17], [-18, -4, -12], [6, 6, -24], [-7, 0, 0], [4, -19, -2], [28, 16, -10], [48, -2, 6], [20, -22, -11], [-25, -8, -8], [-6, -16, 18], [-25, 4, 16], [10, 33, -26], [6, -16, 16], [10, 10, -22], [-8, -16, -12], [26, -4, 28], [10, -30, 24], [14, 24, -12], [4, 23, -7], [2, -20, 6], [2, -16, 12], [5, -12, 2], [-10, 22, -8], [34, -7, 21], [55, 0, -2], [4, 32, -24], [8, -5, -7], [28, 0, 12], [4, -10, 12], [-26, 12, 16], [-7, -12, 2], [13, -16, -4], [-2, -24, 8], [16, 4, -21], [4, 0, -8], [29, -20, 6], [11, 28, -32], [-14, -33, 13], [4, 18, 10], [14, -18, 24], [-8, -33, 12], [14, 1, 21], [-20, -44, 28], [-6, -31, 23], [10, -14, 8], [16, 17, -25], [36, 5, -15], [24, 2, 2], [20, -2, 12], [-17, 20, 6], [40, 2, -8], [28, -6, -9], [20, -24, -4], [-27, 20, 16], [2, 5, -17], [20, 6, 24], [0, 2, -28], [54, -6, -6], [23, -12, -12], [-26, 19, -31], [16, 24, -4], [16, -10, -28], [-36, 11, -18], [0, 20, -28], [-2, 28, -6], [-28, -14, 6], [28, 8, -13], [18, -17, -8], [24, -24, -21], [-44, 6, 4], [24, 4, 25], [-4, -10, 8], [20, -13, 35], [8, 12, 30], [-16, -6, 8], [10, 14, 6], [-16, 17, -6], [0, -16, 23], [-6, 30, -20], [2, -44, 18], [0, -14, -34], [-8, 12, 22], [-18, -38, 16], [44, -1, -12], [-2, 35, -12], [6, -29, 25], [-2, -8, -8], [6, -24, 14], [22, 9, 13], [-15, 0, 16], [4, 3, -30], [-4, 28, 12], [14, -34, 18], [38, 0, -2], [-14, 16, 2], [26, -20, 34], [24, 34, -29], [-10, -14, -24], [-12, 14, -38], [23, -4, 10], [16, 24, -6], [-10, -18, -18], [-3, 36, -8], [16, -32, 0], [-7, 16, 4], [-16, -4, -4], [0, 32, -13], [12, 22, 2], [-12, -9, -8], [-38, 19, -7], [-42, 22, -14], [-32, -3, 18], [0, 0, 12], [-8, -20, 0], [-14, -28, 20], [32, 6, -16], [52, -21, -3], [-34, -5, -9], [4, 3, 26], [-10, 14, 28], [-2, 8, 12], [-35, 0, 22], [-10, -22, 16], [-10, -13, 34], [40, 2, -10], [-26, 27, -20], [-52, -7, 2], [37, 20, 6], [7, 32, -42], [-8, 18, -40], [-16, -17, 1], [-24, 22, 7], [-24, -34, 37], [-26, 21, -14], [-34, -10, 12], [-20, 3, -39], [-40, 12, -1], [58, -14, 16], [-14, 2, -30], [-10, -24, 6], [-56, 16, -10], [38, -15, 17], [-14, -26, 8], [8, 2, 20], [28, -28, -6], [36, -9, 1], [-15, 16, -50], [-16, -26, 24], [72, 1, -9], [-12, 26, 2], [-4, 36, -38], [-41, 8, -4], [-3, -44, 34], [-21, -4, 36], [-24, 20, 2], [-28, 4, 13], [4, -6, 18], [10, -42, 12], [-24, -14, -8], [-19, 8, 34], [16, -28, -28], [-3, -4, -10], [4, 35, -25], [-53, -32, 26], [24, -24, 50], [16, 4, -25], [-14, -10, -32], [-46, 26, 10], [51, 8, -8], [-52, 1, -2], [14, -10, -22], [-14, -30, 34], [-8, -10, 39], [-16, -7, -7], [-16, 22, -39], [16, 24, -20], [-26, 18, 30], [54, -14, -10], [-8, 10, -28], [4, -6, 31], [-52, -8, 16], [-32, 16, 31], [-18, -21, 36], [24, 58, -27], [-40, 2, 14], [50, -8, 30], [-38, 0, 28], [44, -16, 7], [0, -30, 12], [-48, 8, -16], [-44, 4, 10], [-34, -2, 24], [-8, -26, 14], [12, 20, -16], [26, 30, -6], [-13, 0, -24], [-36, -2, 7], [-16, 43, -38], [-64, -17, 8], [21, 24, -18], [20, 28, -22], [-38, -18, 16], [-26, 4, -40], [8, 21, 6], [-22, 34, -46], [22, 15, 25], [14, 18, 2], [-16, 20, -7], [24, 20, -34], [23, 40, -32], [14, 7, 3], [8, -12, -4], [-68, 9, -3], [-36, 18, -30], [50, -6, 28], [-34, 26, 16], [24, 19, -10], [38, -12, 12], [34, -6, -12], [8, -42, 38], [18, -20, 12], [-74, -1, -9], [46, -13, -15], [-10, -26, 10], [-54, -36, 18], [32, -24, 19], [16, -43, 1], [34, 26, -30], [12, 22, -24], [-21, 28, -2], [36, 30, -10], [-14, -14, 44], [-40, -11, 1], [-70, -10, -8], [14, -52, 28], [-24, -55, 37], [22, 22, -12], [20, -32, 18], [-4, 40, -56], [-24, 8, 32], [49, -16, 8], [10, 41, -36], [26, 37, -30], [10, -34, -10], [20, -20, 55], [-16, 18, -25], [58, 4, -16], [-40, -7, -15], [8, -12, -34], [-14, -2, 2], [-54, 33, 12], [-18, 38, -2], [-10, -2, 0], [58, -21, 10], [48, -21, 44], [-24, 14, 17], [-24, 28, 7], [58, 16, -4], [48, 42, -26], [14, 8, -26]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5202_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5202_2_a_bh();".
// 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_5202_2_a_bh(:prec:=3)
    chi := MakeCharacter_5202_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_5202_2_a_bh();".
// 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_5202_2_a_bh( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5202_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 9, 6, 1]>,<7,R![-1, 1]>,<23,R![-24, 0, 6, 1]>,<47,R![192, 0, -12, 1]>],Snew);
    return Vf;
end function;