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