
// Make newform 4851.2.a.bd in Magma, downloaded from the LMFDB on 24 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_bd();".
// 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_bd();".
// 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 := [-2, 0, 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_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, 1], [0, 0], [-2, 0], [0, 0], [-1, 0], [-2, 2], [-3, -1], [-3, -3], [7, 0], [1, -3], [0, -4], [-1, -6], [-4, 2], [-7, 3], [7, 2], [10, -2], [-3, -4], [4, 0], [-6, 2], [7, -2], [-6, 4], [2, 8], [0, -2], [0, -10], [3, 6], [-5, 7], [-4, -6], [-4, -4], [-4, -2], [2, 4], [-9, -5], [-8, 2], [8, -6], [-7, -1], [1, 15], [-9, -7], [-7, 0], [-4, -12], [8, -2], [-12, -2], [-15, -2], [6, 4], [16, 4], [2, -10], [15, 1], [0, 14], [-2, 12], [6, 12], [-2, 12], [10, 4], [-9, -1], [-2, 6], [4, -8], [-11, 6], [-4, 18], [-18, 0], [-8, -14], [6, 12], [-20, -4], [-13, -1], [-26, 4], [-7, 9], [6, 8], [7, 16], [15, -14], [14, 8], [14, 6], [18, -10], [2, -12], [6, -12], [2, 8], [8, 0], [-10, 10], [4, -4], [16, 2], [-9, 8], [-8, 14], [-9, -6], [-16, 6], [-12, 14], [-5, 14], [-7, 0], [10, -12], [-15, 10], [-21, -7], [-11, -16], [4, 0], [16, 2], [11, -3], [-4, 16], [3, -8], [14, 0], [-10, -16], [10, -6], [-12, -14], [0, 0], [-12, 0], [34, -2], [-6, 20], [14, 4], [-3, -15], [-7, 11], [22, 2], [3, -3], [-11, 5], [-2, -4], [20, -8], [-35, 9], [24, -16], [6, 6], [30, -8], [28, -2], [-16, 2], [-12, -16], [-2, 20], [4, 6], [-2, 0], [-8, -8], [0, -22], [-26, -6], [5, -12], [30, -2], [9, -29], [-23, 14], [-14, 18], [-7, 7], [-11, 26], [31, -14], [2, -34], [-4, -4], [-34, -4], [-14, -14], [-18, -8], [-19, 8], [-20, 14], [-14, -14], [-46, -4], [3, -23], [14, -2], [-8, -2], [2, -12], [-36, -2], [-36, 2], [26, 8], [9, -2], [-8, 12], [4, 6], [-9, -25], [16, 6], [20, -16], [-6, 6], [28, -8], [34, 4], [-2, -8], [-26, 6], [13, -6], [21, 25], [-20, -2], [-20, 10], [-5, -5], [-29, -6], [0, 10], [3, -17], [-12, 12], [10, 2], [-19, 12], [-36, 8], [-24, 10], [-18, -24], [-38, 0], [-10, 22], [-34, -8], [24, 28], [0, 10], [16, -8], [20, -16], [33, 3], [0, 8], [26, 12], [-48, 6], [-38, -4], [-16, -24], [-19, 8], [27, -5], [-7, -18], [-4, 16], [40, 12], [0, -22], [-28, -4], [-8, -32], [-27, -16], [0, -2], [-20, 2], [22, -18], [26, 26], [60, 0], [10, -20], [-1, 8], [-40, 6], [-10, 30], [-4, -6], [-3, -33], [34, -8], [36, -2], [-41, -10], [22, 10], [10, 16], [-26, 10], [-12, -30], [32, 14], [-2, 26], [-20, 20], [-8, 38], [-24, 0], [-56, 4], [-17, -22], [2, -28], [0, -26], [12, 4], [18, 24], [42, 10], [33, 15], [-22, -14], [-12, -6], [-46, -18], [24, -24], [36, -18], [-13, -8], [-5, -21], [12, 30], [-25, 14], [-27, -3], [15, 5], [5, -11], [34, 2], [-38, 2], [-7, 0], [-52, -2], [-44, 12], [8, 24], [-29, 24], [26, 12], [-10, 42], [-25, -8], [-17, -11], [14, -26], [26, -14], [-3, -6], [25, -17], [30, -10], [-24, 34], [-47, -3], [29, 6], [10, 4], [16, -2], [50, -2], [10, 8], [-39, 17], [-18, -36], [-46, 8], [-10, 40], [10, 8], [22, -28], [-30, 0], [-4, -6], [-44, -12], [-8, 28], [36, 16], [15, -13], [16, 6], [-1, 6], [-8, 50], [2, 20], [29, -13], [-22, -18], [24, -8], [19, -18], [-22, 22], [-22, -38], [16, 12], [-16, 46], [-6, -2], [14, -10], [-30, -22], [-33, -23], [65, 12], [-18, 16], [-35, -19], [-14, 34], [-41, -15], [-34, 4], [55, 4], [0, 18], [20, 24], [-6, 10], [-24, 14], [-38, -30], [2, 10], [62, 4], [-47, 5], [-12, 18], [-41, 3], [-17, 27], [-29, -12], [36, 6], [-30, 8], [16, -20], [-9, 32], [11, 16], [-8, 12], [4, 22], [-50, 2], [-67, 3], [38, 24], [34, 0], [28, -14], [-37, -8], [-4, 10], [-27, -36], [-9, 21], [-11, 11], [-11, -32], [51, -11], [6, 14], [63, 7], [-18, 32], [42, 0], [56, 6], [-2, -32], [12, -42], [-14, -46], [44, 2], [9, 39], [-24, 40], [-18, 40], [-12, 0], [-15, 2], [-21, 33], [-49, -4], [77, -1], [-18, 16], [-4, -2], [-51, -19], [-40, 14], [6, -26], [-14, -46], [21, 48], [-38, -14], [22, -8], [20, -12], [5, -1], [13, -36], [12, 30], [57, 5], [12, 42], [-69, -5], [-24, -32], [-48, 4], [57, -3], [48, 4], [-48, 14], [19, 29], [8, -48], [2, -64], [-38, -34], [-23, 32], [69, -3], [-38, 8], [-30, 34], [-37, -24], [34, 30], [16, 8], [36, -26], [-18, 0], [53, -8], [41, 30], [-8, 32], [10, 34], [-28, -26], [-4, 6], [-18, 18], [-28, 20], [-74, 8], [35, -27], [-1, -41], [-8, -40], [12, -58], [53, 26], [89, -9], [-21, -14], [5, 26], [-58, 28], [-33, 22], [60, 26], [31, -4], [-18, -16], [-11, -9], [-30, -26], [-6, 0], [-57, 1], [-74, 8], [-12, -12], [18, -32], [80, 10], [-15, 29], [-54, -32], [-5, 30], [-35, -5], [4, -14], [-77, -2], [58, 4], [-3, -61], [-41, -26], [57, -13], [4, 18], [-30, 16], [-18, 58], [38, 12], [-34, -34], [-44, -20], [-50, 36], [-38, 18], [11, -42], [-20, 30], [-35, -36], [-26, 18], [24, 4], [75, 15], [-4, 46], [-22, -30]];
    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_bd();".
// 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_bd(:prec:=2)
    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_bd();".
// 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_bd( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4851_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -2, 1]>,<5,R![2, 1]>,<13,R![-4, 4, 1]>,<17,R![7, 6, 1]>],Snew);
    return Vf;
end function;