
// Make newform 170.4.a.d in Magma, downloaded from the LMFDB on 29 September 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_d();".
// 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_d();".
// 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 := [-36, -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_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], [14, 0], [-12, 6], [-1, -1], [17, 0], [41, -13], [42, -20], [-21, -1], [113, 13], [188, 2], [54, 20], [422, 2], [-81, -35], [357, -43], [-387, -41], [101, -43], [-250, -18], [-219, -39], [-151, 69], [272, -194], [216, -40], [-909, 71], [107, 219], [-906, -100], [416, -216], [-1188, 114], [29, -107], [339, -69], [161, 359], [282, 252], [-1422, -108], [1508, 58], [-264, -354], [1334, -426], [1022, -184], [2072, -62], [-474, -44], [-1218, 40], [-1236, -348], [-1486, 108], [-1752, 312], [-1042, 476], [-372, 306], [2135, 191], [1490, 640], [4853, 147], [-3069, 315], [-1618, -580], [1995, 323], [414, 594], [-826, 724], [3612, -320], [1794, -1100], [1251, -27], [1761, 25], [-4480, 248], [-766, -1180], [3879, 295], [-397, -285], [1101, 205], [5240, -724], [6744, 2], [-694, 720], [-1368, 102], [1247, 717], [-2383, 973], [6489, 373], [1532, -1614], [5154, 240], [3582, -442], [7706, 476], [4322, 144], [-6040, 974], [-3987, -1205], [2532, 798], [1028, -1038], [4692, -834], [3545, 1613], [-1410, -1132], [-928, -1782], [-8904, -682], [-5494, 12], [1784, 658], [8052, 1120], [-6924, 742], [3278, 1584], [7470, -888], [-12961, -667], [1956, 1980], [-8385, -1489], [-11890, 624], [-969, 3061], [5150, -1220], [-5532, 778], [-12180, -590], [-9660, 2350], [-8026, -194], [-9178, 1920], [713, 3169], [20631, -217], [12006, -1310], [3303, -2169], [-13006, 808], [-10852, -886], [5970, 2318], [10926, -2652], [3594, 1662], [-15184, 954], [2750, -1924], [20639, -797], [5907, -3093], [19874, 432], [-12856, 2436], [630, 460], [-9196, 2666], [15453, -1433], [1230, 640], [23991, -159], [-9412, 922], [19949, 993], [10002, -1252], [10149, 2877], [-208, -3014], [-10572, 2814], [16151, 627], [2013, 4025], [-12265, 1165], [9230, 1704], [-13981, -1911], [-26838, 1632], [-26893, 147], [-5605, 5027], [-5586, -1656], [14573, 3021], [-6630, -2016], [-5335, -2943], [270, 4968], [-36534, 440], [-19570, 1352], [24441, 369], [13382, -2392], [5628, 70], [440, -2442], [-7833, -493], [20522, -3884], [11811, -4037], [-4639, 3619], [-5280, -3976], [-17332, 1394], [4014, 5700], [-250, -946], [16446, 4940], [-6121, -1537], [13728, 1398], [14186, 4350], [-17454, 1720], [-13672, -5702], [-8337, -1381], [34995, 719], [-3132, -6646], [-27736, 852], [-28491, -873], [7488, -5526], [-8142, 8192], [20081, 277], [7706, 7884]];
    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_d();".
// 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_d(: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_d();".
// 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_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_170_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-34, -3, 1]>],Snew);
    return Vf;
end function;