
// Make newform 147.4.a.l in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_147_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_147_4_a_l();".
// 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_147_4_a_l();".
// 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, -24, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-16, -1, 1]];
    Rf_basisdens := [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_147_a();"
function MakeCharacter_147_a()
    N := 147;
    order := 1;
    char_gens := [50, 52];
    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_147_a_Hecke(Kf)
    return MakeCharacter_147_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, 1, 0], [-3, 0, 0], [4, -1, 1], [0, 0, 0], [12, -1, -3], [19, 5, -1], [16, 0, 4], [-65, -7, -1], [80, -24, -4], [26, -25, 5], [-39, 22, -2], [81, 19, 1], [82, -2, -14], [143, -69, -3], [-46, -72, -28], [154, -69, -11], [358, 69, -29], [10, -100, 20], [-215, 17, 47], [66, 120, 12], [363, -101, -23], [299, -36, 48], [156, 51, -27], [532, 170, 22], [24, -53, 49], [794, -20, 68], [-865, -51, -45], [862, 23, 1], [519, 331, -47], [686, 26, -22], [573, -80, 4], [-944, 311, 61], [1000, 42, 94], [39, -353, -143], [1736, -6, 14], [-334, 129, -117], [-1562, 158, -166], [-980, 596, 20], [-142, 404, 176], [-828, 406, -162], [-2890, -26, 86], [-1265, 317, -97], [-458, -646, -62], [1703, -18, 66], [-364, -310, 50], [-192, -236, 52], [-1040, -874, 146], [358, 491, 41], [2884, 369, 43], [2375, -87, -189], [1298, 270, 14], [-3558, -186, 162], [-2860, 15, 21], [3458, -511, 191], [2706, -260, 120], [-3960, 122, -186], [-2360, 333, 31], [-1574, -403, -241], [3727, -1291, -241], [-1640, 262, -266], [-233, 395, 101], [310, 667, -479], [3313, 275, -163], [1462, 354, 22], [995, 654, 618], [3254, -21, -175], [2393, -1449, -63], [3037, -52, -52], [-2296, -20, -136], [-4358, 416, -40], [2166, -2302, -342], [-4080, 814, 162], [-4089, 886, -290], [3989, 63, 165], [-4323, 1135, -167], [-1960, 1626, -118], [2026, 1824, -572], [4413, 1363, -527], [-268, -372, 440], [-751, 1830, 834], [5538, -90, -246], [4823, 1671, 285], [10, -150, 826], [-9311, 377, 299], [-12438, 1837, 271], [3926, 1281, 335], [-2286, 2682, 666], [-2255, 686, -742], [-1184, -2738, 874], [1273, 1019, -979], [1490, -510, -46], [-736, -1298, 86], [-4769, -1534, -358], [3646, -479, -509], [-617, 287, -31], [-750, -750, -570], [-2532, -1655, -609], [472, 1086, -782], [-1511, -1537, 497], [9059, -1629, -39], [-4268, 812, 788], [15588, 623, 861], [6972, 2147, -291], [-1286, -2734, -386], [-97, -789, -411], [7879, 464, -400], [-2362, -577, 533], [-12418, -558, 446], [-4088, -2442, 1186], [159, -2060, -1220], [3527, -1182, -702], [5086, -646, 182], [-198, -984, -1032], [-6887, 2729, 359], [-490, -2529, -1635], [-9380, -3282, -314], [-1489, 891, 597], [-142, -284, 1124], [4112, 5331, -895], [8028, 1236, 1680], [11665, -3247, -925], [-9869, 146, 398], [-14074, 3225, 803], [-12278, 1359, -239], [-4339, -5457, 1161], [-11202, -2013, -771], [1974, 568, 1624], [14582, -648, 896], [1847, -2388, 2088], [-2289, -4877, 97], [1515, -437, 2461], [15282, 384, -384], [-11567, 2336, -1180], [4298, 684, 84], [-12280, 5292, -232], [-4861, 3714, 222], [-25618, -2576, -88], [-6380, -1594, -2662], [19322, -5899, -853], [-8822, 2358, -1274], [4604, -3138, -798], [1500, -1775, -1569], [9288, 3472, -800], [9360, 3807, -639], [-11557, 3477, 783], [3788, -4444, -868], [12125, 2997, 1431], [-16236, -3694, 966], [16724, 1662, 1362], [-7750, 924, -1564], [8470, 50, -1618], [5076, -2610, -2250], [12197, -2673, -1143], [35826, -1538, 138], [27605, 1167, 1569], [11868, 5202, 930], [14561, 2403, 549], [-16074, -6020, 864], [-20865, -4436, 4], [-9562, 7537, 1535], [-24438, -6482, 906], [-8672, -3830, 2650], [-10745, 6872, -1468], [11594, 6759, -31], [-8522, -4860, -2132], [30532, -8208, -884], [-10279, -7254, -774], [2773, -715, 1487]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_147_4_a_l();".
// 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_147_4_a_l(:prec:=3)
    chi := MakeCharacter_147_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_147_4_a_l();".
// 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_147_4_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_147_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![6, -24, -1, 1]>,<5,R![1236, -192, -11, 1]>],Snew);
    return Vf;
end function;