
// Make newform 4006.2.a.a in Magma, downloaded from the LMFDB on 30 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4006_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_4006_2_a_a();".
// 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_4006_2_a_a();".
// 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_4006_a();"
function MakeCharacter_4006_a()
    N := 4006;
    order := 1;
    char_gens := [5];
    v := [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_4006_a_Hecke(Kf)
    return MakeCharacter_4006_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], [0], [-3], [-2], [0], [-4], [-4], [-2], [2], [-5], [-2], [-7], [-12], [-4], [0], [-12], [8], [-1], [-15], [12], [3], [-5], [15], [-13], [-8], [6], [-16], [0], [-9], [5], [14], [13], [-12], [-5], [10], [16], [13], [11], [7], [-20], [0], [16], [-8], [-10], [6], [10], [-4], [0], [2], [-8], [-18], [-18], [-18], [4], [-12], [12], [-2], [-20], [-12], [5], [-22], [17], [20], [6], [-4], [-32], [1], [8], [-19], [-6], [-4], [5], [-32], [-24], [-6], [15], [24], [-22], [-1], [4], [12], [34], [40], [19], [18], [-12], [-33], [-13], [-25], [9], [30], [11], [4], [-37], [10], [1], [3], [33], [12], [-20], [-32], [30], [-37], [28], [-20], [-39], [-16], [-18], [10], [22], [-44], [-46], [21], [10], [-7], [-9], [31], [-40], [6], [4], [-2], [17], [-35], [4], [20], [18], [25], [-16], [-8], [-38], [-25], [-30], [-5], [-47], [30], [-35], [-26], [33], [34], [-44], [-39], [18], [-19], [-42], [48], [-32], [-10], [38], [-43], [39], [9], [2], [-4], [53], [-33], [-19], [-43], [35], [-44], [-10], [-15], [-32], [-20], [39], [-47], [0], [-6], [48], [-35], [-36], [15], [-14], [-32], [-11], [-46], [13], [-18], [43], [-61], [8], [56], [15], [-16], [-60], [32], [6], [-2], [37], [-32], [40], [38], [-10], [17], [27], [-6], [30], [-50], [11], [42], [-51], [-38], [37], [34], [4], [15], [-4], [62], [-54], [-33], [30], [-4], [-28], [-61], [-33], [-6], [-34], [2], [-13], [-30], [30], [-21], [-16], [-18], [18], [-71], [-68], [34], [-24], [61], [11], [-19], [12], [28], [12], [60], [-45], [-12], [-24], [-24], [-61], [16], [-20], [58], [50], [15], [-51], [-47], [24], [-6], [62], [40], [6], [23], [-35], [1], [2], [10], [28], [33], [-38], [58], [-12], [52], [-52], [-30], [59], [34], [-22], [36], [-23], [-2], [20], [54], [23], [25], [-42], [-42], [80], [17], [67], [0], [25], [8], [73], [25], [5], [-16], [-22], [79], [13], [7], [-86], [48], [18], [22], [14], [-38], [-11], [-6], [-55], [-56], [-30], [-66], [1], [-44], [-19], [-15], [5], [3], [66], [-22], [74], [-82], [84], [24], [13], [-23], [24], [56], [-30], [4], [-42], [36], [61], [66], [-52], [-51], [-52], [44], [-53], [70], [-11], [43], [-76], [12], [-88], [10], [39], [-59], [-84], [-37], [-12], [22], [40], [66], [-60], [38], [10], [16], [-57], [47], [8], [84], [-70], [90], [70], [6], [40], [-53], [-27], [-26], [-60], [20], [84], [95], [-98], [83], [88], [-36], [5], [-8], [-98], [-44], [-52], [38], [12], [83], [-89], [8], [-34], [26], [-16], [-83], [-45], [88], [19], [-72], [-4], [81], [27], [-6], [-43], [34], [37], [-3], [60], [-47], [60], [38], [-80], [-33], [-70], [41], [-45], [22], [80], [28], [-35], [-4], [-24], [-10], [-34], [75], [100], [34], [18], [-57], [-44], [45], [-39], [-12], [-90], [-77], [13], [50], [65], [67], [-78], [-4], [84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4006_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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