
// Make newform 6027.2.a.o 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_o();".
// 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_o();".
// 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 := [1, -3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, -1, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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, 0], [1, 0, 0], [0, -1, -1], [0, 0, 0], [-2, 0, -2], [2, 1, 2], [-3, -1, 1], [5, 1, -1], [0, -3, 2], [-3, 2, 1], [-1, 1, 1], [-6, 1, 1], [-1, 0, 0], [-7, 3, 3], [0, -1, 1], [0, -1, -2], [1, 5, 5], [-3, 1, -1], [-4, -1, -5], [-5, 7, 3], [1, 3, -5], [-4, -5, -3], [-2, -4, -6], [-4, 4, -4], [1, -2, -1], [-6, 8, 2], [1, -2, 1], [-5, 2, 3], [-1, 1, 5], [10, -6, -2], [-8, 8, 2], [5, 3, -3], [5, 6, -1], [6, 3, 0], [-3, -7, -1], [-6, -6, 4], [7, -5, -7], [-7, 9, 5], [-19, 2, 2], [9, 0, 0], [-11, 1, -3], [1, 1, 7], [-1, 7, -3], [5, -9, -1], [-8, 10, 8], [7, -13, -3], [-2, -5, -3], [-6, -5, 8], [6, 7, -3], [5, -8, 5], [5, -3, -5], [2, -6, -12], [6, -8, -6], [-6, 4, -6], [-2, -6, 8], [-4, -10, 2], [-7, -8, -2], [18, 3, 6], [17, 0, -6], [-18, 1, -2], [1, 7, 5], [1, 7, 13], [1, -14, 7], [12, -7, -3], [-10, 3, 8], [4, -9, -8], [9, -8, -2], [-6, -5, -7], [-11, 5, 9], [-15, -5, -9], [-18, -4, -14], [-9, -12, 9], [-5, 16, 1], [10, 1, 11], [-5, 13, 1], [18, -5, -1], [13, 1, 7], [-7, 1, -1], [8, 4, 12], [20, -20, -8], [-17, 9, -5], [-7, -9, -5], [-17, 13, 5], [-2, 4, 14], [17, 7, -5], [-3, 3, 11], [13, -1, 5], [8, -8, -12], [-13, 2, 8], [4, -9, -17], [-7, -13, -5], [0, -20, 6], [-10, -6, 10], [-13, 17, 11], [-2, 18, -4], [-9, -14, -6], [3, 15, -3], [-18, -4, 8], [25, -1, -5], [6, -1, -9], [-5, -14, 14], [-3, -3, -13], [-5, -14, 0], [-29, -3, -7], [-6, -6, -4], [1, 9, -5], [-16, -4, -16], [-10, -10, -14], [-8, 23, 6], [9, 0, 3], [-7, -17, 7], [7, -12, -8], [2, 2, -14], [-7, -18, 5], [16, -4, -2], [-11, 21, -1], [11, -11, -3], [9, -1, 21], [1, -3, 1], [6, -8, -20], [-20, 16, 8], [-15, 11, -5], [-26, 0, -2], [-26, -6, 2], [-12, 4, -20], [-16, 16, 10], [-10, 8, -12], [24, 4, 10], [2, -6, -12], [10, -6, 2], [23, -15, -15], [17, -9, 3], [22, 3, 11], [-8, -2, 4], [-24, 9, -1], [-24, -4, -14], [-26, -2, -14], [-2, 13, -2], [4, -18, 6], [-16, 11, 12], [17, -12, 3], [7, -13, -13], [8, -24, -12], [12, 0, -18], [2, -12, -8], [7, -14, -12], [4, 2, 6], [-16, 13, 15], [-24, -9, -22], [7, -20, 7], [28, -2, 10], [-32, 19, 13], [9, -14, 4], [-7, 6, 16], [34, -8, -2], [-1, -3, -5], [5, -12, -16], [5, 1, -11], [17, 9, 11], [5, 6, -18], [21, -10, -5], [17, -19, -1], [-9, 0, 20], [30, 5, -5], [1, -11, 15], [7, 3, -7], [-11, -14, 2], [11, 0, 23], [15, -6, -14], [-20, 20, 2], [6, -6, -12], [3, 21, -15], [8, -2, -10], [-40, -5, -11], [14, 18, 12], [37, 5, -7], [-23, -14, 10], [-11, -13, -7], [-26, 0, 2], [14, 5, -12], [32, -8, 2], [49, -5, -5], [-32, 6, 4], [3, 0, 24], [11, -14, -3], [10, 4, -8], [-32, 30, 14], [-19, 24, 17], [-1, 11, 25], [-14, -2, -4], [9, -11, -3], [-9, 13, -1], [17, 3, 11], [-10, 0, -2], [-19, 10, -1], [-22, 20, 20], [-13, 17, -9], [27, -15, -7], [11, -29, 3], [-26, -10, -2], [-16, -2, -2], [17, -16, 3], [1, -7, 1], [51, -2, -1], [34, -10, 12], [4, -6, -4], [-12, -11, -24], [43, 1, -9], [-13, 3, 1], [3, 17, 3], [13, 19, 25], [12, 0, 22], [13, 19, -15], [6, 16, -24], [-38, 3, -9], [-2, 14, 12], [-4, 23, -7], [34, -28, -4], [-30, 12, 14], [7, 1, 15], [9, -33, 3], [25, -12, -5], [26, -5, -6], [21, 12, 6], [4, -8, 0], [-6, 0, -2], [-28, 19, -1], [-14, -5, -22], [-14, 6, -24], [0, 21, 6], [-27, -10, -28], [5, 21, -13], [-7, 21, -13], [16, 6, 22], [-2, 14, -6], [-48, 0, 10], [1, 2, -33], [4, -7, -18], [50, -13, -6], [12, -4, 28], [0, 26, 22], [16, 22, 18], [-22, -18, -6], [4, 13, 19], [31, 10, -4], [18, 18, 22], [22, -7, -16], [-3, 10, -6], [-48, 26, 10], [-33, -10, -19], [35, -46, -24], [-15, -8, -22], [3, 4, 29], [7, -9, 19], [-8, -14, -30], [8, 23, -10], [-15, 1, 17], [24, -14, -14], [27, 7, -1], [-3, 18, 11], [21, -13, 11], [-20, -2, -12], [0, 13, -30], [-11, -10, 11], [8, 5, 25], [5, 0, 13], [-27, 21, 3], [-25, 40, 26], [-4, -36, 12], [-27, 14, 9], [3, -34, -11], [22, -30, -2], [-39, 23, 15], [31, -9, -25], [15, 19, 13], [-7, -9, -5], [-25, -21, 5], [44, 8, 4], [25, 5, -15], [-43, 8, -14], [-38, 50, 22], [15, 32, -16], [-13, -17, 11], [-15, -11, -5], [46, 0, 22], [-9, -1, 33], [-7, -1, 27], [-17, 13, -5], [14, -26, -14], [15, -25, 3], [-17, 8, -21], [58, -10, -8], [5, 5, -19], [65, -21, -13], [10, -8, 36], [23, 9, 37], [-30, 0, 2], [-35, 18, 10], [5, -12, 2], [15, 3, 25], [14, 22, -30], [5, 34, 7], [-2, 14, 14], [-15, 29, -3], [18, 10, 16], [-30, -12, -36], [-8, -15, 4], [-22, 24, -10], [1, 5, -23], [33, 13, 23], [7, -22, -30], [41, -6, 3], [-43, -5, 9], [3, 27, -1], [-26, 1, -2], [25, -9, 5], [44, -12, 6], [-1, -10, -33], [10, -23, -28], [19, -21, -15], [4, 7, 30], [2, 11, 1], [10, 16, 14], [14, -10, 14], [-3, -3, -33], [2, -5, -14], [-43, 10, -6], [40, 12, 28], [46, -24, -10], [-13, 15, 3], [-7, 17, -9], [5, -33, -17], [-11, 38, 13], [0, 5, 25], [-17, 33, -3], [-32, -13, -7], [41, 15, 7], [-44, 31, 23], [-41, 12, 19], [29, -9, 3], [-47, -15, -5], [-13, 1, 13], [-14, -2, -34], [16, -28, -24], [-22, -18, -30], [-46, -11, 10], [-27, 23, -5], [-10, 20, -2], [34, -3, -17], [-40, 10, 20], [49, 1, -5], [-18, -1, 23], [-4, -27, -8], [5, -13, -13], [-15, -13, -43], [-9, 29, -7], [42, -26, -24], [10, 6, 32], [-4, 24, 18], [34, -27, -2], [-61, 18, 6], [28, -24, -22], [17, -23, 1], [-13, 16, -9], [-7, 3, -3], [-4, 16, 28], [-27, -21, -15], [35, -3, -5], [-21, -18, -28], [-37, -17, 9], [14, 10, 0], [7, -19, 5], [-9, -29, 9], [18, 31, -14], [-34, 8, 26], [-9, -7, -5], [-62, 39, 16], [44, -2, -20], [-23, 23, -5], [46, 15, -22], [13, 7, 29], [28, 18, 22], [19, 17, 55], [2, -24, 22], [-41, -9, -11], [45, -33, -5], [24, 5, -13], [-14, -15, -8], [58, -8, -14], [-35, -22, 20], [13, -37, 13], [-73, 9, -5], [-1, -13, -15], [19, 0, 25], [-29, 4, -33], [-22, -6, -16], [-10, -24, -10], [10, 17, 35], [37, 9, 9], [-9, -1, -21], [34, 22, 4], [-29, -4, -7], [10, 2, -2], [4, 16, 10], [42, -15, 5], [34, 22, -2], [-28, -18, -30], [7, -5, -1], [-36, -16, -40], [1, 11, 5], [5, -19, -13], [-60, -12, 4], [-73, -3, -1], [-26, 9, 1], [-68, -6, -2], [4, 21, -1], [14, 17, 46], [-51, -19, -13], [1, 7, -9], [-31, 17, 7], [-24, 32, -12], [-31, 13, 1], [45, 0, 5], [-44, 29, 5], [-41, 1, 21], [29, -8, -2], [13, 23, 33], [-3, 24, 29], [-32, -2, -6], [-70, 1, -3]];
    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_o();".
// 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_o(:prec:=3)
    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_o();".
// 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_o( : 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, -3, 1, 1]>,<5,R![-1, -5, 1, 1]>,<13,R![43, 1, -7, 1]>],Snew);
    return Vf;
end function;