
// Make newform 27.6.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_27_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_27_6_a_d();".
// 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_27_6_a_d();".
// 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;
    Rf_num := [[1, 0], [-1, 3]];
    Rf_basisdens := [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_27_a();"
function MakeCharacter_27_a()
    N := 27;
    order := 1;
    char_gens := [2];
    v := [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_27_a_Hecke(Kf)
    return MakeCharacter_27_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 := 6;
    raw_aps := [[4, 1], [0, 0], [41, -10], [5, -18], [277, -32], [-316, -72], [24, 168], [-1474, 108], [2, -40], [6034, 172], [-793, 522], [3962, 1080], [3878, -2068], [-382, 1548], [-1034, 3184], [1179, 2178], [-33124, -1072], [-26104, 2304], [-18406, -5364], [-19560, -4728], [24905, -2592], [-24664, -288], [-47867, -6560], [-23046, 10236], [91823, -13680], [19207, -2630], [-29404, 13320], [-79695, -5040], [73658, 14040], [200390, -2836], [-24829, 13554], [71561, 2480], [175954, -19556], [-74044, 360], [159785, 45110], [-159061, -30798], [-202420, -10224], [155252, -41040], [-176998, -33760], [396637, 10606], [-371361, 63024], [-292912, 56160], [219388, -84392], [-148267, 110736], [423435, -28014], [-140461, 26514], [475334, 7596], [33080, 20880], [-149852, 192496], [295118, -161424], [-172218, -101820], [-294010, 137624], [260294, -56016], [692832, -17952], [-554356, 40016], [937882, -184664], [-122598, 9324], [-560707, -242514], [340664, -56880], [-334352, -92984], [2404466, -2988], [1645310, 100004], [1219448, -156168], [308690, 450056], [1022621, 319392], [462295, -256358], [-1876846, -89604], [-2230246, 178128], [2025515, 217472], [1458014, 111816], [-3105014, -86012], [3073026, -264048], [-315445, -607518], [-2662120, -54936], [-845476, 251208], [-2747056, 166328], [1892833, 387814], [45452, -319032], [-3440860, -87880], [-1346905, -443520], [144620, -669760], [-629320, 131472], [-3650958, -77184], [4114829, 394416], [-3517825, 474858], [2414356, 412672], [1046850, -19884], [4936127, -444816], [4923121, -402362], [7158689, -131994], [232797, 667056], [4291030, -947504], [-1121632, -797904], [-2032033, -546544], [5158682, -741492], [-3552042, 309048], [8211971, 97106], [-3457356, 649632], [549848, 395928], [-7703260, 111888], [1010048, -1570392], [10459329, -305562], [-1290661, 1362560], [-372596, -1323944], [3124700, -1121328], [-1522186, 973296], [-288539, -1329680], [-5305626, -1082556], [-534010, -1159408], [5732915, 379872], [-1589968, 1163088], [-3097618, -1417824], [4283210, 358772], [1750100, 1299168], [-5900803, 1536030], [-13791194, 568060], [2487860, 1173240], [-7932816, 1126224], [2433605, 980414], [2923459, -872672], [7968086, 880776], [-1673203, 1487520], [9731326, -1254572], [4644156, -1910400], [-11843068, -375912], [-2733813, 814338], [-12396532, 1590336], [15612036, 757536], [20368085, -396162], [5951762, -1658520], [8337512, -2506680], [12553604, -1333528], [-13806397, -804510], [-3762718, 594864], [-8305312, -1263088], [9663281, 893520], [3507738, -1677684], [-10966816, 3764160], [-8445547, 1521038], [-2379834, 3527172], [22223054, 602748], [-18036830, 1156540], [-10407925, -3825054], [24663552, 328704], [-8228158, 3919536], [-6358352, 1984456], [-10795306, -1720944], [12584152, -1667144], [-21534472, -1592640], [-705060, 326016], [11213594, -580032], [-12042228, -4108512], [3057518, 3345084], [11992340, -2789608], [-11303338, -1333332], [-870362, 543040], [-5017489, -2951478], [-35182388, -1681448], [6769451, -3713904], [-10246435, 129758], [-11833781, 1119520], [899940, -2532984], [-23313475, -2585970], [-10210563, 1758240], [15948050, 5317964], [-12313802, -2213168], [-19443067, -1644786], [-11657236, 6645816]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_27_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_27_6_a_d();".
// 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_27_6_a_d(:prec:=2)
    chi := MakeCharacter_27_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 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_27_6_a_d();".
// 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_27_6_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_27_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![-18, -9, 1]>],Snew);
    return Vf;
end function;