
// Make newform 6040.2.a.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6040_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_6040_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_6040_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_6040_a();"
function MakeCharacter_6040_a()
    N := 6040;
    order := 1;
    char_gens := [1511, 3021, 2417, 761];
    v := [1, 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_6040_a_Hecke(Kf)
    return MakeCharacter_6040_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 := [[0], [0], [-1], [-1], [5], [-1], [0], [-4], [3], [5], [-5], [2], [8], [-6], [0], [10], [7], [-14], [-5], [6], [-5], [-14], [-9], [12], [-6], [10], [6], [-11], [-20], [-1], [16], [-6], [-20], [5], [-16], [1], [-10], [9], [-6], [-6], [-4], [8], [3], [-16], [-21], [16], [16], [14], [-18], [-1], [-29], [-20], [-23], [15], [18], [-17], [2], [-16], [-10], [18], [-24], [17], [16], [-5], [-34], [-22], [-4], [-22], [-24], [2], [11], [-20], [32], [3], [-26], [20], [30], [-18], [-11], [-14], [14], [32], [-4], [11], [9], [-25], [8], [4], [3], [-4], [-4], [36], [-32], [-11], [-4], [28], [22], [21], [-27], [-2], [-2], [31], [24], [-3], [11], [34], [23], [-30], [0], [10], [-31], [-34], [35], [-8], [-36], [-43], [28], [-12], [-40], [12], [6], [-46], [-1], [-12], [30], [-3], [-35], [6], [-48], [-30], [42], [14], [26], [-8], [-36], [-24], [4], [24], [36], [-42], [-50], [48], [42], [-30], [-3], [45], [36], [18], [18], [-23], [7], [-12], [-10], [0], [-10], [41], [-12], [4], [2], [26], [-15], [54], [3], [32], [-27], [31], [23], [16], [-2], [23], [48], [0], [43], [-32], [14], [6], [17], [-3], [-24], [-20], [-64], [56], [52], [-50], [0], [-46], [-21], [35], [59], [56], [-2], [-40], [60], [-15], [9], [-20], [19], [26], [-56], [19], [-1], [-20], [22], [-44], [10], [-14], [22], [-9], [17], [52], [42], [-10], [0], [-2], [58], [22], [-32], [66], [-44], [18], [6], [-29], [-3], [-52], [24], [50], [0], [-24], [38], [36], [-36], [-71], [-42], [30], [44], [50], [-60], [-27], [10], [-39], [25], [-53], [-21], [17], [-76], [39], [-16], [20], [48], [-65], [55], [-5], [-36], [6], [-70], [4], [-56], [-73], [-8], [-38], [-26], [75], [45], [58], [-6], [49], [46], [18], [34], [0], [51], [10], [31], [24], [-50], [69], [-39], [33], [-24], [42], [-10], [-1], [72], [29], [2], [15], [-55], [-21], [-70], [-60], [26], [-80], [9], [76], [78], [30], [39], [54], [-15], [-68], [-69], [44], [29], [52], [-40], [71], [-72], [-8], [64], [-74], [-28], [-26], [-59], [12], [54], [24], [-36], [83], [-61], [-84], [35], [-17], [-72], [70], [-10], [67], [34], [49], [-24], [78], [-60], [-26], [31], [20], [25], [-62], [-45], [-30], [13], [20], [-53], [88], [-49], [-8], [34], [-68], [-17], [7], [-3], [45], [-72], [11], [-21], [-60], [5], [-34], [84], [-6], [60], [-75], [44], [-97], [72], [45], [-67], [-82], [67], [16], [6], [-78], [-89], [-92], [-6], [-48], [-39], [22], [-14], [-11], [-5], [48], [-40], [74], [12], [6], [58], [-59], [-76], [8], [-22], [-56], [28], [-97], [14], [-60], [-63], [-31], [-12], [9], [-82], [-78], [-47], [59], [82], [-44], [-88], [-50], [14], [66], [-88], [18], [-74], [-3], [24], [-86], [-60], [92], [-77], [-74], [-67], [98], [-9], [33], [6], [46], [-58], [-16], [-36], [-58], [20], [-16]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6040_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6040_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_6040_2_a_e(:prec:=1)
    chi := MakeCharacter_6040_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_6040_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_6040_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6040_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<7,R![1, 1]>,<11,R![-5, 1]>],Snew);
    return Vf;
end function;