
// Make newform 2240.2.a.bc in Magma, downloaded from the LMFDB on 23 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_bc();".
// 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_bc();".
// 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], [0, -1], [-2, 1], [2, 1], [0, 0], [0, 0], [-2, 3], [0, 2], [-2, -2], [2, 0], [-4, 0], [-4, -3], [-10, 2], [0, 4], [2, 0], [-4, 4], [-4, 0], [6, 4], [0, -5], [-4, 4], [2, 4], [2, -7], [2, -6], [-12, -3], [-12, 2], [-2, 5], [10, -6], [0, -4], [8, -4], [-6, -4], [-16, 2], [2, -4], [-8, 3], [10, -8], [-12, -2], [-4, 5], [-10, -1], [4, 4], [18, -2], [24, 1], [-6, -4], [6, -12], [-16, 0], [8, 1], [4, 5], [16, -1], [10, -4], [10, 6], [8, -5], [-14, -2], [8, 6], [-10, -8], [-8, 6], [2, 8], [8, -6], [-2, -4], [-2, -3], [8, 1], [-10, 3], [-16, 5], [16, -8], [-6, 7], [-10, 0], [-12, 8], [-6, 16], [-12, -4], [10, -6], [26, -3], [12, -12], [-20, 9], [6, 8], [-20, -4], [8, -4], [-26, 1], [-10, 11], [-2, 3], [-6, -12], [16, 0], [14, -7], [16, -3], [6, -4], [-8, 2], [-12, 8], [22, -3], [2, -8], [-6, 6], [16, 2], [16, -7], [-16, 12], [-16, 10], [24, -7], [0, -9], [4, 11], [-14, -12], [18, -14], [4, -16], [-10, -5], [-28, -2], [-10, 2], [12, -16], [26, -4], [28, -8], [26, -5], [-12, -8], [10, 3], [32, 5], [18, -8], [-12, 3], [-26, 0], [2, 16], [-8, 16], [0, 5], [2, 4], [-8, -1], [-40, -4], [-18, -2], [16, 3], [-14, 2], [-14, -4], [-26, 5], [-12, 2], [-8, -8], [-18, -5], [22, -13], [0, 0], [8, -16], [6, -11], [-16, 7], [-24, 4], [16, 7], [-18, -10], [2, -6], [-22, 0], [14, 5], [8, -9], [22, 7], [-10, -9], [8, -14], [-2, -15], [-16, -6], [-20, 18], [18, 4], [-8, -16], [34, 4], [-2, 12], [-8, 20], [-8, 22], [-34, 6], [2, -2], [-36, 14], [0, -8], [-12, 4], [-20, 12], [-8, -9], [-6, 18], [50, -3], [18, 8], [12, 6], [2, 4], [-8, 14], [-24, -14], [-6, 14], [-12, -9], [-36, 4], [-10, 3], [-2, -17], [18, 4], [-36, 0], [2, 4], [-16, 21], [-46, 8], [-32, 12], [-6, -6], [24, -21], [-26, 9], [-32, 8], [34, -4], [-48, 10], [16, -10], [14, 14], [-26, 0], [40, -2], [-14, -6], [-26, 2], [-32, -9], [6, 19], [24, 2], [-10, 8], [4, -20], [-32, 9], [2, 0], [-52, -4], [-30, 23], [22, -23], [22, 0], [10, -23], [-12, -3], [-14, 12], [-24, -10], [-38, -4], [-30, 10], [24, -12], [30, -5], [-48, 2], [-4, 22], [42, 0], [-56, 6], [2, 20], [-6, -12], [24, 14], [-16, 3], [8, 10], [2, 4], [8, -24], [18, 4], [-8, 12], [22, 8], [10, -12], [40, -18], [-2, 7], [-24, 14], [32, 11], [34, 0], [-14, 5], [24, 1], [-4, -11], [-4, 24], [22, 16], [-16, 2], [-52, 4], [-10, -17], [32, 7], [-28, 17], [50, -4], [38, -10], [8, 5], [-64, 0], [-60, -4], [-16, 28], [4, -19], [2, 8], [-6, -1], [64, 4], [-24, 20], [-40, 6], [-8, 5], [-16, -8], [-66, 2], [18, -16], [48, -12], [-30, -6], [-14, -8], [56, -13], [-46, -8], [52, 8], [46, -7], [42, 15], [-16, 4], [-4, -22], [-30, 2], [-10, 21], [6, -20], [0, -12], [6, -11], [-22, -4], [12, 12], [-26, 6], [34, 0], [4, -18], [-46, 1], [-76, 4], [70, 0], [28, -27], [28, -32], [2, 24], [70, -1], [-56, 6], [-12, 9], [-24, 3], [16, 8], [10, -4], [40, -29], [-8, -19], [2, 14], [-2, 6], [56, 8], [-22, 14], [22, -15], [56, -11], [18, -12], [32, 2], [-2, -6], [34, 4], [-16, -18], [-10, 13], [-24, -8], [-16, 3], [10, -7], [-10, -24], [-8, 5], [28, 12], [24, 5], [10, -30], [-12, 28], [-22, 22], [60, -12], [14, 18], [24, -8], [6, -9], [-62, 8], [-12, 14], [0, 12], [-14, 16], [72, 2], [0, 21], [-54, 13], [-34, 35], [-72, 10], [10, 0], [-30, 16], [16, -30], [18, 20], [34, -6], [40, 13], [68, 0], [-32, -14], [22, 0], [66, -8], [70, -4], [0, 10], [-16, 13], [44, 16], [8, 19], [-2, -11], [46, 12], [-22, 26], [-12, -7], [-10, -12], [-46, -6], [-14, 10], [-8, -19], [-26, 42], [20, -24], [-22, 44], [68, 6], [-40, 24], [22, -1], [-8, -28], [18, -12], [38, -23], [28, -15], [62, 5], [-18, -16], [32, -2], [-24, 0], [-62, 6], [-40, -2], [-42, 24], [-62, 10], [56, 10], [24, -21], [-28, 8], [26, -18], [38, -33], [24, 24], [-58, 7], [-56, 7], [16, -34], [16, -12], [30, -41], [40, 10], [-58, -16], [72, -12], [-16, 21], [42, 15], [50, -28], [26, 23], [82, 0], [50, -20], [32, -8], [34, -20], [-80, 2], [-52, 9], [-24, 33], [-66, 17], [-4, -12], [-20, 3], [-18, 35], [6, 3], [-40, 21], [28, 16], [-16, 17], [34, -22], [-40, 14], [-78, 4], [16, -45], [58, -20], [82, 4], [-30, -2], [40, 0], [-38, 25], [-46, 2], [-16, 14], [38, -28], [-10, -11], [20, 0], [8, -12], [26, -3], [38, 0], [-4, -4], [-28, 8], [-6, -14], [-6, -2], [40, 1], [-32, 32], [6, 28], [-20, -11], [-2, -7], [-70, -4], [8, -30], [16, 16], [42, -21], [46, 11], [-36, -20], [26, -8], [24, -14], [-16, 6]];
    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_bc();".
// 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_bc(: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_bc();".
// 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_bc( : 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![-4, 1, 1]>,<13,R![-2, 3, 1]>,<19,R![0, 1]>],Snew);
    return Vf;
end function;