
// Make newform 21.4.g.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_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_21_4_g_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_21_4_g_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 := [531441, -59049, 0, -21141, 486, -441, 1564, -49, 6, -29, 0, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [142839531, -145536102, -81309015, 61874280, -17528769, 6980468, -632573, 161148, -1327, 146502, -22601, -538], [575491554, 410108427, 112284954, -82640331, -21396420, -2790145, 441628, 617943, 226448, -13815, -17114, -661], [-271271106, -15136227, 61499898, -12229137, -1141056, 26593, -33772, 83625, 69172, -26109, -3322, 109], [173722158, 4113747, -5390226, -3432699, 2301324, -658001, 289700, -24405, 6088, -627, 5426, -857], [8693961417, -3646715337, -938815677, -245384073, 198180063, 12125077, 15945431, -5417499, -1385435, -231543, 145853, -4301], [-1180448559, -575773677, 303484887, 90039843, 16879851, -8432491, -3453101, -1557771, 345029, 136449, 19933, -4345], [-151814979, 13338513, -1371249, 7836021, 1005399, -641129, -227449, -82569, 6421, 6255, 209, -1523], [-151814979, -149794191, -1371249, 7836021, 1005399, -641129, -227449, -82569, 6421, 6255, 209, -1523], [5512873689, -2180220300, -784745901, -75013614, 126399249, 34199906, 8057845, -3679350, -2138089, -61380, 116845, 22376], [-66193929, 22720743, 8961597, 2622699, 2637, 335465, -164387, 30441, 4103, 4041, -221, 635], [876936699, -145536102, 181515897, 55832328, -12046257, -12463180, -23405, 86556, 359201, 146502, -10169, -12970]];
    Rf_basisdens := [1, 489398112, 489398112, 69914016, 163132704, 489398112, 489398112, 54377568, 54377568, 489398112, 13226976, 244699056];
    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_21_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 5];
    // 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_21_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 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 := 4;
    raw_aps := [[0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [-5, 0, 0, 3, -1, 0, 0, -1, -1, 0, -1, 3], [0, 2, 0, 0, 0, -2, 4, -2, 2, 1, 0, -4], [-9, 2, 0, 3, -18, 0, 0, 5, 0, 0, 3, -2], [0, -9, 8, 0, 0, 0, -4, 9, 9, 0, 0, 0], [17, 1, 0, 1, -17, 0, 0, -1, 0, 0, -2, 2], [0, 10, 4, 0, 0, 0, -4, -10, -5, 0, 0, -5], [0, 7, -52, 0, 0, 5, 0, -7, -14, 5, 0, 7], [-110, -12, 0, -4, -55, 0, 0, -10, -8, 0, 2, -2], [135, 19, 0, 11, 135, 0, 0, 19, 4, 0, 0, -4], [0, -6, 32, 0, 0, -16, -64, 6, 0, 16, 0, 6], [-87, 18, 0, 31, 0, 0, 0, -13, -26, 0, -31, 18], [0, 0, 20, 0, 0, 14, 20, 0, 27, 0, 0, -27], [0, -28, 0, 0, 0, -6, 40, 28, -28, 3, 0, 56], [0, 54, -32, 0, 0, 0, 16, -54, -54, -19, 0, 0], [114, 38, 0, 38, -114, 0, 0, -38, -89, 0, -76, -13], [0, -62, 0, 0, -181, 0, 0, -23, 62, 0, 39, 85], [0, -18, 68, 0, 0, 4, 0, 18, 36, 4, 0, -18], [-290, 38, 0, -22, -145, 0, 0, 49, 60, 0, 11, -11], [325, 88, 0, -12, 325, 0, 0, 88, 50, 0, 0, -50], [0, -27, -56, 0, 0, 13, 112, 27, 0, -13, 0, 27], [0, 0, -104, 0, 0, -48, -104, 0, 51, 0, 0, -51], [-156, 37, 0, -81, -312, 0, 0, -44, 0, 0, -81, -37], [0, -39, -384, 0, 0, 0, 192, 39, 39, -2, 0, 0], [43, -93, 0, -93, -43, 0, 0, 93, 74, 0, 186, -112], [0, 108, -128, 0, 0, -59, 128, -108, -54, 118, 0, -54], [0, 2, 0, 0, 507, 0, 0, -75, -2, 0, -77, 73], [0, 68, 164, 0, 0, 0, 0, -68, -136, 0, 0, 68], [1163, -177, 0, -264, 0, 0, 0, 87, 174, 0, 264, -177], [0, 0, -180, 0, 0, 13, -180, 0, 6, 0, 0, -6], [0, 27, 0, 0, 0, -88, 376, -27, 27, 44, 0, -54], [221, -176, 0, 5, 442, 0, 0, -171, 0, 0, 5, 176], [0, 54, -128, 0, 0, 80, 128, -54, -27, -160, 0, -27], [0, 13, 0, 0, 18, 0, 0, 242, -13, 0, 229, -255], [-508, 55, 0, 36, -254, 0, 0, 37, 19, 0, -18, 18], [-950, -114, 0, -192, -950, 0, 0, -114, 39, 0, 0, -39], [0, -102, 428, 0, 0, 86, -856, 102, 0, -86, 0, 102], [0, 0, 428, 0, 0, 110, 428, 0, -51, 0, 0, 51], [0, -47, 0, 0, 0, 116, 292, 47, -47, -58, 0, 94], [-807, 324, 0, 3, -1614, 0, 0, 327, 0, 0, 3, -324], [0, -138, 820, 0, 0, -78, -820, 138, 69, 156, 0, 69], [0, 66, 0, 0, -219, 0, 0, -218, -66, 0, -284, 152], [0, -22, -388, 0, 0, -66, 0, 22, 44, -66, 0, -22], [1328, 357, 0, 60, 664, 0, 0, 327, 297, 0, -30, 30], [696, -96, 0, 292, 0, 0, 0, -388, -776, 0, -292, -96], [-850, -267, 0, 299, -1700, 0, 0, 32, 0, 0, 299, 267], [0, 234, -1176, 0, 0, 0, 588, -234, -234, -59, 0, 0], [-1273, -23, 0, -23, 1273, 0, 0, 23, -220, 0, 46, -266], [0, -578, -284, 0, 0, 80, 284, 578, 289, -160, 0, 289], [0, -184, 56, 0, 0, 222, 0, 184, 368, 222, 0, -184], [1988, -349, 0, 126, 994, 0, 0, -412, -475, 0, -63, 63], [0, -231, -192, 0, 0, -267, 384, 231, 0, 267, 0, 231], [0, 0, -756, 0, 0, 160, -756, 0, -369, 0, 0, 369], [0, 341, 0, 0, 0, 52, 112, -341, 341, -26, 0, -682], [0, -276, 536, 0, 0, 0, -268, 276, 276, -285, 0, 0], [-840, 371, 0, 371, 840, 0, 0, -371, -99, 0, -742, 643], [0, -100, 0, 0, 1913, 0, 0, 539, 100, 0, 639, -439], [0, 246, 260, 0, 0, 46, 0, -246, -492, 46, 0, 246], [-6786, -46, 0, 34, -3393, 0, 0, -63, -80, 0, -17, 17], [0, 609, -532, 0, 0, -301, 1064, -609, 0, 301, 0, -609], [-1249, -400, 0, -709, -2498, 0, 0, -1109, 0, 0, -709, 400], [0, -303, 1744, 0, 0, 0, -872, 303, 303, -154, 0, 0], [1021, -180, 0, -180, -1021, 0, 0, 180, 988, 0, 360, 628], [0, -536, 1348, 0, 0, -287, -1348, 536, 268, 574, 0, 268], [927, 389, 0, 569, 927, 0, 0, 389, -90, 0, 0, 90], [1437, -21, 0, -560, 0, 0, 0, 539, 1078, 0, 560, -21], [0, 189, 0, 0, 0, 36, -548, -189, 189, -18, 0, -378], [-2636, -46, 0, 322, -5272, 0, 0, 276, 0, 0, 322, 46], [0, -63, -1176, 0, 0, 0, 588, 63, 63, -146, 0, 0], [0, 1010, -1040, 0, 0, 0, 1040, -1010, -505, 0, 0, -505], [-2274, -198, 0, -296, -1137, 0, 0, -50, 98, 0, 148, -148], [4057, -1285, 0, 399, 4057, 0, 0, -1285, -842, 0, 0, 842], [-1713, 42, 0, 217, 0, 0, 0, -175, -350, 0, -217, 42], [0, 0, -252, 0, 0, -464, -252, 0, -291, 0, 0, 291], [0, -773, 0, 0, 0, 24, -68, 773, -773, -12, 0, 1546], [2979, -547, 0, -547, -2979, 0, 0, 547, 768, 0, 1094, -326], [0, -670, -1652, 0, 0, 58, 1652, 670, 335, -116, 0, 335], [-5310, -820, 0, -1448, -2655, 0, 0, -96, 628, 0, 724, -724], [0, -1266, 416, 0, 0, -742, -832, 1266, 0, 742, 0, 1266], [3191, -720, 0, -687, 0, 0, 0, -33, -66, 0, 687, -720], [0, -251, 0, 0, 0, -772, -1436, 251, -251, 386, 0, 502], [-2531, -116, 0, -665, -5062, 0, 0, -781, 0, 0, -665, 116], [-1124, 1065, 0, 1065, 1124, 0, 0, -1065, -2135, 0, -2130, -5], [0, -92, 1816, 0, 0, 145, -1816, 92, 46, -290, 0, 46], [0, 570, -1516, 0, 0, -254, 0, -570, -1140, -254, 0, 570], [-4215, 596, 0, 164, -4215, 0, 0, 596, 216, 0, 0, -216], [0, 618, -728, 0, 0, 292, 1456, -618, 0, -292, 0, -618], [-3235, -846, 0, -561, 0, 0, 0, -285, -570, 0, 561, -846], [0, 0, 556, 0, 0, 564, 556, 0, 969, 0, 0, -969], [0, 705, -936, 0, 0, 0, 468, -705, -705, 358, 0, 0], [0, 302, 0, 0, 2577, 0, 0, 222, -302, 0, -80, -524], [0, -77, -196, 0, 0, 35, 0, 77, 154, 35, 0, -77], [3921, 2673, 0, 881, 3921, 0, 0, 2673, 896, 0, 0, -896], [0, -150, 1104, 0, 0, 714, -2208, 150, 0, -714, 0, 150], [0, 0, 288, 0, 0, -833, 288, 0, 1494, 0, 0, -1494], [0, -795, 3248, 0, 0, 0, -1624, 795, 795, 270, 0, 0], [2335, -477, 0, -477, -2335, 0, 0, 477, -114, 0, 954, -1068], [-6725, -555, 0, 837, -6725, 0, 0, -555, -696, 0, 0, 696], [1664, 1350, 0, 2178, 0, 0, 0, -828, -1656, 0, -2178, 1350], [0, 98, 0, 0, 0, 894, 2164, -98, 98, -447, 0, -196], [0, -1332, -848, 0, 0, 0, 424, 1332, 1332, -625, 0, 0], [0, 2246, -776, 0, 0, 32, 776, -2246, -1123, -64, 0, -1123], [0, 304, 0, 0, -15787, 0, 0, 643, -304, 0, 339, -947], [15422, -30, 0, 768, 7711, 0, 0, -414, -798, 0, -384, 384], [0, 405, -596, 0, 0, -1355, 1192, -405, 0, 1355, 0, -405], [0, 0, 64, 0, 0, -864, 64, 0, -2073, 0, 0, 2073], [0, -453, 0, 0, 0, -240, 1432, 453, -453, 120, 0, 906], [-53, 773, 0, -308, -106, 0, 0, 465, 0, 0, -308, -773], [-7635, -88, 0, -88, 7635, 0, 0, 88, -418, 0, 176, -594], [0, -231, 0, 0, 13964, 0, 0, 519, 231, 0, 750, -288], [0, 160, 848, 0, 0, 716, 0, -160, -320, 716, 0, 160], [8186, 1616, 0, 414, 4093, 0, 0, 1409, 1202, 0, -207, 207], [-6232, 447, 0, 57, 0, 0, 0, 390, 780, 0, -57, 447], [0, 249, 0, 0, 0, -772, -3332, -249, 249, 386, 0, -498], [9419, 748, 0, 971, 18838, 0, 0, 1719, 0, 0, 971, -748], [0, 1929, -4864, 0, 0, 0, 2432, -1929, -1929, -1338, 0, 0], [0, -1680, 1708, 0, 0, 605, -1708, 1680, 840, -1210, 0, 840], [0, -30, 4652, 0, 0, 52, 0, 30, 60, 52, 0, -30], [6494, -2618, 0, -4518, 3247, 0, 0, -359, 1900, 0, 2259, -2259], [-4053, -963, 0, -1172, 0, 0, 0, 209, 418, 0, 1172, -963], [0, 0, -3940, 0, 0, -559, -3940, 0, 72, 0, 0, -72], [0, 84, 0, 0, 0, 370, 3472, -84, 84, -185, 0, -168], [-133, -1463, 0, -1463, 133, 0, 0, 1463, 4402, 0, 2926, 1476], [0, -1287, -688, 0, 0, 583, 0, 1287, 2574, 583, 0, -1287], [-8796, 536, 0, 2522, -8796, 0, 0, 536, -993, 0, 0, 993], [0, 0, 1744, 0, 0, -564, 1744, 0, 2037, 0, 0, -2037], [3033, -2303, 0, 408, 6066, 0, 0, -1895, 0, 0, 408, 2303], [6589, -585, 0, -585, -6589, 0, 0, 585, 1934, 0, 1170, 764], [0, -744, 0, 0, -10687, 0, 0, 357, 744, 0, 1101, 387], [0, -468, 368, 0, 0, -386, 0, 468, 936, -386, 0, -468], [3633, -1554, 0, 1730, 3633, 0, 0, -1554, -1642, 0, 0, 1642], [4878, -1434, 0, 1294, 0, 0, 0, -2728, -5456, 0, -1294, -1434], [0, 0, 2796, 0, 0, -80, 2796, 0, 87, 0, 0, -87], [9901, 1305, 0, -2114, 19802, 0, 0, -809, 0, 0, -2114, -1305], [0, 2073, -2848, 0, 0, 0, 1424, -2073, -2073, 1998, 0, 0], [-33216, -1039, 0, 352, -16608, 0, 0, -1215, -1391, 0, -176, 176], [0, -435, 2408, 0, 0, -181, -4816, 435, 0, 181, 0, 435], [0, 1401, 0, 0, 0, -356, 5596, -1401, 1401, 178, 0, -2802], [2634, -84, 0, 72, 5268, 0, 0, -12, 0, 0, 72, 84], [0, -700, -9560, 0, 0, 11, 9560, 700, 350, -22, 0, 350], [0, 907, 0, 0, -22498, 0, 0, -11, -907, 0, -918, -896], [0, 881, -11008, 0, 0, 57, 0, -881, -1762, 57, 0, 881], [4042, 2708, 0, 4418, 2021, 0, 0, 499, -1710, 0, -2209, 2209], [0, -138, 4024, 0, 0, 742, -8048, 138, 0, -742, 0, 138], [3851, -2334, 0, -1753, 7702, 0, 0, -4087, 0, 0, -1753, 2334], [0, -4155, 1584, 0, 0, 0, -792, 4155, 4155, -1394, 0, 0], [8140, 1416, 0, 1416, -8140, 0, 0, -1416, -3541, 0, -2832, -709], [0, -2638, 5800, 0, 0, -300, -5800, 2638, 1319, 600, 0, 1319], [9436, 1862, 0, -1224, 9436, 0, 0, 1862, 1543, 0, 0, -1543], [0, 1800, 540, 0, 0, -654, -1080, -1800, 0, 654, 0, -1800], [3267, -4110, 0, -6215, 0, 0, 0, 2105, 4210, 0, 6215, -4110], [0, 0, 1224, 0, 0, 964, 1224, 0, -3303, 0, 0, 3303], [0, -908, 0, 0, -711, 0, 0, 905, 908, 0, 1813, 3], [0, 1450, 4340, 0, 0, -328, 0, -1450, -2900, -328, 0, 1450], [13599, 961, 0, 3701, 13599, 0, 0, 961, -1370, 0, 0, 1370], [0, 0, -5620, 0, 0, -358, -5620, 0, -2523, 0, 0, 2523], [-21977, -893, 0, 1582, -43954, 0, 0, 689, 0, 0, 1582, 893], [0, 4428, 2416, 0, 0, 0, -1208, -4428, -4428, -1093, 0, 0], [0, -506, -7976, 0, 0, 706, 7976, 506, 253, -1412, 0, 253], [0, -296, -892, 0, 0, -1012, 0, 296, 592, -1012, 0, -296], [821, 2373, 0, 4452, 0, 0, 0, -2079, -4158, 0, -4452, 2373], [0, 0, 4264, 0, 0, -1287, 4264, 0, -558, 0, 0, 558], [0, 1825, 0, 0, 0, 4036, 6712, -1825, 1825, -2018, 0, -3650], [0, -1119, 1992, 0, 0, 0, -996, 1119, 1119, 3898, 0, 0], [0, -3028, 0, 0, -17901, 0, 0, 930, 3028, 0, 3958, 2098], [13066, 4336, 0, 2666, 6533, 0, 0, 3003, 1670, 0, -1333, 1333]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_4_g_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_21_4_g_a(:prec:=12)
    chi := MakeCharacter_21_g();
    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_21_4_g_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_21_4_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;