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