
// Make newform 6027.2.a.c in Magma, downloaded from the LMFDB on 24 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_c();".
// 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_c();".
// 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], [3], [0], [-2], [-5], [2], [2], [-3], [1], [8], [1], [1], [-10], [9], [-11], [4], [-2], [-5], [-6], [-4], [11], [-14], [-8], [-1], [14], [9], [-11], [2], [-20], [8], [-2], [9], [11], [-6], [16], [2], [-12], [3], [-9], [-2], [6], [6], [-18], [14], [-14], [-7], [19], [-21], [-15], [14], [0], [-26], [10], [4], [30], [15], [7], [13], [-15], [-20], [-14], [1], [15], [21], [-7], [-33], [-29], [-18], [-20], [-14], [31], [-23], [1], [-30], [11], [-2], [14], [32], [-32], [-30], [-38], [16], [-26], [20], [-24], [0], [-16], [-33], [-13], [-30], [0], [4], [-32], [-16], [9], [24], [-38], [44], [39], [19], [6], [-31], [28], [-4], [-22], [28], [-12], [3], [5], [-40], [-45], [8], [45], [-8], [-18], [16], [-26], [-6], [-14], [-20], [34], [-18], [-24], [48], [-36], [-10], [44], [-28], [-44], [-10], [-8], [25], [46], [9], [-28], [16], [1], [6], [25], [31], [10], [-56], [-4], [-34], [-55], [6], [21], [-3], [51], [18], [15], [1], [3], [38], [-48], [11], [-12], [42], [33], [-21], [58], [49], [-15], [-46], [-52], [-47], [-29], [-35], [-12], [32], [-40], [-18], [37], [-52], [36], [5], [12], [-18], [-37], [48], [58], [-22], [-31], [29], [58], [-38], [59], [0], [-24], [2], [28], [22], [34], [-57], [42], [-18], [14], [-36], [12], [34], [27], [-42], [37], [-36], [14], [-65], [-28], [-14], [-36], [-30], [-24], [-14], [-18], [67], [-12], [-29], [46], [26], [-36], [10], [19], [51], [39], [24], [-22], [-49], [-15], [28], [13], [-25], [2], [36], [6], [40], [16], [7], [-33], [-47], [0], [-34], [58], [4], [-61], [-21], [68], [-19], [-9], [-62], [-15], [-27], [-5], [-41], [-70], [42], [-1], [4], [78], [36], [47], [46], [78], [-31], [35], [81], [55], [34], [-15], [-4], [9], [-23], [4], [-32], [-52], [-50], [32], [18], [44], [-64], [-27], [0], [5], [-16], [42], [2], [10], [38], [-8], [44], [40], [-63], [-36], [54], [42], [-78], [18], [82], [45], [13], [-42], [-8], [-27], [48], [-46], [36], [34], [33], [42], [2], [88], [9], [13], [-72], [68], [15], [-76], [8], [-43], [35], [62], [-55], [3], [10], [0], [-30], [65], [17], [8], [-90], [30], [-36], [36], [37], [7], [2], [81], [66], [-77], [23], [0], [36], [-44], [0], [-56], [72], [-33], [40], [-34], [33], [46], [-22], [-65], [39], [32], [-50], [-22], [32], [24], [-52], [-61], [9], [48], [94], [55], [28], [-48], [42], [34], [-23], [50], [4], [8], [54], [23], [-42], [26], [-63], [-62], [62], [73], [-18], [-78], [-70], [-78], [90], [-54], [1], [-33], [30], [75], [42], [-90], [-26], [-39], [27], [12], [10], [-9], [-92], [22], [76], [-15], [-60], [56], [15], [-40], [2], [42], [-84], [50], [-26], [-68], [-72], [-65], [102], [33], [59], [38], [-102], [-36], [100], [62], [-3], [-21], [12], [63], [48], [75], [62], [89]];
    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_c();".
// 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_c(: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_c();".
// 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_c( : 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![-3, 1]>,<13,R![5, 1]>],Snew);
    return Vf;
end function;