
// Make newform 2040.2.a.q in Magma, downloaded from the LMFDB on 12 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2040_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_2040_2_a_q();".
// 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_2040_2_a_q();".
// 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_2040_a();"
function MakeCharacter_2040_a()
    N := 2040;
    order := 1;
    char_gens := [511, 1021, 1361, 817, 241];
    v := [1, 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_2040_a_Hecke(Kf)
    return MakeCharacter_2040_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]];
    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], [1], [1], [3], [5], [0], [-1], [-1], [0], [1], [4], [-5], [-3], [0], [-7], [9], [-6], [-4], [-14], [8], [-3], [-2], [0], [-2], [14], [6], [-12], [8], [6], [12], [4], [4], [-1], [-2], [-4], [-17], [-12], [1], [-2], [14], [-6], [20], [18], [-14], [20], [6], [0], [-10], [-28], [-13], [-4], [-12], [4], [-2], [-2], [-21], [15], [12], [10], [20], [11], [-11], [-16], [-7], [-23], [32], [-25], [-19], [-18], [-13], [11], [6], [-32], [-8], [4], [-33], [12], [-13], [31], [-31], [-33], [17], [23], [6], [40], [-4], [-18], [-22], [38], [14], [-13], [40], [16], [30], [-14], [-34], [16], [-33], [26], [0], [-33], [6], [-11], [-24], [12], [-40], [-37], [-21], [-10], [8], [-29], [-20], [32], [-10], [-13], [-9], [31], [-12], [-34], [34], [27], [10], [-34], [28], [-32], [30], [14], [27], [22], [28], [-4], [-34], [-4], [4], [-16], [-21], [-33], [7], [7], [-42], [26], [2], [45], [-6], [37], [-29], [14], [32], [49], [-1], [31], [5], [34], [24], [7], [-39], [11], [21], [0], [10], [36], [31], [30], [42], [-62], [44], [-20], [33], [34], [-24], [40], [34], [24], [8], [24], [14], [22], [-26], [20], [10], [-40], [41], [61], [39], [16], [24], [-5], [-52], [-4], [-55], [-53], [-12], [33], [28], [-30], [-32], [32], [47], [2], [-24], [25], [-40], [34], [-21], [24], [41], [-32], [-60], [33], [49], [19], [18], [35], [37], [-39], [-6], [-40], [8], [-66], [6], [26], [28], [10], [-44], [-31], [21], [54], [-48], [16], [21], [-40], [14], [59], [46], [-46], [0], [-52], [-70], [-19], [-68], [-16], [19], [-46], [2], [-52], [-3], [55], [27], [-5], [17], [6], [-33], [-51], [34], [-61], [-72], [2], [29], [52], [64], [16], [-33], [-14], [-39], [66], [28], [-6], [12], [44], [-33], [-60], [-32], [40], [61], [-38], [44], [33], [83], [-65], [6], [0], [-40], [6], [-70], [52], [-44], [45], [48], [45], [-46], [11], [28], [75], [-63], [-49], [69], [-65], [-21], [63], [-2], [16], [27], [-36], [-12], [-86], [79], [17], [20], [-6], [-34], [0], [-23], [-14], [-50], [68], [81], [-72], [-37], [70], [88], [-30], [-54], [-38], [26], [24], [13], [-82], [7], [26], [12], [20], [42], [-10], [3], [-86], [-84], [-7], [30], [42], [-18], [80], [10], [-87], [-72], [78], [23], [14], [-90], [27], [10], [-60], [65], [4], [-49], [70], [17], [-66], [-3], [8], [61], [-38], [-54], [39], [-20], [74], [48], [34], [46], [30], [52], [2], [-34], [38], [-44], [-82], [12], [81], [-34], [54], [80], [-50], [-41], [59], [-2], [-70], [-14], [-13], [-40], [20], [-53], [80], [-50], [50], [10], [56], [94], [-5], [78], [-18], [96], [-44], [-25], [-74], [-90], [60], [2], [10], [84], [-22], [68], [-29], [-11], [11], [-38], [-72], [-7], [-36], [73], [-2], [40], [-38], [27], [76], [66], [37], [-78], [34], [-29], [-4], [8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2040_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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