
// Make newform 27.5.d.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_27_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_27_d_Hecke();"
// 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_5_d_a();".
// 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_5_d_a();".
// 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, 20, 98, 14, 11, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [-220, 1118, 98, -121, 11, -1], [220, 529, -98, 121, -11, 1], [10534, 889, 1666, -410, 187, -17], [22, 5488, 649, 616, -56, 55], [7166, 32954, 5339, 3740, -340, 347]];
    Rf_basisdens := [1, 1098, 549, 549, 1098, 366];
    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_d();"
function MakeCharacter_27_d()
    N := 27;
    order := 6;
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_27_d_Hecke();"
function MakeCharacter_27_d_Hecke(Kf)
    N := 27;
    order := 6;
    char_gens := [2];
    char_values := [[0, 0, 0, 0, -1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 5;
    raw_aps := [[0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [2, 0, 0, 1, 2, 1], [-3, -5, -12, 0, -1, 2], [-53, -5, -1, 2, 52, -1], [0, 16, 8, 5, 10, -5], [-88, -24, -17, 7, -190, -14], [-52, 0, -9, 9, 0, 0], [82, 0, 43, 14, 55, 14], [75, 64, -21, 42, -96, -21], [0, 202, 101, -46, -329, 46], [128, -162, 126, 36, 0, 0], [-1638, 0, 50, -36, -855, -36], [-261, -251, -417, 0, -346, -85], [1175, -191, 40, -80, -1135, 40], [1554, -234, -222, 12, 3084, -24], [-1918, 0, -789, 13, -946, 13], [1293, 112, 195, 0, 1322, 29], [0, -722, -361, -193, 1324, 193], [-442, -420, -572, -152, -580, 304], [-2734, 324, -297, -27, 0, 0], [1911, 151, 474, 0, 1739, -172], [-225, 2053, 252, -504, 477, 252], [1630, 366, 734, 368, 2524, -736], [-9261, 514, 906, 0, -9139, 122], [353, -1766, -245, 490, -598, -245], [0, 946, 473, -700, 2671, 700], [-2778, 990, 663, -327, -4902, 654], [-2716, -162, -792, 954, 0, 0], [-10462, 0, -1380, 331, -4900, 331], [-10474, 2106, -1710, -396, 0, 0], [-1418, 0, 67, 128, -581, 128], [2157, -1142, -186, 372, -2343, -186], [0, -3218, -1609, 1025, -16052, -1025], [-10194, 0, 1838, -885, -5982, -885], [-19845, -2609, -4584, 0, -20479, -634], [0, 1180, 590, 245, 12040, -245], [6626, -810, 2556, -1746, 0, 0], [37694, 0, -837, 82, 18929, 82], [2917, 6610, 539, -1078, -2378, 539], [-15064, 3324, 3700, 376, -30880, -752], [-14848, -2754, 4176, -1422, 0, 0], [-16305, -2879, 942, -1884, 17247, 942], [0, -68, -34, -1684, -25511, 1684], [-9032, -12252, -12184, 68, -18200, -136], [5960, -2754, 450, 2304, 0, 0], [0, -3446, -1723, 1820, 24253, -1820], [19941, 3703, 8142, 0, 19205, -736], [-34013, 1471, -1249, 2498, 32764, -1249], [0, -4556, -2278, 635, 1882, -635], [16190, 10866, 8689, -2177, 36734, 4354], [-24230, 0, -413, 170, -11945, 170], [7473, -4520, -12648, 0, 11081, 3608], [-9998, -10866, -10237, 629, -21254, -1258], [-53706, 0, 2888, 3576, -23277, 3576], [-439, -6491, -2612, 5224, -2173, -2612], [43554, 10818, 10518, -300, 87708, 600], [5078, -7938, 3294, 4644, 0, 0], [4191, 6574, 12327, 0, 5012, 821], [44779, 10912, 473, -946, -44306, 473], [0, 8962, 4481, 2426, -36071, -2426], [-35078, 0, -3344, -3283, -20822, -3283], [-73888, 7128, -6939, -189, 0, 0], [17562, 0, 16607, -588, 8193, -588], [2853, 6652, 9924, 0, 6233, 3380], [7847, -9848, 4141, -8282, -3706, 4141], [-12261, -8213, -11946, 0, -16741, -4480], [0, -7328, -3664, -6934, -79991, 6934], [143974, 0, 19879, 2867, 74854, 2867], [55299, -11414, -15969, 0, 48440, -6859], [25149, 19450, -3198, 6396, -28347, -3198], [29776, -3072, -8062, -4990, 69532, 9980], [19755, -13805, -28722, 0, 20867, 1112], [0, 1792, 896, 10649, 64186, -10649], [52850, -972, 10089, -9117, 0, 0], [-68554, 0, -30381, -3362, -37639, -3362], [60109, -11354, 1775, -3550, -58334, 1775], [47684, -24786, 15894, 8892, 0, 0], [-107246, 0, 28198, -3214, -56837, -3214], [0, 63400, 31700, -10378, 74821, 10378], [-6294, 0, -13501, 3414, 267, 3414], [405, 21304, 45861, 0, -2848, -3253], [9408, 27288, 26682, -606, 20028, 1212], [84494, 16200, -23445, 7245, 0, 0], [-19857, -5225, -21540, 0, -8767, 11090], [48901, 8131, -1057, 2114, -49958, -1057], [54320, 40548, 33721, -6827, 122294, 13654], [-49533, 5506, -2874, 0, -35647, 13886], [-81201, -40076, -5415, 10830, 75786, -5415], [0, -4406, -2203, -6508, 55633, 6508], [-119594, 22866, 18395, -4471, -230246, 8942], [54881, -10523, -1232, 2464, -56113, -1232], [119270, 11178, -6408, -4770, 0, 0], [117650, 0, -26253, -5057, 53768, -5057], [0, 16054, 8027, -4387, -101312, 4387], [-17674, -33072, -30734, 2338, -40024, -4676], [28990, 0, 12742, -6457, 8038, -6457], [-100382, -67278, -60721, 6557, -213878, -13114], [-185776, -162, 1566, -1404, 0, 0], [-17458, -648, -2628, 3276, 0, 0], [98277, -15683, -28911, 0, 95822, -2455], [-191494, 19830, 19408, -422, -382144, 844], [123838, 0, -22445, 5651, 67570, 5651], [62307, -15524, -720, 1440, -63027, -720], [0, -92066, -46033, -2413, -42764, 2413], [47558, 13932, 3915, -17847, 0, 0], [-190987, 78019, 853, -1706, 191840, 853], [7554, 72918, 64662, -8256, 31620, 16512], [-151330, 0, 88755, -5006, -80671, -5006], [203235, 40486, 86118, 0, 198089, -5146], [0, 30778, 15389, 4532, -183653, -4532], [-95344, 4698, 16470, -21168, 0, 0], [12658, 0, -94334, 4358, 10687, 4358], [18057, -36911, -57585, 0, 1820, -16237], [-273598, 17010, -18612, 1602, 0, 0], [-36685, 32380, 9010, -18020, 45695, 9010], [0, 17542, 8771, -475, 217006, 475], [159552, -31320, -31734, -414, 319932, 828], [-249334, 0, 19404, 8713, -115954, 8713], [53425, -37859, -23266, 46532, -76691, -23266], [0, 50452, 25226, -121, 4690, 121], [338097, -37280, -93243, 0, 356780, 18683], [-2247, 11458, -7311, 14622, -5064, -7311], [-45014, -16950, -21211, -4261, -81506, 8522], [-484695, -9377, -4440, 0, -499009, -14314], [-31962, -24426, -42090, -17664, -28596, 35328], [127131, -15170, -58953, 0, 155744, 28613], [274112, -26556, -9986, 16570, 515084, -33140], [46551, 26179, 15612, 0, 83297, 36746], [0, -74840, -37420, -22825, -313952, 22825], [-41506, -59616, 6903, 52713, 0, 0], [-806106, 0, -50797, 6402, -396651, 6402], [0, -80486, -40243, 33446, 82069, -33446], [383834, 53784, -19224, -34560, 0, 0], [445190, 0, 69954, -11657, 210938, -11657], [0, 59668, 29834, 16979, -365930, -16979], [-83722, 12414, -10838, -23252, -120940, 46504], [0, -16934, -8467, 1142, -554963, -1142], [647742, 0, 29162, 14721, 338592, 14721], [-167580, -82728, -81603, 1125, -337410, -2250], [228296, -5832, 25587, -19755, 0, 0], [120367, 10228, -7777, 15554, -128144, -7777], [0, -113438, -56719, -11524, 335161, 11524], [-7252, -68820, -87056, -18236, 21968, 36472], [151796, 27054, -10026, -17028, 0, 0], [36291, 113977, -12180, 24360, -48471, -12180], [-531423, -28628, -78645, 0, -510034, 21389], [139307, 5668, 11893, -23786, -127414, 11893], [0, 101422, 50711, 12809, 528694, -12809], [460140, -86040, -97386, -11346, 942972, 22692], [0, 56248, 28124, -32467, -93590, 32467], [-95626, -197034, -166694, 30340, -251932, -60680], [-157408, 98496, -56601, -41895, 0, 0], [-1000334, 0, 81127, -7444, -507611, -7444], [-403893, -21059, 4011, 0, -450022, -46129], [324785, 76021, 25054, -50108, -299731, 25054], [362774, 34830, -78678, 43848, 0, 0], [-229757, -166748, 15917, -31834, 245674, 15917], [-549232, -7614, -55386, 63000, 0, 0], [216346, 0, -49352, 7679, 115852, 7679], [-7185, -122129, -42225, 84450, -35040, -42225], [135304, 177900, 186371, 8471, 253666, -16942], [0, 124570, 62285, -74752, -313139, 74752], [-277578, -83592, -80658, 2934, -561024, -5868], [464726, 0, -31860, -13310, 219053, -13310], [134857, 131377, 1952, -3904, -132905, 1952], [1020458, -65610, 76842, -11232, 0, 0], [389379, 150370, 240603, 0, 449516, 60137]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_27_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_27_5_d_a();".
// 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_5_d_a(:prec:=6)
    chi := MakeCharacter_27_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_5_d_a();".
// 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_5_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_27_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;