
// Make newform 4002.2.a.e in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4002_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_4002_2_a_e();".
// 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_4002_2_a_e();".
// 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_4002_a();"
function MakeCharacter_4002_a()
    N := 4002;
    order := 1;
    char_gens := [2669, 3133, 553];
    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_4002_a_Hecke(Kf)
    return MakeCharacter_4002_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], [-1], [0], [5], [3], [4], [4], [-1], [-1], [5], [-3], [9], [-6], [10], [6], [-7], [1], [-13], [-5], [-4], [-2], [-6], [-10], [18], [15], [13], [4], [-14], [6], [9], [0], [8], [-2], [-15], [-20], [14], [13], [13], [-6], [12], [-2], [-3], [-16], [18], [9], [-17], [-12], [14], [29], [12], [19], [-4], [17], [-14], [16], [1], [-13], [7], [6], [-29], [-24], [7], [6], [-10], [3], [-12], [13], [-20], [1], [-6], [-24], [14], [2], [6], [-10], [8], [38], [-23], [14], [6], [15], [18], [14], [-10], [18], [-27], [-28], [23], [20], [-23], [15], [-40], [-10], [-38], [16], [34], [-26], [-9], [6], [-32], [25], [11], [-20], [-11], [28], [24], [4], [48], [-2], [40], [-38], [-22], [26], [-37], [-24], [44], [23], [-43], [-20], [0], [26], [28], [9], [-42], [9], [8], [1], [2], [-23], [19], [-31], [-44], [-43], [-18], [5], [-12], [45], [-34], [-53], [4], [0], [-19], [3], [-4], [-41], [-8], [-38], [-28], [13], [-18], [26], [-36], [24], [54], [-15], [-27], [-30], [14], [-33], [24], [10], [8], [7], [-27], [17], [-8], [-38], [44], [-58], [16], [2], [54], [-2], [-20], [-51], [-36], [-6], [-2], [10], [7], [15], [13], [14], [20], [27], [-54], [14], [28], [0], [11], [-6], [64], [-34], [-7], [-50], [-47], [-56], [6], [-33], [-18], [26], [-52], [42], [-8], [-52], [26], [-51], [16], [-11], [50], [0], [-20], [-12], [60], [-26], [-38], [49], [-24], [-51], [-38], [43], [-41], [22], [-24], [-6], [36], [-58], [52], [-65], [-16], [60], [70], [18], [73], [-28], [11], [-12], [11], [16], [36], [0], [-10], [-66], [-30], [-48], [-28], [-26], [-25], [49], [58], [1], [32], [-25], [-66], [-25], [46], [-56], [-14], [55], [80], [60], [-55], [-5], [70], [-14], [14], [-8], [-68], [16], [-59], [-20], [66], [-34], [4], [32], [12], [78], [-43], [-12], [-35], [16], [12], [40], [48], [22], [14], [8], [-8], [10], [2], [-62], [36], [-60], [4], [-51], [-6], [-74], [81], [28], [-77], [53], [2], [-46], [-76], [70], [28], [-16], [-22], [-50], [-41], [82], [9], [26], [78], [-14], [-60], [65], [-24], [-81], [40], [-76], [-20], [64], [74], [-3], [-19], [-26], [59], [73], [64], [-46], [26], [-54], [10], [12], [24], [42], [-86], [7], [-28], [87], [-61], [19], [-18], [-24], [-36], [63], [85], [48], [50], [-41], [-27], [-91], [-34], [33], [-57], [-38], [-65], [-21], [52], [-34], [-2], [24], [31], [-94], [-74], [59], [-38], [10], [86], [-56], [-22], [31], [-91], [46], [-41], [59], [55], [-26], [-2], [61], [21], [-57], [-68], [25], [-32], [-82], [98], [9], [-57], [-70], [12], [83], [25], [8], [-32], [-96], [5], [10], [-13], [81], [-54], [51], [12], [62], [52], [-43], [-22], [-35], [-99], [-42], [44], [-28], [-49], [94], [-18], [-62], [44], [31], [1], [66], [60], [74], [65], [76], [-36], [-94], [-56], [60]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4002_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4002_2_a_e();".
// 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_4002_2_a_e(:prec:=1)
    chi := MakeCharacter_4002_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_4002_2_a_e();".
// 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_4002_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4002_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![1, 1]>,<7,R![0, 1]>,<11,R![-5, 1]>],Snew);
    return Vf;
end function;