
// Make newform 136.4.a.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_136_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_136_4_a_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_136_4_a_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 := [8, -20, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 2, 0], [-14, -1, 1]];
    Rf_basisdens := [1, 1, 2];
    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_136_a();"
function MakeCharacter_136_a()
    N := 136;
    order := 1;
    char_gens := [103, 69, 105];
    v := [1, 1, 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;

function MakeCharacter_136_a_Hecke(Kf)
    return MakeCharacter_136_a();
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, 0], [-1, 0, 1], [-3, 1, -2], [-2, -3, -1], [-27, 2, 1], [-15, 3, 2], [-17, 0, 0], [-72, 2, 6], [-78, -1, 1], [-13, 15, 18], [-8, -25, 3], [55, 27, 38], [64, 14, -40], [-20, -26, -78], [22, 30, 52], [142, -48, 12], [-8, -42, 86], [183, -25, 114], [-282, 102, 60], [136, -51, -59], [266, 56, -96], [406, 23, -43], [-424, -22, -102], [-827, 43, -18], [618, -152, -92], [-1299, -57, -30], [160, 60, -228], [193, -38, 145], [-467, 117, -78], [-732, -42, -80], [592, 198, -58], [-439, -18, -339], [-1021, -31, 70], [-673, -190, -177], [-114, -144, 384], [244, -54, -194], [1072, -146, -328], [-1157, 144, -167], [1482, 103, 133], [-53, 83, -106], [-1016, 162, -442], [2023, 171, -186], [-1642, 138, 440], [1192, -62, 164], [-2003, -207, -758], [778, 89, 451], [-525, 86, 19], [-2610, -40, -354], [-2341, -308, 189], [2373, 131, 378], [924, 318, 664], [-2614, -190, 628], [2020, -406, -120], [274, 538, -276], [585, -441, 50], [-452, -138, -1014], [-131, -775, -402], [-3706, -10, -396], [3547, 283, -98], [-5044, -438, 272], [-647, 222, 905], [-2902, -252, -48], [-814, -490, 508], [4142, 151, 1433], [612, -898, -12], [1693, 437, 1470], [-312, -558, 530], [4274, 308, -164], [7683, 28, 1341], [-5246, 436, 916], [3084, 1146, -688], [-2390, -212, 782], [1480, 371, 1627], [-4415, -349, -38], [3499, 1308, -775], [2808, 1138, 34], [-2119, 23, -1582], [4491, 599, -770], [-2612, -222, 736], [-5426, 424, 208], [9209, 208, 87], [3377, -1677, -1502], [-7622, 411, -115], [6559, 1045, 554], [1348, 409, -603], [-918, 562, 796], [-6150, 260, 336], [3655, 1285, 1210], [4890, 316, -1164], [-13164, 212, -328], [7384, 978, 1702], [-8946, -387, -765], [-40, -285, 707], [-8968, -598, -954], [-4897, 566, 615], [2952, -1953, -273], [-3822, -1324, -1696], [-4620, -750, -3224], [-5526, -38, 1788], [4871, 11, -466], [-15867, 86, 41], [8897, 1491, 642], [-1380, 1154, -722], [5312, -1102, -1296], [5673, 320, 31], [2087, -843, -1786], [13428, -946, -1170], [15372, 534, -80], [-4444, 84, -672], [2406, 864, 2708], [10756, -289, -1777], [-3822, -756, 1560], [-4096, -398, 64], [-1557, 340, -2443], [-5934, -608, -3462], [8854, 1068, 3956], [3427, -1916, 1945], [6954, 154, 3652], [10547, 239, 602], [2582, -1550, -260], [11692, 354, -1964], [-12866, 1136, 1288], [-3291, -1367, 3194], [583, 916, 413], [4857, 1298, -203], [-103, -77, 5066], [-4161, 1059, 34], [14780, -1535, 669], [-8014, 2426, 3252], [6748, 106, -4444], [-9960, -3486, 1038], [-21180, -477, -4597], [452, -251, 3313], [-14479, -2233, 642], [-8353, 461, -690], [12099, -351, -2002], [-11795, -1069, -470], [-10801, -142, -3617], [16388, 226, -1212], [-23026, -1896, 2792], [-24871, 1264, 2275], [-23143, 429, -990], [20222, -169, -11], [-7541, -3282, -301], [-28916, -30, -32], [-12630, 3573, 323], [5241, -2051, 3710], [-18956, -1362, 2212], [5236, 2226, 2238], [-11208, -2672, 568], [10817, -191, -4446], [4006, -4200, -1556], [-13166, -546, -1864], [-2358, -2043, -2457], [8777, 3902, 1121], [-7854, 1365, 1659], [14278, 4186, -1480], [-16864, 4262, 4552], [9358, 80, 8648], [19905, 989, 898], [-34105, -3150, 1655], [-1691, 3299, 1534], [31494, 1732, -958], [24644, -3174, 958], [88, 330, -120], [-2192, -59, 3845], [-9156, -2079, -7427], [-6567, -4499, 2850]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_136_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_136_4_a_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_136_4_a_b(:prec:=3)
    chi := MakeCharacter_136_a();
    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_136_4_a_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_136_4_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_136_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![28, -30, 4, 1]>],Snew);
    return Vf;
end function;