
// Make newform 6240.2.a.bb in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6240_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_6240_2_a_bb();".
// 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_6240_2_a_bb();".
// 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_6240_a();"
function MakeCharacter_6240_a()
    N := 6240;
    order := 1;
    char_gens := [1951, 2341, 2081, 2497, 5761];
    v := [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_6240_a_Hecke(Kf)
    return MakeCharacter_6240_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], [-2], [-2], [-1], [-2], [-2], [8], [-6], [2], [2], [-2], [0], [-6], [-10], [14], [-10], [-2], [6], [2], [12], [-6], [-18], [-14], [6], [-8], [-20], [2], [-2], [-16], [-20], [-10], [-12], [6], [-10], [18], [18], [10], [14], [-16], [-22], [12], [10], [-10], [8], [-12], [10], [10], [-22], [-30], [-10], [10], [24], [-6], [12], [-6], [-2], [-26], [30], [-16], [6], [2], [24], [22], [22], [-18], [22], [4], [10], [-26], [10], [-28], [10], [-10], [22], [-22], [18], [-34], [-6], [20], [-14], [-18], [2], [-8], [4], [30], [10], [-26], [30], [0], [42], [26], [-8], [-2], [4], [22], [-10], [-12], [-22], [-12], [30], [-24], [18], [-24], [-6], [18], [6], [36], [-10], [-20], [-30], [14], [2], [22], [-34], [-10], [32], [10], [12], [2], [6], [22], [-26], [-2], [2], [-46], [-8], [0], [2], [-30], [6], [16], [-18], [-26], [-14], [30], [-22], [18], [22], [-42], [-50], [-40], [30], [-18], [-6], [-6], [18], [-20], [-10], [-14], [10], [32], [12], [-32], [-28], [44], [6], [42], [-42], [30], [-30], [-38], [20], [54], [-6], [-36], [14], [50], [-10], [-14], [10], [48], [2], [-40], [-6], [10], [38], [16], [18], [-26], [24], [62], [6], [-14], [58], [-30], [22], [10], [-22], [14], [-30], [-44], [-18], [48], [50], [34], [2], [54], [4], [6], [20], [2], [30], [-10], [-66], [10], [52], [-58], [8], [34], [54], [-4], [62], [54], [50], [44], [-30], [-34], [-58], [-50], [-34], [54], [26], [-24], [38], [58], [-36], [-24], [-34], [-14], [-52], [2], [-58], [-20], [-42], [50], [30], [-40], [28], [10], [-16], [36], [-38], [6], [48], [22], [78], [14], [72], [10], [-42], [2], [62], [42], [46], [-54], [30], [-54], [66], [24], [-12], [-6], [-74], [38], [-44], [46], [-10], [-30], [-26], [46], [30], [-46], [-32], [-50], [66], [42], [26], [-38], [8], [-44], [-66], [-68], [-30], [-46], [-40], [-30], [-58], [30], [-6], [-18], [36], [-50], [-50], [-46], [-30], [36], [-6], [-28], [-30], [26], [62], [-32], [-12], [-20], [-6], [40], [6], [-54], [58], [-4], [-18], [30], [44], [2], [-10], [18], [-26], [42], [-50], [-88], [-14], [62], [14], [-82], [66], [90], [-10], [72], [66], [-22], [74], [84], [-66], [78], [18], [74], [30], [26], [-56], [26], [22], [-42], [40], [62], [-24], [-54], [74], [-70], [-54], [-10], [-38], [14], [-16], [22], [38], [50], [82], [-38], [14], [-6], [-42], [-60], [-54], [56], [-58], [-50], [38], [-50], [60], [-16], [-46], [-42], [-4], [6], [-38], [-40], [-70], [-62], [14], [62], [94], [-34], [-42], [82], [18], [34], [-42], [38], [12], [50], [-58], [-18], [4], [18], [26], [-70], [6], [-100], [30], [-62], [-66], [22], [70], [38], [-68], [-94], [-42], [58], [-66], [-30], [102], [-92], [-32], [-22], [30], [-66], [4], [-10], [-104], [-66], [42], [-42], [-76], [-46], [22], [96], [-10], [-86], [36]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6240_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6240_2_a_bb();".
// 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_6240_2_a_bb(:prec:=1)
    chi := MakeCharacter_6240_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_6240_2_a_bb();".
// 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_6240_2_a_bb( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![2, 1]>,<11,R![2, 1]>,<17,R![2, 1]>,<19,R![2, 1]>],Snew);
    return Vf;
end function;