
// Make newform 160.4.c.b in Magma, downloaded from the LMFDB on 11 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_160_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_160_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_160_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_160_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 := [49, 0, 15, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 44, 0, 2], [0, -32, 0, -4], [120, 0, 16, 0]];
    Rf_basisdens := [1, 7, 7, 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_160_c();"
function MakeCharacter_160_c()
    N := 160;
    order := 2;
    char_gens := [31, 101, 97];
    v := [2, 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_160_c_Hecke();"
function MakeCharacter_160_c_Hecke(Kf)
    N := 160;
    order := 2;
    char_gens := [31, 101, 97];
    char_values := [[1, 0, 0, 0], [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+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 := [[0, 0, 0, 0], [0, 0, 1, 0], [-3, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 2, 0, 0], [0, -4, 0, 0], [0, 0, 0, 3], [0, 0, 13, 0], [158, 0, 0, 0], [0, 0, 0, 4], [0, 26, 0, 0], [-170, 0, 0, 0], [0, 0, -79, 0], [0, 0, 61, 0], [0, 46, 0, 0], [0, 0, 0, -15], [82, 0, 0, 0], [0, 0, 173, 0], [0, 0, 0, -22], [0, -40, 0, 0], [0, 0, 0, -8], [0, 0, -235, 0], [-6, 0, 0, 0], [0, 100, 0, 0], [1014, 0, 0, 0], [0, 0, -11, 0], [0, 0, 453, 0], [-2014, 0, 0, 0], [0, 152, 0, 0], [0, 0, 697, 0], [0, 0, 0, -1], [0, -44, 0, 0], [0, 0, 0, 49], [-2550, 0, 0, 0], [0, 0, 0, 50], [0, -330, 0, 0], [0, 0, 57, 0], [0, 0, 93, 0], [0, -178, 0, 0], [0, 0, 0, -65], [1542, 0, 0, 0], [0, 0, 0, -104], [0, 236, 0, 0], [0, 254, 0, 0], [0, 0, 0, -6], [0, 0, 0, -21], [0, 0, -1071, 0], [0, 0, -1239, 0], [-1770, 0, 0, 0], [0, -504, 0, 0], [0, 0, 0, -128], [-1618, 0, 0, 0], [0, 0, 0, -115], [0, -432, 0, 0], [0, 0, -151, 0], [-3262, 0, 0, 0], [0, 0, 0, 48], [0, 114, 0, 0], [-1290, 0, 0, 0], [0, 0, 173, 0], [0, 514, 0, 0], [0, 0, -1859, 0], [0, 0, 0, 58], [0, 252, 0, 0], [0, 306, 0, 0], [0, 0, 0, 69], [0, 848, 0, 0], [0, 0, -1479, 0], [-7522, 0, 0, 0], [0, -760, 0, 0], [0, 0, 0, -34], [0, 0, 129, 0], [0, -750, 0, 0], [0, 0, 0, -51], [0, 0, 1497, 0], [-7974, 0, 0, 0], [0, -742, 0, 0], [418, 0, 0, 0], [1414, 0, 0, 0], [0, 0, 0, -85], [3290, 0, 0, 0], [0, 0, 0, -40], [0, 1524, 0, 0], [0, 0, 0, -86], [0, 0, -1371, 0], [-3458, 0, 0, 0], [0, 972, 0, 0], [5806, 0, 0, 0], [0, 0, -1251, 0], [0, 0, -1287, 0], [0, 0, 0, 104], [0, 0, 761, 0], [0, 0, 0, 5], [0, 0, 0, -265], [0, 0, 3837, 0], [17502, 0, 0, 0], [2874, 0, 0, 0], [0, 0, -3651, 0], [-19330, 0, 0, 0], [0, 0, 177, 0], [0, 862, 0, 0], [0, 0, 2305, 0], [-10458, 0, 0, 0], [0, 0, 0, 309], [0, -1624, 0, 0], [0, 0, 2865, 0], [0, -216, 0, 0], [0, 0, 0, 566], [24054, 0, 0, 0], [0, 0, 4341, 0], [0, -226, 0, 0], [0, -800, 0, 0], [0, 0, 0, 213], [0, 0, 0, -214], [-25538, 0, 0, 0], [0, 0, -5515, 0], [0, 0, 7361, 0], [0, 1930, 0, 0], [0, 0, 0, 319], [11530, 0, 0, 0], [0, -2220, 0, 0], [0, -202, 0, 0], [0, 0, 5177, 0], [0, 0, 0, 107], [-8942, 0, 0, 0], [3670, 0, 0, 0], [0, 0, 0, 200], [0, 0, 8001, 0], [0, -498, 0, 0], [0, 0, 0, -589], [0, 0, -4351, 0], [0, 0, 0, -756], [0, 570, 0, 0], [27850, 0, 0, 0], [-33550, 0, 0, 0], [0, -154, 0, 0], [0, 0, 6757, 0], [0, 250, 0, 0], [1434, 0, 0, 0], [0, 0, 0, -975], [21322, 0, 0, 0], [0, 0, -5387, 0], [0, 0, -2415, 0], [24082, 0, 0, 0], [0, 0, 0, -346], [0, 1950, 0, 0], [0, 1844, 0, 0], [0, 0, 0, 809], [0, 0, -2867, 0], [0, -3210, 0, 0], [-33186, 0, 0, 0], [0, 0, -3799, 0], [0, 0, -759, 0], [0, 0, -1851, 0], [0, 0, 0, 152], [0, 0, 0, -794], [24286, 0, 0, 0], [0, -3528, 0, 0], [-36722, 0, 0, 0], [0, 0, -4091, 0], [0, 724, 0, 0], [0, 0, 4189, 0], [0, 0, 0, 381], [0, 4676, 0, 0], [0, 0, 15153, 0], [0, 0, 0, 376], [0, 5014, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_160_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_160_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_160_4_c_b(:prec:=4)
    chi := MakeCharacter_160_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_160_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_160_4_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_160_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![16, 0, 1]>],Snew);
    return Vf;
end function;