
// Make newform 6042.2.a.k in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6042_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_6042_2_a_k();".
// 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_6042_2_a_k();".
// 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_6042_a();"
function MakeCharacter_6042_a()
    N := 6042;
    order := 1;
    char_gens := [2015, 4771, 2281];
    v := [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_6042_a_Hecke(Kf)
    return MakeCharacter_6042_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 := [[1], [1], [1], [-2], [-5], [2], [-5], [-1], [-1], [-4], [7], [-2], [0], [-11], [6], [-1], [3], [-6], [-8], [8], [-8], [-3], [-12], [7], [-7], [13], [0], [-12], [1], [-15], [3], [20], [-12], [2], [6], [-16], [-7], [-1], [3], [7], [0], [14], [-3], [-4], [18], [26], [-5], [4], [-20], [16], [-6], [-9], [-23], [30], [6], [1], [-12], [-20], [-9], [-9], [20], [0], [25], [20], [-22], [-10], [-8], [26], [-11], [1], [-18], [13], [14], [1], [-4], [15], [15], [-15], [-2], [-9], [18], [1], [-4], [-22], [6], [0], [30], [-12], [-28], [33], [16], [16], [-26], [-16], [-16], [24], [30], [3], [-3], [-8], [-16], [-33], [34], [-6], [-34], [45], [-37], [18], [0], [-22], [-8], [-29], [-6], [-11], [-8], [-12], [21], [-18], [-22], [42], [-28], [-29], [22], [-5], [8], [34], [11], [-8], [8], [20], [10], [-20], [-40], [-28], [12], [0], [27], [40], [-7], [12], [-25], [-15], [50], [-38], [23], [-26], [-6], [-39], [5], [52], [-44], [-32], [-14], [45], [-51], [-12], [37], [37], [-23], [-12], [36], [-18], [22], [-13], [32], [-16], [-34], [20], [-6], [32], [-24], [34], [-14], [-4], [-39], [-54], [-37], [16], [-3], [28], [47], [44], [-27], [-19], [-20], [-60], [12], [-52], [-29], [-40], [26], [8], [46], [38], [-32], [56], [30], [0], [-42], [0], [-26], [29], [27], [24], [-51], [30], [36], [-39], [45], [28], [-11], [-8], [16], [-8], [18], [47], [28], [-35], [42], [57], [41], [-19], [8], [32], [3], [-75], [12], [-27], [-28], [26], [22], [31], [-22], [-2], [-64], [-40], [-8], [-26], [57], [48], [60], [-1], [2], [-7], [-71], [21], [8], [-12], [36], [-6], [-10], [-9], [-32], [-14], [-14], [45], [-26], [8], [58], [-34], [-32], [-67], [-19], [-51], [78], [-26], [38], [22], [30], [-34], [-75], [56], [-65], [-38], [18], [-73], [-60], [-12], [-13], [81], [-27], [62], [57], [-54], [-84], [-56], [-24], [-17], [62], [50], [-60], [47], [-34], [-44], [4], [-9], [-44], [62], [42], [-36], [44], [52], [20], [-69], [-30], [-20], [-19], [38], [-46], [-21], [-6], [47], [16], [-19], [-17], [-44], [62], [-50], [-35], [-11], [60], [-58], [-41], [-41], [74], [-34], [28], [-92], [16], [-78], [-3], [-52], [22], [41], [33], [-6], [-2], [-42], [-50], [5], [54], [-30], [-75], [-23], [6], [85], [32], [5], [57], [8], [-70], [46], [-68], [64], [80], [50], [-72], [-6], [-68], [-30], [-4], [42], [0], [4], [81], [-46], [-46], [4], [-74], [-53], [-64], [36], [33], [82], [44], [-26], [-78], [-3], [6], [-32], [-12], [-18], [-64], [26], [-16], [74], [-97], [46], [-74], [-24], [54], [36], [-4], [8], [21], [-87], [54], [12], [-20], [42], [-16], [-70], [48], [-12], [-30], [20], [46], [48], [68], [20], [-77], [18], [-14], [-25], [32], [-6], [36], [-82], [-30], [-23], [-104], [-57], [-72], [4], [-99], [12], [-16], [-11], [-34], [-75]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6042_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6042_2_a_k();".
// 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_6042_2_a_k(:prec:=1)
    chi := MakeCharacter_6042_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_6042_2_a_k();".
// 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_6042_2_a_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6042_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<7,R![2, 1]>,<11,R![5, 1]>],Snew);
    return Vf;
end function;