
// Make newform 4851.2.a.bi in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4851_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_4851_2_a_bi();".
// 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_4851_2_a_bi();".
// 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, -4, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-3, 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_4851_a();"
function MakeCharacter_4851_a()
    N := 4851;
    order := 1;
    char_gens := [4313, 199, 442];
    v := [1, 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_4851_a_Hecke(Kf)
    return MakeCharacter_4851_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, -1], [0, 0, 0], [1, -1, -1], [0, 0, 0], [1, 0, 0], [1, 3, -1], [2, 4, -2], [3, -1, 1], [-4, 0, -2], [1, 3, -1], [2, -2, 4], [1, -1, 3], [4, 2, -2], [-4, 4, 2], [1, 1, 3], [0, 2, 0], [1, -3, 3], [2, 0, 0], [-1, -5, 1], [4, -4, 0], [7, -3, 1], [4, -4, 0], [2, -6, 0], [10, 0, 4], [0, -2, -4], [-2, -4, -2], [6, -2, 2], [-9, 3, -3], [10, 0, 6], [-2, 8, -4], [-6, -2, 0], [4, 0, 0], [2, 0, 6], [8, -4, 0], [1, -5, -1], [-6, -2, -4], [0, -2, -6], [-1, -5, 1], [-16, 4, -2], [4, -2, 10], [0, -4, -4], [-8, 2, -4], [2, -2, 8], [-12, -2, -4], [6, 4, 8], [-4, 0, 2], [-10, 2, -2], [14, 6, -2], [-6, -2, -2], [-4, -10, 8], [-2, -8, 8], [5, 5, 3], [1, -1, 7], [3, -1, -7], [1, 3, -5], [-13, -5, -3], [14, 8, 0], [23, -1, 1], [4, 14, -6], [-7, -1, 7], [7, -5, 13], [0, -6, 2], [8, -4, -4], [8, 0, 0], [-14, 0, -2], [-4, -2, -8], [16, -4, 0], [0, -6, 0], [4, -8, 8], [-7, 11, -1], [-17, 1, 5], [4, 12, -8], [-2, -10, 6], [6, -12, 2], [13, 1, 3], [-12, 4, 4], [-8, 6, -2], [10, -12, 6], [-4, 2, -4], [-14, 12, -4], [3, -1, 5], [11, 1, 13], [7, 7, 1], [8, -6, 14], [-13, 3, -11], [-8, -12, 8], [8, -14, 2], [12, 6, 8], [0, 2, 2], [23, -9, -3], [-7, 5, -9], [-6, 6, 12], [12, -12, -4], [-29, -1, 1], [-11, 17, 3], [24, -4, -2], [-6, 4, -12], [9, -5, 15], [9, 5, 7], [8, 2, 10], [4, 16, -8], [-7, 3, -5], [8, 8, -2], [-10, 0, 0], [20, 0, 12], [10, -8, 10], [3, -17, -7], [-20, -6, 2], [-20, 4, 0], [-25, 5, 5], [7, 7, -3], [6, 20, -6], [18, -12, 8], [-14, 10, -16], [36, 4, 0], [6, -12, 6], [12, 4, 2], [7, 7, -7], [16, -10, -10], [-17, 3, -3], [20, -10, 4], [20, -2, -4], [-18, -8, 4], [-6, 18, 4], [-6, 6, 6], [30, -4, 4], [-33, 5, -15], [7, 7, -3], [8, 12, -10], [-2, -4, -4], [-24, 0, -6], [11, -13, -3], [7, -17, -7], [-27, 3, 7], [12, 2, 10], [13, 11, -5], [-7, -17, 11], [-11, 1, -9], [-9, 13, -7], [-31, 7, -1], [17, 13, -1], [3, -19, 13], [-5, -5, 5], [-29, 7, 5], [16, 6, -2], [-11, -3, -9], [22, 4, 0], [-10, -12, -4], [12, 8, 0], [-12, -16, 6], [8, -22, 22], [29, -1, -1], [-11, 9, -17], [10, -2, 20], [-20, 16, -16], [22, 14, -6], [0, -4, 10], [19, -3, 5], [2, -4, 12], [8, 18, -10], [22, 2, -2], [-3, -5, -9], [18, 10, -2], [-1, 11, -11], [28, -14, 4], [-40, 8, 4], [31, 7, -7], [6, -20, 4], [2, -16, 0], [1, -17, -9], [7, 11, 1], [-26, 12, -16], [-8, 8, 8], [-14, -12, 14], [-10, 14, 14], [-34, -4, -4], [4, -28, 6], [6, 4, 16], [5, 13, -9], [-25, -7, 1], [7, 15, -7], [-8, 0, 2], [-10, 8, -20], [16, -18, -6], [-4, 20, 4], [13, -5, -5], [-28, 6, 2], [-6, -30, 12], [-10, -8, 10], [-14, -10, 8], [-10, 8, -8], [-31, 13, -21], [-21, -9, 13], [11, 25, 1], [-29, -1, 5], [19, -19, 1], [2, -12, 2], [-2, -24, 4], [4, -2, 16], [0, 4, -10], [10, 24, -16], [21, 13, -5], [36, 6, -4], [-5, 1, -15], [40, 12, -4], [22, 0, 0], [4, -20, -8], [-23, 13, -5], [-6, -4, -24], [-12, -4, -10], [-6, 8, 8], [19, 9, -7], [1, -23, 3], [16, -4, 0], [14, 6, -6], [26, 12, 0], [0, 16, -24], [-30, 12, 2], [-2, -6, 16], [26, -12, 6], [-14, 8, 2], [1, -15, 7], [28, -14, 4], [-11, -19, 11], [20, -8, -4], [-6, -12, 4], [3, 21, -19], [6, -18, 2], [-3, -3, -21], [21, 9, -13], [2, 4, 20], [9, -11, 11], [-34, 6, -10], [9, -33, -1], [-48, 0, -2], [-34, -2, -14], [-34, -4, -2], [-19, -1, -1], [14, 14, -20], [9, -23, 7], [24, -8, -10], [12, 8, 24], [14, 2, 0], [6, 16, 4], [12, -6, -6], [-8, 24, 4], [-4, 20, -16], [13, 1, 15], [-12, 0, -28], [7, 15, 13], [14, 32, -24], [48, 10, -4], [-22, 2, 2], [-34, 16, -12], [2, 4, -8], [52, 0, 8], [-3, -13, 27], [-23, 21, -1], [3, -7, -7], [-17, -3, -27], [-14, -14, 22], [-44, 0, -8], [-22, -16, -8], [35, -3, 17], [3, 37, -7], [10, 26, 0], [22, -20, 24], [9, -29, 11], [0, -20, 0], [-10, -20, -4], [8, 14, -2], [-5, -25, -3], [44, -6, 20], [-2, 2, 4], [-1, -11, -3], [-6, -26, 16], [-12, -24, 16], [26, -12, -16], [-2, 32, -14], [-8, -16, 10], [-12, -24, -10], [40, 0, 0], [-2, -30, 16], [3, -19, -3], [63, -5, 9], [-24, -4, -2], [-49, -7, 5], [2, 16, -4], [26, -10, 8], [-14, -20, -6], [-14, 4, 10], [28, 24, -16], [21, 3, -1], [52, 12, 2], [-20, -2, 10], [4, -22, 8], [-2, -2, -30], [38, 8, 8], [-4, -20, -2], [7, 27, 9], [25, -25, -5], [-9, 1, -3], [8, -4, -22], [16, 0, -18], [19, 7, 13], [-1, -15, 1], [-28, 4, -26], [-12, 14, 4], [44, 8, 2], [16, 6, 12], [-18, -2, 10], [-36, 2, -18], [-63, -9, -5], [-44, 0, 0], [-40, -8, -8], [10, 4, 12], [4, -8, 4], [-56, 0, 0], [40, 6, 20], [-38, 20, -16], [23, 19, 13], [-25, 33, 1], [14, -16, 20], [-24, 32, 12], [11, 1, -27], [28, 14, -14], [-4, -24, 0], [-32, -8, -22], [49, 9, -13], [-31, 11, 7], [16, 26, -14], [48, 30, -14], [-1, -17, -27], [-10, 22, -32], [-24, 8, -22], [-23, -11, 19], [23, 29, -19], [0, -14, 0], [-6, 32, -30], [20, -12, 4], [6, -24, -4], [6, 12, 14], [-10, 28, 2], [25, 25, -1], [34, 12, 6], [3, 23, 1], [-2, -20, 8], [40, 4, -16], [-4, 20, 8], [-30, 4, 4], [28, -24, -4], [13, 31, -25], [40, 6, -14], [-33, 15, -11], [6, 4, -6], [62, -8, -6], [5, -19, -13], [-32, 4, 20], [-19, -21, 19], [-8, -4, 10], [-20, -18, -10], [40, -10, 18], [36, 4, 24], [31, -21, 13], [-4, 16, -8], [-21, -43, 29], [-10, 36, -30], [14, 6, -14], [22, -36, 4], [30, -10, 12], [-42, 6, -16], [9, -23, 7], [-53, 5, 13], [-20, 4, 12], [55, -3, 29], [19, 7, 1], [25, -23, -17], [7, 13, -19], [-14, 8, -24], [10, -12, 28], [-13, -39, 21], [52, -14, 20], [-2, -10, 18], [9, 31, -21], [4, -24, -8], [-27, -35, 7], [59, -5, 33], [-6, -8, -20], [22, 6, -12], [-9, 7, 5], [45, 23, -9], [58, -12, 24], [4, -28, -18], [44, -12, 50], [22, 6, -6], [38, -16, 6], [-1, -33, 5], [15, -15, -7], [8, 20, 32], [-3, -17, 7], [1, -13, -9], [-30, 0, 10], [18, 22, -16], [-42, 4, -28], [20, 2, 0], [-59, 5, 11], [35, -15, 13], [-6, 28, 12], [-46, -10, -22], [-32, -12, 8], [14, 8, 36], [-14, 16, 12], [-56, 20, 0], [-24, -16, -6], [-6, -8, -12], [-31, 31, 11], [3, -21, 25], [22, 2, -4], [-46, 16, 12], [22, 10, 24], [14, 32, -18], [77, -17, -9], [-46, 14, -32], [30, -26, 24], [6, 16, 16], [17, 15, 35], [-6, 42, -12], [-7, -41, 3], [6, 26, 2], [-4, 36, -12]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4851_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4851_2_a_bi();".
// 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_4851_2_a_bi(:prec:=3)
    chi := MakeCharacter_4851_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_4851_2_a_bi();".
// 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_4851_2_a_bi( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4851_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-7, -4, 2, 1]>,<5,R![26, -7, -4, 1]>,<13,R![94, -27, -4, 1]>,<17,R![328, -40, -8, 1]>],Snew);
    return Vf;
end function;