
// Make newform 6240.2.a.q in Magma, downloaded from the LMFDB on 20 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_q();".
// 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_q();".
// 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], [-5], [-1], [1], [3], [6], [-3], [4], [0], [-5], [11], [-6], [0], [-9], [-12], [5], [-8], [-13], [-10], [17], [4], [-11], [-7], [0], [-4], [13], [-8], [2], [-4], [4], [2], [-9], [9], [4], [18], [-1], [-24], [-18], [-18], [-23], [-18], [-21], [18], [-8], [-8], [16], [-22], [-16], [-3], [-5], [8], [12], [-10], [-16], [-4], [-2], [16], [30], [-24], [12], [-25], [10], [6], [18], [14], [6], [17], [-30], [4], [0], [-22], [10], [12], [-18], [8], [35], [-18], [-32], [-18], [16], [8], [-22], [7], [17], [7], [5], [11], [-37], [41], [17], [-15], [26], [-14], [24], [9], [2], [-14], [-38], [4], [24], [-3], [30], [-23], [-31], [-30], [20], [-8], [35], [2], [17], [-42], [36], [-48], [-6], [11], [19], [18], [-20], [-18], [28], [27], [10], [18], [0], [52], [-48], [-46], [27], [10], [38], [7], [-8], [-42], [-54], [-36], [32], [-27], [50], [-2], [-1], [-12], [24], [-14], [23], [-27], [-47], [-29], [-6], [-38], [40], [-20], [-25], [18], [-22], [21], [-23], [-44], [-11], [32], [-25], [-56], [6], [30], [16], [35], [-56], [0], [7], [-44], [-30], [-6], [-3], [-21], [30], [-48], [-3], [32], [-18], [-37], [58], [-56], [28], [2], [60], [30], [41], [-38], [41], [-26], [-42], [5], [27], [12], [19], [34], [40], [32], [41], [-14], [-7], [53], [49], [41], [-59], [-50], [-36], [-51], [55], [40], [32], [28], [-64], [-40], [-62], [26], [4], [66], [-22], [55], [-22], [-54], [-52], [-7], [5], [-15], [0], [14], [-45], [42], [19], [38], [-36], [-14], [-36], [58], [34], [30], [18], [-16], [19], [68], [-30], [54], [58], [-32], [75], [-28], [-48], [-2], [-46], [-12], [-79], [1], [65], [-38], [71], [-22], [-47], [-6], [27], [-64], [-74], [12], [37], [-31], [-51], [-19], [-10], [82], [-47], [-7], [76], [72], [41], [-48], [-32], [-22], [-10], [-11], [32], [32], [36], [4], [36], [-62], [72], [44], [40], [-34], [36], [16], [60], [14], [-74], [60], [-51], [-30], [43], [-80], [-22], [29], [-41], [-4], [7], [67], [43], [-24], [-12], [41], [62], [-72], [22], [14], [37], [21], [-17], [-74], [-56], [0], [26], [-58], [-29], [72], [-45], [4], [-69], [-15], [-22], [70], [-27], [40], [-78], [-8], [20], [32], [60], [8], [10], [-15], [1], [49], [72], [-58], [-70], [89], [11], [72], [29], [0], [3], [63], [2], [-29], [-78], [-9], [44], [78], [54], [6], [-10], [48], [-4], [-50], [-64], [-72], [11], [-15], [88], [-84], [6], [-74], [67], [18], [-35], [-28], [65], [-88], [82], [54], [-86], [-80], [6], [-40], [12], [-30], [30], [4], [-27], [26], [-12], [35], [-74], [-45], [-14], [34], [0], [-65], [74], [22], [53], [25], [30], [-9], [43], [10], [-2], [-15], [-7], [-26], [37], [-63], [43], [-96], [-64], [57], [-32], [28], [-45], [-52], [27], [-18], [80], [50], [88], [45], [39], [16], [-62]];
    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_q();".
// 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_q(: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_q();".
// 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_q( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6240_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![5, 1]>,<11,R![1, 1]>,<17,R![-3, 1]>,<19,R![-6, 1]>],Snew);
    return Vf;
end function;