
// Make newform 4864.2.a.bh in Magma, downloaded from the LMFDB on 30 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4864_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_4864_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_4864_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 := [2, -4, -1, 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_4864_a();"
function MakeCharacter_4864_a()
    N := 4864;
    order := 1;
    char_gens := [3839, 2053, 4353];
    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_4864_a_Hecke(Kf)
    return MakeCharacter_4864_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 := [[0, 0, 0], [1, -1, 1], [1, 1, 0], [-1, -1, 0], [-2, 0, 1], [-2, 2, -2], [-2, 2, -1], [-1, 0, 0], [-4, 0, 0], [-1, 1, -3], [-1, 5, -1], [1, -1, -1], [-1, -1, 3], [0, -2, -1], [1, -3, -2], [-5, 1, 3], [10, -2, 0], [1, 5, -2], [-1, -3, -3], [-6, 2, -4], [8, 0, 3], [-1, -3, -1], [-6, -2, 4], [1, -3, -1], [-8, 2, 0], [-6, -2, 0], [2, -6, 0], [4, 4, -2], [1, -5, 1], [-1, 3, 3], [3, -3, 1], [8, -6, 1], [-2, 2, 3], [-2, -4, 3], [9, -7, -4], [10, -6, 0], [12, -4, -2], [0, 8, -2], [-5, -3, 5], [-8, 4, -2], [3, -3, -1], [6, -2, 0], [-11, 5, -4], [9, 1, -1], [8, 4, 4], [-21, -1, 2], [4, 4, 2], [3, -3, 9], [-10, 6, 6], [-7, -3, 2], [2, -2, 5], [-9, 3, 6], [-7, -11, 5], [2, 4, -7], [0, 6, 0], [-15, 5, -2], [-13, 1, 3], [-14, 6, -4], [13, -11, 0], [2, 16, -4], [-12, 6, -11], [6, -6, 10], [3, 1, 5], [-13, -1, -2], [10, 0, 10], [-18, -2, 4], [-23, 3, -1], [6, 16, -6], [0, 14, -1], [-7, 5, 6], [-2, 8, -6], [5, -7, 14], [6, -2, 6], [6, -6, 10], [-2, 2, 0], [-19, 7, 1], [11, -5, 4], [-25, 3, -6], [-13, 7, -11], [-19, 5, 3], [-28, 4, 2], [-15, 3, 9], [-18, -6, 6], [-13, 3, -9], [21, 3, -5], [0, -18, 7], [-14, -8, -2], [4, -12, 1], [13, -7, 6], [-17, 7, -16], [20, -2, 5], [2, 10, -6], [16, -4, -10], [8, 8, 6], [10, -4, 5], [10, 10, -6], [-6, 14, -6], [3, -17, 5], [-16, -16, 6], [5, -11, 8], [-12, -8, 8], [11, 11, 0], [1, 7, 5], [-2, -4, -10], [-26, 2, 4], [-8, 8, 3], [-14, 0, -5], [-2, 0, -10], [-25, -3, -1], [2, 0, -12], [2, -2, -10], [19, -5, -4], [0, 16, -9], [-8, -8, -2], [-7, 21, 2], [18, -8, 14], [6, 12, -5], [-13, 3, -12], [9, -11, -2], [-2, 18, -12], [8, 0, -4], [-10, 0, 4], [11, -11, -11], [-18, 6, -10], [28, -6, 3], [6, -14, 12], [-8, 0, -14], [7, -1, 12], [25, -11, 18], [22, 10, -4], [20, -14, 19], [-21, 9, -1], [-2, 10, 10], [-1, 19, -6], [-24, 12, 7], [-4, 12, -3], [6, 14, 0], [6, 14, -6], [13, 7, -11], [-44, 4, -7], [-16, -12, 4], [-13, -1, -6], [37, -3, 4], [-1, 13, -7], [-28, 12, -4], [-14, 2, -4], [-14, -10, 4], [0, -2, 2], [8, 10, -3], [1, -1, 3], [-8, -4, -2], [12, -8, 5], [2, 8, -5], [0, 4, -14], [5, -1, 15], [-14, 10, 4], [2, 2, -18], [18, 0, -14], [16, -12, -11], [22, 6, 8], [-4, 16, -8], [25, -7, 7], [6, -14, 16], [-12, -8, -12], [-33, -5, -7], [-26, 2, 6], [9, -1, 3], [5, 5, -12], [7, -5, 13], [9, -7, 4], [21, 3, -3], [-13, 1, 15], [-6, -22, 6], [12, 2, 4], [42, -2, 6], [2, -26, 1], [52, 4, -2], [19, 7, 6], [38, -6, 6], [-9, -1, -16], [-29, -9, -2], [-12, -24, 8], [2, -14, 20], [-8, 18, 2], [1, 9, -20], [27, 3, -4], [-4, -8, 10], [36, -12, -6], [-3, 9, -15], [18, -18, -4], [-3, -27, 13], [8, -10, 15], [17, 3, -1], [7, -7, 1], [-22, -10, 0], [32, -2, 0], [-6, 6, 1], [-14, -2, -8], [14, -8, 18], [9, 25, -4], [12, -4, 8], [15, -23, -3], [-21, 1, 11], [-7, -7, -7], [-36, 12, -2], [35, 7, -2], [25, 1, 8], [-4, 16, 8], [-26, -2, 15], [8, 0, -22], [4, 14, -16], [-14, -10, 12], [35, 15, -2], [2, -6, 6], [-23, -1, 3], [17, -3, -7], [-6, 6, 0], [-44, -2, 0], [-11, -17, 5], [7, 19, 6], [30, -2, -12], [1, 15, -21], [9, -11, 5], [-7, 5, 6], [10, -10, 8], [29, 9, -6], [18, -16, 4], [-25, 5, 11], [-4, -8, -6], [20, -22, -5], [-3, -15, 14], [-19, -1, -9], [-17, -11, 15], [-38, 8, -12], [-50, 12, -5], [-35, 17, 14], [-22, 6, -5], [-17, 11, -2], [18, 14, -12], [-7, 3, 13], [18, -26, -4], [-26, 8, 17], [-8, -20, -8], [26, 10, -8], [-13, -1, -9], [-29, 11, 4], [1, 5, -18], [-21, 17, 1], [16, 28, -8], [-39, 1, 8], [16, -8, -2], [2, -14, 15], [-26, 18, 4], [11, 11, -15], [-33, -25, 8], [0, 0, -6], [-13, 7, -2], [27, -11, 3], [9, -25, -5], [-42, -12, 6], [49, -1, 3], [-28, -12, 10], [7, 19, -18], [-12, -32, 6], [-6, -14, 13], [-13, 1, 13], [-15, -21, 9], [-4, -24, 9], [4, -4, -6], [13, 1, -2], [-29, -15, -1], [45, 19, -3], [6, 18, -19], [17, -19, -2], [21, -23, 15], [32, 12, -8], [-4, 22, 1], [31, -19, 23], [-6, -16, 0], [32, -22, 13], [4, -24, 18], [14, -2, -18], [-1, 3, -2], [-26, 14, 12], [8, 14, 3], [-47, 9, -8], [4, 0, 7], [20, -4, -16], [27, -21, 0], [4, -14, 20], [18, 6, 8], [22, -28, 23], [-6, 28, -14], [46, -14, 8], [42, 10, 8], [8, 4, -4], [-38, 14, -14], [43, 15, 2], [-25, -19, 13], [30, 8, -7], [58, -14, 9], [-58, 6, 4], [-7, -7, 16], [9, 27, -9], [-12, 34, -9], [1, -15, -13], [0, 4, -28], [37, -21, -21], [-19, -7, 22], [-60, 8, -8], [3, -13, 0], [-12, 8, -8], [28, -18, 4], [-31, 15, -15], [8, -4, 0], [-27, 9, -5], [-4, -18, -15], [1, 11, -19], [54, -2, -5], [24, 0, 23], [-9, 25, 3], [60, -12, -3], [6, -34, 4], [-35, 15, 5], [0, -6, -12], [-29, -1, 1], [18, 18, 6], [11, -19, 23], [-5, -51, 17], [-47, 9, 0], [-61, 7, -6], [36, -8, -10], [26, -18, 8], [-18, -12, 19], [36, -18, 17], [-38, 0, 7], [-25, -3, -17], [41, -23, -23], [-8, -12, 17], [-2, 30, -22], [41, -13, 1], [0, -4, -29], [-37, 13, -11], [-5, 25, -25], [-6, 26, -24], [-12, 16, 20], [30, 2, 12], [41, 7, -3], [-68, -12, 4], [-45, 7, -2], [27, 29, -5], [20, -18, 4], [17, 45, -14], [28, 0, 2], [1, 35, -19], [-44, 10, 4], [24, -28, 20], [26, -18, 40], [16, 20, 3], [9, -33, 11], [-19, 17, -6], [-40, -10, 0], [-23, -25, 11], [3, 25, -11], [0, -22, -3], [-65, 3, 7], [-12, 28, -30], [-2, -26, 16], [7, -13, 5], [-32, -4, 4], [-31, -9, 1], [-49, -1, -8], [-5, -19, 13], [-7, 1, 8], [7, 43, -26], [-52, -8, -4], [-24, -28, 0], [-26, -34, 19], [22, -2, 5], [10, -8, 22], [-24, 28, 6], [10, -26, 15], [-21, -25, 2], [-10, -14, -7], [9, -33, -11], [60, -4, 4], [-14, 10, 26], [-4, -12, -14], [24, -26, -17], [-41, 17, 13], [4, 38, -22], [4, -32, -6], [-2, 18, -24], [-26, 0, -23], [0, 4, 6], [-12, -6, -26], [-25, -3, -13], [3, 27, -1], [-3, -37, 13], [-26, -14, 16], [-47, 11, -3], [-18, 14, 9], [-10, -14, 10], [28, 10, 2], [-35, 23, -15], [19, -1, -10], [-11, -7, -6], [-7, -19, -7], [-47, -1, 9], [4, 34, -21], [-7, -11, 3], [17, 13, 2], [-20, -4, -2], [24, -6, 17], [-14, -8, 0], [22, 26, -8], [-58, 10, -18], [5, 23, -15], [38, -8, -8], [-12, 8, -18], [-57, -19, 7], [13, -13, 19], [-13, 3, -12], [-9, 41, -1], [48, -14, 8], [6, 10, -22], [-11, -21, -11], [28, 16, 13], [-6, 8, 19], [5, -3, 4]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4864_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4864_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_4864_2_a_bh(:prec:=3)
    chi := MakeCharacter_4864_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_4864_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_4864_2_a_bh( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4864_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![8, -6, -2, 1]>,<5,R![4, 1, -4, 1]>,<7,R![-4, 1, 4, 1]>,<11,R![-4, 5, 6, 1]>],Snew);
    return Vf;
end function;