
// Make newform 4002.2.a.j in Magma, downloaded from the LMFDB on 07 May 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_j();".
// 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_j();".
// 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], [-4], [1], [1], [-2], [2], [1], [-1], [-5], [-7], [-5], [10], [0], [-10], [3], [5], [5], [-9], [-10], [4], [6], [0], [-18], [3], [-11], [-18], [-10], [-14], [-1], [-10], [-18], [0], [11], [16], [22], [1], [-3], [-4], [4], [6], [3], [14], [6], [13], [-9], [2], [18], [-23], [-24], [-5], [-26], [-3], [-12], [-12], [29], [-27], [-3], [26], [21], [4], [7], [0], [-16], [3], [-16], [-9], [-36], [15], [20], [24], [-26], [24], [32], [16], [10], [14], [29], [12], [-26], [-17], [16], [18], [18], [12], [15], [-40], [-9], [-30], [33], [-3], [-18], [-12], [30], [40], [-8], [14], [9], [2], [4], [35], [7], [-26], [39], [34], [12], [-24], [-14], [-32], [-40], [22], [-18], [-4], [-33], [24], [-20], [15], [21], [20], [24], [-46], [16], [-49], [-2], [51], [-2], [21], [-2], [-39], [31], [-41], [14], [-35], [38], [-37], [36], [-13], [36], [-3], [2], [30], [31], [-9], [10], [53], [-18], [26], [4], [-43], [18], [-32], [-54], [4], [2], [39], [-35], [-28], [52], [-39], [-14], [2], [-12], [39], [-11], [-33], [20], [-12], [32], [10], [-26], [-14], [20], [10], [-48], [-7], [10], [2], [26], [-44], [9], [51], [19], [0], [-12], [37], [12], [2], [-24], [-60], [35], [-60], [-8], [22], [33], [54], [23], [4], [6], [-1], [8], [-8], [4], [14], [-44], [52], [-20], [-1], [-46], [17], [-58], [-62], [-28], [-12], [26], [-30], [-18], [15], [-12], [-55], [-22], [23], [1], [14], [-8], [8], [-12], [50], [-36], [-39], [-18], [64], [16], [30], [-73], [-68], [-27], [18], [-55], [40], [44], [16], [14], [-62], [-36], [24], [-22], [42], [-71], [-45], [-70], [-39], [24], [13], [-46], [45], [-34], [42], [-4], [7], [30], [20], [11], [59], [-66], [44], [-30], [0], [-68], [-46], [3], [-76], [-18], [28], [18], [-4], [-18], [58], [31], [-30], [25], [-80], [42], [32], [-48], [4], [-14], [6], [-12], [-6], [10], [32], [24], [20], [-62], [-69], [28], [-30], [-69], [44], [-79], [39], [-44], [-34], [50], [46], [-16], [22], [24], [-26], [39], [-8], [-31], [-78], [-20], [-20], [-4], [85], [-20], [41], [60], [28], [-66], [56], [-46], [-65], [-25], [28], [57], [-9], [38], [-82], [2], [10], [-68], [68], [-80], [-78], [88], [7], [-8], [15], [27], [15], [-6], [-2], [-28], [87], [-29], [12], [-30], [-17], [45], [-51], [-58], [23], [-35], [56], [-51], [-37], [74], [44], [50], [66], [67], [-68], [18], [-13], [6], [-50], [22], [-56], [-52], [-13], [47], [52], [-83], [-11], [27], [-34], [-40], [-11], [31], [-59], [4], [-15], [64], [-22], [24], [-21], [1], [-30], [-20], [59], [-5], [40], [60], [12], [51], [82], [-91], [59], [72], [55], [58], [-52], [-20], [39], [86], [11], [-19], [-78], [-92], [-60], [77], [-2], [2], [80], [-68], [57], [75], [34], [12], [-20], [-39], [-98], [12], [-54], [-24], [58]];
    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_j();".
// 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_j(: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_j();".
// 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_j( : 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![4, 1]>,<11,R![-1, 1]>],Snew);
    return Vf;
end function;