
// Make newform 36.5.d.b in Magma, downloaded from the LMFDB on 21 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_36_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_36_d_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_36_5_d_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_36_5_d_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 := [9, 3, 4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 0], [1, 8, -4, 2], [8, -4, 4, 0]];
    Rf_basisdens := [1, 1, 1, 1];
    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_36_d();"
function MakeCharacter_36_d()
    N := 36;
    order := 2;
    char_gens := [19, 29];
    v := [1, 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_36_d_Hecke();"
function MakeCharacter_36_d_Hecke(Kf)
    N := 36;
    order := 2;
    char_gens := [19, 29];
    char_values := [[-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 := 5;
    raw_aps := [[-2, 1, 0, 0], [0, 0, 0, 0], [-4, -4, 2, 2], [-5, 10, 3, 1], [-9, 18, 7, 1], [86, -24, 12, 12], [146, 8, -4, -4], [-45, 90, 3, 21], [-12, 24, -28, 20], [-208, -28, 14, 14], [77, -154, 21, -49], [-1078, -48, 24, 24], [-166, 56, -28, -28], [145, -290, -159, 7], [-66, 132, -50, 58], [-1312, 68, -34, -34], [309, -618, -139, -85], [818, 480, -240, -240], [-415, 830, 33, 191], [-384, 768, 48, 168], [2738, -1056, 528, 528], [-467, 934, -171, 319], [-999, 1998, 553, 223], [6662, -784, 392, 392], [5210, 1104, -552, -552], [-6400, -1084, 542, 542], [233, -466, -159, -37], [-267, 534, -939, 603], [-9106, 600, -300, -300], [2102, 2480, -1240, -1240], [399, -798, -1305, 453], [2799, -5598, -913, -943], [25874, -856, 428, 428], [1487, -2974, 591, -1039], [-37300, -868, 434, 434], [-1265, 2530, 2007, -371], [4874, 336, -168, -168], [-3453, 6906, -237, 1845], [1434, -2868, -2470, 518], [15308, -1828, 914, 914], [363, -726, 2091, -1227], [8174, 1368, -684, -684], [-3072, 6144, 448, 1312], [4370, -3552, 1776, 1776], [-952, 5588, -2794, -2794], [5469, -10938, -459, -2505], [-1555, 3110, -2259, 1907], [-701, 1402, 2427, -863], [3225, -6450, 841, -2033], [-31018, 6312, -3156, -3156], [-6010, -5968, 2984, 2984], [3144, -6288, 1640, -2392], [-36694, -2544, 1272, 1272], [-909, 1818, 3, 453], [-35842, 6656, -3328, -3328], [-2286, 4572, 1810, 238], [53408, 3716, -1858, -1858], [-4713, 9426, -1713, 3213], [18350, -1128, 564, 564], [15470, 9728, -4864, -4864], [-5629, 11258, 3843, 893], [55760, -11164, 5582, 5582], [-6843, 13686, 7173, -165], [-9462, 18924, 6890, 1286], [96866, 8544, -4272, -4272], [79208, -1900, 950, 950], [-989, 1978, 195, 397], [-94270, -14880, 7440, 7440], [-17493, 34986, 9083, 4205], [-10414, 12768, -6384, -6384], [-197002, 1328, -664, -664], [15240, -30480, 3384, -9312], [14761, -29522, -11535, -1613], [46274, 10752, -5376, -5376], [16425, -32850, -7911, -4257], [-13608, 27216, -10792, 12200], [-136612, 17852, -8926, -8926], [54146, 16512, -8256, -8256], [-73030, -9256, 4628, 4628], [-153958, 20208, -10104, -10104], [-6687, 13374, -3247, 4967], [-27442, -6504, 3252, 3252], [-15222, 30444, 5754, 4734], [51266, -12000, 6000, 6000], [16357, -32714, -7971, -4193], [12147, -24294, 11971, -12059], [-115486, 19112, -9556, -9556], [105866, -35376, 17688, 17688], [143900, -1732, 866, 866], [-30577, 61154, 17031, 6773], [-6723, 13446, 4269, 1227], [-2274, 4548, -17138, 9706], [-27093, 54186, 5619, 10737], [-675, 1350, 8765, -4045], [22469, -44938, -2619, -9925], [-2268, 4536, -12396, 7332], [75128, 33716, -16858, -16858], [245162, 9752, -4876, -4876], [-6747, 13494, -14955, 10851], [-59650, 33144, -16572, -16572], [-3185, 6370, 14847, -5831], [-8764, -18676, 9338, 9338], [13401, -26802, -6295, -3553], [283130, 9080, -4540, -4540], [-4177, 8354, 5967, -895], [-196126, 20832, -10416, -10416], [-42339, 84678, 21181, 10579], [85430, 14768, -7384, -7384], [49848, -99696, -7864, -20992], [339074, -21696, 10848, 10848], [7055, -14110, 14631, -10843], [-49390, 15840, -7920, -7920], [153662, -70240, 35120, 35120], [41575, -83150, 16935, -29255], [-43065, 86130, 24207, 9429], [288242, -65656, 32828, 32828], [-19633, 39266, -9345, 14489], [4560, -9120, 14016, -9288], [-23404, 91820, -45910, -45910], [-34041, 68082, -17369, 25705], [77738, 32016, -16008, -16008], [471122, -55104, 27552, 27552], [-64972, 21164, -10582, -10582], [-42285, 84570, 13443, 14421], [3679, -7358, -31377, 13849], [16904, -67564, 33782, 33782], [-430858, 35112, -17556, -17556], [54990, -109980, 25566, -40278], [4735, -9470, 36423, -20579], [54950, -47352, 23676, 23676], [-71715, 143430, 50301, 10707], [23286, -46572, -28522, 2618], [22685, -45370, -31611, 4463], [-71818, -792, 396, 396], [-335182, 12200, -6100, -6100], [-526846, -36096, 18048, 18048], [-569608, 17972, -8986, -8986], [52031, -104062, -44337, -3847], [-95836, 72140, -36070, -36070], [321842, 38888, -19444, -19444], [1017, -2034, -5175, 2079], [141992, 100628, -50314, -50314], [73835, -147670, 33747, -53791], [42225, -84450, -39663, -1281], [-427282, -3816, 1908, 1908], [-43404, 86808, 22852, 10276], [-73414, 98160, -49080, -49080], [-175942, 37880, -18940, -18940], [-1231, 2462, 24417, -11593], [-21144, 42288, -48984, 35064], [972194, 50304, -25152, -25152], [-621682, -46048, 23024, 23024], [-106137, 212274, 14007, 46065], [-114114, 228228, 12734, 50690], [-30755, 61510, 4173, 13291], [-37896, 75792, 73816, -17960], [-73533, 147066, 18219, 27657], [-633718, -76168, 38084, 38084], [153938, -50880, 25440, 25440], [1159628, 2588, -1294, -1294], [84123, -168246, -29093, -27515], [-663478, -137512, 68756, 68756], [-20071, 40142, -50991, 35531], [-64893, 129786, 59763, 2565], [82058, 175736, -87868, -87868], [177486, -354972, -91250, -43118], [85737, -171474, -54639, -15549], [-1125118, -960, 480, 480]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_36_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_36_5_d_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_36_5_d_b(:prec:=4)
    chi := MakeCharacter_36_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_36_5_d_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_36_5_d_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_36_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<5,R![-796, 12, 1]>],Snew);
    return Vf;
end function;