
// Make newform 6027.2.a.d in Magma, downloaded from the LMFDB on 06 May 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_d();".
// 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_d();".
// 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 := [[-1], [1], [-2], [0], [0], [-2], [2], [4], [8], [-6], [-4], [6], [1], [12], [0], [-6], [-12], [-14], [-4], [4], [-2], [8], [-4], [10], [-6], [6], [-12], [12], [14], [-6], [8], [20], [-18], [0], [18], [16], [-2], [-20], [16], [-18], [8], [-2], [20], [-14], [-2], [-8], [4], [20], [28], [-2], [6], [12], [-18], [-4], [18], [12], [6], [28], [6], [-2], [16], [6], [-16], [24], [-14], [18], [4], [-14], [-8], [26], [-6], [0], [4], [22], [20], [-8], [22], [38], [-6], [6], [28], [30], [16], [-2], [-16], [-4], [-6], [2], [-2], [40], [12], [-24], [-32], [-4], [4], [8], [14], [10], [8], [-34], [-28], [2], [36], [-30], [12], [26], [12], [34], [-24], [-14], [-4], [-42], [-6], [0], [40], [-42], [-28], [32], [-14], [0], [-46], [-22], [22], [8], [28], [30], [-10], [48], [-16], [42], [12], [8], [-32], [38], [-6], [-18], [6], [32], [-18], [-26], [8], [30], [-40], [48], [50], [16], [-54], [26], [16], [0], [-2], [26], [-20], [32], [-4], [56], [0], [-30], [-22], [-10], [-36], [-30], [-56], [-20], [-58], [16], [40], [-2], [34], [-42], [48], [-14], [20], [-6], [0], [10], [60], [-58], [8], [-58], [56], [-44], [38], [50], [40], [-10], [62], [-48], [10], [-40], [-18], [-8], [-20], [-18], [12], [-14], [26], [-2], [-14], [-40], [6], [-20], [14], [18], [-60], [-66], [4], [56], [-54], [8], [26], [-26], [-40], [-4], [56], [62], [48], [42], [-52], [6], [-58], [-4], [-10], [-40], [12], [-18], [-6], [-16], [-48], [12], [-50], [20], [8], [58], [36], [-48], [-54], [18], [-4], [-16], [56], [-28], [8], [-50], [-46], [48], [68], [36], [-4], [-72], [14], [-6], [-32], [22], [46], [36], [-26], [20], [6], [42], [16], [40], [-66], [-42], [-38], [-64], [10], [2], [-12], [18], [30], [20], [6], [-80], [-6], [-4], [-8], [14], [42], [-12], [24], [-40], [-72], [26], [28], [-52], [-38], [-58], [-56], [2], [2], [60], [-74], [20], [30], [30], [88], [30], [-84], [44], [62], [42], [-40], [32], [20], [34], [-68], [-30], [-60], [6], [-16], [18], [-6], [-4], [16], [-66], [60], [72], [42], [-50], [-8], [10], [62], [16], [2], [-62], [-44], [-20], [12], [-26], [-90], [78], [28], [-84], [-52], [84], [-90], [-46], [-14], [20], [86], [-30], [-10], [-8], [22], [84], [82], [-4], [88], [-10], [-76], [58], [10], [44], [-90], [26], [-48], [28], [66], [56], [-26], [-54], [-28], [84], [60], [98], [-18], [8], [42], [-56], [-92], [-48], [66], [-28], [-10], [-36], [96], [78], [-30], [26], [62], [-14], [32], [2], [4], [48], [-80], [-74], [-92], [-40], [-62], [6], [8], [-16], [-48], [-6], [80], [-46], [4], [98], [-22], [-66], [64], [90], [-66], [48], [46], [-50], [36], [20], [14], [-18], [32], [-28], [74], [22], [-72], [-48], [2], [-24], [-2], [-10], [48], [-84], [-82], [-82], [16], [70], [92], [88]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6027_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![1, 1]>,<5,R![2, 1]>,<13,R![2, 1]>],Snew);
    return Vf;
end function;