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