
// Make newform 2240.4.a.q in Magma, downloaded from the LMFDB on 05 January 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_2240_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_2240_4_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_2240_4_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_2240_a();"
function MakeCharacter_2240_a()
    N := 2240;
    order := 1;
    char_gens := [1471, 1541, 897, 1921];
    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_2240_a_Hecke(Kf)
    return MakeCharacter_2240_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 := 4;
    raw_aps := [[0], [-1], [-5], [-7], [-39], [17], [-15], [74], [14], [237], [180], [318], [-348], [-22], [193], [208], [452], [-340], [-408], [-528], [-554], [-539], [164], [-576], [-827], [1114], [-1643], [710], [-1321], [342], [1348], [238], [-776], [326], [-3282], [-2627], [-258], [134], [4181], [-2901], [4404], [-1752], [3595], [3448], [3576], [-2868], [1359], [5693], [-6381], [-3556], [872], [2129], [778], [-4990], [-3630], [290], [1854], [1836], [-6154], [-437], [-347], [-1281], [-3611], [8206], [4911], [-606], [5308], [-10638], [-2194], [-938], [-4995], [752], [-1175], [-9520], [11620], [1708], [-125], [-13077], [2161], [-9614], [8808], [13207], [-13201], [-11890], [-4474], [-14302], [-9801], [4968], [9408], [1692], [-4199], [-398], [-13786], [1891], [-3171], [21803], [19174], [-3210], [-3220], [-5443], [14988], [3024], [-8964], [-14022], [-20020], [-18349], [-5616], [8243], [-4385], [14378], [7235], [25350], [-4634], [12446], [-7145], [-20846], [1751], [-15016], [-7902], [-10019], [11456], [-30912], [-27839], [-19996], [4356], [-19327], [6917], [27106], [-18024], [-5347], [3833], [-32120], [-18859], [-26776], [14722], [41042], [-17735], [43979], [-5301], [6737], [-4366], [36963], [30924], [-1806], [-12780], [-20706], [20494], [36246], [-39596], [16224], [-11022], [33600], [43240], [-3024], [-40694], [-28728], [-9239], [30192], [-36515], [46356], [-23800], [44752], [8870], [-35616], [-21894], [-56037], [-13168], [24911], [-16219], [44042], [-39000], [19010], [-19101], [-46970], [-1786], [59116], [-21603], [29043], [6972], [28924], [504], [20688], [64366], [-38358], [-22032], [-65510], [60692], [-60753], [-58023], [42420], [-44786], [59562], [45979], [33816], [-16764], [13171], [8055], [628], [-68927], [-68595], [-73502], [67754], [-34366], [30242], [62046], [-8953], [-51122], [85280], [66870], [50668], [-78716], [78918], [-26612], [-75269], [33530], [81448], [-56194], [29940], [10058], [-67730], [-5946], [-86190], [-9915], [-50618], [-37301], [30810], [-62067], [-52569], [-19707], [39180], [-72488], [-43030], [9680], [24537], [-60293], [56069], [5760], [-42480], [-53325], [15498], [-81936], [32584], [60473], [-34486], [-107941], [7194], [87984], [60118], [-35545], [79662], [-86966], [33790], [-31160], [8472], [55566], [-55635], [3490], [-108500], [-42087], [-24653], [111238], [3366], [-6728], [99421], [-43306], [-99482], [-63245], [-108450], [25768], [-68758], [-63656], [59084], [-101771], [28284], [-25534], [-87007], [49284], [-123930], [10361], [-69658], [28805], [-153355], [48816], [-14454], [66995], [423], [-36646], [-94278], [48800], [-65200], [26211], [78649], [56952], [-105716], [-60806], [-25688], [6678], [48561], [-138364], [169199], [-91423], [74158], [-100461], [118280], [172563], [1514], [-79086], [-129544], [-29664], [-37356], [-103128], [108281], [-13426], [-181226], [107022], [-18104], [125886], [-162417], [-137735], [-129051], [-5446], [86384], [131854], [81404], [-71426], [177568], [165275], [-1020], [-145000], [-31748], [21706], [-23156], [-152646], [-35563], [-112876], [-155193], [46135], [-74338], [158950], [114305], [-101086], [116340], [69098], [63835], [-88950], [157268], [11922], [-18952], [1522], [87451], [-215364], [-204916], [40355], [192809], [-51981], [47754], [89414], [89306], [-117162], [-203838], [11332], [-174388], [24168], [-222603], [61772], [55798], [12443], [190378], [57165], [-127867], [199180], [218628], [-151951], [-143458], [-149658], [119864], [72371], [192531], [-101722], [-126561], [177870], [8530], [202544], [-67294], [86032], [126189], [130887], [-30655], [-92508], [73887], [-36095], [145047], [-37369], [-264028], [-203625], [200144], [-82912], [140032], [170129], [197910], [-194624], [229800], [-172066], [7813], [-67284], [239178], [152866], [277079], [106788], [-11316], [-199315], [111026], [176918], [21020], [-161162], [-2520], [-96161], [1780], [233238], [-50211], [93039], [54014], [-172514], [-41108], [123743], [205227], [-161092], [168762], [-99232], [10828]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2240_4_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_2240_4_a_q(:prec:=1)
    chi := MakeCharacter_2240_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_2240_4_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_2240_4_a_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![1, 1]>,<11,R![39, 1]>],Snew);
    return Vf;
end function;