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