
// Make newform 4002.2.a.d in Magma, downloaded from the LMFDB on 16 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_d();".
// 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_d();".
// 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], [-3], [-3], [2], [0], [3], [-1], [1], [-1], [8], [-5], [3], [1], [-11], [-6], [-5], [14], [12], [0], [-6], [10], [-8], [10], [-8], [-14], [-5], [-5], [16], [-11], [2], [-16], [-6], [6], [-13], [-19], [7], [-7], [-18], [-17], [0], [-20], [-17], [-12], [-11], [0], [3], [-16], [23], [5], [24], [-18], [21], [-8], [12], [1], [-6], [-20], [-14], [18], [26], [-6], [-28], [5], [31], [-20], [21], [-2], [5], [-14], [10], [-27], [-10], [12], [-4], [6], [-12], [6], [-10], [-20], [33], [-10], [-32], [-26], [19], [36], [3], [-17], [32], [24], [-16], [-24], [-13], [30], [-2], [27], [23], [-22], [34], [-1], [-22], [-19], [2], [-15], [30], [-20], [-3], [0], [28], [0], [-20], [-26], [25], [-31], [19], [29], [-12], [-34], [34], [-16], [8], [19], [-26], [-49], [-8], [-27], [34], [12], [-50], [-14], [16], [-33], [-18], [13], [8], [-18], [-34], [-20], [0], [30], [12], [-2], [54], [-28], [47], [-47], [5], [42], [41], [-8], [20], [14], [-4], [0], [-20], [8], [-11], [42], [11], [6], [-48], [36], [-48], [-10], [8], [0], [-5], [-11], [1], [32], [-36], [16], [-10], [-8], [39], [-16], [-40], [-32], [38], [30], [42], [24], [-2], [-36], [12], [-6], [-37], [35], [-24], [-2], [47], [-62], [28], [-56], [-48], [48], [22], [-14], [-6], [-4], [58], [-65], [1], [-44], [-16], [57], [46], [-15], [-36], [28], [-54], [-67], [6], [12], [-57], [70], [-37], [-18], [2], [8], [30], [20], [39], [-34], [16], [71], [51], [-48], [-66], [-36], [-22], [-38], [34], [51], [6], [-13], [-6], [24], [71], [39], [12], [-50], [40], [63], [-30], [60], [-64], [-12], [12], [4], [2], [12], [29], [-62], [46], [15], [5], [5], [-6], [-34], [-44], [-78], [14], [-46], [37], [-59], [36], [-17], [-5], [-27], [42], [-44], [-21], [14], [12], [14], [18], [62], [26], [-57], [-66], [-41], [10], [74], [25], [15], [44], [12], [-35], [-48], [18], [-4], [20], [-8], [54], [35], [-48], [-70], [-81], [57], [4], [-39], [-60], [12], [12], [57], [10], [40], [-54], [52], [-14], [46], [-86], [-54], [48], [-31], [-12], [2], [-25], [-1], [-39], [-45], [49], [-64], [-72], [-22], [-58], [-8], [-24], [1], [-53], [-29], [37], [38], [-36], [33], [24], [54], [-76], [-33], [-81], [20], [14], [19], [61], [-46], [31], [-69], [84], [68], [62], [-58], [8], [16], [-23], [-21], [52], [73], [38], [-6], [-2], [48], [-37], [36], [-31], [-46], [44], [63], [-62], [68], [-20], [-18], [92], [-93], [94], [-34], [49], [88], [-62], [-3], [56], [-68], [-16], [11], [-50], [-4], [-12], [-4], [67], [-48], [-82], [-11], [-34], [-29], [51], [-59], [99], [85], [-56], [-30], [4], [78], [54], [88], [53], [16], [72], [-51], [4], [-41], [-14], [-51], [57], [-98], [28], [8], [55], [-93], [-6], [-37], [1], [-6], [-25], [-22], [82], [14], [-15], [-39], [36], [-33]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4002_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![3, 1]>,<7,R![3, 1]>,<11,R![-2, 1]>],Snew);
    return Vf;
end function;