
// Make newform 6027.2.a.j in Magma, downloaded from the LMFDB on 25 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_j();".
// 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_j();".
// 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 := [-4, -1, 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], [0, -1], [3, -1], [6, 0], [0, -3], [-9, 0], [-3, -1], [9, 0], [-1, 0], [-5, -1], [3, 2], [-6, 5], [-4, -4], [-5, 3], [-2, -1], [-1, -5], [3, -3], [2, -5], [-6, 0], [2, -2], [14, -3], [11, -1], [-3, 6], [-6, 3], [8, -6], [10, 2], [12, 0], [12, 2], [1, -2], [16, -3], [-6, -4], [-12, 4], [2, 0], [3, 1], [-4, -1], [6, -9], [-11, 5], [8, 6], [4, 2], [4, 6], [-6, 0], [-18, 0], [8, -9], [-12, 1], [7, -8], [6, -9], [-6, 0], [-16, 8], [9, -3], [-18, -4], [3, 1], [-13, 11], [0, -3], [-9, 6], [-5, -6], [17, -10], [-1, 1], [1, -7], [3, 8], [8, -1], [-8, 9], [21, -6], [14, -5], [-19, -2], [-5, -1], [1, -9], [6, -8], [16, -1], [5, -2], [19, 6], [-8, 6], [-3, 2], [14, 4], [-4, -6], [0, -12], [9, 3], [12, 10], [12, 10], [8, 8], [-27, -3], [-6, -6], [-20, 4], [4, 4], [8, 12], [2, 11], [-8, 5], [-22, 8], [29, -1], [-9, -3], [-6, 6], [-8, 0], [-1, -2], [-27, -1], [-1, 7], [-8, 20], [2, 9], [-28, 9], [9, -11], [7, -2], [-8, 4], [-4, 0], [-12, 22], [-1, 17], [9, 15], [-16, -5], [10, -9], [-16, 0], [6, -7], [26, 2], [17, -18], [-33, 9], [33, -3], [-12, 4], [0, -2], [-23, 1], [-24, 2], [-2, 14], [-18, -12], [10, 16], [-20, 20], [12, 0], [2, 14], [26, -8], [-23, -1], [6, -18], [33, -1], [-35, -3], [26, -10], [-14, 5], [18, 0], [-12, 15], [-39, 3], [-15, 11], [-11, -10], [6, 12], [18, -7], [-5, 18], [4, -10], [10, -10], [31, -11], [27, -9], [8, -7], [-30, -8], [-36, 3], [17, -2], [22, 13], [35, -5], [24, -3], [22, -15], [29, 10], [-24, 2], [-12, 12], [40, -3], [3, 21], [-8, -6], [-18, 21], [-46, 1], [-20, 10], [-22, -11], [-5, 10], [-19, 5], [30, 6], [12, 3], [-8, -1], [-23, 12], [42, -2], [-24, 0], [18, 14], [-40, -2], [35, -6], [24, 8], [-27, -9], [-22, 25], [-10, 26], [18, -16], [22, -7], [-5, -15], [-38, 8], [24, 2], [-18, 21], [42, -5], [-14, 4], [6, 12], [8, -3], [-12, -12], [-3, 15], [37, -15], [-28, 20], [-20, -10], [-18, 0], [16, -17], [-8, -10], [30, 0], [20, 6], [26, 10], [-53, 5], [-36, 18], [23, -6], [2, 12], [22, 3], [-3, -21], [43, -5], [12, -3], [-27, -13], [-28, 10], [15, -7], [-24, 30], [13, -13], [-24, 6], [-16, 2], [-49, -8], [-21, -3], [16, -27], [-28, -10], [-43, 17], [30, -10], [6, 24], [-15, 0], [29, -8], [-14, -9], [26, -10], [-30, 24], [30, -9], [18, -1], [-48, 0], [-16, -11], [-42, 5], [36, 6], [-29, -3], [-48, -6], [-30, 18], [40, -8], [-20, -1], [-18, 7], [-12, -11], [-17, 13], [28, -2], [0, 2], [-30, -6], [-39, -6], [24, 9], [-50, -2], [4, -9], [9, -6], [-25, -5], [-6, 15], [-33, 22], [16, 1], [-18, 1], [45, -7], [-6, -12], [14, 17], [14, -6], [42, -16], [-7, 23], [-50, 5], [11, 7], [1, 5], [6, -9], [0, 23], [-30, 25], [49, 12], [3, -1], [-10, 29], [52, -4], [43, -20], [68, 1], [-15, -21], [53, -5], [9, -9], [-2, -24], [8, 0], [37, 1], [16, -16], [-10, 6], [57, 0], [35, 1], [16, 3], [62, 2], [-30, 12], [-40, -2], [-3, -15], [-30, -12], [26, 2], [-24, 12], [-73, 1], [13, -22], [-66, -6], [55, -17], [-48, 18], [-22, 16], [35, -1], [32, -18], [2, 29], [32, -19], [52, -6], [38, -26], [-42, 17], [-29, -19], [65, -7], [-41, -3], [-30, 0], [24, -15], [7, -1], [16, 14], [2, 6], [-11, 16], [6, -23], [-16, -4], [-31, 19], [-2, 1], [-34, -18], [-14, 22], [-8, -11], [-38, 5], [-25, 23], [24, 15], [-2, -15], [42, 4], [60, -12], [-36, 6], [6, -25], [-12, 3], [72, -8], [-23, 1], [-46, -8], [28, -28], [-30, 6], [36, -33], [-32, -1], [9, -23], [-3, -16], [-4, -6], [-74, -5], [0, 27], [27, -31], [36, -28], [-20, -16], [22, -14], [-26, 26], [20, -28], [-48, 15], [54, 10], [57, -7], [-36, -11], [-28, 38], [52, -10], [-61, 4], [-5, -10], [-37, 9], [4, -22], [38, -8], [-42, 6], [-20, 4], [-26, 14], [68, -1], [-9, -30], [-6, -14], [3, 3], [-36, 27], [42, -2], [-16, 0], [18, -40], [-13, 27], [-63, -8], [4, -10], [52, 0], [-36, 20], [-17, -1], [-60, -3], [-6, -16], [48, -26], [38, 11], [-62, -4], [9, 9], [38, -17], [45, -3], [48, 6], [27, 23], [-44, 22], [38, -36], [46, -16], [-60, -5], [-4, 7], [21, -39], [44, -1], [54, 16], [35, -5], [-40, -14], [-32, 15], [-12, 7], [24, -28], [-58, 4], [-10, -1], [49, 3], [-6, 0], [29, -29], [50, -7], [21, -13], [6, 14], [-24, 29], [-42, -6], [-7, 21], [-29, 31], [42, -2], [36, -26], [-51, 1], [-24, -12], [-52, -4], [-84, -3], [6, 8], [52, -25], [-14, -31], [10, 24], [51, 3], [-54, 2], [-12, 16], [-54, 28], [10, -13], [-25, -16], [47, -27], [24, -3], [-15, 7], [-21, 26], [30, 28], [-11, -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_j();".
// 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_j(: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_j();".
// 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_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6027_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-4, 1, 1]>,<5,R![-2, -3, 1]>,<13,R![-4, 1, 1]>],Snew);
    return Vf;
end function;