
// Make newform 160.4.n.b in Magma, downloaded from the LMFDB on 17 March 2026.
// To make the character of type GrpDrchElt, type "MakeCharacter_160_n();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_160_n_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_n_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_n_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, 0, 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_160_n();"
function MakeCharacter_160_n()
    N := 160;
    order := 4;
    char_gens := [31, 101, 97];
    v := [2, 4, 3];
    // 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_n_Hecke();"
function MakeCharacter_160_n_Hecke( : Kf := false)
    N := 160;
    order := 4;
    char_gens := [31, 101, 97];
    char_values := [[-1, 0], [1, 0], [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 := 4;
    raw_aps := [[0, 0], [2, 2], [10, 5], [18, -18], [0, -16], [-33, 33], [67, 67], [116, 0], [-110, -110], [0, 132], [0, -68], [65, 65], [-304, 0], [154, 154], [-306, 306], [217, -217], [-204, 0], [-748, 0], [166, -166], [0, 524], [277, -277], [-1232, 0], [-290, -290], [0, -800], [-651, -651], [-894, 0], [-534, -534], [1014, -1014], [0, -574], [-489, 489], [1690, -1690], [0, 2392], [1921, 1921], [-3132, 0], [0, 1862], [0, -852], [311, 311], [482, 482], [-1810, 1810], [-1739, 1739], [-2148, 0], [3790, 0], [0, 460], [1923, -1923], [653, 653], [-200, 0], [0, 4960], [-3482, -3482], [986, -986], [0, -1196], [-1821, 1821], [-664, 0], [-3776, 0], [0, 3288], [-1177, -1177], [1462, 1462], [0, 3134], [0, -8492], [2807, 2807], [5640, 0], [4870, 4870], [1619, -1619], [4166, -4166], [0, -6012], [255, -255], [-2125, -2125], [0, -4912], [5337, 5337], [1698, -1698], [0, 268], [-1095, 1095], [5296, 0], [1914, -1914], [-2163, 2163], [-7124, 0], [454, 454], [0, 13082], [-2907, -2907], [9410, 0], [0, -16270], [-3772, 0], [-6788, 0], [0, 11652], [165, -165], [3248, 0], [-12106, -12106], [0, -7194], [-705, -705], [9898, 0], [2746, 2746], [-2990, 2990], [-536, 0], [4330, -4330], [0, -10688], [-14380, 0], [2522, 2522], [0, -5700], [-16202, 0], [5550, 5550], [11130, 0], [-6326, 6326], [11505, 11505], [-1830, -1830], [0, -23730], [0, -3104], [17759, 17759], [-350, 350], [19361, -19361], [-2856, 0], [3224, 0], [11230, -11230], [4839, -4839], [3163, 3163], [19668, 0], [0, 10660], [6432, 0], [-1482, -1482], [6682, -6682], [15185, -15185], [16356, 0], [-16380, 0], [15597, -15597], [405, 405], [-4054, -4054], [0, 27760], [13372, 0], [0, -22356], [-27544, 0], [15330, -15330], [-14067, 14067], [5308, 0], [-11058, -11058], [0, 6476], [8143, 8143], [-18150, 0], [0, -30152], [4255, -4255], [-8242, 8242], [-12709, -12709], [0, 22600], [0, -11880], [9868, 0], [25106, 25106], [-12974, 12974], [0, 17966], [27624, 0], [26031, -26031], [-31415, -31415], [-24620, 0], [-13550, -13550], [-21281, -21281], [-35304, 0], [-8886, -8886], [-4166, 4166], [12726, -12726], [0, -20100], [11400, 0], [0, 13498], [26499, 26499], [23798, 0], [-11098, 11098], [-7753, 7753], [24806, -24806], [0, 1496], [-4419, -4419], [-9202, -9202], [0, 1444], [5779, 5779]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_160_n_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_160_4_n_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_n_b(:prec:=2)
    chi := MakeCharacter_160_n();
    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_n_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_n_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_160_n();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![8, -4, 1]>],Snew);
    return Vf;
end function;