
// Make newform 177.4.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_177_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_177_4_a_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_177_4_a_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 := [512, -736, -146, 315, 25, -34, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [-7944, 4790, 2919, -783, -154, 27, 1], [-1296, -3992, 1380, 797, -202, -30, 7], [-920, 3610, -867, -1129, 218, 49, -9], [-10784, 2102, 6473, 42, -776, -9, 24], [-852, 3340, -2175, -811, 412, 33, -15]];
    Rf_basisdens := [1, 1, 584, 584, 584, 584, 292];
    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_177_a();"
function MakeCharacter_177_a()
    N := 177;
    order := 1;
    char_gens := [119, 61];
    v := [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_177_a_Hecke(Kf)
    return MakeCharacter_177_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 := 4;
    raw_aps := [[-1, -1, 0, 0, 0, 0, 0], [3, 0, 0, 0, 0, 0, 0], [-5, 2, -1, 1, 1, -1, -1], [-8, 3, 3, -2, 0, 1, 1], [-15, -10, -1, -5, -4, -1, 0], [-16, 5, -1, -2, 2, 8, 3], [-48, 4, 10, 20, -5, -13, -9], [-5, -5, -4, -3, -12, -10, 9], [-41, -15, -2, -9, -6, 14, -19], [-58, -7, -5, 28, 21, 3, 24], [-34, -3, -9, -16, 19, -1, 16], [-37, 9, 0, 1, 17, 44, -30], [-112, 19, -33, 20, 42, -15, 29], [-117, -26, 33, 7, 16, -9, 8], [-167, -14, -1, 17, -7, 28, 21], [-167, 58, 1, -5, -5, -59, -75], [-59, 0, 0, 0, 0, 0, 0], [48, 67, -19, 6, 23, -63, -6], [214, -35, 33, -60, -69, -4, 0], [26, 23, -27, -80, 38, 90, 37], [-29, -40, 59, -83, 39, -40, -5], [381, -124, -1, 77, -10, 7, 46], [44, 93, 35, -98, -34, -5, -105], [-33, 18, 7, 147, -71, -250, -119], [-194, -191, -27, -74, -7, -134, 36], [-159, 81, -70, 153, -37, 75, 122], [117, -55, 12, -67, -100, 67, -95], [-223, 17, 78, 183, 44, -40, -91], [206, -140, -52, -30, -16, -171, 42], [-126, 29, -87, -176, -87, 4, -32], [-80, 165, -187, -274, 43, 416, 130], [200, 160, -4, 12, -88, 250, 152], [-287, -2, 185, -135, -126, 7, -186], [442, -43, 131, 176, 37, 36, 164], [-904, 170, 270, -214, 27, -3, -237], [-444, -255, -279, 74, -49, 243, 176], [-353, -174, -255, 127, 315, 227, 495], [-255, 14, -79, -447, -11, -114, -65], [-584, -61, -335, -166, 165, -169, 194], [-442, 901, 91, 136, -14, 133, 15], [355, 64, 291, -47, -14, 131, -136], [179, 46, 169, -397, -467, -68, -109], [44, 317, -133, -84, -141, 230, 84], [-545, 159, 126, 407, 297, -101, 42], [455, 409, -188, 109, 50, 175, 11], [-45, 422, -209, 515, -23, -628, -245], [-128, -670, -156, 482, -175, -49, -159], [-927, -331, -296, -169, -53, -129, 286], [-1042, -32, 114, 612, 69, -453, -431], [2077, 178, 215, -283, -252, -184, 196], [-70, -466, 6, -504, 336, 302, 490], [193, 70, 161, 113, 313, 323, -459], [-415, 333, 218, -259, -161, -531, -578], [-120, 291, 291, -204, 55, -500, -508], [4877, -451, -354, 437, 203, 53, 150], [-1853, 388, 195, 195, -170, -92, 246], [1839, -198, 301, 311, 168, 647, 70], [-2053, 520, 245, 215, -88, -5, 148], [-296, -261, 367, -228, 329, -360, 236], [-1491, 155, 124, -251, 239, 355, 68], [-2254, 650, 466, -104, 97, 471, -251], [1672, -542, 428, -74, -908, -743, -296], [-908, 936, 200, -48, -10, 392, 374], [-1725, 289, -460, 1171, 899, -90, -46], [837, 89, 502, -339, -366, 106, -771], [-1165, -1264, 57, -1103, -101, 351, 47], [-514, -1582, 128, -354, 130, 464, 624], [3223, 35, -106, 599, 290, 472, 479], [2897, -350, -989, -851, 257, 1304, 153], [2136, -1105, -11, 30, -284, 790, 211], [-2503, 811, -774, 133, 820, 958, 383], [-202, 1964, 342, -332, -339, -524, -189], [-1769, 876, 259, 769, -467, -757, -587], [-419, -781, 750, 275, 478, -1000, -835], [2104, 457, 869, -1282, 353, -128, -312], [-3742, 1121, 607, -592, -262, 68, -649], [-5193, -869, -178, 9, -418, -135, -395], [4427, 632, -817, -837, 385, 383, -477], [2097, 533, -1072, 581, -314, -410, -385], [1876, -678, -1516, 942, 548, 194, 1546], [1725, -825, -94, -357, 765, 1663, -538], [1230, -703, 165, 144, -432, -568, 85], [-4415, 52, 145, -863, 493, 206, 175], [2926, 1906, 256, -590, 957, 49, 605], [2884, 1202, -82, -684, 163, 615, 113], [-1612, -934, -1990, 654, 27, 537, 777], [3385, -1516, 583, -519, 14, -887, -700], [3869, 2476, 821, 1855, 1017, 14, 21], [-6632, 538, 656, 1910, -780, -583, -124], [-4518, -1404, -264, -2258, -1246, -459, -326], [-2033, -1409, 1076, 2193, -517, -971, 688], [-5074, -2454, -658, -436, 370, 1059, 1608], [-130, -96, -546, 854, -1333, -1645, 47], [2039, 340, 1555, 1153, 37, -2128, -873], [682, 1453, -873, 616, 345, 798, 908], [-1943, 316, 185, -1431, -495, -1158, -373], [4871, -1786, -597, -605, 383, 2174, 1087], [6833, -1480, -597, 1023, 169, 1044, 1075], [-2014, -765, -1551, -860, -577, -926, -656], [-472, 475, -363, 3138, -744, -1764, -1085], [-1039, -2313, 1070, -877, -420, -1334, -271], [1457, -1943, -926, 1523, 1260, 1683, 1741], [5214, 1275, -93, -534, -457, 1818, 2006], [-1877, 1880, 2229, -1337, -43, 884, 495], [-8810, 15, 1267, 1848, -59, -1102, 132], [-4729, 2636, 2289, -1375, -1318, -339, -820], [-4667, -2802, -1797, 19, -886, 93, -1786], [-6829, 222, 2195, 441, -375, -2974, -3171], [2958, -3770, 668, 456, -809, -2222, -1845], [1603, 141, 988, 243, 62, 2586, 1625], [-8411, -1706, 1193, -523, -700, 625, -706], [-7622, -78, -298, 1258, -757, 712, -1017], [1102, 426, -1010, 2482, -1431, -1825, 679], [-6576, 447, 895, -302, -1335, -566, -762], [-275, -650, -2791, 2217, 540, 739, -710], [-7552, -2741, 71, 446, 2239, -1254, 1122], [-5499, -321, 2478, -2513, 830, 576, -1433], [7115, -535, -2758, 675, -542, 1839, -273], [2710, -2321, 1929, 1592, -727, 497, 550], [-2993, -471, 614, 2379, 20, -922, 1303], [-2501, 4008, -417, 3999, 839, 416, 1303], [-6184, -1106, -708, 578, -2104, -914, 722], [-3879, 2329, 1318, -1657, 720, 553, -735], [-10615, -2024, 1205, -9, 1232, -543, -1914], [-8671, 4449, -568, 255, 883, 493, 1722], [734, -1623, 1691, -3428, -356, 295, 1217], [-7732, -80, -618, 1942, -310, -3408, -3514], [-3469, 1959, 3136, -2113, -646, -1008, -3377], [5335, 137, -2330, 653, 469, 2285, -704], [-300, 3793, -375, -1968, -1369, -1636, 124], [10622, -3464, 220, 1144, 649, 3045, 2727], [-3462, -1560, -1868, 3120, 1952, 2357, 2312], [-5471, -1849, 354, -2095, -436, 527, 915], [1387, -135, 646, 1889, 2170, -2264, 1659], [4354, 523, 2157, -4464, -2183, -1730, -868], [-1151, -2119, -1538, 3985, 2546, -1286, -703], [9394, 1014, -1914, 1216, 402, -1354, 2048], [-4906, -1574, 588, 4590, -2082, -3752, -640], [8307, -1249, 3358, -5685, -725, -331, 2280], [-2745, -5411, 1962, -1761, -2588, 1894, -2219], [6155, -1215, -2042, -1763, -1633, 2915, -754], [-9180, -257, -1537, 82, 1694, 3635, 925], [-5459, -774, 2219, 1487, -1251, -1453, 687], [9890, -1761, -1471, -730, 3831, -574, 364], [-673, -5008, -4181, -941, 1565, 342, 3565], [-1940, 4365, 1943, -3288, -1927, -1964, -1466], [-5667, 1645, -2724, -529, 1212, -1586, -3087], [-2586, 2101, 3017, -1708, 721, -4032, -864], [-10682, -1668, -2598, -386, -146, -4074, 458], [4911, -337, -596, 619, 1950, 2628, 3299], [-4766, 3112, 916, 1732, -378, 2284, -4614], [-6636, 8000, -1618, 2822, -110, -560, 2986], [7036, -368, 2064, -1980, -84, 192, -3668], [-8320, -393, -119, 1332, -173, -3270, 14], [-2308, 1747, 63, -3534, -2499, 1988, 1886], [-6811, -1964, -835, 1457, 6, -846, 2882], [1741, -6905, -854, 3249, 2064, 3431, 1379], [-19718, 4053, -1143, 5004, 4023, 1274, 2114], [4814, 1041, -847, 1602, -1209, -2562, -3878], [-6704, 2063, -1379, 5730, -79, -826, 2022], [-5961, 3318, -1579, 6169, 6885, 362, 1461], [7337, 3333, 1550, 641, 385, -291, -1620], [-6902, -304, 1896, -1870, -2838, -6045, -1106], [-9291, 4933, -1146, -2697, 8, 912, -3147], [-12348, -819, -2105, 1230, -899, 2458, -572], [-6652, -2031, -581, 4328, -195, -5288, -4018], [-5210, 4326, 606, 1202, -3792, 3439, 674], [6087, -1436, -2981, -5353, 1077, 5796, 3209]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_177_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_177_4_a_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_177_4_a_a(:prec:=7)
    chi := MakeCharacter_177_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_177_4_a_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_177_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_177_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![-844, 244, 637, -70, -145, -7, 8, 1]>],Snew);
    return Vf;
end function;