
// Make newform 58.4.a.d in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_58_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_58_4_a_d();".
// 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_58_4_a_d();".
// 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 := [-54, -42, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-27, -4, 1]];
    Rf_basisdens := [1, 1, 3];
    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_58_a();"
function MakeCharacter_58_a()
    N := 58;
    order := 1;
    char_gens := [31];
    v := [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_58_a_Hecke(Kf)
    return MakeCharacter_58_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 := [[2, 0, 0], [1, -1, 0], [6, 2, 1], [8, 0, -4], [3, 1, -2], [-4, 8, 11], [-18, -12, -16], [-52, -8, 10], [-66, -6, 12], [29, 0, 0], [-25, -11, -36], [-10, -12, 38], [186, 4, -6], [11, -15, 2], [207, 33, 2], [276, -116, -27], [90, -86, 4], [134, -80, 40], [-88, 36, -72], [-18, 2, -4], [-178, 40, 30], [-691, -37, -38], [-150, 162, 12], [282, 68, 154], [14, 176, -34], [1098, -16, -66], [-154, 90, -176], [552, 116, 272], [2, -170, 109], [666, -48, 12], [1028, 132, 2], [300, 72, -90], [-246, 84, 154], [-874, -18, 4], [-132, 120, -345], [-460, 196, 112], [-142, -208, -42], [-1849, -215, -116], [2334, -126, -172], [510, 112, -36], [-1890, 22, -156], [-70, 2, -341], [-1668, 396, -78], [-1006, 380, 150], [-1026, -520, 60], [2588, 132, 200], [-1357, 509, 4], [1946, 46, -640], [3606, 254, 472], [1310, -84, 582], [-2112, -472, -309], [354, 174, 716], [2288, -396, 261], [-6087, 247, 124], [2538, -374, -307], [2331, 137, -652], [-1758, -36, -772], [-1087, -181, 216], [-2842, -256, -20], [-2772, 404, 543], [386, -414, 616], [2190, 248, -12], [-2917, 385, 814], [1092, 60, 186], [3014, 182, 1005], [-834, 864, 540], [-6133, -307, -436], [2570, 952, -1064], [2874, 1066, 1592], [-1948, 68, 157], [5586, 160, 384], [-459, -381, 1518], [-4336, -72, 282], [6422, -570, 699], [-1720, -716, -430], [2016, -1520, -680], [-5874, 540, 670], [2930, -118, 327], [8088, 656, 767], [2726, -1660, -432], [-1914, 2374, 264], [-2062, 1732, 932], [-954, 58, 108], [-4162, -360, 882], [-4462, 1022, -276], [3204, -936, -186], [-2742, -224, -756], [-262, -1104, -1488], [1230, -2488, -1832], [-7474, -1254, -1020], [4083, 513, 1754], [-1587, 1039, 1776], [836, 708, 2700], [2553, 2227, 670], [-694, -854, -308], [3933, -1209, 1016], [840, -1952, 29], [3138, 1606, -1549], [4712, -60, -640], [10682, -984, 470], [2348, -776, -1472], [-11562, 1480, -748], [6855, -1351, 772], [-4554, 2980, 872], [10232, -588, 448], [9458, 664, -140], [1206, -2538, 1272], [-1746, 434, -1501], [-1911, -1313, -2274], [-5362, 1112, 622], [1805, 191, -2456], [1610, -1770, -275], [-7446, 2468, 538], [8867, -1555, -2712], [-10, 146, 408], [1866, 1440, -948], [10064, -204, 564], [-12930, -1878, -248], [-10182, -2328, -4346], [1509, -3221, -880], [2966, -3240, -3088], [-17284, 512, 1881], [13146, -476, -952], [10944, -604, 2748], [-11212, 168, 2564], [19080, -1004, -1905], [17240, 1548, -2361], [-13170, 3730, 1508], [-32164, -492, 762], [-5950, 2184, -2702], [-5731, 547, -3824], [-1380, -2924, -1586], [-7744, 2168, -1130], [-1150, 684, -864], [3498, 760, 3684], [-8422, -3152, 732], [3390, 2892, 2670], [-6430, -630, -424], [-13482, 176, 1240], [-6702, -3836, 4214], [-13708, -1776, -3584], [-7980, 2504, 459], [-4564, 4500, 4986], [6879, -2239, -72], [6470, 1872, 2616], [3771, 3205, -850], [9278, -4824, -5108], [19590, 2150, -4363], [7157, 1135, 5226], [426, -5450, -3184], [12284, 2748, 5837], [-1482, -964, 928], [18536, -5740, -280], [12675, 2537, -4248], [-18940, -3224, 3662], [18441, -285, 1872], [27230, 218, -3292], [17634, -3672, 60], [-8830, 4032, 2076], [-558, 1686, 1317], [-20253, -1455, 3162], [3972, -2972, -7405], [-3697, -999, -1446], [-24180, 5568, 2738], [-8658, -2858, -7059], [4911, 6065, 1582], [-27766, 878, -2372], [-40906, 1756, -230]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_58_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_58_4_a_d();".
// 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_58_4_a_d(:prec:=3)
    chi := MakeCharacter_58_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_58_4_a_d();".
// 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_58_4_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_58_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![96, -41, -2, 1]>],Snew);
    return Vf;
end function;