
// Make newform 6018.2.a.g in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6018_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_6018_2_a_g();".
// 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_6018_2_a_g();".
// 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_6018_a();"
function MakeCharacter_6018_a()
    N := 6018;
    order := 1;
    char_gens := [4013, 1771, 1123];
    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_6018_a_Hecke(Kf)
    return MakeCharacter_6018_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], [0], [3], [-2], [-2], [1], [-1], [-1], [-6], [-10], [-2], [3], [-2], [2], [-11], [-1], [6], [-4], [-8], [-7], [16], [-7], [5], [-7], [4], [-15], [-1], [0], [20], [18], [8], [10], [-14], [-8], [-5], [11], [-10], [0], [-2], [15], [-10], [12], [22], [-22], [15], [-16], [-14], [18], [-27], [-18], [0], [-10], [-12], [-8], [-11], [3], [0], [17], [0], [3], [30], [1], [8], [-26], [-4], [-25], [-5], [14], [15], [19], [-11], [16], [-14], [10], [7], [-26], [-12], [20], [-10], [20], [29], [-3], [-39], [-4], [-1], [9], [8], [-13], [-20], [-12], [18], [37], [-24], [-12], [9], [-30], [-25], [20], [16], [-42], [-22], [5], [-31], [-29], [38], [7], [16], [39], [5], [33], [29], [-5], [-26], [-20], [-13], [-20], [37], [6], [45], [-32], [-19], [18], [14], [5], [-24], [-26], [-16], [-26], [54], [44], [4], [-4], [2], [18], [32], [-44], [40], [-6], [28], [43], [-11], [26], [-23], [-24], [28], [38], [20], [20], [18], [13], [-28], [-19], [45], [-28], [6], [-8], [38], [-8], [35], [37], [-2], [-55], [14], [-30], [15], [10], [-37], [14], [6], [-24], [-35], [-8], [12], [4], [53], [12], [-47], [-16], [34], [-4], [45], [-32], [12], [-41], [0], [-38], [54], [49], [-23], [-42], [16], [-34], [21], [-24], [-28], [-22], [-10], [22], [-24], [-12], [48], [-58], [-40], [-30], [8], [-16], [-11], [2], [26], [-18], [-51], [19], [50], [54], [-35], [8], [60], [8], [13], [-49], [14], [-71], [-20], [15], [3], [-42], [25], [21], [47], [5], [43], [53], [9], [-12], [-63], [66], [47], [30], [39], [-71], [22], [20], [-46], [13], [66], [-40], [54], [43], [49], [-78], [48], [-52], [48], [41], [-78], [-51], [-36], [-21], [5], [22], [-74], [-29], [17], [29], [-8], [22], [-75], [26], [-33], [-70], [12], [26], [-58], [-49], [-8], [-84], [-30], [-38], [60], [-33], [14], [32], [10], [64], [48], [-37], [-20], [33], [-22], [42], [3], [46], [56], [2], [25], [40], [-45], [-6], [-62], [-71], [-48], [-82], [-36], [-14], [54], [-66], [-6], [48], [-17], [-56], [16], [14], [-2], [-48], [-8], [-41], [52], [-86], [22], [6], [-38], [-25], [-54], [-66], [10], [20], [-74], [-20], [-7], [-8], [6], [68], [-36], [0], [89], [60], [-6], [-65], [22], [-12], [33], [-45], [72], [-76], [-34], [-46], [12], [-59], [-37], [-88], [-18], [18], [52], [52], [6], [62], [-28], [27], [32], [-42], [-23], [45], [90], [-1], [16], [-53], [0], [41], [-38], [-16], [-21], [27], [47], [-55], [6], [-36], [34], [-34], [42], [62], [-39], [89], [92], [20], [-96], [76], [0], [88], [-68], [53], [-1], [-15], [-28], [-80], [51], [-96], [-26], [-6], [57], [79], [21], [24], [-57], [33], [48], [-27], [74], [-14], [-61], [-6], [30], [15], [-96], [33], [-20], [-72], [-40], [16], [-80], [6], [-41], [-58], [72], [28], [85], [102], [-3], [-88], [-84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6018_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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