
// Make newform 21.7.f.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_f_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_7_f_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_21_7_f_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 := [38787984, 3107772, 1563109, -92833, 38792, -787, 212, -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, 1, 0, 0, 0, 0, 0, 0], [22182278913510768, -2109023702500351, 780366646056751, -65749585575908, 4245938445433, -303603349712, 18232571539, -1585013359], [-29614389599556, -1592248206780, -177516832405, -305021724904, 2053828205, -1444380025, -15616321, -8019055], [699314583065439780, 313020438506182988, 19609334703803473, 2723907868150765, 219959885092027, 22563530233273, 1000103862583, 27322708193], [-963541759742958, -7877491417656, -878247070906, -1531993215028, 10161113066, -7145928130, 224426993, -39673486], [-852798172253616144, 130709559191228591, -30001211870054633, 2527744179224764, -218751249599021, 11672037067696, -700951590437, 59472187739], [14206534053079428, -114813811933007845, -4492583429983241, 4754608612331077, -171439510285889, 31940434175641, 436984015237, 226304020214]];
    Rf_basisdens := [1, 1, 23802631772961636, 11465622241311, 11901315886480818, 11465622241311, 11901315886480818, 11901315886480818];
    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_f();"
function MakeCharacter_21_f()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [6, 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_21_f_Hecke();"
function MakeCharacter_21_f_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 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 := 7;
    raw_aps := [[0, -1, -1, 0, 0, 0, 0, 0], [9, 0, 9, 0, 0, 0, 0, 0], [-6, 0, 3, 0, -1, -2, 1, -1], [77, 5, 35, -10, -1, 3, -2, 2], [-265, -1, 265, 2, -2, -9, 9, -1], [864, -11, -1728, 14, -17, 5, -10, 0], [626, -124, 626, -70, 0, 24, 24, -54], [-3908, 360, 1954, -720, -27, 2, -1, -27], [0, 1302, -4144, 0, 4, 0, 54, -4], [3949, -61, 0, 466, -61, 183, 0, -122], [3401, 78, 3401, 122, 0, -282, -282, -44], [0, 2756, -11970, 0, 71, 0, -475, -71], [6362, 424, -12724, -340, 256, 436, -872, 0], [-57678, 257, 0, 6060, 257, 1051, 0, 514], [13376, -1702, -6688, 3404, -770, -112, 56, -770], [-3055, -1947, 3055, 2096, -298, -2463, 2463, -149], [151147, -757, 151147, -1460, 0, -377, -377, 703], [-67056, -2144, 33528, 4288, 964, 104, -52, 964], [101116, 18853, -101116, -19020, 334, -1851, 1851, 167], [28372, 1706, 0, -7094, 1706, 1332, 0, 3412], [77984, -10023, 77984, -10218, 0, -887, -887, 195], [0, -51686, -127061, 0, 484, 0, 822, -484], [-73913, 49367, 147826, -25496, 1625, -4843, 9686, 0], [-296972, -3980, 148486, 7960, 78, -252, 126, 78], [135633, -217, -271266, 3574, -6931, 1627, -3254, 0], [26172, 18362, 26172, 10524, 0, -1150, -1150, 7838], [171248, 66020, -85624, -132040, 3809, -1102, 551, 3809], [0, 13174, 213257, 0, 221, 0, 13797, -221], [-461604, -60313, 461604, 66954, -13282, -9505, 9505, -6641], [207826, 752, 0, 79822, 752, -22470, 0, 1504], [710795, -7674, 0, 110874, -7674, 9108, 0, -15348], [-1998582, -45426, 999291, 90852, 11951, 10546, -5273, 11951], [-1095856, 63440, 1095856, -47140, -32600, 2616, -2616, -16300], [137218, 148075, -274436, -78448, 8821, -15639, 31278, 0], [0, -241524, -264490, 0, 2386, 0, -24222, -2386], [1669257, -57491, -1669257, 53058, 8866, 27665, -27665, 4433], [-2062460, -66168, -2062460, -72784, 0, -6568, -6568, 6616], [0, -53928, 2228260, 0, -13866, 0, -12534, 13866], [107108, -52238, -214216, 25454, 1330, 14284, -28568, 0], [-4321840, -139438, 2160920, 278876, -2588, 22508, -11254, -2588], [825638, 231894, -825638, -267262, 70736, -13506, 13506, 35368], [-225966, 556869, 451932, -285654, 14439, 34401, -68802, 0], [0, -210274, -2094508, 0, 22818, 0, 85752, -22818], [5283921, -395034, -5283921, 388056, 13956, -95698, 95698, 6978], [-1444958, -24058, 0, 98854, -24058, 47004, 0, -48116], [1284316, 118884, 1284316, 131484, 0, -33420, -33420, -12600], [-736836, 16226, 0, -690072, 16226, 37078, 0, 32452], [-1415129, 448061, 2830258, -217370, -13321, -71719, 143438, 0], [1463571, 204299, 1463571, 208218, 0, -109885, -109885, -3919], [-4576940, 370634, 2288470, -741268, 83381, -103274, 51637, 83381], [0, 811582, 410630, 0, -15712, 0, -12174, 15712], [7123984, -24598, 0, 135424, -24598, 8814, 0, -49196], [3599365, -112257, 3599365, -151526, 0, 97303, 97303, 39269], [153105, -1025613, -306210, 521472, -17331, -18447, 36894, 0], [-14207820, -50250, 7103910, 100500, -37132, 196660, -98330, -37132], [358646, 413726, -358646, -478312, 129172, 185958, -185958, 64586], [-2164993, 807407, -2164993, 818018, 0, -69333, -69333, -10611], [-14937270, 60822, 7468635, -121644, -82323, 113638, -56819, -82323], [-1421600, 350489, 1421600, -413442, 125906, 30393, -30393, 62953], [-5861240, -2680, 0, -1091546, -2680, -199614, 0, -5360], [3494250, -1134513, 3494250, -1097776, 0, -16253, -16253, -36737], [10845479, -126059, -21690958, 99494, -72929, 55633, -111266, 0], [-10582760, -582219, 21165520, 425328, -268437, -10841, 21682, 0], [708214, -256648, 708214, -229580, 0, 211240, 211240, -27068], [23076890, 424360, -11538445, -848720, -98912, -34304, 17152, -98912], [0, 396728, 23191769, 0, 34557, 0, -40569, -34557], [0, 1597182, -8992728, 0, -119949, 0, -224617, 119949], [30765075, 5678, 0, 310152, 5678, 531442, 0, 11356], [-19249138, 1359676, 19249138, -1285534, -148284, -597276, 597276, -74142], [-9203248, -1170604, 18406496, 371116, 428372, -78336, 156672, 0], [-7186320, 282332, -7186320, 531234, 0, 125432, 125432, -248902], [0, -2756384, 2376428, 0, -17920, 0, 14664, 17920], [-2473653, -586932, -2473653, -685886, 0, -16356, -16356, 98954], [0, 2006732, 3001498, 0, -116839, 0, 531075, 116839], [5710794, 8657, 0, 5450544, 8657, -505241, 0, 17314], [6863340, -1076502, -3431670, 2153004, 484838, -151904, 75952, 484838], [-12512866, -3005506, 12512866, 3104762, -198512, -257274, 257274, -99256], [638496, 2224458, -319248, -4448916, 63093, -78746, 39373, 63093], [0, 776706, 49763408, 0, 82852, 0, -843534, -82852], [-3356217, -2298012, -3356217, -1997752, 0, -287564, -287564, -300260], [-11344684, 80374, 22689368, -129040, 177706, -262670, 525340, 0], [-97266928, 122559, 0, 481182, 122559, -258189, 0, 245118], [74716214, 2022174, -74716214, -2000482, -43384, 229710, -229710, -21692], [-2984992, 11249721, 5969984, -5547942, -153837, -223867, 447734, 0], [46390754, -1652546, -23195377, 3305092, -291215, 1934206, -967103, -291215], [0, -6900586, -46089781, 0, -90513, 0, 348987, 90513], [-24748268, -509710, 0, 8585674, -509710, 39204, 0, -1019420], [0, -52488, -27552693, 0, -407016, 0, 470720, 407016], [-3858176, 2649872, 7716352, -1063256, -523360, -415936, 831872, 0], [7464470, -218649, 0, -1244532, -218649, 1559973, 0, -437298], [39182512, 2232274, -19591256, -4464548, 522078, 304032, -152016, 522078], [-17174766, -5863756, -17174766, -5102658, 0, 283808, 283808, -761098], [30816645, -3345130, -30816645, 3265866, 158528, -1752742, 1752742, 79264], [24208117, 487499, 0, 1586518, 487499, -92625, 0, 974998], [0, -5050682, -41170602, 0, 63313, 0, -840631, -63313], [30452802, 7668648, -60905604, -4352712, 1036776, 188256, -376512, 0], [-11404914, -5656452, 5702457, 11312904, -312943, 1338826, -669413, -312943], [-33757690, 6267962, -33757690, 5307836, 0, 96282, 96282, 960126], [107489600, -1204488, -53744800, 2408976, 1195995, 81630, -40815, 1195995], [0, 3105528, 23486686, 0, -461511, 0, 2120631, 461511], [5183342, 552036, 0, -6964284, 552036, -1312752, 0, 1104072], [-203108953, 2406471, 203108953, -2296990, -218962, 366123, -366123, -109481], [27553135, -912967, 27553135, -1698752, 0, 1455949, 1455949, 785785], [0, 5588960, 6296114, 0, 869748, 0, -2772780, -869748], [-28095236, 4890865, 28095236, -3662316, -2457098, -660039, 660039, -1228549], [-3599381, 3781872, -3599381, 2387256, 0, 965208, 965208, 1394616], [2807095, -24935479, -5614190, 12582766, -230053, 524249, -1048498, 0], [-98972552, 10501744, 49486276, -21003488, -483846, -420396, 210198, -483846], [-62211628, -8316176, 62211628, 8121032, 390288, 2052072, -2052072, 195144], [-14663665, -40168, 29327330, 615520, -1190872, -162144, 324288, 0], [204434562, 5729138, -102217281, -11458276, 649526, 1360728, -680364, 649526], [-87396512, 1154838, 87396512, -1983720, 1657764, 1074126, -1074126, 828882], [-25792580, -387934, 0, -28280648, -387934, 544998, 0, -775868], [-52859324, -1481883, -52859324, -1405244, 0, -1380551, -1380551, -76639], [40340651, -1410009, 0, -4449006, -1410009, 824475, 0, -2820018], [-154300624, 3113186, 154300624, -2430394, -1365584, -924726, 924726, -682792], [-38304788, -16384385, 76609576, 8443604, -502823, 596265, -1192530, 0], [-56536864, 10838690, -56536864, 13590296, 0, -123270, -123270, -2751606], [0, 11066904, -230618689, 0, -64331, 0, 2005287, 64331], [186589666, -327538, 0, -4504814, -327538, -1335864, 0, -655076], [56753494, 2261205, 56753494, 4581638, 0, 1041437, 1041437, -2320433], [-216981939, 1176392, 0, 17990364, 1176392, 929116, 0, 2352784], [-311841142, -10212550, 155920571, 20425100, 1549189, 4693934, -2346967, 1549189], [-38974741, 11719791, 38974741, -11756260, 72938, 350703, -350703, 36469], [101936380, -11600408, -50968190, 23200816, 530281, -2933766, 1466883, 530281], [85233811, -148039, 0, -12324800, -148039, -3327165, 0, -296078], [0, -17551248, 121803702, 0, -1089312, 0, -90160, 1089312], [-459901292, 10582780, 229950646, -21165560, 1117200, 181944, -90972, 1117200], [13844691, -40508944, -27689382, 21596830, -2684716, -543734, 1087468, 0], [214100588, 6712022, -107050294, -13424044, 2826575, 7060274, -3530137, 2826575], [423518494, 1219755, -423518494, -1097364, -244782, -1616613, 1616613, -122391], [370887508, -563590, 0, -10124792, -563590, 809430, 0, -1127180], [0, 3131014, -202211841, 0, 692734, 0, 835160, -692734], [-324322668, -1770430, 0, -19979652, -1770430, 1138306, 0, -3540860], [-136559880, -13296630, 68279940, 26593260, 2891540, 2251132, -1125566, 2891540], [-15987241, 8553682, 31974482, -6044680, 3535678, -3019994, 6039988, 0], [221646620, -2459326, 221646620, -1821652, 0, 1989306, 1989306, -637674], [139225236, -4824504, 139225236, -1600636, 0, 2697664, 2697664, -3223868], [208174319, -39570095, -416348638, 20031308, -492521, 1469083, -2938166, 0], [258868964, -23532156, -258868964, 23228378, 607556, -6290100, 6290100, 303778], [-356859132, -14473236, 713718264, 8617644, -2762052, -565848, 1131696, 0], [0, 11609366, 513727157, 0, -1294869, 0, 3869499, 1294869], [546532738, -7216016, -546532738, 4926348, 4579336, 655056, -655056, 2289668], [48195847, -761017, 0, 16725688, -761017, -4614231, 0, -1522034], [17096630, 4684629, 17096630, 3250902, 0, -80507, -80507, 1433727], [211976962, 28210286, -423953924, -10857416, -6495454, 1070594, -2141188, 0], [229434364, 27862127, -458868728, -15155714, 2449301, 3188243, -6376486, 0], [108227976, 6909092, 108227976, 6857124, 0, 4613756, 4613756, 51968], [148762556, -8577856, -74381278, 17155712, 2228054, 5470940, -2735470, 2228054], [0, -67234256, -201291142, 0, -718516, 0, -4804764, 718516], [0, -76372, 152168728, 0, 291158, 0, 2921598, -291158], [471666528, 24280416, -943333056, -14259342, 4238268, 2446554, -4893108, 0], [97705746, 1665749, 0, 15500232, 1665749, -778901, 0, 3331498], [-250170792, -32436864, 125085396, 64873728, -5834458, -898868, 449434, -5834458], [57097152, 29698243, -57097152, -25913832, -7568822, 3999059, -3999059, -3784411], [817082116, -656284, 0, 11593762, -656284, 4759122, 0, -1312568], [0, -72331390, 3673926, 0, -292351, 0, -7956475, 292351], [-208162444, 26103866, 104081222, -52207732, -5582636, 3649388, -1824694, -5582636], [16812809, 10973802, -33625618, -2600484, -5772834, 2503130, -5006260, 0], [-396187847, -23667727, -396187847, -26268092, 0, -3010511, -3010511, 2600365], [0, 17093456, -364248934, 0, -47604, 0, -8409804, 47604], [113076514, -758578, 0, 40405522, -758578, 2741208, 0, -1517156], [267981113, 4895340, 0, -42977562, 4895340, -762750, 0, 9790680], [693705730, 18578380, -346852865, -37156760, -7513263, -6325374, 3162687, -7513263], [-38215870, 13542856, 38215870, -14773408, 2461104, 6714696, -6714696, 1230552], [-272583522, -18750424, -272583522, -21340890, 0, 6508580, 6508580, 2590466], [-58605183, 1701530, 58605183, -2951766, 2500472, -9439162, 9439162, 1250236], [573083576, 32679897, 573083576, 31685302, 0, -6356863, -6356863, 994595]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_7_f_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_21_7_f_b(:prec:=8)
    chi := MakeCharacter_21_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 7));
    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_7_f_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_21_7_f_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<2,R![30470400, -5045280, 1950436, 129428, 39854, 818, 227, 5, 1]>],Snew);
    return Vf;
end function;