
// Make newform 6240.2.a.br 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_br();".
// 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_br();".
// 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 := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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], [0, 1], [1, 0], [2, 2], [4, 1], [-4, 0], [-2, 2], [4, -1], [-2, 0], [-2, 0], [4, -2], [4, -1], [-2, 0], [0, -3], [6, 0], [0, 1], [-8, -1], [10, -2], [-8, -2], [4, 1], [6, -4], [2, 2], [-2, 2], [4, -2], [4, -4], [-10, -2], [-6, -2], [4, 2], [16, 0], [-18, 0], [0, 6], [2, 4], [-4, 1], [14, 0], [8, -5], [4, 3], [-2, -8], [8, -2], [6, 4], [0, -2], [10, -2], [-22, 0], [0, 0], [0, 2], [-8, -1], [20, 1], [14, 2], [-6, -6], [16, 3], [10, -4], [-8, -6], [2, 6], [4, 2], [-2, -2], [4, -3], [-18, -4], [-2, -8], [12, 2], [2, -4], [24, -1], [-16, 4], [-14, 0], [-14, 4], [4, 5], [-6, 0], [12, -8], [14, -6], [6, 4], [-8, -3], [20, -4], [6, 8], [4, 5], [12, 1], [-18, -2], [6, -8], [-18, 4], [2, -8], [8, -8], [-2, -6], [-16, -5], [2, -4], [-8, 8], [-4, 8], [14, 8], [-6, 10], [2, -4], [8, 5], [12, 6], [-16, -3], [0, -9], [-8, -2], [28, 1], [4, -2], [18, 4], [-22, 8], [-4, 4], [6, 6], [20, 0], [-6, -12], [-20, 6], [-14, -4], [-8, -8], [-22, -6], [12, 5], [-18, 4], [32, 2], [-14, 0], [-12, -8], [30, -4], [30, 0], [4, 7], [-20, 5], [-22, 4], [8, -3], [-4, 0], [-10, 4], [0, 0], [6, -6], [-6, 4], [-2, -12], [12, -1], [-28, -7], [-10, -2], [-18, 2], [-8, 4], [-12, 6], [30, 4], [4, -13], [4, -7], [-32, 0], [-2, -4], [30, -4], [2, 0], [2, 4], [24, 7], [-34, 0], [-22, -4], [-4, -15], [2, 4], [-28, -2], [4, 3], [-10, -8], [32, 5], [-2, 16], [-6, -2], [24, 2], [-4, -11], [38, 0], [10, -12], [-4, -10], [-12, 6], [20, 6], [0, 14], [8, 10], [6, -8], [-22, 8], [10, 8], [44, -1], [-6, 2], [16, 3], [-24, -4], [-2, 0], [-12, -13], [-8, 6], [14, 4], [26, -8], [6, 8], [-8, 15], [46, 2], [-32, -4], [10, -6], [-24, -12], [10, -12], [-4, 7], [2, -16], [52, -2], [30, 12], [-48, 5], [0, 18], [14, 4], [-26, -12], [-4, 15], [14, -14], [-2, 8], [32, -7], [-14, 0], [-16, -3], [10, 12], [20, -11], [-24, -2], [26, 4], [36, -10], [-6, 14], [-30, 8], [-2, -16], [-2, 0], [28, 2], [-46, -4], [-24, -10], [6, -16], [-6, 8], [0, -15], [-26, 12], [28, 3], [-52, 0], [22, 12], [-40, -4], [2, -12], [6, -2], [20, 12], [4, 7], [8, -5], [-22, -12], [28, -8], [18, -12], [-36, -3], [-6, 8], [30, -12], [28, 5], [-34, -12], [-24, 15], [-4, -18], [14, 4], [-30, -14], [0, 2], [4, 18], [-24, -3], [46, 0], [-32, -2], [-36, -1], [2, 0], [12, 20], [28, -3], [26, 16], [-30, 12], [-8, -14], [-16, 2], [-28, -3], [-72, 0], [20, -8], [30, -2], [30, -12], [-8, 4], [-16, -7], [-24, 17], [-44, -7], [12, -12], [14, 16], [-10, 12], [44, -5], [-14, 0], [-26, -8], [-8, 13], [-42, 0], [-72, 1], [-10, 0], [2, 6], [-20, 6], [-20, -12], [-34, -6], [6, -12], [30, 16], [-8, 18], [2, 12], [-18, 20], [16, -13], [-26, -12], [54, -4], [64, -3], [10, 14], [8, -4], [10, -20], [16, 15], [20, -7], [-34, -2], [-38, 8], [24, -6], [60, -6], [-12, -3], [36, 10], [-26, -6], [40, -9], [8, -12], [2, -28], [50, 0], [4, -27], [-54, 0], [-2, -18], [28, 12], [-42, -12], [-24, 1], [-2, -24], [-26, -18], [40, -14], [6, 4], [-48, 4], [72, -5], [26, 12], [-46, 0], [24, 0], [-44, -12], [8, 26], [-14, -6], [4, 6], [-10, -4], [0, 5], [6, 0], [-12, -2], [-54, -12], [-14, 0], [-76, 4], [52, 3], [-30, -16], [24, 15], [0, 23], [34, -2], [10, -20], [-48, -4], [-46, 6], [-18, 2], [0, -7], [22, 16], [34, 0], [-4, -5], [-8, -15], [28, 4], [-2, -8], [54, 10], [-42, -12], [56, -2], [28, -5], [-36, 9], [12, 21], [-2, 18], [22, -8], [2, 24], [-4, -14], [14, 0], [66, 6], [2, 4], [64, 0], [-14, 0], [-8, 14], [-42, 16], [0, -5], [0, 1], [54, 4], [-12, 5], [10, -10], [10, -4], [-4, -6], [30, -12], [34, -14], [24, 13], [-48, -13], [50, 2], [60, 1], [-2, 20], [66, 4], [-20, 10], [-56, -1], [44, -2], [66, -8], [-14, 0], [48, -11], [-30, -8], [32, 0], [-24, -4], [28, -17], [38, 6], [-48, -6], [-26, -20], [16, 23], [8, 24], [42, -10], [-46, -4], [-14, 8], [-54, 8], [-2, -20], [48, -15], [14, -8], [-20, -29], [4, 15], [60, 7], [14, -12], [0, 13], [44, -2], [-38, 4], [-14, 20], [24, 13], [-4, 4], [-16, 9], [-6, 4], [20, -13], [66, 12], [64, -2], [42, 0], [14, -14], [18, -18], [16, 17], [6, 0], [-22, 24], [80, -2], [-18, 24], [30, 12], [32, 15], [-8, 21], [-62, 4], [-2, -8], [-28, 4], [-32, -8], [10, -8], [62, -10], [64, -9], [60, 16], [46, 8], [-4, -4], [54, 6], [14, 0], [-52, 5], [-8, -20], [2, 2], [26, -28], [-76, -6], [54, -16], [-12, 3], [40, -18]];
    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_br();".
// 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_br(: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_br();".
// 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_br( : 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, 0, 1]>,<11,R![-8, 0, 1]>,<17,R![-28, -4, 1]>,<19,R![8, -8, 1]>],Snew);
    return Vf;
end function;