
// Make newform 6240.2.a.p in Magma, downloaded from the LMFDB on 25 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_p();".
// 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_p();".
// 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], [4], [0], [1], [2], [8], [4], [6], [-8], [6], [-6], [-4], [4], [6], [8], [-10], [4], [8], [-14], [0], [12], [-6], [-6], [-2], [-12], [4], [-2], [-14], [12], [8], [-14], [-16], [6], [8], [-2], [-4], [-12], [-2], [0], [-18], [24], [-6], [6], [0], [8], [-4], [-12], [-10], [26], [-16], [18], [8], [2], [-12], [-10], [24], [-26], [-22], [-20], [6], [20], [-8], [26], [-2], [0], [-14], [36], [-2], [26], [0], [-4], [22], [8], [-36], [30], [-18], [-14], [26], [0], [-10], [-8], [34], [16], [12], [-14], [18], [-2], [12], [-28], [-16], [4], [24], [24], [20], [30], [42], [-4], [14], [4], [-18], [44], [42], [40], [26], [-28], [-22], [-16], [-38], [-36], [-26], [-46], [8], [40], [2], [-20], [28], [14], [0], [-42], [-30], [38], [-4], [16], [6], [-10], [0], [-52], [-34], [40], [-36], [-40], [38], [42], [-14], [-26], [4], [-18], [-22], [0], [-10], [-44], [-12], [54], [0], [-42], [-54], [32], [-4], [-18], [-30], [12], [12], [-44], [8], [-16], [50], [-6], [46], [-28], [-54], [4], [-40], [42], [-52], [-8], [-26], [-30], [-26], [-24], [-18], [56], [34], [16], [42], [-16], [38], [-28], [-58], [-60], [-24], [-10], [18], [-52], [-18], [-66], [44], [42], [-8], [-14], [12], [-40], [46], [-44], [-38], [-14], [62], [26], [52], [-66], [40], [-10], [2], [40], [30], [0], [60], [42], [24], [-62], [-2], [-44], [-60], [-16], [-38], [60], [-30], [4], [30], [-34], [16], [18], [-52], [-44], [46], [-6], [-16], [-20], [48], [46], [-16], [-8], [10], [-68], [-60], [-62], [-42], [-16], [40], [-36], [8], [20], [-2], [26], [-16], [20], [48], [-40], [4], [-34], [-30], [68], [74], [30], [56], [-34], [4], [-42], [50], [-44], [-28], [-10], [46], [74], [48], [-34], [10], [28], [70], [-10], [52], [50], [-64], [2], [-20], [20], [-50], [-6], [24], [-44], [40], [12], [-42], [-44], [-72], [50], [-42], [16], [50], [54], [-60], [-78], [-16], [-2], [-10], [8], [-42], [0], [-12], [10], [46], [80], [44], [40], [-22], [-44], [-74], [0], [-26], [-44], [-10], [-46], [60], [-12], [-6], [56], [40], [90], [-30], [-72], [-46], [-90], [76], [18], [50], [40], [12], [44], [54], [62], [-2], [-16], [-36], [-64], [52], [-50], [-6], [-38], [-4], [-10], [42], [70], [56], [-66], [-96], [14], [68], [-24], [54], [-32], [34], [46], [52], [14], [74], [-96], [-80], [-14], [28], [38], [-70], [60], [24], [20], [10], [-18], [-20], [74], [-72], [32], [-36], [14], [-40], [14], [72], [-40], [-6], [18], [42], [62], [18], [-12], [-54], [-56], [12], [-72], [70], [76], [-68], [50], [-58], [72], [-60], [56], [-22], [-96], [42], [40], [-74], [-18], [-78], [-60], [-14], [-42], [56], [14], [-46], [76], [8], [66], [6], [12], [-88], [-22], [54], [80], [92], [-6], [-60], [-90], [-26], [68], [-16], [2], [-18], [-36], [-6], [16], [0]];
    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_p();".
// 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_p(: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_p();".
// 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_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-4, 1]>,<11,R![0, 1]>,<17,R![-2, 1]>,<19,R![-8, 1]>],Snew);
    return Vf;
end function;