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