
// Make newform 162.8.c.e in Magma, downloaded from the LMFDB on 10 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_162_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_162_c_Hecke();"
// 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_8_c_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_8_c_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 := [1, -1, 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_162_c();"
function MakeCharacter_162_c()
    N := 162;
    order := 3;
    char_gens := [83];
    v := [2];
    // 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_162_c_Hecke();"
function MakeCharacter_162_c_Hecke( : Kf := false)
    N := 162;
    order := 3;
    char_gens := [83];
    char_values := [[0, -1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
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 := 8;
    raw_aps := [[-8, 8], [0, 0], [0, 165], [508, -508], [-3024, 3024], [0, -5039], [-3189, 0], [1508, 0], [0, 75600], [82665, -82665], [0, 174892], [-323569, 0], [0, 308118], [-336680, 336680], [383196, -383196], [760206, 0], [0, 2225664], [-2244815, 2244815], [0, -1473188], [-5006892, 0], [-5898301, 0], [-7028768, 7028768], [2651196, -2651196], [-6770901, 0], [-16176386, 16176386], [-2701614, 2701614], [0, -2068916], [-9154500, 0], [8686451, 0], [0, 24977553], [-4986592, 0], [0, 22340460], [-12863079, 12863079], [0, -44190236], [0, 2628873], [55301896, -55301896], [0, 22371061], [52360568, 0], [0, 54795732], [-123645147, 123645147], [98729724, 0], [91985534, 0], [-131978604, 131978604], [0, -147907355], [-26956101, 0], [135830516, 0], [0, 2193400], [-19944296, 19944296], [125985036, -125985036], [0, -23504567], [-127756797, 0], [0, 93487812], [134780197, -134780197], [13147032, 0], [0, 128470929], [97751652, -97751652], [39685107, 0], [-350732608, 0], [-539602046, 539602046], [100851885, -100851885], [0, 254112268], [0, 543084201], [-729877156, 0], [0, -638769912], [637760569, -637760569], [654377325, -654377325], [-807688748, 807688748], [0, 187280350], [0, -282555660], [760612090, -760612090], [468893358, -468893358], [153906480, 0], [749527624, -749527624], [0, 541727938], [1329738956, 0], [0, -719948724], [-2320670190, 2320670190], [1183726631, 0], [0, -1267639467], [0, 1019488201], [0, -1041285864], [1958448757, -1958448757], [2842557828, 0], [-84129181, 0], [1870465492, -1870465492], [1013326428, -1013326428], [-165755502, 0], [1380796705, -1380796705], [-2784912966, 2784912966], [0, -2789185400], [3972576072, 0], [2344669644, -2344669644], [3083807156, 0], [0, 3718506960], [0, -436425656], [-504602784, 0], [0, 4600852842], [71493738, 0], [-2121816832, 0], [-2113506865, 0], [-6255533636, 6255533636], [-6508464645, 0], [0, 173248836], [-7288814415, 7288814415], [0, -4290362480], [628378103, 0], [8968024104, -8968024104], [-7574844513, 0], [0, 6059461428], [655797205, -655797205], [0, -3856114604], [-4898890546, 0], [0, 6654557109], [-10553138072, 10553138072], [6377746040, 0], [-668702619, 668702619], [0, -3246324980], [7463205252, 0], [0, 10162996218], [2830682580, -2830682580], [0, -3962748407], [11861546749, -11861546749], [-9868356462, 9868356462], [13628654388, 0], [2162695780, -2162695780], [2939137707, 0], [13091445313, -13091445313], [-3030147792, 0], [10684763236, -10684763236], [0, 16770474490], [16805228348, 0], [0, -20783379612], [0, -10060116716], [6598932014, 0], [0, 6079249329], [0, -4850490419], [-23346566685, 0], [0, -9360555152], [0, -26237285643], [-15930350985, 0], [-16928061712, 0], [-8735916351, 8735916351], [0, -14436945476], [5566664256, 0], [-15076619194, 0], [-12000400584, 12000400584], [901660546, -901660546], [28143615657, -28143615657], [0, 9515426812], [-12468168408, 0], [0, 6710737849], [-35935021950, 0], [-1402411756, 0], [0, 819690168], [-2845061840, 2845061840], [43299590232, -43299590232], [-32631702784, 0], [-46954624587, 46954624587], [-28095090445, 0], [0, 27219724029], [23044106892, -23044106892], [22213141767, 0], [0, -27077080964], [-25766364420, 0], [0, 9518727438], [5519172336, -5519172336], [16544033300, 0], [-41005524131, 41005524131]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_162_c_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_162_8_c_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_8_c_e(:prec:=2)
    chi := MakeCharacter_162_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_8_c_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_8_c_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_162_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<5,R![27225, -165, 1]>],Snew);
    return Vf;
end function;