
// Make newform 162.4.a.f in Magma, downloaded from the LMFDB on 25 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_f();".
// 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_f();".
// 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 := [-26, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-2, 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], [-5, -1], [9, -1], [-11, 2], [31, 1], [2, 1], [64, -5], [35, 1], [115, -7], [107, 3], [138, 14], [235, 2], [-55, -24], [249, 15], [82, 14], [83, -2], [-517, 15], [-577, 12], [-172, -32], [-166, -21], [181, 13], [621, 21], [-226, 40], [-53, 22], [-649, 43], [767, 81], [1456, -1], [-358, 18], [-357, -87], [1016, 66], [-1001, 59], [619, -34], [915, -88], [-649, 145], [797, 159], [1331, 3], [780, -16], [1479, 111], [-3009, 33], [-3164, 56], [-990, 116], [4049, -29], [683, 246], [134, -230], [-1244, 106], [-2813, -197], [-1615, -57], [1461, 90], [-1933, 225], [-2234, 287], [-477, -3], [711, 350], [1528, -55], [4695, 108], [149, -341], [226, -382], [4760, -132], [-673, -99], [-2729, -187], [3071, 63], [-6181, 25], [1952, 453], [3499, -139], [5871, 50], [2123, -47], [2809, 49], [3831, -376], [-3033, 96], [-1809, -325], [-677, -160], [1704, -282], [4541, -147], [-809, -131], [1332, -73], [487, 11], [-625, -647], [-5974, -234], [5059, -322], [-6441, -346], [-14037, -141], [-525, 371], [-1600, 658], [7994, 45], [-9931, -399], [-7359, 468], [3114, 189], [-1021, -462], [-8333, 341], [9475, -389], [-8840, -91], [-2595, 231], [-6728, 226], [-11077, -524], [-12157, -162], [-4692, -852], [9487, 299], [1262, 253], [16544, -36], [3438, -910], [-9451, 228], [11666, 136], [1647, 1392], [5471, 178], [4455, 170], [3578, 699], [-3175, 112], [-2818, -1256], [9287, 55], [7907, -1272], [8271, 431], [7106, -462], [21763, -232], [1853, 336], [16992, 368], [9223, 650], [18839, -810], [-3912, 618], [-2125, -971], [-2959, -23], [-973, 939], [19927, -95], [-12985, 463], [-24816, 147], [22853, 9], [4234, 1532], [-23921, 319], [20400, 372], [5429, 27], [-3481, -1323], [-23740, -225], [-1137, 2241], [-17529, 995], [9566, -1068], [13503, 1209], [-14285, -875], [8322, 1056], [2423, -873], [-11409, -1041], [-15982, -179], [-21328, -171], [2027, 841], [9835, -65], [6620, 1240], [-21902, -44], [3945, 1161], [11387, 1125], [-3009, -435], [-2961, 2186], [27896, -1208], [-17553, -637], [-13274, 32], [19864, 481], [-11213, -601], [13057, -254], [18765, -159], [-11080, -42], [5127, 753], [16038, -1576], [5003, 163], [-13059, -672], [-36606, 885], [3467, -1359], [1752, -3012], [-26257, 562], [-7771, -2591], [13544, -600], [-16681, 2463]];
    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_f();".
// 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_f(: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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_162_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-216, 9, 1]>],Snew);
    return Vf;
end function;