
// Make newform 162.4.a.g in Magma, downloaded from the LMFDB on 16 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_g();".
// 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_g();".
// 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], [4, -1], [10, 1], [13, 2], [30, -1], [-1, 1], [69, 5], [-34, 1], [-122, -7], [104, -3], [124, -14], [-233, 2], [-31, 24], [-234, 15], [-68, 14], [-85, -2], [-532, -15], [-589, -12], [140, -32], [-145, 21], [168, -13], [-600, 21], [266, 40], [-75, -22], [692, 43], [686, -81], [-1457, -1], [-376, -18], [270, -87], [950, -66], [1060, 59], [-653, -34], [1003, 88], [794, 145], [638, -159], [1328, -3], [796, 16], [-1368, 111], [3042, 33], [3220, 56], [-1106, -116], [-4078, -29], [437, -246], [-364, -230], [-1350, -106], [-2616, 197], [-1558, 57], [-1371, 90], [-2158, -225], [2521, 287], [474, -3], [361, -350], [-1583, -55], [-4587, 108], [-490, -341], [-608, -382], [4892, 132], [-574, 99], [2542, -187], [3008, -63], [6206, 25], [1499, -453], [-3638, -139], [5821, -50], [-2170, -47], [2760, -49], [4207, 376], [3129, 96], [-1484, 325], [517, -160], [-1986, -282], [4688, 147], [-678, 131], [1405, 73], [-476, 11], [-22, -647], [-5740, 234], [-5381, -322], [-6095, 346], [13896, -141], [-896, -371], [2258, 658], [7949, -45], [-9532, 399], [7827, 468], [-2925, 189], [-559, 462], [8674, 341], [9864, 389], [8749, -91], [2826, 231], [-6954, -226], [10553, -524], [-11995, 162], [3840, -852], [-9188, 299], [-1009, 253], [16580, 36], [4348, 910], [-9679, -228], [-11530, 136], [-255, 1392], [-5293, 178], [4285, -170], [2879, -699], [3287, 112], [1562, -1256], [-9232, 55], [9179, 1272], [7840, -431], [7568, 462], [-21995, -232], [1517, -336], [16624, -368], [-8573, 650], [19649, 810], [4530, 618], [1154, -971], [2936, -23], [-1912, -939], [20022, 95], [13448, 463], [24963, 147], [22844, -9], [-2702, 1532], [-24240, -319], [-20028, 372], [5402, -27], [-2158, 1323], [-23515, 225], [3378, 2241], [-18524, -995], [10634, 1068], [-12294, 1209], [-13410, 875], [-7266, 1056], [3296, 873], [10368, -1041], [15803, -179], [-21157, 171], [-1186, 841], [9900, 65], [-5380, 1240], [-21858, 44], [-2784, 1161], [10262, -1125], [2574, -435], [-5147, -2186], [-29104, -1208], [-16916, 637], [13306, 32], [19383, -481], [10612, -601], [13311, 254], [-18924, -159], [-11038, 42], [-4374, 753], [17614, 1576], [-4840, 163], [12387, -672], [37491, 885], [4826, 1359], [-4764, -3012], [26819, 562], [5180, -2591], [14144, 600], [-19144, -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_g();".
// 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_g(: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_g();".
// 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_g( : 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;