
// Make newform 2240.2.a.x in Magma, downloaded from the LMFDB on 25 April 2024.
// 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_2_a_x();".
// 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_2_a_x();".
// 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+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], [1], [1], [1], [-1], [1], [1], [0], [4], [1], [10], [8], [2], [4], [11], [-4], [-8], [-10], [4], [12], [6], [7], [8], [-6], [1], [-12], [-5], [-10], [-13], [0], [-8], [0], [-2], [2], [6], [7], [6], [22], [-5], [-21], [0], [12], [21], [-10], [6], [-4], [17], [-21], [-7], [-10], [-24], [15], [20], [-10], [2], [-26], [-6], [22], [-26], [-9], [7], [7], [-5], [0], [19], [22], [12], [30], [-36], [28], [21], [-32], [-17], [6], [-32], [-20], [-9], [-17], [-11], [10], [-12], [15], [25], [-2], [-6], [-12], [15], [-26], [32], [18], [-33], [24], [-18], [-31], [-41], [13], [-2], [44], [-20], [9], [34], [-24], [-20], [-42], [-4], [-41], [-36], [-33], [9], [-10], [-3], [-26], [22], [20], [49], [-34], [17], [-36], [-4], [-21], [-36], [-18], [21], [-42], [32], [33], [1], [12], [8], [-11], [-17], [8], [3], [-4], [0], [54], [21], [17], [-29], [-23], [34], [-49], [46], [-6], [30], [-44], [18], [-18], [52], [18], [44], [40], [2], [8], [44], [-40], [-5], [-24], [-19], [-50], [-10], [26], [-34], [-6], [-40], [9], [-32], [55], [1], [10], [36], [42], [-15], [6], [44], [20], [59], [39], [56], [2], [-46], [42], [-28], [-38], [-42], [48], [-52], [-15], [-35], [-42], [2], [-24], [25], [2], [32], [3], [-21], [-38], [33], [-13], [30], [-14], [50], [-28], [60], [47], [50], [-30], [18], [10], [58], [-2], [58], [45], [-34], [34], [-4], [30], [12], [-22], [-50], [50], [33], [54], [5], [-46], [-67], [5], [3], [20], [-18], [-66], [-8], [-55], [-7], [47], [-66], [-36], [37], [-52], [-24], [-36], [51], [-14], [35], [28], [-28], [-54], [-3], [-24], [-40], [-2], [-8], [68], [-58], [-29], [50], [44], [-79], [27], [-36], [2], [56], [53], [-74], [-64], [-29], [-30], [-36], [60], [-6], [-26], [-71], [-32], [-14], [67], [-12], [-46], [49], [26], [-65], [23], [32], [14], [5], [-63], [-16], [28], [4], [60], [71], [-45], [-54], [-70], [8], [-10], [-50], [-51], [56], [37], [25], [66], [65], [64], [-19], [-8], [40], [32], [-40], [-60], [-48], [45], [34], [-22], [28], [-34], [-50], [-63], [53], [13], [-42], [54], [-62], [-50], [6], [-8], [-59], [76], [-26], [-62], [2], [-18], [-78], [91], [-28], [37], [-29], [6], [-44], [-73], [46], [72], [16], [-15], [52], [84], [-58], [90], [-36], [67], [-56], [14], [15], [-81], [-5], [18], [42], [-12], [24], [-6], [-10], [24], [30], [75], [28], [-32], [-77], [-16], [-47], [-89], [-38], [-64], [-35], [-42], [86], [44], [-55], [-29], [-74], [-69], [-74], [-22], [92], [-86], [-86], [-57], [-11], [1], [-56], [-43], [-91], [23], [-3], [52], [21], [52], [34], [30], [43], [70], [18], [36], [-12], [17], [-28], [34], [-50], [63], [44], [92], [-27], [22], [-4], [-4], [-40], [0], [93], [24], [-90], [-21], [103], [34], [90], [72], [-57], [7], [28], [10], [-54], [90]];
    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_2_a_x();".
// 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_2_a_x(:prec:=1)
    chi := MakeCharacter_2240_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_2240_2_a_x();".
// 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_2_a_x( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, 1]>,<11,R![1, 1]>,<13,R![-1, 1]>,<19,R![0, 1]>],Snew);
    return Vf;
end function;