
// Make newform 116.4.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_116_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_116_4_a_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_116_4_a_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 := [-3, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1, 2]];
    Rf_basisdens := [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_116_a();"
function MakeCharacter_116_a()
    N := 116;
    order := 1;
    char_gens := [59, 89];
    v := [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_116_a_Hecke(Kf)
    return MakeCharacter_116_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], [-5, 2], [-10, 4], [-16, 1], [-27, -6], [-22, -22], [-16, -12], [-18, 10], [-29, 0], [10, 3], [-72, 48], [48, -2], [120, 9], [298, -49], [-17, 54], [362, -70], [-306, 102], [264, -204], [-52, 166], [-436, 168], [-410, 165], [-114, 90], [-16, -382], [-948, -42], [-456, 216], [-696, -290], [142, 116], [-793, -54], [-242, 204], [-260, -112], [1024, 120], [752, 532], [-390, 374], [1637, -318], [366, -656], [612, 338], [876, 557], [368, 358], [2190, 204], [-664, -978], [-2031, 474], [-352, -156], [1548, -722], [2194, -456], [-3966, -72], [-480, -59], [-4564, 194], [-548, -794], [-3020, 90], [-117, 1372], [-3362, -418], [-3039, -16], [1924, 79], [-223, 208], [-4442, -125], [-1406, -926], [-2754, -1507], [-34, -1368], [-2385, 684], [3350, -870], [-4010, -648], [5128, 141], [1360, -540], [2745, -1108], [7050, -618], [8356, -19], [8490, 394], [10252, -118], [587, -2226], [-9486, 28], [-1926, 2385], [10596, 496], [-4151, -282], [-216, 2112], [4886, -2260], [-5416, -112], [-2739, -1370], [-5077, 1096], [-8606, -924], [-6816, -2570], [2418, 1242], [-9870, 66], [-2400, 2512], [954, 4214], [-4764, -428], [-3906, -1968], [6234, -1380], [-7750, 80], [12150, -1222], [-8728, 2353], [1110, -1523], [8198, 2088], [7168, 2771], [11694, 2722], [-13906, -2299], [-1825, 3770], [8447, -8], [9284, -1628], [18336, -816], [-2134, 3240], [5966, 3800], [2620, 2921], [16754, 88], [-240, -2908], [-1578, -2530], [-14838, 1302], [-5701, 320], [5286, 5889], [-2868, 2162], [16302, 29], [3631, -2370], [13760, -3868], [-11364, 3265], [7788, 2278], [-18258, 574], [-19142, -1500], [-2710, 2018], [17060, -1130], [-10276, -3253], [-15034, 1232], [6661, -1884], [-4094, -3140], [-27188, 204], [-14332, -1664], [801, -3918], [-8811, -6758], [2752, 2798], [-13448, -2820], [-7028, 8508], [-6488, -3309], [1684, 4804], [-1468, 7656], [-13722, -1642], [11558, 6876], [-20322, 4266], [16116, -5730], [-3232, 6034], [11318, -458], [16276, -2092], [1218, 3568], [-10167, -1546], [-3204, 296], [11688, -2419], [174, -7970], [3022, -3517], [-14154, 2344], [-14011, 796], [672, -4493], [18440, 202], [4263, -10482], [-23038, 5896], [7326, 11100], [29994, 855], [-10516, 3700], [13206, 7877], [21624, -5826], [-16806, 2184], [-11034, -7384], [-6849, 13874], [-21708, -4231], [19919, 3544], [-654, 1471], [-7844, 9812], [-22359, 1632], [-5462, 8995], [-11732, 5010], [-48140, -1380]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_116_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_116_4_a_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_116_4_a_a(:prec:=2)
    chi := MakeCharacter_116_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_116_4_a_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_116_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_116_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-13, 0, 1]>],Snew);
    return Vf;
end function;