
// Make newform 2240.2.a.bf 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_bf();".
// 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_bf();".
// 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 := [-4, -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, 3], [-4, 0], [0, 0], [-2, -1], [0, 2], [-6, 2], [2, -4], [0, 0], [4, 1], [2, 2], [-4, -4], [-2, 4], [-8, 4], [8, -4], [2, -4], [-4, 1], [-4, 4], [-6, -4], [-2, 3], [14, 2], [-12, 1], [0, -6], [6, -7], [10, -6], [0, -8], [-20, 0], [-6, 0], [-4, 10], [-14, -4], [-12, 1], [6, 0], [8, -6], [20, 1], [-6, -7], [0, 0], [-10, -2], [4, -5], [-14, 0], [10, 0], [8, 4], [-20, -1], [-20, 1], [-8, 7], [14, -4], [2, -2], [-12, 9], [-14, 6], [-4, 6], [-6, 0], [0, 2], [6, -4], [-8, -2], [-6, 4], [14, -3], [16, 1], [-6, 5], [16, -3], [0, 4], [-18, -3], [-14, 12], [-8, -4], [2, -4], [0, 8], [14, 2], [-10, -9], [32, 0], [-4, -11], [-22, 8], [-8, -8], [16, -4], [-18, 5], [10, 5], [-2, 11], [18, 0], [4, 8], [-10, -3], [12, 7], [18, -4], [8, -10], [24, -8], [-10, 13], [10, 0], [14, -6], [0, 10], [0, -7], [8, -12], [8, 2], [-4, -9], [-12, 17], [20, -9], [-26, 8], [-22, 6], [-4, 8], [6, -1], [8, 2], [-6, -10], [-12, 16], [-22, 12], [-8, 4], [14, 1], [4, -8], [-2, -7], [20, -1], [26, -4], [4, 7], [-14, 4], [10, -12], [-4, -12], [-12, -1], [2, -4], [-32, -1], [32, -4], [-14, -10], [12, -19], [30, -2], [2, -8], [-6, -13], [16, -2], [-4, -4], [-18, 15], [-26, -1], [-24, -8], [-48, 0], [10, -5], [12, -15], [32, 0], [-44, -3], [26, -14], [10, -14], [10, 12], [18, 3], [24, 7], [10, 1], [6, 7], [-28, 10], [-26, 13], [-16, -2], [0, 14], [-2, 4], [-8, -8], [14, 4], [26, 4], [-28, -8], [32, -6], [18, -26], [-6, 6], [-16, -14], [0, 8], [24, 8], [-32, 16], [-4, 21], [2, 22], [-18, -9], [46, -4], [8, 6], [-6, -4], [-40, -6], [20, 6], [-6, 18], [-20, 19], [-8, -8], [-38, 5], [-2, 15], [-18, 20], [-32, 12], [6, 12], [4, 7], [-6, 20], [-24, -8], [2, 10], [36, -11], [-34, 5], [8, -24], [-34, -8], [-24, 10], [28, -10], [-38, 14], [34, 8], [-8, -2], [-10, 6], [-22, 2], [-24, 7], [-10, -5], [-16, 6], [-22, 8], [-8, 12], [-36, 7], [-10, 0], [8, 0], [6, 5], [-10, 25], [42, 4], [-42, -5], [28, 1], [10, -12], [16, -26], [-10, 20], [10, 22], [-4, 4], [-46, -3], [48, -10], [8, -22], [-22, -8], [-20, -18], [18, 12], [-10, 24], [-56, 2], [-24, -13], [-8, 10], [18, -20], [-24, 0], [-38, -8], [-8, 0], [10, 0], [42, -4], [40, -18], [-34, 15], [56, 2], [-24, 11], [-38, 16], [-2, -9], [-52, -5], [-52, 1], [40, -4], [-6, 16], [12, -10], [40, 8], [70, -1], [-8, -9], [28, -3], [-6, -16], [2, -6], [-36, 11], [-32, 0], [16, 8], [-20, -16], [12, 9], [26, 8], [-34, -3], [-40, 0], [40, -20], [36, 2], [-60, 3], [40, -12], [26, 2], [2, 20], [24, 8], [-14, 2], [-34, 16], [-12, 21], [26, 0], [-28, 0], [-14, 31], [46, 13], [16, 16], [8, -26], [-18, -6], [10, -29], [-30, -4], [-12, -8], [46, 1], [-38, 24], [-8, -4], [-22, -18], [-10, -8], [8, -2], [30, -13], [16, 16], [-22, 32], [12, -7], [16, -4], [26, -8], [-42, 15], [-12, -14], [28, -11], [28, 1], [-32, -16], [22, -32], [-24, 11], [28, -17], [-6, -22], [50, 6], [24, -12], [26, -6], [-34, -3], [48, -11], [-6, -20], [36, 14], [-6, -14], [-10, 12], [24, -6], [10, 19], [20, -36], [0, 27], [70, -5], [42, -8], [52, 7], [48, -16], [-12, 19], [-22, -22], [-16, 4], [54, 2], [-32, 0], [18, 2], [40, -16], [-26, 27], [-30, 0], [-24, 22], [24, 24], [-6, 8], [28, -2], [20, 15], [38, -1], [14, -29], [-20, 30], [10, 0], [-2, 12], [48, -18], [58, -20], [-46, 22], [4, -13], [12, -16], [-40, 6], [2, -12], [42, 8], [-22, 0], [0, 14], [40, 21], [-16, -28], [-40, 3], [-74, 1], [42, -4], [2, 6], [-12, -3], [42, -12], [18, 14], [38, 6], [4, -25], [18, 6], [40, 4], [-18, 16], [8, 18], [0, 0], [26, -7], [4, -12], [-38, 24], [-2, 13], [-12, -11], [-74, 9], [-62, -8], [-32, 6], [60, -4], [-6, 26], [-72, 6], [-38, 0], [34, 14], [-40, 30], [-28, 13], [12, -8], [-62, 10], [-6, -23], [-32, 28], [-10, -1], [4, -15], [-20, -2], [-40, 8], [-2, 3], [-40, -6], [34, -12], [-20, 16], [-4, 31], [-18, 21], [-6, 12], [-18, 13], [70, -8], [10, 8], [8, 28], [2, 0], [12, -2], [-4, -27], [52, -5], [-62, -9], [8, -8], [-4, -1], [-50, 11], [10, -35], [-36, -13], [68, -8], [-36, -13], [50, -14], [64, -6], [2, -24], [-44, -3], [10, -36], [14, 0], [74, 2], [8, -8], [22, 11], [22, -26], [32, 6], [34, -20], [54, -11], [36, 0], [-12, 0], [-10, 23], [-46, -20], [-48, -8], [64, -12], [-46, 26], [14, 10], [4, 3], [-64, 16], [-46, -20], [-36, -23], [-26, 5], [38, -20], [-32, -18], [20, -20], [46, 9], [-46, -3], [-16, -20], [-54, 24], [-84, 14], [64, -22]];
    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_bf();".
// 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_bf(: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_bf();".
// 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_bf( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-4, 1, 1]>,<11,R![8, 7, 1]>,<13,R![-2, -3, 1]>,<19,R![4, 1]>],Snew);
    return Vf;
end function;