
// Make newform 6240.2.a.bg 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_bg();".
// 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_bg();".
// 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
        poly := [-14, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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, 0], [-1, 0], [-1, 0], [0, -1], [-2, 1], [1, 0], [-2, -1], [2, 0], [-4, 1], [0, 0], [4, 0], [4, 1], [4, 1], [0, -2], [4, 0], [4, 1], [-4, 0], [-4, 3], [-8, 0], [-6, 1], [-4, -2], [-10, -1], [-12, 0], [-4, -1], [10, 1], [-4, 4], [12, 0], [-6, -1], [16, 0], [8, -2], [4, 4], [10, 2], [10, 0], [-10, -3], [4, -5], [-4, 0], [-14, 0], [-18, 1], [0, 0], [-6, -4], [-6, 0], [0, 3], [4, -2], [2, -1], [6, 0], [0, 0], [-16, 0], [-14, 2], [-16, 2], [-8, -4], [-6, 1], [-6, 1], [-10, 2], [-2, 2], [-8, 2], [-22, 2], [-4, 0], [0, 6], [-2, -6], [-10, 0], [-4, 4], [-14, -2], [-14, 5], [4, -6], [14, 0], [-6, -4], [2, -4], [-10, 4], [-10, -1], [-4, -2], [-10, 6], [-24, 0], [0, -2], [18, -4], [10, 2], [20, 2], [4, 4], [4, 1], [18, -4], [2, 6], [-10, -4], [-12, 4], [8, -8], [-2, 0], [-22, 1], [-10, -1], [-8, 9], [-22, 5], [12, -7], [12, 3], [-6, -5], [-18, -5], [-12, -7], [14, -4], [6, 0], [10, 2], [-40, -1], [-10, -4], [8, 6], [0, -2], [-20, 0], [-34, 2], [-10, 3], [14, -4], [34, 3], [-26, 5], [16, 2], [10, -10], [8, 0], [0, -7], [0, 6], [-16, 7], [-30, 4], [2, -6], [0, 8], [-14, -4], [-14, 9], [12, -1], [-2, -4], [-6, 6], [16, -2], [6, 6], [-16, 9], [-8, -10], [18, -4], [20, -4], [16, -4], [28, 4], [36, -2], [12, 1], [-26, 0], [-12, -6], [2, 9], [34, 2], [2, -4], [26, 4], [-14, 6], [-28, 4], [24, -9], [2, 0], [2, -4], [-8, -7], [-4, -8], [-20, -8], [14, -4], [-6, -3], [40, -5], [-2, 1], [26, -3], [4, -10], [14, -4], [-14, -2], [20, 8], [-8, 7], [0, 2], [8, 6], [14, 3], [44, 3], [-26, -2], [12, -1], [-12, 4], [-42, 3], [-10, -2], [2, 12], [22, 4], [-32, 8], [18, 5], [-2, 6], [-18, -6], [20, 1], [-32, 4], [12, -6], [24, -2], [-26, 9], [26, -11], [22, 4], [38, -6], [-20, -1], [0, -8], [2, 12], [-40, -1], [14, -4], [26, 6], [2, 2], [12, -2], [-32, 4], [38, -4], [14, 3], [22, -8], [38, -5], [2, -8], [8, -10], [-18, -5], [-24, 5], [-16, 4], [18, 11], [-18, -4], [22, 2], [10, 2], [-32, 9], [26, 8], [22, -9], [20, -9], [-12, 7], [10, -9], [-44, -5], [-8, -2], [-8, 4], [-16, -13], [2, -7], [10, -6], [16, -12], [0, -4], [4, -8], [-32, 8], [10, 12], [20, 2], [-30, -6], [20, -2], [-54, 0], [-60, 1], [12, -10], [2, 8], [6, 10], [-34, 7], [-8, -9], [-6, -3], [4, 8], [20, -14], [30, -3], [-6, -4], [2, -3], [0, 2], [10, 6], [16, -6], [24, 0], [6, 8], [-38, -8], [50, 0], [0, 14], [36, -12], [26, -3], [-8, -4], [-8, -6], [22, -4], [-4, -6], [-14, 2], [-30, 9], [-34, -6], [-30, 2], [34, -4], [-2, 4], [28, -4], [-8, -13], [-8, -5], [22, -13], [-18, 4], [14, 1], [18, 0], [-22, 9], [44, -2], [-18, 1], [24, -8], [54, -4], [18, -14], [26, -1], [-40, -9], [4, -9], [-10, 7], [-18, 8], [50, 0], [-38, -9], [-2, 5], [48, 4], [-26, 2], [-4, 1], [-4, 12], [-20, 12], [14, -16], [66, 0], [40, -3], [0, 8], [-14, 10], [-16, -16], [-8, 12], [60, 0], [-18, -8], [-22, 2], [-48, 8], [38, 6], [-44, 10], [-12, 0], [-6, -6], [-8, -4], [38, -8], [-32, 2], [-8, -4], [-20, -5], [62, -4], [6, -3], [26, -2], [-34, 16], [-34, 3], [-14, -7], [32, 4], [26, 3], [-2, -15], [52, -3], [32, -16], [50, -2], [-12, 5], [2, -12], [-14, 14], [-48, -6], [-64, 2], [-32, 7], [-6, -5], [-62, -3], [-28, -10], [14, -10], [-28, -4], [28, 2], [16, 10], [4, 3], [-22, -10], [-72, 1], [-22, 10], [2, -7], [32, -15], [-22, -16], [-8, 2], [12, 11], [24, -8], [8, -10], [78, -2], [-36, -4], [-28, 16], [-18, 18], [-30, 14], [16, 2], [-52, 3], [22, 1], [-52, -5], [40, 8], [-42, 0], [6, -12], [12, -17], [-22, -15], [-16, 8], [4, 19], [-62, 6], [-70, 7], [48, -7], [68, -2], [-28, -3], [-36, -14], [-70, 5], [-4, 16], [-36, -10], [-8, -18], [-10, 0], [34, 8], [-54, -6], [48, -12], [32, -18], [2, -14], [-38, 10], [-68, 3], [8, 3], [18, 6], [12, 16], [20, -10], [60, -2], [-14, 5], [-26, 8], [-10, -1], [16, 8], [22, -11], [74, 2], [6, -12], [38, 0], [26, 8], [34, 10], [-58, 8], [-30, 2], [-12, 0], [20, -10], [-18, 20], [32, 4], [32, -11], [-46, 12], [14, 6], [-50, 13], [24, 18], [-14, -7], [14, 12], [-16, 2], [22, -14], [-6, -7], [26, 16], [-16, 2], [22, 13], [32, 5], [46, -12], [-52, 5], [2, -3], [-22, 0], [14, 0], [-38, -9], [6, -5], [14, -16], [8, -25], [-74, 7], [-66, 1], [6, 10], [16, -20], [46, 11], [-76, 0], [26, 14], [44, -1], [0, -24], [8, 13], [8, 18], [-34, 6], [-4, -6], [70, -2], [42, -9], [16, 1], [10, 6], [4, -26]];
    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_bg();".
// 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_bg(:prec:=2)
    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_bg();".
// 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_bg( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-14, 1, 1]>,<11,R![-12, 3, 1]>,<17,R![-8, 5, 1]>,<19,R![-2, 1]>],Snew);
    return Vf;
end function;