
// Make newform 21.8.e.a in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_e_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_8_e_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_8_e_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 := [2868273, -663492, 698554, -70480, 36131, -1072, 362, -4, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [287577, -28707, 29038, -663, 334, -3, 1, 0], [-797319, -1251, 1486, -471, 238, -3, 1, 0], [-851625, 88515, -89182, 1335, -670, 3, -1, 0], [-444825, -3032736, 401365, -272324, 7135, -2882, 28, -8], [-4290009840, 1508080917, -419640535, 25170582, -3380155, 191574, -6055, 477], [1509692976, -633490725, 159089943, -24402118, 1963707, -214630, 6279, -541], [119000043, 796868436, -106459397, 71969297, -2136137, 804941, -8876, 2357]];
    Rf_basisdens := [1, 6912, 6912, 6912, 3789072, 60625152, 60625152, 8660736];
    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_e();"
function MakeCharacter_21_e()
    N := 21;
    order := 3;
    char_gens := [8, 10];
    v := [3, 2];
    // 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_e_Hecke();"
function MakeCharacter_21_e_Hecke(Kf)
    N := 21;
    order := 3;
    char_gens := [8, 10];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, -1, 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 := 8;
    raw_aps := [[0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 27, 0, 0, 0], [-46, 0, -2, 0, -46, -1, -9, -2], [161, -35, 0, 7, 273, 0, 14, 0], [0, 83, 0, 41, -1274, -41, -83, 10], [-883, -203, -74, 15, 0, 0, 0, 0], [0, -782, 0, 20, -3026, -20, 782, -94], [17127, 0, -236, 0, 17127, 155, 731, -236], [37038, 0, 34, 0, 37038, -204, -1762, 34], [-19314, 7115, -496, -27, 0, 0, 0, 0], [0, -3452, 0, -370, -73113, 370, 3452, 12], [115677, 0, 1290, 0, 115677, 1223, -16319, 1290], [-162486, -14150, 112, 666, 0, 0, 0, 0], [-14283, 53895, 1384, 2407, 0, 0, 0, 0], [-195988, 0, 7518, 0, -195988, 1652, 12602, 7518], [0, 54619, 0, -3565, -677168, 3565, -54619, -2194], [0, 17311, 0, -7985, 857270, 7985, -17311, -8856], [-91390, 0, -14124, 0, -91390, 1972, 76820, -14124], [0, -37609, 0, 7699, -1065341, -7699, 37609, 8976], [-41400, -215574, -10338, 10548, 0, 0, 0, 0], [0, 262693, 0, 24483, -42643, -24483, -262693, 6074], [2929975, 0, 25296, 0, 2929975, -15864, 425490, 25296], [-3091288, 280143, -11560, 8591, 0, 0, 0, 0], [-1196496, 0, -1276, 0, -1196496, 6714, -175206, -1276], [-5700578, -32665, 2590, -8227, 0, 0, 0, 0], [0, -393324, 0, 42820, -3064570, -42820, 393324, 54892], [3800275, 0, 64476, 0, 3800275, -3933, 576879, 64476], [-2232654, 0, 60272, 0, -2232654, -77883, -370395, 60272], [0, 74317, 0, 24383, -5959695, -24383, -74317, -22962], [-9683128, -765052, 31630, 63026, 0, 0, 0, 0], [-11631809, 1287422, 76816, -23500, 0, 0, 0, 0], [1501922, 0, -290174, 0, 1501922, 30305, 820263, -290174], [0, 1748830, 0, 2754, 8431488, -2754, -1748830, 179344], [10796413, 108239, 171712, -188557, 0, 0, 0, 0], [9152856, 0, -133448, 0, 9152856, 463626, -1725658, -133448], [0, 815145, 0, -203229, 26479742, 203229, -815145, 20844], [0, 1104746, 0, 54974, -28702734, -54974, -1104746, -213316], [-34594660, 0, -259968, 0, -34594660, -256916, 1240376, -259968], [55009340, -308266, 231466, -72856, 0, 0, 0, 0], [-46630338, 0, 498486, 0, -46630338, 608106, 1077472, 498486], [0, 4784556, 0, 227070, 42244812, -227070, -4784556, 161894], [1804921, 1295977, -40386, 684239, 0, 0, 0, 0], [25407892, 0, 315282, 0, 25407892, -218942, 5054760, 315282], [0, 3540894, 0, -579762, -20532625, 579762, -3540894, 265920], [-83688914, -4458520, -230850, -54470, 0, 0, 0, 0], [0, -3071912, 0, -12796, -31120068, 12796, 3071912, 88476], [-16701508, -715958, 378508, -954282, 0, 0, 0, 0], [-39890270, 12092197, -274068, 83951, 0, 0, 0, 0], [0, -2926655, 0, 1246479, 88164030, -1246479, 2926655, 479410], [4191823, 0, 605010, 0, 4191823, 1465293, 692475, 605010], [166777084, 0, 191114, 0, 166777084, -636110, -452692, 191114], [-165975314, 8359362, -500504, 44086, 0, 0, 0, 0], [0, -16700103, 0, 429111, -67837018, -429111, 16700103, -276654], [-10172690, -6022111, 106884, -203075, 0, 0, 0, 0], [-207028016, 0, -324578, 0, -207028016, -656576, 11412550, -324578], [0, -12951850, 0, -1311754, -13497332, 1311754, 12951850, -1656812], [0, 8980795, 0, -2903863, -18816188, 2903863, -8980795, 863768], [-106419694, 0, 200892, 0, -106419694, -429089, -23848195, 200892], [0, -1886653, 0, 105121, -65381315, -105121, 1886653, 833178], [-228135422, 15269980, 541646, -2396810, 0, 0, 0, 0], [0, -14603803, 0, -3452375, -146419511, 3452375, 14603803, 1788804], [357397842, 9219511, 2479528, -572487, 0, 0, 0, 0], [-292526653, -8372909, 507520, 3500439, 0, 0, 0, 0], [0, 26137258, 0, 1264966, -268977634, -1264966, -26137258, 1121100], [-140997991, 0, -933524, 0, -140997991, -397994, -38673630, -933524], [115911686, 0, -5705952, 0, 115911686, -3141169, 650129, -5705952], [-332200267, 0, 2214352, 0, -332200267, 425125, 9921045, 2214352], [-496981127, 15313096, -1830852, -3590548, 0, 0, 0, 0], [0, -6818382, 0, 6955496, -209315258, -6955496, 6818382, 2223378], [635249582, -33865916, 191164, -3500952, 0, 0, 0, 0], [0, -1237096, 0, -2999362, 271192612, 2999362, 1237096, -467942], [-614285344, 0, -3255544, 0, -614285344, 284858, -7623542, -3255544], [0, 2383276, 0, -5692338, -234545419, 5692338, -2383276, -3503776], [385736141, 0, -857618, 0, 385736141, 3510259, 85906413, -857618], [-153887537, 63441539, 41968, 3878027, 0, 0, 0, 0], [-232001334, 0, 1885798, 0, -232001334, -3066318, -28270900, 1885798], [0, -37946390, 0, -4440012, 266363592, 4440012, 37946390, 7641898], [-504681699, 0, 1521678, 0, -504681699, -7553425, -2934791, 1521678], [-224525982, 0, 2628106, 0, -224525982, -1906968, -52372162, 2628106], [0, 2788578, 0, 4725246, -586173043, -4725246, -2788578, 9025668], [-415317914, -59795058, -1278848, 14922622, 0, 0, 0, 0], [-1102338847, 86196099, 4109358, -1105659, 0, 0, 0, 0], [0, -35763788, 0, -3127942, -801689228, 3127942, 35763788, -2814910], [518497547, -26966435, -4847942, -561081, 0, 0, 0, 0], [219304666, 0, 3614020, 0, 219304666, -724977, -4071011, 3614020], [-939651106, 0, -7037676, 0, -939651106, 6283283, -110715769, -7037676], [482802096, 37746396, 13486602, 835626, 0, 0, 0, 0], [952154339, 0, 8664012, 0, 952154339, 1192438, 92956922, 8664012], [81213664, 54353710, -13485112, -7390526, 0, 0, 0, 0], [1292982055, 75270979, -9946584, -6919621, 0, 0, 0, 0], [-225579068, 0, 13709606, 0, -225579068, 15994234, 4249508, 13709606], [0, 38703144, 0, 1436758, 472586186, -1436758, -38703144, 14711230], [0, 164633180, 0, -24610870, 902913465, 24610870, -164633180, 6692708], [-842311752, -240507893, -16146866, -2066415, 0, 0, 0, 0], [847233779, 0, -21484896, 0, 847233779, -4895075, 31839785, -21484896], [-2873396010, 98334150, 15487320, 9878370, 0, 0, 0, 0], [259408210, 0, -16881074, 0, 259408210, -6506375, 44450317, -16881074], [0, 75849142, 0, -1718986, 2609483008, 1718986, -75849142, 19354400], [-2006947363, 0, 1544592, 0, -2006947363, 774805, -47183143, 1544592], [1690293119, 0, 22087246, 0, 1690293119, 1400695, -136435107, 22087246], [435057968, -77321588, 31419484, -7194396, 0, 0, 0, 0], [0, 116292245, 0, -14142269, -2556120580, 14142269, -116292245, 11101076], [0, 71468045, 0, 27437193, 2300770620, -27437193, -71468045, -20273628], [-1065660182, 0, -14255628, 0, -1065660182, -2742974, 91726794, -14255628], [0, 222084789, 0, 22516921, 1791651721, -22516921, -222084789, -13146772], [0, -316081216, 0, -23062112, 726253279, 23062112, 316081216, -24278304], [-676612776, -161676079, -17010178, 15755415, 0, 0, 0, 0], [-21170714, 0, 17256404, 0, -21170714, 3582706, -88353766, 17256404], [0, -51397250, 0, 2672458, 2022981644, -2672458, 51397250, 24888420], [-569603201, -15440620, 4628392, 37217564, 0, 0, 0, 0], [2293847987, 0, 5565088, 0, 2293847987, 9744850, 297569544, 5565088], [0, -51973582, 0, 11244154, -1064467670, -11244154, 51973582, -22906884], [2546770258, 315887448, 5156968, 31008232, 0, 0, 0, 0], [0, -471311113, 0, 40265587, -51962729, -40265587, 471311113, 7657260], [1811303670, 377412549, -7502696, -38206157, 0, 0, 0, 0], [0, -138260360, 0, -37811310, 3463159542, 37811310, 138260360, -17420306], [111916485, -207376365, 32352664, 37687687, 0, 0, 0, 0], [0, -22530816, 0, -23235420, 3373819446, 23235420, 22530816, -56124064], [-3110321532, 0, 10543024, 0, -3110321532, 27735423, -84098419, 10543024], [2439343418, -4748826, -51748774, 1815152, 0, 0, 0, 0], [0, 54038063, 0, 21078797, -4293839205, -21078797, -54038063, -35386], [1652858347, -27768602, 17175960, 12281486, 0, 0, 0, 0], [6618021338, 0, -35304636, 0, 6618021338, 51025637, 105590951, -35304636], [0, -602411759, 0, 24758621, 4209355774, -24758621, 602411759, 42050996], [2410572817, 0, 45578976, 0, 2410572817, -9355035, -283316955, 45578976], [-6456199404, -165402195, 42439086, -78092355, 0, 0, 0, 0], [-7282187262, 0, -5399688, 0, -7282187262, 13057016, 427170976, -5399688], [-2316289454, 0, -26871796, 0, -2316289454, -30222584, -965053036, -26871796], [-4600461567, 10357362, -79907876, -37452896, 0, 0, 0, 0], [2710929135, 0, -39228054, 0, 2710929135, 26172023, -617060063, -39228054], [0, -1031169815, 0, -28340455, 1913771757, 28340455, 1031169815, 50862444], [971174974, 370427304, 53116360, 53912632, 0, 0, 0, 0], [4977507717, 0, -3073064, 0, 4977507717, 69132812, 344830826, -3073064], [1278772566, -516161532, -59289692, -85851236, 0, 0, 0, 0], [2780882634, 0, 89594542, 0, 2780882634, -76846272, 312097838, 89594542], [-4023859477, -441121578, -99322224, -7235874, 0, 0, 0, 0], [0, -153528132, 0, -5639808, 6181958406, 5639808, 153528132, 63249392], [0, 510543722, 0, 2594146, -1629986564, -2594146, -510543722, 98592744], [7630264556, -458689645, 68100778, 40266035, 0, 0, 0, 0], [0, -1481911100, 0, -35577886, -9434885720, 35577886, 1481911100, -38431078], [-10083760672, -803023986, 2922444, 14761182, 0, 0, 0, 0], [10179424998, 0, -31537158, 0, 10179424998, -20689623, -580849943, -31537158], [0, -341394248, 0, -44348224, 123785808, 44348224, 341394248, -148764768], [-5702789328, -363610089, -61054692, 102779643, 0, 0, 0, 0], [0, 499582683, 0, 52916881, 13280452549, -52916881, -499582683, -24158506], [12134890872, -334034860, 7457660, 79182684, 0, 0, 0, 0], [-13247335393, -858721239, 49660518, -20256297, 0, 0, 0, 0], [0, -750401842, 0, 96551042, 8736740536, -96551042, 750401842, -59714732], [-7591304468, 0, 912288, 0, -7591304468, -2039958, -412834374, 912288], [7569323938, 0, 43240016, 0, 7569323938, 156187180, -221521444, 43240016], [8565841946, 0, 205061064, 0, 8565841946, -69286406, 309102674, 205061064], [2048024226, 961590240, -95951238, -77345466, 0, 0, 0, 0], [-3028417729, -202470741, 85840056, 138725451, 0, 0, 0, 0], [-22438778342, 0, 26823304, 0, -22438778342, 76509202, 815611078, 26823304], [0, 925176825, 0, 164530197, 11680827143, -164530197, -925176825, -85284480], [-2379091162, 247543428, 32250950, -222950290, 0, 0, 0, 0], [12416167801, 0, 74528712, 0, 12416167801, 76691061, -850868739, 74528712], [3133214436, 0, 196997718, 0, 3133214436, 95695968, -440486342, 196997718], [-4008293417, 1271865068, -34869896, -137845324, 0, 0, 0, 0], [0, 450435657, 0, -90301567, 15036348208, 90301567, -450435657, -203558094], [-6838924314, 0, 153621180, 0, -6838924314, 29790258, 1450312150, 153621180], [-9425601022, -873107180, -80203146, 149519138, 0, 0, 0, 0], [14479259429, -2255462366, 64425808, -82713420, 0, 0, 0, 0], [-12268851962, 0, -15615160, 0, -12268851962, -184107443, -2146192363, -15615160], [0, 2419415596, 0, 170331652, -5537707066, -170331652, -2419415596, -59019264], [0, 1984508794, 0, 71215548, -21587159706, -71215548, -1984508794, -8702570], [0, 591722568, 0, 139936266, 18847905983, -139936266, -591722568, 123694596], [0, 37391579, 0, 96551161, -24880872773, -96551161, -37391579, 261826890]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_21_8_e_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_8_e_a(:prec:=8)
    chi := MakeCharacter_21_e();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_8_e_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_8_e_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_21_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<2,R![103063104, -2314656, 3158496, 90072, 83256, 762, 307, -1, 1]>],Snew);
    return Vf;
end function;