
// Make newform 40.6.c.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_40_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_40_c_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_40_6_c_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_40_6_c_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 := [9, 0, 969, 0, 460, 0, 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], [0, -160458, 0, -82106, 0, -7420, 0, -182], [-29412, 203676, -19752, 86042, -2232, 7372, -66, 176], [106884, 203676, -113112, 86042, -15432, 7372, -438, 176], [-390564, 203676, -259752, 86042, -17592, 7372, -282, 176], [29412, 646692, 19752, 295142, 2232, 26548, 66, 656], [29412, 991374, 19752, 454172, 2232, 40168, 66, 974], [-147060, -1339296, -98760, -594422, -11160, -51700, -330, -1244]];
    Rf_basisdens := [1, 639, 639, 639, 639, 639, 639, 639];
    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_40_c();"
function MakeCharacter_40_c()
    N := 40;
    order := 2;
    char_gens := [31, 21, 17];
    v := [2, 2, 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_40_c_Hecke();"
function MakeCharacter_40_c_Hecke(Kf)
    N := 40;
    order := 2;
    char_gens := [31, 21, 17];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 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 := 6;
    raw_aps := [[0, 0, 0, 0, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0, 0, 0], [1, 0, -1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 0], [-92, 0, -1, 0, 1, 0, 0, 0], [0, -2, -2, 0, 0, -1, 4, 1], [0, -40, -9, 0, 0, 1, 0, 2], [172, 8, -19, 0, -1, 0, 0, -4], [0, -22, 1, 0, 0, -10, -9, -4], [734, -12, 28, 6, -4, 0, 0, 6], [528, 0, -12, 12, 0, 0, 0, 0], [0, -410, 5, 0, 0, 26, -16, 1], [2950, 28, -39, -15, -16, 0, 0, -14], [0, -243, -54, 0, 0, -108, 54, 0], [0, 532, -81, 0, 0, 50, 9, 28], [0, -134, -70, 0, 0, -191, 36, -17], [11460, -8, -41, 48, 13, 0, 0, 4], [15482, 118, -269, -18, -8, 0, 0, -59], [0, -115, 214, 0, 0, 124, 10, -16], [-15704, -240, 494, 108, -2, 0, 0, 120], [0, 1124, -103, 0, 0, -311, 8, -40], [5408, -208, 608, -60, -28, 0, 0, 104], [0, 1669, 302, 0, 0, -236, -342, -176], [-5238, -156, 312, 126, -48, 0, 0, 78], [0, 1544, 107, 0, 0, 49, 488, 86], [84006, -356, 940, -30, -20, 0, 0, 178], [0, 770, -115, 0, 0, 106, 599, 164], [0, -3329, 1564, 0, 0, -144, 108, -320], [-67494, -78, 99, -96, 192, 0, 0, 39], [0, 4488, -548, 0, 0, 520, -288, 156], [0, -2594, -2133, 0, 0, -244, -129, 352], [-103716, 808, -1775, 168, -413, 0, 0, -404], [0, -4332, -1310, 0, 0, -620, -1800, -222], [106724, -816, 1791, 120, 129, 0, 0, 408], [22914, -554, 463, 834, 88, 0, 0, 277], [-136760, -1072, 3038, -168, -190, 0, 0, 536], [0, -702, 4531, 0, 0, -138, 904, -753], [0, -2781, -2992, 0, 0, 1356, -868, 696], [0, 13654, 153, 0, 0, -886, 819, -44], [0, -18398, 2547, 0, 0, 1514, 288, -149], [156668, -112, 537, -336, 79, 0, 0, 56], [8854, 180, -516, -330, 396, 0, 0, -90], [47712, 1312, -4328, 408, 640, 0, 0, -656], [0, 15112, -403, 0, 0, -433, 440, 82], [0, 930, -4644, 0, 0, -669, 1620, 1119], [-278296, 240, -1314, 204, 510, 0, 0, -120], [-81908, -1368, 5541, -1320, -801, 0, 0, 684], [0, 23810, -441, 0, 0, -4100, -1221, -976], [0, -6437, 1112, 0, 0, -116, 1188, -8], [359526, -2020, 3212, 1842, -4, 0, 0, 1010], [0, -3372, -4599, 0, 0, -3375, -504, 144], [-523648, 3008, -8232, 396, 316, 0, 0, -1504], [-354946, 988, -1043, -75, -1352, 0, 0, -494], [-31468, 560, 1443, -2496, -347, 0, 0, -280], [0, 29800, -6444, 0, 0, -316, 2232, 1672], [0, 21868, 1665, 0, 0, -5548, 4293, -584], [-89814, 2054, -6517, -642, 2024, 0, 0, -1027], [1080704, -1680, 2328, 1068, 804, 0, 0, 840], [0, 25774, 6633, 0, 0, 6842, -3504, -659], [-972474, 5928, -12951, -2397, 528, 0, 0, -2964], [0, 27575, -21168, 0, 0, -6500, 2172, 3368], [0, -29402, -773, 0, 0, -1956, 9648, 1693], [0, 23769, 242, 0, 0, 7740, 4022, 2304], [793896, -1792, 7862, -3792, 410, 0, 0, 896], [0, -66580, 7442, 0, 0, -3848, -7000, -3658], [0, -40034, 6516, 0, 0, -3619, -180, -2063], [868948, -1528, 371, 4176, -727, 0, 0, 764], [0, 17008, -1766, 0, 0, 9602, -4568, 1360], [0, -17435, -6490, 0, 0, -8456, -16794, -3752], [739966, 3932, -9364, 930, -1396, 0, 0, -1966], [0, -92384, -18558, 0, 0, 11006, -8784, 4156], [-1341128, -3712, 5938, 3120, 222, 0, 0, 1856], [0, -994, 20807, 0, 0, 12568, 3719, -904], [0, 72846, -26651, 0, 0, 6534, -4160, 5805], [1094740, 11648, -18685, -8520, -1915, 0, 0, -5824], [0, 27894, 3447, 0, 0, 19944, -9657, 1368], [-812334, -4970, 20407, -3510, -4472, 0, 0, 2485], [0, -55690, 24840, 0, 0, 12373, 12972, 101], [427394, -7684, 18168, 402, 640, 0, 0, 3842], [-679178, 308, -6655, -867, 6752, 0, 0, -154], [4364652, 8192, -20803, 144, 179, 0, 0, -4096], [500050, -14594, 30967, -282, 5800, 0, 0, 7297], [373472, -13120, 36240, 2388, -5828, 0, 0, 6560], [0, -189896, -5211, 0, 0, 2579, 21840, 5926], [-2710680, 10784, -21082, 816, -6694, 0, 0, -5392], [0, 212315, 17856, 0, 0, 14920, -14184, -3424], [-1089234, 4748, -8779, 2037, -5128, 0, 0, -2374], [0, 177412, 20268, 0, 0, 23762, -24024, -4106], [3066222, 13100, -40276, 8154, -628, 0, 0, -6550], [0, 205540, -15005, 0, 0, -20962, 14377, 1684], [0, 159663, -20376, 0, 0, -6576, -28440, -2928], [-1750048, -9872, 37208, -4440, -8088, 0, 0, 4936], [0, 19644, 70033, 0, 0, 45252, -59, -4968], [-4345964, -21528, 40787, 8472, 4561, 0, 0, 10764], [3656860, 11136, -6255, -13632, -7953, 0, 0, -5568], [0, -52110, -37267, 0, 0, -18286, 2079, 4212], [-1395650, -300, -24820, 18582, 6988, 0, 0, 150], [4420010, 3152, -17856, 2664, 7312, 0, 0, -1576], [0, 130883, -26996, 0, 0, -44036, 16360, -136], [-1404962, -11036, 41596, -17154, 3148, 0, 0, 5518], [0, 91719, 21916, 0, 0, 48972, 1864, 5784], [0, -57110, -38817, 0, 0, -15274, -43848, -4061], [0, -31173, -42624, 0, 0, -40212, 25308, 5544], [-4986666, -1276, 11297, -6315, -1792, 0, 0, 638], [-2741324, -10368, 24795, 4608, -3483, 0, 0, 5184], [0, -237200, -17618, 0, 0, 37610, 35632, 18172], [0, -340723, 42142, 0, 0, 4880, 29862, -1480], [0, 121008, -8908, 0, 0, 7280, -15768, 84], [2138776, -9696, -862, 24036, 1066, 0, 0, 4848], [4385766, -18256, 33785, 13839, -1984, 0, 0, 9128], [0, 42596, 167663, 0, 0, 39850, -23647, -30292], [0, -106630, -110480, 0, 0, -49793, -45812, 2975], [0, -451236, 59579, 0, 0, -2545, 18144, -8796], [3004628, -56584, 121043, 11184, 9233, 0, 0, 28292], [1681064, 35088, -78330, -8568, -822, 0, 0, -17544], [-980850, -14476, 20405, 11553, 4232, 0, 0, 7238], [0, -2495, -48258, 0, 0, 10256, 23046, 16312], [0, 317416, -47747, 0, 0, -32676, 25209, 8056], [0, 91870, 76716, 0, 0, 15593, 44388, -3347], [6544380, -35440, 97673, -18360, 9287, 0, 0, 17720], [-976750, 24722, -64057, 1596, 656, 0, 0, -12361], [0, 403464, 32471, 0, 0, 14661, -30520, -9666], [0, -270438, -92430, 0, 0, -77163, -18612, -669], [0, -336111, 17602, 0, 0, 86824, -15102, 10824], [8226828, -53912, 114901, 24912, -5033, 0, 0, 26956], [-9234742, 17698, -30325, -30672, 16752, 0, 0, -8849], [5885414, 18452, -47188, 11910, -10852, 0, 0, -9226], [1154656, -31312, 41584, 10740, 25956, 0, 0, 15656], [0, 439268, 73377, 0, 0, -5228, -7035, -17128], [0, 103010, 33237, 0, 0, 84268, -25656, 5075], [-5283828, -65360, 146005, 9792, 7603, 0, 0, 32680], [0, -17200, -62955, 0, 0, 99244, -35199, 25400], [9272432, 31952, -99388, 17616, 1892, 0, 0, -15976], [0, 222774, 124187, 0, 0, -17712, 44024, -19575], [-1163830, 7676, 32928, -24558, -27560, 0, 0, -3838], [6948098, -38304, 106968, -12864, 1656, 0, 0, 19152], [0, -294510, 141946, 0, 0, 42781, 87300, -2373], [0, -190307, -63106, 0, 0, 67244, -85870, 8896], [0, 469262, -155898, 0, 0, -2885, 5292, 31661], [-7998774, -124112, 263872, 36984, 9424, 0, 0, 62056], [5356836, 34192, -30401, -52560, -2519, 0, 0, -17096], [5565522, -26886, 56439, 13704, -2928, 0, 0, 13443], [0, -247586, -85655, 0, 0, -30934, -90941, -7244], [0, -316751, 53658, 0, 0, -100840, 50778, -20744], [8332538, -39802, 63299, -4362, 40568, 0, 0, 19901], [6436632, 18576, -13398, -36120, 3078, 0, 0, -9288], [0, 906498, -79334, 0, 0, -93999, -58580, -14649], [0, -14948, -95008, 0, 0, -26918, 60840, 25786], [-12298396, -34680, 85407, -10008, 11301, 0, 0, 17340], [0, -68424, -137457, 0, 0, -120330, 58653, 15156], [0, -1151006, -81551, 0, 0, -89548, 101632, 18727], [-790450, 21904, -151339, 57531, 39048, 0, 0, -10952], [0, -1195937, -111136, 0, 0, 8312, 207272, 65344], [0, 170896, -133299, 0, 0, 87404, -94023, 25336], [0, 445663, -300016, 0, 0, -16276, -101380, 36472], [1038816, -152928, 306144, 59868, 16308, 0, 0, 76464], [-1568424, 59712, -35502, -67596, -46182, 0, 0, -29856], [-12180850, -95444, 244757, -13179, 7032, 0, 0, 47722], [0, 407740, 113917, 0, 0, 126245, 80992, 18664], [4883182, -8108, -49908, 75846, -5668, 0, 0, 4054], [0, 614173, 105642, 0, 0, 25040, -171198, -50360], [0, 447228, -91620, 0, 0, -127194, -147816, -36678], [0, 1815302, -95195, 0, 0, 75454, 37171, 41564], [-5946252, 101992, -246341, 23016, -31655, 0, 0, -50996], [0, 140488, 424333, 0, 0, -26301, -70416, -104210], [0, -127180, -80195, 0, 0, 13544, -14859, 15776], [-15708704, 24768, 5736, -60240, -7416, 0, 0, -12384], [0, -202910, 127314, 0, 0, -29491, -231420, -77645]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_40_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_40_6_c_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_40_6_c_a(:prec:=8)
    chi := MakeCharacter_40_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_40_6_c_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_40_6_c_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_40_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;