
// Make newform 4864.2.a.f in Magma, downloaded from the LMFDB on 25 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_f();".
// 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_f();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [-1], [2], [1], [-4], [1], [-5], [-1], [9], [5], [-2], [10], [12], [-10], [-12], [9], [-9], [-2], [-13], [-10], [-7], [-10], [8], [-6], [-6], [-4], [8], [-11], [15], [-6], [-6], [14], [-13], [2], [8], [8], [2], [-10], [-24], [-14], [12], [2], [-23], [-4], [-12], [5], [-13], [16], [-1], [14], [-10], [-19], [-16], [6], [-6], [-16], [-6], [-3], [6], [-20], [24], [-21], [-12], [33], [21], [31], [1], [-2], [22], [26], [-35], [3], [16], [-13], [-17], [8], [12], [-26], [36], [30], [-34], [-15], [18], [8], [-20], [20], [-20], [17], [-24], [40], [6], [-8], [12], [-20], [-10], [-11], [-6], [-32], [17], [4], [-4], [22], [-20], [0], [20], [3], [-48], [-42], [-28], [-34], [10], [-12], [2], [10], [8], [-24], [-34], [37], [46], [-19], [15], [-20], [15], [4], [-30], [20], [-10], [-3], [29], [-6], [20], [50], [20], [-2], [27], [1], [-9], [11], [5], [5], [9], [50], [3], [19], [7], [22], [-40], [12], [16], [-30], [-41], [10], [26], [18], [-51], [-30], [43], [13], [37], [-3], [-38], [-8], [32], [12], [-18], [-2], [10], [-50], [6], [54], [-15], [5], [-40], [-37], [32], [-18], [14], [-36], [-14], [16], [-9], [-43], [38], [30], [17], [0], [63], [4], [6], [17], [40], [-24], [-4], [-21], [-66], [18], [-39], [16], [-61], [15], [62], [-18], [13], [28], [-12], [36], [-39], [27], [-3], [-25], [13], [-62], [44], [-47], [66], [8], [21], [36], [-34], [24], [-46], [52], [18], [-19], [33], [20], [-54], [-48], [30], [6], [22], [-25], [36], [14], [42], [-25], [-2], [36], [0], [-56], [-44], [54], [-25], [65], [56], [56], [48], [-36], [-40], [-23], [-64], [-65], [-43], [-72], [-44], [44], [-58], [52], [-18], [22], [70], [36], [-56], [-19], [-18], [7], [-29], [27], [-19], [0], [49], [21], [21], [-33], [38], [11], [-24], [-83], [-50], [6], [-6], [-3], [-43], [49], [42], [56], [-7], [-34], [11], [64], [-66], [56], [-4], [0], [14], [42], [-42], [-18], [-19], [50], [14], [39], [1], [63], [80], [-34], [15], [23], [-32], [34], [-8], [-54], [-34], [-4], [-24], [0], [18], [-48], [-73], [-78], [-23], [-73], [-3], [64], [-29], [-22], [71], [17], [-12], [12], [-74], [-81], [9], [66], [0], [42], [35], [66], [19], [7], [75], [11], [21], [-42], [57], [8], [30], [-3], [-44], [36], [4], [4], [-64], [-26], [86], [72], [5], [54], [-21], [8], [48], [18], [24], [27], [-34], [-44], [-18], [2], [-8], [32], [53], [-23], [-75], [-80], [-20], [57], [20], [-66], [45], [-8], [90], [90], [-25], [-62], [-1], [-68], [47], [-90], [84], [-44], [16], [63], [-102], [34], [-72], [4], [-80], [-46], [-20], [64], [38], [-54], [42], [-60], [-38], [-71], [-20], [-36], [33], [-42], [26], [46], [-45], [34], [-63], [26], [-60], [82], [-9], [-12], [-22], [7], [27], [-87], [-2], [-37], [-61], [-45], [40], [-63]];
    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_f();".
// 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_f(:prec:=1)
    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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4864_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![-2, 1]>,<7,R![-1, 1]>,<11,R![4, 1]>],Snew);
    return Vf;
end function;