
// Make newform 170.4.a.f in Magma, downloaded from the LMFDB on 02 October 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_170_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_170_4_a_f();".
// 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_170_4_a_f();".
// 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 := [-19, 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_170_a();"
function MakeCharacter_170_a()
    N := 170;
    order := 1;
    char_gens := [137, 71];
    v := [1, 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;

function MakeCharacter_170_a_Hecke( : Kf := Kf)
    return MakeCharacter_170_a();
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 := [[2, 0], [1, 1], [5, 0], [23, 3], [-7, -9], [-16, -16], [-17, 0], [130, -2], [1, 13], [64, -38], [-37, 41], [44, -74], [-224, 10], [-18, 84], [-88, 6], [-106, -64], [-534, 18], [-654, 20], [-380, -18], [37, 211], [572, -142], [117, -45], [322, 76], [938, -34], [122, -68], [-698, 82], [-352, 298], [117, 57], [-50, 192], [352, -190], [150, -4], [-807, -373], [-232, 148], [1995, -239], [-1862, 192], [-1594, -334], [1578, -460], [2329, 129], [1489, -175], [608, -82], [-2150, 386], [1368, 206], [1744, 684], [2268, 354], [944, 734], [-1785, -383], [-969, 733], [-2082, -8], [-2283, 97], [2254, 494], [-4522, -128], [-1220, 992], [760, -510], [-3488, -572], [192, -360], [-4598, 100], [3362, 4], [4820, 316], [2318, 1356], [-7646, -156], [-1091, -99], [5302, 852], [1620, 230], [-5099, -53], [4920, -674], [798, -1844], [7534, -870], [-1770, 452], [793, 1021], [4526, 116], [-4154, 1204], [1518, 966], [-3053, 1527], [1224, -1360], [5049, -481], [1278, 980], [-9486, -618], [4910, -560], [-7922, 44], [18, -1856], [-2353, -1199], [814, -3034], [2185, -677], [-11468, 468], [6157, -145], [-648, -318], [-1176, 78], [-8848, -880], [-7338, 884], [6420, -146], [-2034, -1524], [6967, 193], [10591, -2045], [2882, -98], [2199, 3649], [3071, 519], [10878, -856], [-326, 1856], [2364, -254], [-2864, 638], [-17019, -1759], [804, -2588], [-1714, 1504], [894, -3348], [-3733, 2513], [-10484, 1664], [14250, -2332], [1310, -1040], [-1464, -4044], [-18504, 666], [-3089, 2403], [6282, -2240], [4496, 1498], [6225, -2437], [6610, 466], [-1344, -1338], [15165, -2531], [18504, 1310], [-4860, -2406], [-6416, 3496], [-4918, 744], [-16836, 2950], [10006, -940], [16113, 3729], [-6839, -1737], [-6754, -3866], [-400, 1562], [-10287, 1723], [2484, 4934], [-5902, 1792], [-26542, -146], [-6909, -5453], [5669, -6557], [-10768, -4524], [-9794, -4870], [8826, -4930], [-2360, 7868], [-1713, 687], [-31162, 2252], [-4146, 1512], [415, -6839], [558, 156], [-23939, -2183], [-18637, -4797], [16694, -2688], [-11889, -2235], [-12752, 1114], [24360, -2602], [-15134, 7470], [-13480, 3078], [19722, 764], [13728, 4650], [30788, -762], [17979, 1983], [-27971, 4905], [-23405, 3949], [-7904, -5664], [2544, -2862], [1186, 1636], [21072, -5726], [20359, -2801], [9564, 2472], [13990, -7880], [1330, -9798], [18270, 1248], [-25031, 1117], [-5191, -8625], [31456, 6966]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_170_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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