
// Make newform 162.6.c.e in Magma, downloaded from the LMFDB on 19 April 2024.
// 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_6_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_6_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)
    N := 162;
    order := 3;
    char_gens := [83];
    char_values := [[0, -1]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, 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+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 := 6;
    raw_aps := [[-4, 4], [0, 0], [0, 66], [-176, 176], [60, -60], [0, 658], [-414, 0], [956, 0], [0, -600], [-5574, 5574], [0, 3592], [-8458, 0], [0, -19194], [-13316, 13316], [19680, -19680], [-31266, 0], [0, -26340], [31090, -31090], [0, 16804], [6120, 0], [-25558, 0], [-74408, 74408], [6468, -6468], [-32742, 0], [-166082, 166082], [22002, -22002], [0, 79264], [227988, 0], [-8530, 0], [0, 195438], [173000, 0], [0, -151260], [128454, -128454], [0, -154196], [0, -29454], [203872, -203872], [0, -136142], [-171124, 0], [0, 676200], [-133158, 133158], [-693396, 0], [377174, 0], [265344, -265344], [0, -295298], [201294, 0], [652448, 0], [0, 1147060], [-701960, 701960], [-1236108, 1236108], [0, -105830], [-438678, 0], [0, -28464], [-892562, 892562], [-110124, 0], [0, -140802], [938760, -938760], [-1114506, 0], [567704, 0], [1213258, -1213258], [-687738, 687738], [0, 830908], [0, 1312626], [1690220, 0], [0, 1502040], [-810842, 810842], [-903558, 903558], [-1121972, 1121972], [0, 2752174], [0, -1917492], [-1836590, 1836590], [622014, -622014], [3740616, 0], [-16232, 16232], [0, -293606], [3180116, 0], [0, 2979840], [-3459774, 3459774], [-3904162, 0], [0, -5441154], [0, -1969946], [0, -139020], [-4327430, 4327430], [-2799360, 0], [-5902414, 0], [446512, -446512], [-3495252, 3495252], [-1206126, 0], [-233546, 233546], [1744890, -1744890], [0, 2917864], [-5310756, 0], [-2344656, 2344656], [9815312, 0], [0, 5945196], [0, -6478316], [4717944, 0], [0, 1907706], [8019738, 0], [1911620, 0], [-11990050, 0], [-4458092, 4458092], [9026118, 0], [0, -6848988], [5463222, -5463222], [0, 10232380], [15943682, 0], [9477132, -9477132], [2453490, 0], [0, 9299784], [11461702, -11461702], [0, -11278376], [93782, 0], [0, 14964150], [5068876, -5068876], [15591872, 0], [-10970130, 10970130], [0, 2837044], [-6056856, 0], [0, 1088922], [-7418028, 7418028], [0, -767654], [-1422626, 1422626], [6162306, -6162306], [15062148, 0], [5876356, -5876356], [3600774, 0], [-9225158, 9225158], [-26392320, 0], [9794848, -9794848], [0, -4075838], [-16570852, 0], [0, -14414136], [0, -16794440], [13294262, 0], [0, 2147862], [0, 13105918], [-23715378, 0], [0, 8400484], [0, -5410230], [-26077734, 0], [19002116, 0], [31017282, -31017282], [0, 15628960], [15842052, 0], [2061758, 0], [-30390024, 30390024], [29773786, -29773786], [-8641002, 8641002], [0, 33566284], [39019104, 0], [0, 18138178], [30531186, 0], [-43553284, 0], [0, 13415160], [-3108164, 3108164], [-1190352, 1190352], [-47199616, 0], [-1335954, 1335954], [14723786, 0], [0, 26919642], [3731604, -3731604], [21873546, 0], [0, -17602496], [16731684, 0], [0, -55538178], [38678424, -38678424], [9584984, 0], [10365034, -10365034]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_162_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_162_6_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_6_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, 6));
    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_6_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_6_c_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_162_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![4356, -66, 1]>],Snew);
    return Vf;
end function;