
// Make newform 6027.2.a.g in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6027_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_6027_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_6027_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_6027_a();"
function MakeCharacter_6027_a()
    N := 6027;
    order := 1;
    char_gens := [4019, 493, 2794];
    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_6027_a_Hecke(Kf)
    return MakeCharacter_6027_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 := [[2], [-1], [0], [0], [4], [1], [-4], [-7], [6], [-8], [-1], [7], [1], [-7], [0], [4], [-8], [-2], [13], [-6], [-1], [-7], [-8], [-14], [2], [14], [-9], [-14], [11], [-8], [17], [-8], [-12], [-1], [-6], [16], [-22], [-12], [-6], [-12], [-2], [21], [12], [-15], [8], [-8], [-16], [-20], [18], [-15], [-4], [-6], [22], [-26], [28], [18], [6], [-8], [-23], [-18], [31], [34], [-11], [-12], [15], [-22], [-15], [-23], [-12], [34], [10], [10], [25], [-29], [-21], [20], [28], [-1], [-28], [7], [-18], [-5], [16], [-11], [-16], [24], [12], [-1], [30], [29], [24], [6], [31], [28], [-37], [0], [6], [-2], [5], [33], [-20], [-24], [8], [28], [23], [11], [10], [24], [6], [-1], [5], [-6], [-34], [-15], [-8], [-42], [37], [46], [36], [28], [1], [-23], [-30], [30], [3], [-42], [26], [-16], [-1], [19], [-19], [22], [7], [22], [-24], [-7], [-20], [-20], [-18], [28], [28], [10], [-8], [14], [-25], [26], [-51], [-48], [-12], [-12], [-6], [-36], [-35], [18], [23], [60], [-7], [-54], [39], [0], [-36], [-32], [37], [24], [26], [14], [-5], [55], [-35], [6], [-4], [14], [-6], [-59], [-7], [-12], [-43], [-6], [24], [-37], [-39], [10], [47], [26], [2], [22], [22], [-43], [15], [24], [-25], [16], [-44], [-50], [-12], [-60], [33], [26], [-12], [-42], [-8], [-3], [-42], [10], [54], [8], [40], [44], [-50], [-12], [-3], [-66], [-32], [-42], [64], [-39], [-8], [70], [2], [-18], [-53], [67], [6], [60], [-24], [-58], [-40], [12], [43], [10], [-19], [-52], [3], [48], [-71], [4], [49], [30], [-2], [12], [44], [49], [-71], [14], [66], [20], [-7], [6], [-44], [18], [-39], [70], [-2], [11], [30], [32], [7], [63], [-12], [-22], [19], [48], [5], [-1], [-60], [79], [40], [-12], [-76], [54], [43], [16], [19], [23], [-62], [-40], [-30], [-55], [65], [-12], [-30], [-67], [-4], [33], [-28], [10], [-25], [-44], [-67], [-32], [-12], [-48], [42], [60], [-27], [-84], [-71], [24], [-62], [-57], [-38], [42], [0], [-58], [-15], [-83], [36], [-33], [14], [-11], [-78], [10], [24], [-52], [30], [-25], [-16], [14], [5], [20], [-37], [-33], [-56], [-51], [66], [65], [29], [-7], [72], [-54], [15], [-12], [55], [-44], [-1], [42], [-90], [-44], [-55], [-69], [-3], [-44], [30], [-5], [-18], [-54], [67], [32], [18], [-2], [20], [34], [6], [56], [85], [-70], [62], [18], [-46], [-76], [-30], [78], [28], [64], [37], [18], [30], [-83], [-2], [62], [85], [-4], [60], [-7], [-33], [-70], [-12], [-98], [92], [1], [18], [6], [-16], [18], [36], [-18], [37], [30], [15], [-12], [-33], [-36], [22], [45], [12], [-90], [-8], [-30], [-101], [58], [-2], [-54], [72], [-94], [18], [5], [-82], [90], [-75], [-70], [10], [-98], [78], [-80], [-9], [102], [26], [41], [66], [-36], [-62], [-85], [36], [-42], [-90], [-18], [18], [-12], [8], [-22]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6027_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6027_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_6027_2_a_g(:prec:=1)
    chi := MakeCharacter_6027_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_6027_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_6027_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6027_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-2, 1]>,<5,R![0, 1]>,<13,R![-1, 1]>],Snew);
    return Vf;
end function;