
// Make newform 6240.2.a.bo 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_bo();".
// 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_bo();".
// 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], [-1, -1], [1, 1], [-1, 0], [-3, 1], [-2, 0], [1, -1], [4, -2], [0, 2], [1, 1], [3, -1], [-2, 2], [-4, 2], [-11, 1], [4, -2], [1, -3], [8, -2], [1, 1], [-6, 0], [-13, 1], [-8, -2], [-3, -3], [-1, 3], [-12, 2], [-8, 0], [5, -1], [-16, 2], [-2, 0], [-16, 0], [12, 0], [6, -4], [-7, 3], [-3, -3], [0, 6], [2, 0], [-1, -5], [-4, 2], [2, -4], [10, -2], [5, -3], [2, 2], [-11, -3], [-10, 4], [8, 0], [-8, -4], [0, -6], [14, 0], [4, 6], [-13, -1], [9, -7], [0, 2], [-8, 8], [-14, 0], [-16, 4], [-8, 10], [10, -4], [-28, 2], [-10, 4], [-12, -4], [4, -2], [-1, 3], [2, -10], [-2, -8], [-6, 0], [22, 0], [-26, 0], [25, 3], [-6, 8], [-16, 2], [16, -2], [18, 2], [-2, -4], [12, -6], [14, 4], [-24, 2], [9, -7], [-2, 4], [-8, -6], [6, -2], [20, -2], [0, -6], [-10, -4], [-3, 3], [1, -5], [-9, 3], [-5, -1], [7, -5], [-25, 7], [1, -1], [3, 11], [-11, 13], [-2, 2], [-18, 8], [32, -4], [-27, -3], [18, 4], [10, 10], [-2, -4], [-12, 0], [8, 2], [21, 3], [18, -8], [7, -7], [7, -5], [34, -4], [4, -10], [-20, 0], [3, -5], [-10, -10], [27, -9], [-18, 12], [-8, 2], [16, 6], [14, 8], [-13, 3], [-1, -7], [-18, 4], [4, -8], [30, -4], [-16, 6], [-15, 5], [2, 0], [-2, -8], [-8, -6], [-12, 14], [-20, 12], [-10, -6], [17, -7], [-6, 16], [-38, 0], [5, -13], [-4, 2], [22, -4], [-14, 16], [16, 10], [16, 2], [-1, 11], [14, 8], [-18, -8], [19, -1], [-16, 8], [44, -2], [18, -4], [-3, -15], [39, 3], [15, -13], [29, -9], [-2, 4], [-2, -4], [12, -10], [0, 0], [-29, -7], [18, -2], [6, -10], [7, -3], [17, 9], [-24, 2], [33, -7], [-20, -10], [1, 17], [-24, 2], [18, -14], [-22, 0], [32, 6], [-23, -5], [20, 10], [-36, -2], [-19, 1], [-20, 10], [30, 4], [-6, -10], [19, -1], [17, -1], [6, -12], [36, -2], [17, -7], [-32, 0], [30, 4], [7, -1], [46, 2], [12, 2], [36, 6], [54, 0], [-40, 2], [-2, 4], [-7, -11], [18, 0], [11, 11], [-10, -8], [6, -8], [3, 9], [39, -5], [-36, 12], [21, -3], [30, -12], [-16, 2], [12, -18], [-11, 15], [-14, -8], [-29, 9], [-57, 3], [17, 5], [15, -13], [-1, -1], [2, 8], [-4, 8], [-19, 5], [1, 15], [-32, 2], [20, 10], [-16, 12], [12, 14], [16, 2], [54, -4], [-30, -6], [24, -6], [2, 0], [22, 12], [-5, 11], [-6, -4], [26, 0], [20, -10], [25, -17], [1, -19], [-1, 19], [52, 0], [18, -10], [-43, 5], [42, 8], [5, -17], [-14, 8], [-32, -2], [2, 10], [-20, -2], [42, 0], [-6, 0], [14, -14], [-30, 2], [24, -18], [-3, 11], [-44, 0], [2, 0], [-2, -4], [42, 6], [-40, 6], [-43, 5], [-28, -6], [8, 0], [2, -8], [-6, -8], [-20, -14], [-23, -3], [27, 15], [47, -13], [2, 8], [7, 15], [-66, -4], [-1, -5], [2, 6], [35, -7], [-8, -14], [10, -20], [-16, 10], [19, -7], [5, 21], [37, -11], [-27, 13], [-54, -4], [-30, 12], [17, -3], [23, -5], [-4, -12], [-8, 6], [-35, -11], [4, -6], [4, -2], [-2, -4], [-62, -2], [17, 3], [28, -6], [-16, 4], [16, 10], [-12, -10], [36, -12], [-30, -16], [4, -18], [8, -10], [32, 2], [30, 8], [12, 8], [24, -6], [20, 14], [-22, -8], [26, 8], [60, -8], [-9, -5], [-74, -2], [11, -9], [-28, 6], [-30, 0], [47, 9], [-9, 21], [12, -16], [-47, -7], [19, 5], [-25, 23], [-8, 14], [-16, -6], [53, 7], [-26, -4], [44, 2], [18, -6], [-62, 4], [-43, -7], [-5, 3], [29, -19], [42, 8], [-36, 10], [-32, 8], [22, -4], [-14, 4], [31, -25], [36, -2], [19, -25], [-28, -10], [-69, -5], [13, 19], [-10, 20], [-58, 4], [7, -13], [80, 4], [22, 4], [64, -2], [-44, 10], [-4, 6], [-12, 22], [-52, 6], [30, 10], [35, 7], [15, -17], [-3, 29], [-40, 0], [-22, 8], [-58, 10], [29, -27], [59, -9], [-16, -2], [-25, 11], [-8, -10], [5, 21], [-53, -9], [6, 10], [15, -17], [-2, 8], [-19, -15], [12, -6], [-22, 8], [-10, 20], [-22, -8], [-58, 8], [-48, 12], [28, 18], [-34, 10], [-20, 10], [16, 18], [-9, 7], [49, -11], [-36, 0], [-44, 12], [-22, -4], [18, -4], [41, -5], [46, 8], [-29, 11], [12, 12], [-9, 11], [4, 14], [10, -12], [30, -4], [22, -12], [0, 6], [18, 8], [-72, -6], [-8, -10], [-2, 12], [-58, 16], [-20, 10], [-39, 19], [-50, 12], [-32, 18], [5, -19], [86, -2], [17, -3], [-14, 24], [30, 4], [4, -6], [-23, 27], [-6, 24], [-22, 0], [75, 3], [-19, -23], [-26, 4], [27, -9], [9, -5], [-34, -12], [22, 20], [-23, -15], [-57, 11], [-46, 16], [-73, -1], [25, 3], [-67, -5], [-40, -14], [44, -22], [27, -17], [-4, -8], [-28, 6], [-17, 1], [28, 2], [-47, 9], [-26, 4], [-56, -4], [-74, -4], [-16, -14], [5, -5], [7, -5], [56, -14], [18, 10]];
    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_bo();".
// 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_bo(: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_bo();".
// 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_bo( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-6, 3, 1]>,<11,R![-6, -3, 1]>,<17,R![-2, 5, 1]>,<19,R![2, 1]>],Snew);
    return Vf;
end function;