
// Make newform 147.4.a.k in Magma, downloaded from the LMFDB on 12 September 2025.
// 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_k();".
// 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_k();".
// 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 := [-2, 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_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 := 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]];
    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 := [[1, 1], [3, 0], [-10, -7], [0, 0], [-10, 24], [-52, -25], [-58, 45], [-96, 22], [14, -28], [148, -62], [52, 50], [-124, 48], [-10, 219], [-360, -100], [48, -250], [134, -360], [308, 226], [-8, 3], [-72, -524], [494, -232], [-52, -401], [-472, 236], [-508, -80], [194, -339], [-244, 599], [-914, 323], [-1100, 222], [-270, 52], [-312, 4], [1398, 424], [-620, -1196], [-1180, 944], [384, 94], [-36, -652], [-594, -832], [-2676, 96], [-304, 981], [1464, 1064], [2496, -286], [-838, 925], [1130, -52], [-1292, 1201], [-1314, 1056], [474, -2024], [-3446, 552], [2864, -1076], [1460, -588], [376, 420], [3292, 1030], [3788, 717], [-2072, -766], [1702, 1796], [444, 2757], [1316, -3054], [-3370, -777], [3162, 2120], [-5078, 63], [-3028, -994], [-1750, 2024], [344, 1746], [-288, -2022], [-6234, -2057], [2768, 1330], [5464, 674], [-4792, -761], [1490, -3864], [-9388, 336], [-984, 3508], [3998, 2000], [4992, -2959], [-1334, 6543], [-102, -6164], [1432, 6880], [-1198, 2776], [1292, 1976], [-1080, -8452], [2212, -1702], [2288, 8411], [-248, 7250], [3716, -5905], [684, -4018], [-2718, 1424], [-3290, -4980], [-11408, 213], [11112, -2696], [3786, 8700], [3798, 6904], [-1506, 8736], [954, 569], [-4, -3824], [-5908, -1558], [4288, -4514], [7732, 1328], [-3826, -5580], [-2192, -4952], [-16560, 312], [-9358, 3139], [-8950, -3545], [-1852, 11036], [16782, -1640], [14548, -612], [-14078, 4536], [-5164, -8702], [-4888, -6354], [-3308, -332], [792, -10375], [-5804, 5798], [-6554, -35], [2318, 10560], [-10552, -1613], [-6216, -1700], [8248, -2200], [-10016, 5838], [-13020, 11724], [-10068, -12200], [-11080, -7670], [3832, 1410], [-2808, -15134], [-27244, 1146], [-26170, 2048], [12008, 5677], [-9156, -6344], [1434, -10783], [6602, -7924], [1116, -13456], [4292, 4958], [-8336, -5452], [17384, 5520], [2732, -9906], [-8612, -10683], [4312, 2696], [7294, 9660], [-18856, -13684], [21992, 6340], [32762, 5153], [18528, -7443], [6018, 5597], [-40116, 1792], [-13682, 16363], [1586, -2472], [-27492, -11044], [10498, -4576], [21832, 12612], [8274, 15460], [3684, -19065], [8496, 706], [11520, 829], [-11878, -7303], [-17504, -1638], [38982, -1816], [23640, -2968], [13886, 7989], [-10216, 4360], [-9312, -22834], [-13144, 6768], [32190, -1932], [4196, -5308], [42282, -7647], [28192, -7657], [-12598, 12831], [-10830, -19184], [-10766, 528], [4828, 16268], [23276, -3536], [-28488, -10282], [-28568, -1408], [-14348, -21636], [-5096, -16907]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_147_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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