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