
// Make newform 177.4.a.b in Magma, downloaded from the LMFDB on 16 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_b();".
// 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_b();".
// 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 := [-44, -1736, 63, 484, -7, -41, 0, 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], [-12, 0, 1, 0, 0, 0, 0], [-716, -1588, 51, 287, 24, -9, -1], [4, -332, -197, 47, 32, -1, -1], [2156, 6420, -1915, -999, 232, 33, -7], [2748, -508, -3119, 149, 392, -3, -11]];
    Rf_basisdens := [1, 1, 1, 128, 32, 256, 256];
    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 := [[0, -1, 0, 0, 0, 0, 0], [-3, 0, 0, 0, 0, 0, 0], [-1, 1, -1, -1, 1, -1, 0], [-8, 1, -1, 1, -1, 2, 0], [0, 9, 2, 0, -1, 3, -3], [-10, -2, 0, 2, 1, -5, 4], [-1, 7, 4, 0, -4, -5, 7], [-28, 8, 1, -5, 2, -3, -7], [-26, -12, 11, 15, 2, 13, -5], [31, -15, 8, 8, -19, 6, 1], [-85, -9, 2, -8, 13, 18, -9], [-71, -14, 2, -14, 18, 17, -6], [-50, -29, -11, 15, 39, -14, 2], [-68, -43, 8, 12, -25, -7, -5], [-53, 24, -40, -34, -21, -20, -14], [101, -43, -11, -19, 19, 13, 18], [59, 0, 0, 0, 0, 0, 0], [-135, -85, -4, 52, 73, 6, -19], [-205, -42, -9, 15, -47, -23, 43], [-170, -12, -14, 14, -47, 13, 16], [-115, 74, -4, -58, -29, -74, -6], [-376, 33, 32, 80, 27, 35, 1], [-280, 51, -33, -7, -15, -14, 72], [83, 82, 14, 14, -13, 16, -54], [-195, 82, -51, -111, -3, -19, 35], [725, -157, 33, -75, 60, -28, 24], [-692, 99, -54, -44, 40, -56, 27], [-534, -160, 7, 25, -12, 123, -51], [-376, -35, -75, 41, -80, 29, 100], [-49, 80, 39, -71, 109, 35, -29], [-389, -128, 115, 63, 83, 37, 15], [190, -318, 114, -34, 110, 48, -4], [-90, -85, -70, -68, 145, -157, 19], [-321, 128, -65, 11, -181, -77, -69], [159, -67, -58, -80, -162, -37, 49], [-645, 37, -172, -98, -137, -122, -29], [-629, 117, -181, -87, 125, -153, 114], [73, 208, 268, -54, -123, 196, -236], [-1599, 9, -60, 54, 53, -104, 69], [-946, 109, -95, 169, -13, 4, 148], [212, 137, -250, -224, 57, -241, 141], [-763, 346, -162, -58, 217, -44, 76], [17, 8, 67, -111, 155, -53, -75], [141, 21, 119, 323, 84, 326, -224], [1168, 317, -112, -130, -392, -72, 163], [303, -74, 24, -102, 185, -88, 254], [1337, 447, 410, 434, -200, 255, -77], [-423, 225, -167, -187, -138, -96, 158], [1501, -475, 62, 334, -104, 153, -85], [1814, 294, -187, -103, -171, -64, 233], [864, -106, 554, 310, 36, -58, 136], [835, 519, -265, 271, -201, -159, 32], [889, -705, -219, 73, -34, 196, 100], [-559, -4, 75, -103, -323, 367, -393], [43, -881, -175, 1, 408, 74, -4], [-512, 574, -397, -771, 281, -266, 53], [1238, 251, -258, -244, 195, 215, -131], [-238, 65, 40, 136, -747, 3, -103], [-363, -430, -161, -9, 449, -231, -193], [1327, -431, 79, 415, -158, -2, 476], [-1287, -65, 60, 460, -86, -189, 337], [-1062, 219, 283, 533, 8, 891, -484], [-1712, -848, 70, -122, 214, 94, -306], [611, -310, 378, 62, -532, -95, -112], [-622, -20, 137, -193, -74, -273, 433], [2665, -971, -67, 193, -209, -3, -242], [1960, -680, 428, 120, 362, 72, 236], [1378, 158, 165, 223, -286, 35, -253], [-215, -128, -220, -478, -835, -402, 290], [972, 898, 92, 234, -857, 239, 320], [2940, 834, 305, -285, -312, -383, -423], [2659, -1396, 447, 197, 252, -396, 165], [-2315, -831, -657, -1023, 819, -485, -336], [-3688, 1316, 641, 537, 356, 187, 31], [2161, -904, 237, 157, -421, 849, -667], [-5050, -76, -860, -212, 435, -731, 502], [2748, 225, -74, 420, 766, 452, 9], [-219, -1061, 621, 731, -389, -115, -174], [-512, 378, -465, -1091, 672, -319, -305], [-3298, 202, 308, 260, -80, 128, -82], [-2003, 1907, 601, 397, 288, -122, -176], [-3616, 712, -996, -606, 775, -1273, 262], [-1393, -570, 602, -312, 545, 1092, -702], [-3189, 887, 244, -868, 434, 121, -519], [4095, 1715, 144, 498, -292, -105, 31], [4983, -25, 84, 84, -52, 489, -761], [5798, 961, -380, 166, 231, -433, -317], [-2097, -484, 18, 284, 545, 476, 78], [1938, 1181, -241, -995, -188, -877, -350], [4262, 1413, 553, 531, 506, 1275, -402], [-363, 2193, 147, 771, -668, -374, -524], [3128, 613, -309, 691, 68, 123, 552], [-4929, -1313, -2, -416, 520, 105, -1075], [1841, 960, 350, 166, -185, 418, -160], [-1007, 18, -1039, -311, -951, 259, 379], [-2517, 802, -866, -612, -615, -528, 418], [5231, 1990, 116, 432, 1029, 1238, -592], [-4489, -1420, 1002, 1964, -375, -362, 848], [-2209, -370, 655, 1863, -401, 137, -295], [-6952, -1006, -846, 268, 743, -35, 146], [-1962, 2780, 27, -439, -142, 291, 243], [6710, 2443, -236, 142, 774, 224, 633], [-9569, 352, 401, 87, -447, -249, -553], [1373, 56, 204, -596, -345, -1306, 1102], [2805, 2308, -453, -801, 511, -1051, 97], [-2046, -3115, -10, 550, -641, 1369, 517], [4158, 163, 2286, 1124, 15, 793, 115], [-3841, 3158, -786, -348, -1235, -34, 366], [-7063, -1746, -321, -551, 1750, -1032, -457], [-3710, -1408, 1551, 313, 696, 979, -281], [-4066, 2319, -924, -1580, -307, -925, -457], [-1499, -3200, 421, -65, 172, 1558, -633], [-4035, -1053, 1280, 2164, 536, 1165, -761], [5617, -1870, 863, 571, -347, -1103, -11], [762, -2739, -162, -498, 141, -651, 283], [-4523, -856, -685, -403, -495, -89, -607], [-3464, -2238, 599, -631, 2854, -583, -1223], [-11478, -3777, 1094, -136, -1010, -876, 243], [-7141, 2893, 86, -1658, -117, -518, -1], [-2240, 700, -1579, 1121, -442, 1293, 669], [-1589, -1570, 644, -1164, 2891, 98, -814], [8146, -1954, 948, -276, -136, 596, -398], [-922, 495, -1210, -1876, 550, 222, -741], [-6464, 891, -520, 282, 425, -1141, 393], [1175, 3613, -631, -375, -438, -278, 1242], [2104, -3229, 2085, 1541, 1075, 978, -598], [240, 828, 28, -184, -2430, -416, -452], [718, 934, 1269, -1251, -1100, -779, -1329], [-1469, 1631, 1655, 899, -2140, 1062, 606], [383, 610, -283, 201, 59, -1093, 2429], [3817, -2823, 190, 762, 456, 1579, 461], [-6226, 1421, -369, -485, 2118, 79, 632], [-5560, 133, -758, -404, 1162, 1822, -1279], [-8976, 4232, -57, -57, 912, -115, 657], [-7433, -5300, -441, -1347, -609, -2261, 905], [3298, 3540, -975, 1391, -1362, 67, 1021], [-9196, -2142, 164, -1068, -478, 36, 42], [13396, -1980, 436, -960, 1050, 360, 548], [4101, -2415, -131, 1669, -88, -792, -218], [15258, -114, 1195, 1337, 890, 1369, -1497], [13439, 4691, -1003, -479, -928, -1124, 172], [16878, 283, -1019, -1291, -625, -1320, 1546], [-2347, 2595, -2331, 107, -2693, -531, 1220], [-10015, 6258, -1065, -2259, -1461, 797, -1003], [1917, -1984, 1400, 2652, -25, 3752, -1760], [207, -1078, -2041, -1499, 2927, 1193, -1161], [11962, -3382, 725, -1735, 1434, 623, -1487], [-567, 1556, 1859, 1061, -345, 3805, -2389], [16686, 834, -2050, 198, 3110, 50, -656], [-10922, 570, 5, 1221, -2384, -1319, 1251], [-2816, -1712, -2530, -2810, -164, -3752, 3442], [158, -3828, 528, 3308, 3128, 1878, 108], [19500, -1188, -948, -1960, -68, -572, -128], [2513, 1956, 3615, 3325, -3295, 2385, 65], [14631, -3180, -259, -1335, 4119, -647, -155], [14824, 4396, -3653, -2895, -817, -1580, -633], [-5068, 2831, -798, -1532, 1646, -378, 355], [199, 846, -4033, -2751, -41, 43, -1267], [4851, 4474, 1955, -453, -4839, -619, -103], [5879, 3210, 1183, -395, -2219, 911, -715], [-335, 3930, -3252, 340, -1175, -2702, 2544], [-8455, -269, 207, -521, 1160, -330, -450], [9934, 537, -1409, -1867, -294, -3031, 860], [21310, -4612, 2987, 1121, 532, 1211, -2259], [-1273, 6146, 1951, 1661, -261, 241, 269], [-16985, 5254, 359, 629, -1613, 1597, -501], [25716, -1825, 3305, 3195, -940, 3127, -588], [-18663, -3742, 1514, -618, 2363, 2642, -604]];
    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_b();".
// 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_b(: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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_177_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![44, -1736, -63, 484, 7, -41, 0, 1]>],Snew);
    return Vf;
end function;