
// Make newform 169.4.e.f in Magma, downloaded from the LMFDB on 24 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_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_169_4_e_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 := [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], [0, 1296, 0, -585, 0, 65, 0, -9], [0, 4176, 0, -1885, 0, 325, 0, -29], [4176, 0, -1885, 0, 325, 0, -29, 0]];
    Rf_basisdens := [1, 1, 1040, 130, 65, 1040, 2080, 1040];
    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 := [[1, 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, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, -3], [0, -1, 0, -1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 11, 5, 0], [0, -12, 0, 17, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [18, 0, 1, 0, 17, 0, 0, -17], [0, 0, 0, 0, 0, 32, -13, 0], [0, 0, -92, 0, 0, 0, 0, 12], [0, 0, 26, 0, 0, 0, 0, 96], [0, 34, 0, -13, 0, -34, 13, 0], [0, -5, 0, 51, 0, 0, 0, 0], [0, 22, 0, 63, 0, 0, 0, 0], [72, 0, 215, 0, -143, 0, 0, 143], [0, -121, 0, -139, 0, 121, 139, 0], [-74, 0, 0, 0, -30, 0, 0, 0], [0, 0, 0, 0, 0, 124, 123, 0], [434, 0, 624, 0, -190, 0, 0, 190], [0, -232, 0, -75, 0, 0, 0, 0], [0, 0, 0, 0, 0, 231, 25, 0], [0, 260, 0, -49, 0, -260, 49, 0], [-524, 0, 0, 0, -40, 0, 0, 0], [0, 182, 0, 535, 0, -182, -535, 0], [0, 388, 0, -83, 0, 0, 0, 0], [0, 0, 0, 0, 0, -508, -359, 0], [0, 0, -700, 0, 0, 0, 0, -142], [152, 0, 0, 0, -304, 0, 0, 0], [0, 0, 1076, 0, 0, 0, 0, -140], [0, 43, 0, 391, 0, -43, -391, 0], [-1246, 0, -1010, 0, -236, 0, 0, 236], [0, 0, 16, 0, 0, 0, 0, 344], [604, 0, 0, 0, -567, 0, 0, 0], [0, 0, 0, 0, 0, 202, 1163, 0], [676, 0, -171, 0, 847, 0, 0, -847], [0, 0, 0, 0, 0, 244, -1067, 0], [0, 441, 0, 1053, 0, -441, -1053, 0], [1802, 0, 0, 0, 924, 0, 0, 0], [0, 0, 0, 0, 0, -430, -1305, 0], [0, -902, 0, -859, 0, 0, 0, 0], [-1642, 0, -2756, 0, 1114, 0, 0, -1114], [0, 0, 109, 0, 0, 0, 0, 29], [358, 0, 0, 0, 558, 0, 0, 0], [-2916, 0, -2042, 0, -874, 0, 0, 874], [0, -206, 0, 397, 0, 0, 0, 0], [0, 1395, 0, 1153, 0, 0, 0, 0], [680, 0, 1350, 0, -670, 0, 0, 670], [0, 0, 747, 0, 0, 0, 0, -537], [0, 1737, 0, 1607, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1018, 347, 0], [0, -101, 0, -235, 0, 101, 235, 0], [-1334, 0, 0, 0, -621, 0, 0, 0], [0, 993, 0, -503, 0, -993, 503, 0], [0, 0, 0, 0, 0, -2536, -733, 0], [-3516, 0, -2424, 0, -1092, 0, 0, 1092], [0, 0, -1045, 0, 0, 0, 0, -151], [0, 0, 4476, 0, 0, 0, 0, 480], [-834, 0, -3024, 0, 2190, 0, 0, -2190], [0, 1193, 0, -437, 0, 0, 0, 0], [2830, 0, -572, 0, 3402, 0, 0, -3402], [0, 484, 0, -1145, 0, -484, 1145, 0], [0, 0, 4260, 0, 0, 0, 0, -1056], [0, 0, 0, 0, 0, -1487, 841, 0], [0, -1748, 0, 1415, 0, 1748, -1415, 0], [-584, 0, 0, 0, 2858, 0, 0, 0], [1434, 0, 0, 0, -3345, 0, 0, 0], [0, 2580, 0, 189, 0, -2580, -189, 0], [0, 0, 0, 0, 0, 122, -2161, 0], [1686, 0, 0, 0, 1841, 0, 0, 0], [-7332, 0, -5579, 0, -1753, 0, 0, 1753], [0, -409, 0, 3259, 0, 0, 0, 0], [0, 1650, 0, 3283, 0, 0, 0, 0], [0, 54, 0, 1335, 0, -54, -1335, 0], [0, 0, 5238, 0, 0, 0, 0, 858], [2246, 0, 236, 0, 2010, 0, 0, -2010], [0, 378, 0, 1417, 0, 0, 0, 0], [0, 0, 0, 0, 0, -2721, 1933, 0], [-1730, 0, 0, 0, -4392, 0, 0, 0], [0, 0, 0, 0, 0, -696, 4163, 0], [0, 530, 0, 1721, 0, 0, 0, 0], [0, 0, 0, 0, 0, 5646, 2373, 0], [0, 0, 2793, 0, 0, 0, 0, 6781], [0, 6139, 0, 3131, 0, -6139, -3131, 0], [0, -1417, 0, -4241, 0, 0, 0, 0], [-13098, 0, -11189, 0, -1909, 0, 0, 1909], [0, 0, -426, 0, 0, 0, 0, 2190], [10028, 0, 0, 0, 111, 0, 0, 0], [0, 0, 0, 0, 0, 1276, 1915, 0], [0, 2216, 0, -4041, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1669, 3867, 0], [0, 1734, 0, -4597, 0, -1734, 4597, 0], [-11404, 0, 0, 0, -1008, 0, 0, 0], [0, 2137, 0, -4573, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1070, -3529, 0], [0, 0, 953, 0, 0, 0, 0, 6329], [0, -178, 0, 1067, 0, 178, -1067, 0], [5812, 0, 10206, 0, -4394, 0, 0, 4394], [0, -112, 0, 363, 0, 0, 0, 0], [-11030, 0, 0, 0, 1303, 0, 0, 0], [0, 0, -9068, 0, 0, 0, 0, -4580], [0, 5763, 0, -1415, 0, -5763, 1415, 0], [7172, 0, 0, 0, -1301, 0, 0, 0], [0, 797, 0, 767, 0, 0, 0, 0], [1744, 0, -281, 0, 2025, 0, 0, -2025], [0, 0, -11347, 0, 0, 0, 0, -8085], [-1648, 0, 0, 0, -3555, 0, 0, 0], [0, -4152, 0, -2483, 0, 4152, 2483, 0], [0, 1566, 0, 1497, 0, 0, 0, 0], [0, 4416, 0, 10755, 0, -4416, -10755, 0], [-1772, 0, 0, 0, -5578, 0, 0, 0], [0, 0, 7193, 0, 0, 0, 0, 1559], [-9572, 0, -8438, 0, -1134, 0, 0, 1134], [0, -9088, 0, -97, 0, 0, 0, 0], [0, 0, 0, 0, 0, 4610, 7429, 0], [0, -1534, 0, -5613, 0, 1534, 5613, 0], [0, 0, 0, 0, 0, -1235, 10333, 0], [23590, 0, 19614, 0, 3976, 0, 0, -3976], [0, 0, 0, 0, 0, -6848, -6145, 0], [0, 0, -3994, 0, 0, 0, 0, -11282], [0, 0, -9600, 0, 0, 0, 0, -7682], [-1360, 0, 5668, 0, -7028, 0, 0, 7028], [0, -9636, 0, 1149, 0, 0, 0, 0], [0, 0, -1769, 0, 0, 0, 0, -6007], [7254, 0, 0, 0, 1538, 0, 0, 0], [0, 0, 0, 0, 0, 1986, -9709, 0], [0, -5846, 0, 3389, 0, 0, 0, 0], [-7954, 0, 0, 0, 10266, 0, 0, 0], [0, 0, 0, 0, 0, 1964, 5279, 0], [8656, 0, 3802, 0, 4854, 0, 0, -4854], [-22248, 0, 0, 0, -1238, 0, 0, 0], [0, -1199, 0, 2463, 0, 1199, -2463, 0], [0, -8978, 0, 2993, 0, 0, 0, 0], [0, -729, 0, 8639, 0, 0, 0, 0], [0, 0, 22856, 0, 0, 0, 0, 1704], [0, 0, -10644, 0, 0, 0, 0, -4218], [0, 0, 0, 0, 0, -7068, -7881, 0], [0, -474, 0, 2943, 0, 0, 0, 0], [0, 0, 0, 0, 0, 131, 6303, 0], [0, 0, 0, 0, 0, -5978, -9369, 0], [14234, 0, 4078, 0, 10156, 0, 0, -10156], [0, 0, 2531, 0, 0, 0, 0, 10489], [0, 1522, 0, 8351, 0, -1522, -8351, 0], [0, 4201, 0, -4287, 0, 0, 0, 0], [-5584, 0, -4234, 0, -1350, 0, 0, 1350], [0, -540, 0, 2803, 0, 540, -2803, 0], [0, 0, 12718, 0, 0, 0, 0, 12764], [0, 0, 0, 0, 0, 6446, 10275, 0], [0, 595, 0, -3295, 0, -595, 3295, 0], [-17242, 0, 0, 0, -15436, 0, 0, 0], [-8920, 0, 0, 0, -16580, 0, 0, 0], [0, 1397, 0, -5271, 0, -1397, 5271, 0], [0, 0, 0, 0, 0, -9371, -12973, 0], [0, 0, -3465, 0, 0, 0, 0, 2725], [2552, 0, 0, 0, -13307, 0, 0, 0], [0, 0, -34184, 0, 0, 0, 0, -4164], [0, 0, 18077, 0, 0, 0, 0, -4875], [-33844, 0, 0, 0, 690, 0, 0, 0], [-13260, 0, -8504, 0, -4756, 0, 0, 4756], [0, 0, 0, 0, 0, -2450, 24729, 0], [-12246, 0, 0, 0, -3532, 0, 0, 0], [0, -13027, 0, -12273, 0, 13027, 12273, 0], [0, -2964, 0, 10349, 0, 0, 0, 0], [-2514, 0, -6373, 0, 3859, 0, 0, -3859], [0, -2509, 0, -16547, 0, 2509, 16547, 0], [-30852, 0, -38101, 0, 7249, 0, 0, -7249], [0, -7136, 0, -9529, 0, 0, 0, 0], [0, 10859, 0, 10909, 0, -10859, -10909, 0], [0, 0, -4926, 0, 0, 0, 0, 8902], [19586, 0, 802, 0, 18784, 0, 0, -18784]];
    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_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_169_4_e_f(: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_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_169_4_e_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![256, 0, -144, 0, 65, 0, -9, 0, 1]>],Snew);
    return Vf;
end function;