
// Make newform 6027.2.a.i 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_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_6027_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
        poly := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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], [-1, 0], [1, 0], [0, 0], [-2, 0], [3, 1], [4, -2], [0, 2], [1, -3], [-9, 1], [-2, -4], [-5, 2], [1, 0], [4, -4], [-1, 4], [1, 1], [10, 0], [4, 8], [-3, 2], [8, 2], [-2, -4], [-7, 6], [6, 2], [0, 2], [-7, -7], [2, -12], [-9, -1], [-5, 9], [0, 10], [-4, -2], [-4, 12], [4, 8], [-9, -7], [-5, -11], [-4, -14], [0, -4], [-12, 2], [6, -6], [-5, 10], [-5, -2], [12, 0], [-4, -6], [4, -2], [0, -4], [-18, -2], [0, -8], [-9, -6], [-13, 5], [1, 4], [3, 3], [-8, -6], [-4, -16], [-6, -10], [6, -12], [-4, -18], [6, -2], [-9, 10], [15, -7], [-7, -4], [9, -7], [6, 8], [4, 6], [7, 7], [-11, 0], [-3, 15], [13, 1], [-5, -12], [29, 2], [12, 0], [-2, 4], [6, 8], [-19, -5], [3, 7], [-33, 2], [-12, 0], [-3, 8], [-4, 18], [-12, -2], [-4, 14], [-4, -14], [36, -2], [12, 8], [-6, 8], [2, 10], [2, 12], [-10, -16], [6, -8], [0, -8], [-21, 14], [-11, -2], [4, 22], [-12, -12], [8, -6], [-14, 4], [-16, -4], [17, -6], [-22, 4], [18, -2], [-2, -8], [-11, 14], [7, 4], [-16, 14], [5, -18], [18, -2], [-12, -8], [-8, -6], [-20, 12], [-8, 4], [-1, -9], [-17, -1], [18, 8], [3, 4], [-12, 0], [3, 7], [4, -18], [-24, 2], [14, -14], [-28, -8], [4, -2], [-10, 10], [-40, 2], [-20, -12], [26, -8], [8, 22], [44, -2], [-20, 16], [6, -6], [-24, 8], [-28, 14], [-48, 0], [-40, -8], [-18, -16], [3, -14], [6, -8], [-9, 20], [-16, 18], [-12, 2], [-39, 7], [-22, -12], [-11, 25], [13, -19], [-12, 30], [0, -4], [48, 6], [-22, 22], [-3, 34], [-34, -2], [27, -20], [9, -9], [9, 23], [-18, 28], [1, -16], [-39, 12], [23, -2], [-30, 14], [2, 0], [11, -16], [6, -24], [32, 6], [-15, -6], [21, 27], [-16, 22], [41, -8], [19, -8], [-52, 2], [-6, -24], [-3, -12], [-39, -7], [25, 24], [16, -30], [-24, 2], [26, 18], [30, 2], [-25, -10], [-8, 8], [6, 2], [-15, -8], [18, 16], [14, -28], [-13, 21], [-28, 20], [20, 2], [50, 2], [5, 6], [-13, 1], [18, -16], [-26, -8], [-7, 13], [30, 16], [0, 20], [-24, -20], [-30, -20], [-12, -12], [-26, 12], [-7, -25], [26, 24], [0, 24], [12, 24], [-6, 26], [-16, -8], [-6, -34], [25, 13], [20, -10], [19, 3], [20, 16], [38, 0], [43, -7], [10, 28], [32, 24], [10, 28], [0, -8], [20, 16], [20, -8], [-42, 2], [5, -4], [0, -40], [17, 18], [22, 4], [6, 22], [-22, 6], [-44, -18], [1, 33], [-5, 23], [3, 0], [48, 2], [-2, 22], [-3, 8], [-7, -39], [24, -4], [-7, -43], [7, -32], [12, -32], [-30, -6], [-30, -28], [8, -2], [-4, -28], [-39, 17], [19, 3], [-23, 1], [-32, 8], [14, 38], [-58, -8], [-36, -6], [-15, 22], [-17, -22], [16, -2], [21, -21], [15, 18], [14, -18], [-9, -27], [29, -12], [59, 10], [5, 35], [8, 26], [-18, -14], [-5, -5], [26, -14], [2, -40], [34, 30], [9, 5], [-36, -30], [-66, 10], [13, -41], [45, 1], [19, 24], [21, 9], [44, 14], [-23, -10], [-12, 42], [-41, 1], [23, -9], [24, 32], [-54, 16], [2, -26], [24, -34], [-38, 8], [-52, -16], [60, 2], [-2, -4], [-19, -30], [-36, 8], [-19, 20], [2, -18], [40, 4], [18, 8], [36, 32], [24, -10], [46, 10], [24, 0], [-10, 20], [-5, 41], [-60, 0], [16, 30], [12, 4], [-6, 2], [12, 52], [2, -44], [-43, 2], [-35, 2], [4, 20], [32, -6], [-5, 37], [12, -14], [16, -28], [-4, 42], [18, 24], [-43, 5], [42, 28], [24, 0], [-14, -36], [57, -14], [59, -3], [-18, -28], [2, -22], [39, 31], [34, -34], [52, -20], [3, -3], [-5, -11], [4, -18], [21, 27], [5, -18], [-10, -36], [-60, 12], [-8, -2], [33, 27], [-83, 4], [4, 22], [50, -10], [68, 8], [-2, -46], [-66, 2], [-5, -53], [-19, -12], [-12, 6], [-17, 20], [-44, -6], [-23, 48], [-3, 13], [2, 12], [46, 16], [-2, -20], [-12, -32], [0, 4], [-80, -2], [23, 35], [18, 40], [2, -12], [3, 8], [14, -12], [20, -24], [37, 6], [-61, 7], [26, -32], [-28, 20], [12, -24], [24, 38], [8, -10], [44, -12], [-29, -1], [5, 36], [-4, -4], [36, -20], [-75, -5], [14, 6], [-56, 20], [84, 0], [48, -16], [33, -20], [52, -24], [20, 18], [26, 24], [68, -6], [-33, 9], [26, 24], [-4, 14], [9, 21], [-14, -22], [-20, 42], [1, 47], [56, 20], [18, -46], [-16, 22], [-26, 6], [36, -2], [-52, 26], [35, -18], [47, 17], [-38, 26], [-65, -10], [8, 58], [36, -28], [56, 24], [11, 15], [73, -21], [20, 12], [-18, 42], [-3, -64], [-66, -4], [-20, -6], [8, 0], [35, -15], [16, -26], [-36, 46], [41, 8], [48, 18], [-26, 48], [0, 18], [-12, 16], [40, -36], [-84, -12], [-44, -4], [-14, 18], [-59, -2], [-6, 30], [31, -32], [67, -9], [76, -6], [0, 42], [14, -26], [4, 16], [-76, -14], [-33, -7], [1, 12], [14, -42], [-9, -46], [46, -30], [49, 11], [-74, -14], [-41, 32]];
    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_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_6027_2_a_i(:prec:=2)
    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_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_6027_2_a_i( : 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, 2, 1]>,<5,R![-1, 1]>,<13,R![7, -6, 1]>],Snew);
    return Vf;
end function;