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