
// Make newform 169.4.e.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_169_e_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_169_4_e_g();".
// 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_169_4_e_g();".
// 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 := [256, 0, -144, 0, 65, 0, -9, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [-1296, 0, 585, 0, -65, 0, 9, 0], [0, -181, 0, 0, 0, 0, 0, -1], [116, 0, 0, 0, 0, 0, 1, 0], [4176, 0, -1885, 0, 325, 0, -29, 0], [0, -256, 0, 585, 0, -65, 0, 9], [0, -256, 0, 377, 0, -65, 0, 9]];
    Rf_basisdens := [1, 1, 1040, 260, 65, 1040, 1040, 832];
    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_169_e();"
function MakeCharacter_169_e()
    N := 169;
    order := 6;
    char_gens := [2];
    v := [5];
    // 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_169_e_Hecke();"
function MakeCharacter_169_e_Hecke(Kf)
    N := 169;
    order := 6;
    char_gens := [2];
    char_values := [[0, 0, -1, 0, 0, 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, -1, 0, -2, 0, 0, 1, -2], [-1, 0, -4, 0, 3, -3, 0, 0], [0, 0, 0, 0, 0, 0, 5, 10], [0, 1, 0, 7, 0, 0, 0, 0], [0, -15, 0, -1, 0, 0, 15, -1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, -27, 0, 0, 16, 0, 0], [0, -5, 0, 73, 0, 0, 0, 0], [89, 0, 56, 0, 33, -33, 0, 0], [13, 0, -47, 0, 60, -60, 0, 0], [0, 0, 0, 0, 0, 0, 100, 120], [0, 44, 0, 73, 0, 0, -44, 73], [0, 20, 0, 259, 0, 0, -20, 259], [0, 0, -276, 0, 0, -97, 0, 0], [0, 0, 0, 0, 0, 0, 140, 100], [190, 0, 0, 0, -165, 0, 0, 0], [0, -55, 0, -377, 0, 0, 0, 0], [0, 0, 151, 0, 0, -200, 0, 0], [0, 91, 0, -283, 0, 0, -91, -283], [0, -105, 0, -11, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -85, 250], [140, 0, 0, 0, 40, 0, 0, 0], [0, 0, 0, 0, 0, 0, -100, -180], [0, 125, 0, -523, 0, 0, -125, -523], [0, 469, 0, -27, 0, 0, 0, 0], [151, 0, 471, 0, -320, 320, 0, 0], [1080, 0, 0, 0, -320, 0, 0, 0], [367, 0, 968, 0, -601, 601, 0, 0], [0, 0, 0, 0, 0, 0, -260, 230], [0, 0, 271, 0, 0, -388, 0, 0], [1853, 0, 1372, 0, 481, -481, 0, 0], [820, 0, 0, 0, 840, 0, 0, 0], [0, -16, 0, 663, 0, 0, 0, 0], [0, 0, -52, 0, 0, 245, 0, 0], [0, -40, 0, 1913, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 540, 960], [254, 0, 0, 0, -417, 0, 0, 0], [0, -1103, 0, -781, 0, 0, 0, 0], [0, 1721, 0, -993, 0, 0, -1721, -993], [0, 0, -1994, 0, 0, -223, 0, 0], [473, 0, 28, 0, 445, -445, 0, 0], [970, 0, 0, 0, 105, 0, 0, 0], [0, 0, 1824, 0, 0, -335, 0, 0], [0, -352, 0, 1421, 0, 0, 352, 1421], [0, 1731, 0, -1313, 0, 0, -1731, -1313], [0, 0, 832, 0, 0, -1325, 0, 0], [111, 0, 1056, 0, -945, 945, 0, 0], [0, -33, 0, -1111, 0, 0, 33, -1111], [0, 1051, 0, -1823, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -1700, 330], [4162, 0, 0, 0, -276, 0, 0, 0], [0, 0, 0, 0, 0, 0, -300, -5576], [0, 2680, 0, -989, 0, 0, 0, 0], [0, 0, -5736, 0, 0, -45, 0, 0], [-1493, 0, -1377, 0, -116, 116, 0, 0], [3831, 0, 2244, 0, 1587, -1587, 0, 0], [0, 0, -2598, 0, 0, -45, 0, 0], [0, -695, 0, -3921, 0, 0, 695, -3921], [0, 0, 5897, 0, 0, 804, 0, 0], [0, 0, 0, 0, 0, 0, -725, -7990], [2601, 0, 2904, 0, -303, 303, 0, 0], [0, -3148, 0, 3359, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -2120, -300], [-5080, 0, 0, 0, 1120, 0, 0, 0], [-3450, 0, 0, 0, 1980, 0, 0, 0], [0, 0, 0, 0, 0, 0, -2355, 630], [0, 2815, 0, -3299, 0, 0, 0, 0], [-2690, 0, 0, 0, -1295, 0, 0, 0], [0, 0, -4368, 0, 0, 349, 0, 0], [0, -815, 0, -1323, 0, 0, 815, -1323], [0, -3552, 0, 3511, 0, 0, 3552, 3511], [0, 0, 0, 0, 0, 0, 1200, 5304], [807, 0, 3888, 0, -3081, 3081, 0, 0], [0, 0, 3019, 0, 0, 5088, 0, 0], [0, 5355, 0, 193, 0, 0, -5355, 193], [0, -1653, 0, 4729, 0, 0, 0, 0], [-3890, 0, 0, 0, -3135, 0, 0, 0], [0, -3189, 0, 6287, 0, 0, 0, 0], [0, 280, 0, -1139, 0, 0, -280, -1139], [0, 1020, 0, -5163, 0, 0, 0, 0], [-11373, 0, -11768, 0, 395, -395, 0, 0], [0, 0, 0, 0, 0, 0, 625, -8862], [0, 2455, 0, -10251, 0, 0, -2455, -10251], [0, 0, 4509, 0, 0, 2488, 0, 0], [7677, 0, 8652, 0, -975, 975, 0, 0], [-11260, 0, 0, 0, -2100, 0, 0, 0], [0, 4295, 0, -337, 0, 0, 0, 0], [0, -7016, 0, 3143, 0, 0, 7016, 3143], [0, 5860, 0, -5299, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1080, 14120], [6920, 0, 0, 0, 5220, 0, 0, 0], [0, 2225, 0, 3867, 0, 0, -2225, 3867], [0, 1111, 0, -7763, 0, 0, 0, 0], [-59, 0, 1116, 0, -1175, 1175, 0, 0], [0, 0, 0, 0, 0, 0, 6500, 3220], [0, 0, 2924, 0, 0, -1027, 0, 0], [0, 4540, 0, 9287, 0, 0, -4540, 9287], [6738, 0, 0, 0, 6575, 0, 0, 0], [6299, 0, 1056, 0, 5243, -5243, 0, 0], [0, 0, 0, 0, 0, 0, -7785, -3110], [5860, 0, 0, 0, 6020, 0, 0, 0], [0, 3004, 0, 15133, 0, 0, -3004, 15133], [0, 0, -16144, 0, 0, -3513, 0, 0], [-1253, 0, -4898, 0, 3645, -3645, 0, 0], [-5620, 0, 0, 0, -2880, 0, 0, 0], [0, 0, 0, 0, 0, 0, -4047, 3214], [0, -5001, 0, 11853, 0, 0, 5001, 11853], [0, 0, 0, 0, 0, 0, -81, 6522], [6380, 0, 0, 0, 1600, 0, 0, 0], [-14291, 0, -11211, 0, -3080, 3080, 0, 0], [0, 0, 8012, 0, 0, -3351, 0, 0], [0, 1492, 0, 18799, 0, 0, -1492, 18799], [0, -2876, 0, -14607, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 2840, 14420], [0, -9175, 0, 4619, 0, 0, 0, 0], [0, 0, 11189, 0, 0, -4840, 0, 0], [0, -3973, 0, 20949, 0, 0, 0, 0], [9997, 0, 12008, 0, -2011, 2011, 0, 0], [2229, 0, -1514, 0, 3743, -3743, 0, 0], [0, 0, 19612, 0, 0, 5585, 0, 0], [0, 6000, 0, -16479, 0, 0, -6000, -16479], [-1081, 0, 7531, 0, -8612, 8612, 0, 0], [19210, 0, 0, 0, -4580, 0, 0, 0], [0, -5967, 0, 20711, 0, 0, 0, 0], [0, -8305, 0, -12519, 0, 0, 8305, -12519], [14150, 0, 0, 0, 8220, 0, 0, 0], [0, 1720, 0, -6413, 0, 0, 0, 0], [0, 0, 10328, 0, 0, 1575, 0, 0], [-8920, 0, 0, 0, -5860, 0, 0, 0], [0, 0, 0, 0, 0, 0, -17855, -6890], [0, 2225, 0, -20157, 0, 0, -2225, -20157], [0, -8193, 0, 19709, 0, 0, 8193, 19709], [-3671, 0, -7316, 0, 3645, -3645, 0, 0], [-3747, 0, 1902, 0, -5649, 5649, 0, 0], [0, 5415, 0, -249, 0, 0, 0, 0], [0, 3255, 0, -16833, 0, 0, -3255, -16833], [0, -13907, 0, 1361, 0, 0, 0, 0], [0, 941, 0, -15053, 0, 0, 0, 0], [0, 0, -7418, 0, 0, -7471, 0, 0], [10573, 0, 1913, 0, 8660, -8660, 0, 0], [0, 0, 0, 0, 0, 0, -1700, -13968], [0, 2795, 0, -33641, 0, 0, -2795, -33641], [0, 0, -2576, 0, 0, -7197, 0, 0], [0, 0, 0, 0, 0, 0, 13728, -9236], [5843, 0, 13063, 0, -7220, 7220, 0, 0], [0, -4055, 0, -14293, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 5755, 4590], [18630, 0, 0, 0, 8065, 0, 0, 0], [-4820, 0, 0, 0, -4480, 0, 0, 0], [0, 0, 0, 0, 0, 0, 11516, -16192], [0, 3176, 0, -38583, 0, 0, 0, 0], [11691, 0, 19831, 0, -8140, 8140, 0, 0], [11140, 0, 0, 0, 15140, 0, 0, 0], [-46117, 0, -43768, 0, -2349, 2349, 0, 0], [-3143, 0, -16292, 0, 13149, -13149, 0, 0], [25760, 0, 0, 0, -3420, 0, 0, 0], [0, 0, 2028, 0, 0, 13765, 0, 0], [0, -160, 0, 24973, 0, 0, 0, 0], [-21350, 0, 0, 0, -3605, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1760, -10390], [0, -2499, 0, -1033, 0, 0, 2499, -1033], [0, 0, 9954, 0, 0, 8693, 0, 0], [0, 0, 0, 0, 0, 0, 10040, 2160], [0, 0, 15564, 0, 0, -10135, 0, 0], [0, 15716, 0, -7113, 0, 0, -15716, -7113], [0, 0, 0, 0, 0, 0, -8500, -26640], [-3069, 0, -6764, 0, 3695, -3695, 0, 0], [0, 0, -5897, 0, 0, 1496, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_169_4_e_g();".
// 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_169_4_e_g(:prec:=8)
    chi := MakeCharacter_169_e();
    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_169_4_e_g();".
// 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_169_4_e_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![16, 0, -84, 0, 437, 0, -21, 0, 1]>],Snew);
    return Vf;
end function;