
// Make newform 4010.2.a.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4010_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_4010_2_a_e();".
// 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_4010_2_a_e();".
// 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_4010_a();"
function MakeCharacter_4010_a()
    N := 4010;
    order := 1;
    char_gens := [2407, 3211];
    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_4010_a_Hecke(Kf)
    return MakeCharacter_4010_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], [3], [-3], [4], [-7], [0], [-6], [10], [7], [8], [2], [4], [8], [14], [-10], [2], [3], [-3], [-6], [0], [14], [-10], [3], [2], [9], [-12], [10], [-6], [-12], [2], [13], [10], [-20], [12], [18], [-16], [8], [-11], [5], [2], [12], [14], [3], [5], [22], [-1], [13], [20], [-21], [-30], [-13], [2], [8], [-21], [-15], [-28], [-2], [22], [-21], [4], [-22], [12], [-16], [18], [-13], [18], [-7], [35], [-6], [0], [-2], [-31], [-25], [-21], [5], [13], [1], [30], [-5], [22], [17], [4], [-35], [4], [-20], [-2], [-13], [14], [-27], [10], [8], [12], [20], [-21], [5], [42], [-11], [-28], [18], [33], [-16], [-10], [-38], [-42], [-22], [39], [30], [17], [-27], [4], [-22], [25], [-8], [-8], [34], [18], [19], [0], [-38], [-6], [3], [-36], [17], [27], [10], [0], [-2], [-31], [-15], [-16], [-48], [13], [42], [-20], [-21], [-47], [-42], [-15], [12], [27], [-26], [-22], [45], [45], [-46], [28], [-25], [-56], [28], [-38], [4], [-12], [28], [52], [-5], [40], [38], [-58], [-22], [-36], [-52], [-8], [38], [-11], [7], [38], [-40], [4], [-20], [7], [-18], [49], [-10], [25], [52], [37], [-56], [5], [53], [-28], [-21], [-27], [-16], [-40], [-27], [-16], [10], [27], [14], [24], [-28], [-48], [8], [-56], [17], [9], [18], [-36], [-15], [-8], [-22], [10], [-45], [-32], [-15], [-46], [25], [-48], [18], [2], [-16], [33], [30], [22], [-32], [42], [3], [44], [-68], [-40], [45], [-51], [38], [50], [9], [55], [48], [2], [-41], [-55], [47], [42], [44], [-42], [50], [9], [30], [-8], [4], [67], [-16], [-10], [-46], [-30], [-2], [12], [5], [24], [-22], [-18], [38], [-25], [-11], [-20], [67], [28], [-32], [23], [-16], [38], [-35], [29], [-62], [-50], [70], [-78], [-11], [44], [-83], [-27], [-66], [55], [68], [19], [-42], [-35], [-23], [-68], [14], [32], [-22], [32], [-72], [72], [69], [-57], [-40], [65], [7], [48], [-31], [2], [59], [0], [17], [9], [-70], [-52], [-26], [18], [-20], [-6], [32], [-82], [28], [65], [45], [-16], [-21], [-80], [-18], [4], [-27], [-70], [15], [87], [29], [-60], [17], [-77], [77], [-16], [54], [47], [70], [-16], [38], [34], [37], [-47], [40], [84], [92], [-87], [75], [-1], [17], [68], [74], [18], [-65], [-48], [69], [-50], [-43], [53], [-78], [-47], [-73], [28], [32], [64], [10], [-26], [-10], [-3], [78], [-46], [-52], [7], [73], [20], [48], [74], [8], [-26], [32], [-3], [-50], [54], [-75], [42], [43], [5], [37], [-71], [-15], [-62], [42], [54], [-77], [-37], [-40], [39], [17], [68], [-96], [-52], [-70], [-6], [-60], [-62], [37], [34], [20], [-45], [-18], [-58], [-50], [9], [63], [13], [70], [2], [78], [52], [-6], [-30], [-1], [98], [-76], [12], [-42], [-13], [-90], [-32], [-12], [44], [10], [-57], [-42], [0], [-26], [-102], [-56], [65], [37], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4010_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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