
// Make newform 2240.2.a.bg 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_bg();".
// 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_bg();".
// 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
        poly := [-8, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [0, -1], [1, 0], [1, 0], [4, -1], [-2, 1], [2, 1], [0, 2], [0, -2], [2, -1], [8, 0], [2, 0], [2, -2], [-4, 2], [0, -3], [-6, 2], [8, 0], [-2, -2], [-4, 0], [-8, 0], [-6, 0], [-8, 3], [0, 4], [10, -2], [2, 5], [6, 0], [8, 1], [4, -2], [2, 1], [2, 0], [-8, 0], [0, 2], [10, -2], [0, 2], [-6, 4], [-8, 3], [6, -4], [-12, 2], [8, 1], [-2, -1], [-4, 8], [-2, 6], [8, 5], [2, -2], [-6, -6], [16, 0], [-12, 1], [8, 1], [8, -5], [-2, -6], [-6, -2], [-16, -1], [26, 0], [0, -2], [-14, 4], [-8, 6], [6, 0], [0, 4], [-6, 8], [2, -7], [-16, -3], [-18, 3], [24, -3], [8, -2], [10, 3], [-14, 0], [12, 0], [2, 4], [-20, 2], [-10, 4], [10, 5], [0, 0], [-8, 5], [26, -2], [12, 0], [8, -4], [-6, 5], [-34, -1], [-22, 7], [2, -4], [-24, -4], [2, -7], [-16, 1], [10, -8], [-8, -6], [-12, 2], [10, -3], [10, 2], [6, 2], [16, 4], [-8, -7], [-8, 6], [-16, 6], [28, -3], [20, -5], [16, -1], [22, -8], [-30, 0], [-8, 12], [10, -9], [-12, 8], [18, 6], [8, -4], [-6, 8], [-20, 0], [-6, 7], [-16, 8], [-22, -1], [16, -7], [26, 4], [-8, 7], [18, 4], [-6, 0], [-8, 2], [0, 1], [2, 0], [16, -1], [16, 0], [-6, 0], [-4, 3], [-10, 2], [2, -10], [6, 9], [36, 0], [0, 4], [-14, -5], [10, 7], [-8, -2], [-32, 0], [22, 5], [-4, 7], [16, 4], [-32, -5], [18, 6], [-14, -8], [-6, -4], [-2, 1], [-24, 3], [-18, -1], [18, -1], [8, -2], [18, -3], [8, 0], [20, -2], [-2, -6], [16, 2], [30, -4], [18, 0], [16, -12], [-16, 4], [26, 8], [-30, -2], [-20, -4], [32, 8], [-12, -10], [8, -16], [-16, -1], [-22, -6], [2, -11], [-18, 6], [-28, 0], [42, 2], [-16, -6], [24, -4], [34, -8], [8, -1], [0, -16], [14, 3], [-38, 3], [-34, 0], [4, 4], [38, 0], [0, -19], [10, 8], [24, 6], [-6, -6], [-4, -13], [42, -3], [-8, -4], [14, -10], [-24, -8], [8, 0], [50, -4], [-22, -4], [-8, -8], [46, -4], [18, -2], [-24, -1], [-14, 15], [-40, -4], [34, 0], [-12, -6], [20, 5], [-10, -2], [28, -8], [2, -1], [42, -3], [42, -2], [26, 9], [-32, 9], [2, -16], [24, 2], [38, 0], [34, -12], [24, -10], [-26, -5], [-24, 18], [-12, 4], [26, -4], [-48, -4], [-62, -2], [-10, -16], [-48, 8], [0, -13], [-24, -14], [-14, -10], [32, 6], [-6, 22], [8, -2], [-54, 0], [18, -4], [24, 2], [26, 11], [56, -6], [-40, 3], [34, -16], [-14, -11], [-16, 1], [-48, 1], [12, -24], [42, -10], [8, 2], [16, -8], [2, -9], [0, 11], [-24, -15], [-6, 18], [-6, -18], [-20, 5], [-24, 6], [-44, 4], [-40, -8], [16, 5], [34, 8], [-6, -1], [16, 10], [0, -16], [0, -18], [-60, 1], [-16, 10], [-30, -8], [-14, -8], [-8, 4], [26, -10], [30, 4], [20, 11], [-62, 0], [0, 4], [14, 17], [10, -9], [8, 18], [36, 10], [22, 14], [-10, -3], [-6, 0], [8, -14], [10, 13], [-6, -8], [4, -8], [-14, 0], [54, -10], [-44, -4], [34, 5], [8, -4], [2, -20], [16, -11], [-52, 8], [2, -4], [-14, -9], [-32, 14], [8, 17], [0, 11], [-64, 0], [-10, 20], [0, 19], [-16, -7], [50, 8], [26, -16], [-32, -4], [66, 2], [10, -3], [-24, -7], [-6, -6], [24, 0], [-30, -16], [6, 6], [64, -6], [-10, -7], [16, -4], [0, 7], [-22, -15], [10, -8], [-24, -11], [-28, -4], [20, -11], [-14, 20], [28, -22], [-66, 2], [16, 16], [-6, 6], [-16, 0], [26, 3], [18, -20], [4, 6], [40, -14], [26, -10], [8, -18], [24, 9], [-6, 1], [10, 11], [8, -6], [26, -6], [14, 8], [-32, 0], [-38, 4], [10, -22], [-4, -3], [24, -4], [8, -28], [18, -10], [-46, 8], [-6, 14], [-40, 22], [48, -3], [20, -16], [0, 7], [-46, 9], [2, -16], [-38, 16], [48, -19], [26, -20], [10, -2], [-2, -20], [-48, 5], [10, -4], [36, 8], [-34, 12], [-4, -12], [32, -10], [30, -1], [-72, 0], [26, 18], [10, -11], [48, -11], [-46, -3], [-6, 12], [24, -10], [8, 14], [18, -12], [0, 26], [-22, 2], [26, 14], [-24, -4], [-52, 3], [-16, 12], [26, -28], [-2, -17], [-48, -2], [-62, 11], [-12, -13], [-16, 28], [0, 8], [82, -5], [-56, -2], [66, 4], [16, 16], [16, 13], [-22, 31], [-6, -4], [10, 11], [22, 12], [-6, -12], [-56, -4], [2, 12], [72, 4], [0, -15], [40, 9], [30, 1], [-28, -16], [-48, 3], [10, -25], [30, -21], [60, -7], [72, -12], [8, -7], [26, 6], [8, 16], [50, -14], [-52, 15], [-46, -4], [-34, -6], [18, -14], [0, -10], [-38, 13], [-2, 22], [80, -6], [2, 20], [10, -3], [-24, 12], [-24, 12], [10, 5], [-14, 16], [-4, -14], [12, 16], [-22, 24], [-58, 2], [-104, 1], [16, 12], [-22, 8], [0, -15], [18, 25], [-74, 4], [16, 10], [-32, 4], [58, -21], [-58, -9], [-12, 20], [26, -8], [-32, 20], [16, -20]];
    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_bg();".
// 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_bg(:prec:=2)
    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_bg();".
// 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_bg( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-8, 1, 1]>,<11,R![4, -7, 1]>,<13,R![-6, 3, 1]>,<19,R![-32, -2, 1]>],Snew);
    return Vf;
end function;