
// Make newform 162.4.c.b 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_4_c_b();".
// 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_c_b();".
// 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 := 4;
    raw_aps := [[-2, 2], [0, 0], [0, -3], [-29, 29], [57, -57], [0, -20], [-72, 0], [-106, 0], [0, -174], [210, -210], [0, -47], [2, 0], [0, 6], [-218, 218], [-474, 474], [81, 0], [0, -84], [-56, 56], [0, 142], [360, 0], [-1159, 0], [160, -160], [-735, 735], [-954, 0], [-191, 191], [363, -363], [0, 628], [675, 0], [1730, 0], [0, -1866], [1379, 0], [0, -579], [-654, 654], [0, 3004], [0, -1803], [-2459, 2459], [0, 196], [-1564, 0], [0, -1974], [2217, -2217], [-2475, 0], [1568, 0], [-1140, 1140], [0, -2045], [-3735, 0], [1163, 0], [0, -2126], [2752, -2752], [3972, -3972], [0, -4502], [4842, 0], [0, 5334], [3994, -3994], [-1008, 0], [0, 924], [-1014, 1014], [-2970, 0], [245, 0], [-4376, 4376], [-240, 240], [0, 6838], [0, 5118], [-5560, 0], [0, -7662], [-3485, 3485], [7059, -7059], [-9290, 9290], [0, 3814], [0, -1929], [6586, -6586], [6042, -6042], [-3762, 0], [7261, -7261], [0, -1640], [-7396, 0], [0, 4992], [9453, -9453], [8588, 0], [0, 1716], [0, 9889], [0, -5556], [2104, -2104], [7614, 0], [7805, 0], [5209, -5209], [-4236, 4236], [-16002, 0], [-7319, 7319], [-9483, 9483], [0, -10793], [2583, 0], [1254, -1254], [17336, 0], [0, 15171], [0, -8930], [15210, 0], [0, -19641], [22428, 0], [-8152, 0], [-2860, 0], [9664, -9664], [14859, 0], [0, -8193], [16572, -16572], [0, 6244], [-14794, 0], [26769, -26769], [-3078, 0], [0, -1002], [20653, -20653], [0, -27128], [24518, 0], [0, 474], [1132, -1132], [6725, 0], [21126, -21126], [0, -19460], [-11664, 0], [0, 3345], [-9393, 9393], [0, 1762], [-25517, 25517], [-26898, 26898], [23940, 0], [-23060, 23060], [-14175, 0], [8692, -8692], [-29556, 0], [36691, -36691], [0, 19798], [-21976, 0], [0, 13236], [0, 6325], [-3238, 0], [0, 40416], [0, 4759], [27414, 0], [0, -6176], [0, -6879], [-16902, 0], [24086, 0], [-7854, 7854], [0, -5771], [-17568, 0], [31322, 0], [-41856, 41856], [-15662, 15662], [39864, -39864], [0, 9160], [5076, 0], [0, -14978], [-22860, 0], [-32506, 0], [0, -35868], [33586, -33586], [28902, -28902], [28271, 0], [-19140, 19140], [31619, 0], [0, 20913], [-17529, 17529], [-53604, 0], [0, -11117], [-27297, 0], [0, -25086], [20982, -20982], [11477, 0], [-8588, 8588]];
    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_4_c_b();".
// 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_c_b(:prec:=2)
    chi := MakeCharacter_162_c();
    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_c_b();".
// 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_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_162_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![9, 3, 1]>],Snew);
    return Vf;
end function;