
// Make newform 171.5.c.c in Magma, downloaded from the LMFDB on 21 September 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_171_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_171_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_171_5_c_c();".
// 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_171_5_c_c();".
// 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 := [142, 0, 35, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [19, 0, 1, 0], [0, 28, 0, 1]];
    Rf_basisdens := [1, 1, 3, 3];
    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_171_c();"
function MakeCharacter_171_c()
    N := 171;
    order := 2;
    char_gens := [20, 154];
    v := [2, 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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_171_c_Hecke();"
function MakeCharacter_171_c_Hecke(Kf)
    N := 171;
    order := 2;
    char_gens := [20, 154];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 0]];
    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]];
    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 := 5;
    raw_aps := [[0, 1, 0, 0], [0, 0, 0, 0], [10, 0, 1, 0], [31, 0, 6, 0], [-64, 0, 17, 0], [0, 10, 0, -11], [-63, 0, -24, 0], [0, 38, 57, -19], [-37, 0, 113, 0], [0, -208, 0, -3], [0, 242, 0, 56], [0, -282, 0, 72], [0, 430, 0, -36], [922, 0, -351, 0], [1852, 0, 85, 0], [0, 204, 0, 63], [0, -250, 0, 303], [-46, 0, 171, 0], [0, 428, 0, -151], [0, -516, 0, -390], [-3963, 0, 342, 0], [0, 566, 0, -76], [-8188, 0, -262, 0], [0, 1374, 0, -726], [0, 640, 0, 70], [4498, 0, -2420, 0], [0, 2860, 0, -860], [0, 3714, 0, -93], [0, 1004, 0, 227], [0, -910, 0, -1092], [0, -4458, 0, -330], [-3110, 0, -5573, 0], [15071, 0, -4642, 0], [2660, 0, -3981, 0], [-10868, 0, 4819, 0], [0, -2456, 0, 1894], [10834, 0, -4008, 0], [7026, 0, 2028, 0], [0, 3886, 0, 708], [0, -3688, 0, 1638], [0, 5866, 0, -2136], [0, 12360, 0, 1578], [23479, 0, -6056, 0], [0, -8998, 0, -1156], [-4002, 0, 9456, 0], [13359, 0, 1974, 0], [0, 3334, 0, -5213], [0, -6926, 0, 382], [0, 2078, 0, -2511], [-41638, 0, -501, 0], [20688, 0, -3201, 0], [-20005, 0, 9236, 0], [0, -9190, 0, 2648], [-17688, 0, -15543, 0], [0, 10600, 0, -5148], [-50888, 0, -13031, 0], [0, -14154, 0, -1440], [75567, 0, -3039, 0], [-27476, 0, -819, 0], [0, 10156, 0, 3444], [14518, 0, -23979, 0], [0, -2134, 0, 177], [0, 9078, 0, -5472], [44465, 0, 16730, 0], [-44461, 0, 27705, 0], [0, -3506, 0, -1965], [0, 29708, 0, 4883], [0, -21356, 0, -8612], [-120094, 0, 9275, 0], [142376, 0, 5217, 0], [-139145, 0, 14161, 0], [15141, 0, 22326, 0], [2090, 0, 48516, 0], [0, 26594, 0, -5953], [0, -19854, 0, -3339], [0, 4826, 0, 11190], [83048, 0, 35543, 0], [-97428, 0, 28293, 0], [0, 22654, 0, 7530], [0, 19646, 0, 13916], [51196, 0, 8530, 0], [0, -45032, 0, 15001], [0, -51768, 0, 96], [0, 29346, 0, -1710], [0, 6526, 0, 4078], [102232, 0, 23569, 0], [0, -41776, 0, 17310], [61153, 0, 14634, 0], [-209414, 0, 36529, 0], [98236, 0, -26565, 0], [-46784, 0, -74471, 0], [74306, 0, 41432, 0], [0, 36336, 0, 19824], [-263996, 0, -16742, 0], [-44758, 0, -25551, 0], [398043, 0, -11871, 0], [0, -18028, 0, 25506], [0, -11950, 0, 24456], [0, -8862, 0, -27411], [454626, 0, -11661, 0], [0, 37136, 0, -4522], [163730, 0, 9473, 0], [0, 44866, 0, -38616], [0, -22928, 0, 25314], [-299686, 0, -19068, 0], [-210117, 0, -34548, 0], [-177070, 0, -19285, 0], [-79734, 0, 4932, 0], [0, 49334, 0, -15234], [0, -62860, 0, 1466], [0, -46204, 0, -23578], [-179190, 0, 41955, 0], [43264, 0, 77935, 0], [-76012, 0, 7374, 0], [123000, 0, -133833, 0], [0, -61472, 0, 1908], [-2260, 0, -53277, 0], [-196335, 0, -116574, 0], [609512, 0, 19703, 0], [0, 66082, 0, 39885], [0, -87566, 0, 36607], [0, -5432, 0, 31210], [0, 5056, 0, -8925], [0, 56340, 0, 13002], [442298, 0, 46809, 0], [428040, 0, -29358, 0], [322288, 0, 29094, 0], [-696225, 0, 3192, 0], [-78109, 0, 18786, 0], [-299732, 0, 68514, 0], [101810, 0, 81081, 0], [0, -50810, 0, 6264], [0, 181588, 0, -19670], [-331650, 0, 7647, 0], [-695293, 0, 77066, 0], [523591, 0, 59040, 0], [0, 89134, 0, 23361], [0, -193322, 0, -20465], [0, 142912, 0, -62331], [-293029, 0, 120794, 0], [0, 203598, 0, -25323], [-26080, 0, -134569, 0], [-720749, 0, 152394, 0], [0, 39192, 0, -1743], [0, -169014, 0, -6519], [0, 77248, 0, -33072], [-729092, 0, -93102, 0], [0, -72736, 0, -804], [867070, 0, -78099, 0], [0, -63642, 0, -73986], [0, 266146, 0, 43633], [571404, 0, 125067, 0], [72558, 0, 144381, 0], [0, 222368, 0, -49284], [0, 91108, 0, -52601], [0, -136324, 0, 44256], [-403671, 0, 100227, 0], [702271, 0, -184487, 0], [218277, 0, -306330, 0], [0, -175786, 0, -55269], [-792452, 0, 27562, 0], [0, -100722, 0, 16590], [315742, 0, -122592, 0], [0, 224640, 0, 60126], [0, -166214, 0, -73428], [0, 78964, 0, 68652], [0, 93282, 0, 27606], [44740, 0, -181947, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_171_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_171_5_c_c();".
// 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_171_5_c_c(:prec:=4)
    chi := MakeCharacter_171_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 5));
    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_171_5_c_c();".
// 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_171_5_c_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_171_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,5,sign)));
    Vf := Kernel([<2,R![142, 0, 35, 0, 1]>,<5,R![92, -21, 1]>],Snew);
    return Vf;
end function;