
// Make newform 58.4.a.c in Magma, downloaded from the LMFDB on 19 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_c();".
// 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_c();".
// 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 := [-6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [-1, 1], [-5, -6], [-8, 8], [-45, 3], [-25, -8], [-22, -16], [54, 4], [-14, 78], [29, 0], [33, -109], [20, -4], [152, 80], [-65, -53], [-257, 99], [-479, -52], [-90, -250], [514, 12], [-456, 20], [398, -34], [-428, -176], [-159, 361], [-914, 38], [-472, -72], [184, -100], [-56, 472], [890, -58], [-500, 276], [-997, 478], [-42, -680], [-274, -248], [546, 604], [252, 548], [-2390, -190], [-861, 344], [152, -380], [948, -596], [-195, -513], [958, -146], [-1614, -392], [2942, 362], [2737, -270], [4462, -160], [-1252, 1480], [-2070, -424], [-2608, -588], [2193, 571], [2182, -1206], [-4734, 122], [-2000, 1720], [-3049, 28], [3318, -1582], [-2503, -680], [-1413, -2335], [2983, 1054], [2253, 2015], [-774, 1616], [3131, -571], [6610, -224], [207, 2440], [-6446, -1074], [890, -1848], [155, -1949], [4606, 1424], [4467, 994], [-2034, -1668], [-4727, 2011], [3190, -444], [-1466, 3270], [1881, -444], [5674, 696], [-5323, -743], [-5030, 1316], [5325, 1662], [-3694, 1336], [-3316, 2656], [-84, -1644], [3949, -1950], [2331, -676], [30, 300], [2066, -1894], [11642, 1080], [-2950, -3402], [584, 3056], [2150, -4070], [-2462, -3140], [1694, 4864], [5074, -2840], [-6442, -3064], [1522, -5706], [3327, -4037], [-18233, 337], [-1516, -420], [-1875, 1873], [-1682, 2558], [-2129, 73], [-12675, 1560], [-15163, -1950], [-6552, -2300], [-6080, 2768], [7568, 1264], [5530, 1176], [10693, -785], [-4938, -2268], [2744, -2148], [6578, -2116], [11590, 5226], [-7815, -3378], [-1271, -7331], [4640, -2852], [-13089, 3457], [-14869, 1798], [-7572, -1756], [6589, 2659], [3682, 3510], [10186, 2804], [-23960, 1332], [6910, -7154], [-2132, -3220], [1683, 293], [-13914, 3624], [-4215, -6788], [-7198, -6612], [2436, -4492], [-14104, 864], [255, 7828], [-2793, 812], [8998, -1642], [-6250, 1016], [4332, 6984], [13699, 3389], [-25678, 5024], [4310, -9012], [20078, 5384], [3126, -11616], [-10910, 540], [6392, -1456], [1118, 414], [18034, -5140], [16728, 9988], [-15600, -1072], [24663, 8736], [31838, 3400], [1561, -10433], [922, -5908], [13431, 8711], [-3598, 3384], [-7509, -2798], [-23683, 10237], [4382, 6034], [-18767, 5004], [-1050, 188], [-18140, 5812], [-6831, -7065], [-6230, 11596], [-22885, -9827], [-21426, -6426], [5214, 9944], [-22466, 12720], [-5405, 1038], [-2025, 15931], [4499, 6736], [-43097, 5019], [-12346, 4732], [-43997, -6], [-16805, -7973], [-6950, 6858], [14048, -4356]];
    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_c();".
// 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_c(:prec:=2)
    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_c();".
// 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_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_58_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-5, 2, 1]>],Snew);
    return Vf;
end function;