
// Make newform 6240.2.a.x 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_x();".
// 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_x();".
// 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], [4], [-1], [2], [-4], [0], [6], [0], [10], [2], [-4], [4], [-6], [-4], [-2], [-4], [-8], [-2], [16], [4], [10], [-2], [-10], [8], [4], [10], [-14], [16], [-8], [-2], [-8], [-14], [8], [2], [-4], [-12], [2], [0], [-2], [0], [14], [-22], [24], [-8], [4], [12], [26], [-6], [-24], [18], [0], [-30], [24], [-18], [8], [2], [10], [-12], [-6], [-28], [-16], [10], [34], [4], [2], [20], [10], [-18], [-8], [32], [-6], [28], [36], [30], [2], [34], [2], [-16], [18], [24], [34], [0], [12], [-30], [-26], [-30], [-4], [-12], [24], [-20], [24], [4], [16], [-6], [-6], [-20], [18], [12], [-30], [12], [-6], [-8], [-42], [4], [-34], [32], [10], [40], [-6], [-10], [-20], [-40], [2], [-44], [8], [26], [0], [-38], [-22], [-30], [-28], [-28], [-18], [-6], [-8], [48], [34], [20], [4], [-32], [-30], [-6], [34], [-6], [-28], [-14], [-38], [-52], [-38], [-16], [36], [-50], [-16], [10], [-14], [0], [12], [-46], [2], [44], [40], [-12], [0], [-40], [-30], [58], [-38], [-12], [50], [20], [-32], [22], [20], [-48], [10], [34], [-54], [4], [-62], [16], [6], [32], [10], [60], [50], [-56], [-34], [44], [-24], [34], [6], [-44], [-26], [-30], [-12], [-14], [-32], [-6], [-12], [-40], [-38], [12], [-6], [26], [50], [14], [-48], [-62], [-16], [-22], [-30], [44], [42], [40], [-36], [-6], [0], [50], [6], [-16], [-28], [-56], [-70], [56], [-46], [60], [-14], [54], [-56], [-38], [4], [12], [54], [18], [24], [-16], [-12], [-30], [0], [24], [-54], [36], [-60], [-70], [-54], [64], [-24], [44], [-64], [-24], [18], [-58], [-72], [-52], [-20], [-4], [-8], [-14], [-6], [-68], [58], [66], [-20], [70], [52], [42], [14], [32], [12], [-6], [58], [14], [0], [-78], [-30], [20], [-30], [62], [-20], [-58], [-40], [58], [60], [28], [-46], [-62], [64], [72], [-32], [24], [-54], [20], [-56], [-38], [26], [-24], [-30], [62], [4], [-66], [12], [-86], [-58], [-56], [-78], [56], [28], [26], [18], [-24], [-76], [32], [38], [-12], [-50], [-40], [-6], [0], [50], [50], [52], [84], [-22], [28], [-24], [70], [18], [-40], [-2], [30], [12], [-50], [50], [60], [28], [8], [-6], [-38], [58], [40], [-36], [60], [36], [-22], [-18], [50], [32], [-86], [-30], [-54], [0], [-30], [72], [78], [-12], [-24], [18], [-60], [-66], [18], [-16], [94], [-70], [-96], [36], [58], [-28], [-38], [58], [56], [-12], [-20], [-14], [18], [4], [-22], [-16], [72], [-60], [-66], [32], [26], [20], [8], [6], [2], [-14], [42], [78], [92], [-26], [-4], [-12], [56], [50], [28], [16], [-14], [42], [60], [-12], [0], [98], [16], [26], [48], [-38], [-22], [-14], [-4], [6], [-30], [0], [18], [66], [-84], [76], [26], [90], [84], [-40], [50], [-42], [-48], [-48], [78], [-72], [46], [26], [68], [56], [-66], [-46], [92], [66], [92], [80]];
    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_x();".
// 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_x(: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_x();".
// 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_x( : 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![-4, 1]>,<17,R![-2, 1]>,<19,R![4, 1]>],Snew);
    return Vf;
end function;