
// Make newform 9240.2.a.bi in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9240_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_9240_2_a_bi();".
// 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_9240_2_a_bi();".
// 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_9240_a();"
function MakeCharacter_9240_a()
    N := 9240;
    order := 1;
    char_gens := [2311, 4621, 6161, 3697, 5281, 2521];
    v := [1, 1, 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_9240_a_Hecke(Kf)
    return MakeCharacter_9240_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], [-2], [-6], [-4], [0], [6], [0], [-10], [2], [4], [0], [6], [-4], [-2], [-4], [-16], [2], [0], [12], [-6], [2], [-10], [-8], [-12], [-10], [-14], [16], [-4], [10], [-12], [-18], [-8], [-2], [-4], [-8], [-18], [12], [22], [-24], [-14], [-18], [8], [-20], [16], [28], [6], [10], [-16], [-6], [28], [-14], [-24], [14], [16], [14], [-6], [4], [-10], [-4], [8], [10], [-2], [4], [-30], [-12], [-18], [18], [24], [16], [14], [20], [0], [22], [14], [-14], [-14], [4], [-10], [0], [-14], [-8], [4], [2], [26], [-18], [40], [-12], [32], [0], [-12], [12], [-24], [-2], [-38], [-28], [-42], [-20], [-10], [-36], [10], [20], [2], [28], [26], [0], [18], [16], [30], [-22], [-4], [16], [2], [20], [8], [30], [-20], [-10], [2], [-26], [-12], [20], [-42], [38], [-48], [40], [30], [28], [-8], [-24], [22], [-30], [-38], [6], [12], [30], [42], [36], [-18], [-16], [36], [30], [40], [6], [-46], [12], [-24], [22], [-30], [-20], [24], [-28], [56], [-8], [-14], [2], [-34], [12], [-38], [-40], [-52], [18], [56], [56], [6], [34], [-42], [36], [30], [-24], [-6], [32], [-54], [4], [6], [8], [46], [8], [28], [6], [-62], [40], [-10], [-42], [-60], [-54], [-64], [2], [36], [-20], [-34], [12], [-6], [2], [14], [42], [56], [-26], [32], [-10], [42], [-20], [-2], [-16], [28], [-70], [-4], [-14], [-42], [0], [12], [40], [-22], [32], [-70], [48], [14], [-34], [56], [2], [8], [12], [46], [-6], [24], [-40], [52], [-18], [28], [-64], [-6], [28], [0], [66], [-34], [-60], [-24], [12], [52], [-24], [30], [42], [72], [0], [4], [-28], [-48], [22], [-38], [0], [10], [30], [-20], [38], [-12], [6], [18], [-16], [12], [38], [30], [18], [-28], [-50], [42], [68], [-18], [-18], [12], [58], [-32], [42], [8], [20], [22], [-22], [12], [64], [-16], [72], [-26], [-28], [-72], [18], [-82], [40], [-54], [-66], [-12], [26], [20], [-26], [-34], [-64], [-74], [-60], [60], [-46], [-26], [-80], [-4], [36], [34], [4], [78], [-32], [-66], [16], [6], [50], [44], [24], [18], [-60], [-32], [34], [18], [28], [-38], [-18], [-40], [42], [-46], [-36], [-20], [-48], [46], [-10], [94], [-16], [-36], [-60], [-20], [14], [-6], [42], [48], [54], [-38], [70], [16], [46], [-20], [70], [68], [-16], [-42], [28], [-22], [14], [-80], [46], [18], [0], [36], [18], [48], [46], [-78], [-24], [-44], [-28], [-22], [78], [-40], [-86], [-4], [12], [-32], [-18], [88], [62], [84], [16], [-54], [10], [18], [62], [10], [88], [-46], [28], [24], [-8], [-10], [-76], [-48], [34], [-90], [36], [20], [-48], [90], [32], [10], [68], [78], [-34], [-62], [-16], [42], [6], [56], [46], [-78], [-12], [20], [-22], [46], [52], [44], [-70], [78], [-32], [-8], [-30], [24], [62], [6], [88], [-60], [-38], [-18], [-84], [-70], [-68], [-24], [26], [-84], [4], [96], [94], [18], [-6], [-90], [-92], [-8], [-36], [82], [-34], [-16], [-70], [-14], [20], [-16], [-14], [-50], [-52], [24], [44], [34], [18], [38], [102], [-100], [22], [-70], [-76], [-32], [-20], [70], [-44], [2], [-88], [-4], [18], [36], [-64], [92], [80], [2], [-60], [-2], [30], [-16], [-96], [54], [-38], [34], [-110], [-42], [-72], [52], [46], [-60], [-68], [-8], [-2], [72], [10], [78], [44], [-26], [92], [102], [-40], [-52], [54], [96], [-54], [8], [-34], [-78], [-48], [-80], [78], [4], [84], [-72], [18], [-66], [68], [82], [86], [62], [-80], [32], [38], [-44], [-30], [-96], [2], [-20], [82], [22], [-44], [-50], [-64], [-6], [-72], [-108], [-50], [104], [38], [-54], [-14], [92], [-120], [-66], [-80], [28], [58], [-92], [-32], [-100], [-80], [-42], [-46], [-4], [-72], [78], [76], [70], [20], [2], [84], [58], [-22], [0], [100], [-34], [-20], [-80], [64], [2], [-26], [-28], [106], [-18], [-24], [-6], [-70], [-100], [-102], [-76], [-74], [-56], [-86], [-20], [86], [-36], [-58], [-24], [98], [-36], [42], [-94], [-64], [114], [-28], [-98], [-122], [20], [-58], [24], [38], [-86], [78], [-104], [-78], [-56], [44], [-62], [0], [-126], [28], [110], [-28], [-78], [30], [32], [116], [124], [-26], [82], [-88], [-104], [96], [-58], [-124]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9240_2_a_bi();".
// 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_9240_2_a_bi(:prec:=1)
    chi := MakeCharacter_9240_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(4603) - 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_9240_2_a_bi();".
// 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_9240_2_a_bi( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<13,R![2, 1]>,<17,R![6, 1]>,<19,R![4, 1]>,<23,R![0, 1]>,<37,R![10, 1]>],Snew);
    return Vf;
end function;