
// Make newform 162.4.a.e in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_162_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_162_4_a_e();".
// 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_162_4_a_e();".
// 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, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 3]];
    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_162_a();"
function MakeCharacter_162_a()
    N := 162;
    order := 1;
    char_gens := [83];
    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_162_a_Hecke(Kf)
    return MakeCharacter_162_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, 0], [-6, 1], [8, -2], [-18, -8], [5, 14], [-60, 11], [-4, -22], [-90, -4], [-162, 7], [-124, 36], [-217, -2], [-96, -44], [-304, 6], [192, -36], [204, 76], [504, 32], [371, 18], [-52, -138], [570, 8], [425, 96], [-220, 50], [-132, -60], [-384, 89], [-382, 56], [708, 176], [-1060, 60], [-84, -260], [821, -174], [384, -261], [-412, -174], [-402, 292], [-1620, 193], [764, 268], [-2070, -289], [-952, -252], [551, 246], [-64, 64], [438, 696], [-1950, 39], [3264, -236], [2114, 136], [-606, 32], [1595, -396], [-1842, 155], [140, -256], [-1696, -622], [-1564, 18], [-294, -348], [1649, -510], [2472, 733], [3948, -564], [305, -236], [-1722, -104], [-4584, -39], [2904, 20], [1878, 901], [-5740, 114], [-70, -192], [-2664, -293], [4484, 168], [-2610, -55], [4412, -492], [-6756, -80], [539, -356], [1086, -919], [860, -790], [602, 712], [-2370, 1092], [1682, 544], [-7344, 88], [594, 180], [-5056, -996], [-1714, 512], [116, -1700], [-6234, -1160], [7620, 1076], [-5929, 138], [-492, -95], [-3439, 532], [8628, -216], [-5707, 838], [1380, -1036], [-313, 1980], [2396, 2880], [840, -1812], [4392, 60], [-4615, -468], [1260, 868], [2696, -3136], [-588, 1216], [3594, 2112], [-796, 1532], [-10674, 224], [15032, 1242], [-3996, -1464], [-60, 1732], [2712, -1948], [13016, 1146], [-16543, 982], [9380, 168], [-7350, -1537], [-10632, 564], [19428, -325], [-352, 2728], [-14965, -72], [-16554, 344], [-22992, 1037], [540, -4492], [9263, -1416], [-2080, -98], [5582, 2184], [-9888, -563], [6464, 732], [-21160, -140], [-21636, 853], [16868, -828], [-5922, 456], [-15228, 1820], [6738, -4468], [-4345, -42], [-19741, 1268], [-10740, -796], [5688, 3540], [-1420, 2754], [6270, 1081], [-9535, 2954], [-12906, 1620], [15608, 1458], [3890, -4728], [-12004, -192], [-6960, -444], [-24040, -1286], [2738, 504], [-588, -1815], [-7429, -1948], [486, -3393], [11576, -3882], [3714, -4725], [18012, -4723], [11432, 3528], [3606, 4997], [3452, 512], [-9870, 2528], [-17374, -376], [-4326, 8076], [34886, 1344], [13260, 3813], [10124, -4760], [5502, -1036], [-17137, 2074], [-21648, -2108], [2372, -172], [-27330, -3452], [10400, -604], [-9186, 5508], [3332, -5262], [-34836, -1293], [24335, -1448], [6498, 815], [-12282, -3708], [-29196, -531], [19136, 3702], [-23562, -4404], [-19080, -5548], [-18516, -4240], [-5632, -966], [2099, -4014]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_162_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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