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


// To make the newform (type ModFrm), type "MakeNewformModFrm_4009_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_4009_2_a_a(:prec:=1)
    chi := MakeCharacter_4009_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_4009_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_4009_2_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4009_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>],Snew);
    return Vf;
end function;