
// Make newform 169.4.c.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_169_c_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_c_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_c_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 := [16, 4, 5, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [16, -5, 5, -1], [4, 0, 0, 1]];
    Rf_basisdens := [1, 1, 20, 5];
    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_c();"
function MakeCharacter_169_c()
    N := 169;
    order := 3;
    char_gens := [2];
    v := [2];
    // 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_c_Hecke();"
function MakeCharacter_169_c_Hecke(Kf)
    N := 169;
    order := 3;
    char_gens := [2];
    char_values := [[-1, 0, 1, 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, 3, -4, 0], [-2, 0, 0, -1], [10, 11, -10, 11], [0, -12, -34, 0], [0, 0, 0, 0], [-18, -17, 18, -17], [26, -32, -26, -32], [0, 12, -104, 0], [0, -96, 70, 0], [-26, 0, 0, 34], [0, -5, -102, 0], [0, -22, 126, 0], [-72, 143, 72, 143], [278, 0, 0, 121], [-74, 0, 0, -30], [246, 124, -246, 124], [434, -190, -434, -190], [0, 232, -150, 0], [-50, -231, 50, -231], [98, 0, 0, -260], [-524, 0, 0, -40], [1070, 0, 0, 182], [0, 388, 166, 0], [718, 508, -718, 508], [0, -142, -558, 0], [-152, 0, 0, 304], [0, 140, -1216, 0], [782, 0, 0, 43], [-1246, -236, 1246, -236], [0, 344, -328, 0], [604, 0, 0, -567], [2326, 202, -2326, 202], [676, 847, -676, 847], [2134, -244, -2134, -244], [-2106, 0, 0, -441], [1802, 0, 0, 924], [-2610, -430, 2610, -430], [0, -902, 1718, 0], [1642, -1114, -1642, -1114], [0, 29, 80, 0], [-358, 0, 0, -558], [-2916, -874, 2916, -874], [0, -206, -794, 0], [0, -1395, 2306, 0], [-680, 670, 680, 670], [0, 537, -1284, 0], [0, -1737, 3214, 0], [-694, 1018, 694, 1018], [470, 0, 0, 101], [1334, 0, 0, 621], [-1006, 0, 0, 993], [-1466, -2536, 1466, -2536], [3516, 1092, -3516, 1092], [0, -151, -894, 0], [0, -480, -3996, 0], [-834, 2190, 834, 2190], [0, 1193, 874, 0], [-2830, -3402, 2830, -3402], [2290, 0, 0, -484], [0, -1056, 5316, 0], [1682, -1487, -1682, -1487], [-2830, 0, 0, 1748], [584, 0, 0, -2858], [1434, 0, 0, -3345], [378, 0, 0, 2580], [4322, -122, -4322, -122], [-1686, 0, 0, -1841], [-7332, -1753, 7332, -1753], [0, -409, -6518, 0], [0, -1650, 6566, 0], [-2670, 0, 0, -54], [0, -858, -4380, 0], [2246, 2010, -2246, 2010], [0, -378, 2834, 0], [-3866, 2721, 3866, 2721], [1730, 0, 0, 4392], [8326, -696, -8326, -696], [0, 530, -3442, 0], [-4746, -5646, 4746, -5646], [0, -6781, 3988, 0], [6262, 0, 0, 6139], [0, 1417, -8482, 0], [13098, 1909, -13098, 1909], [0, 2190, -2616, 0], [10028, 0, 0, 111], [3830, 1276, -3830, 1276], [0, -2216, -8082, 0], [-7734, 1669, 7734, 1669], [9194, 0, 0, -1734], [11404, 0, 0, 1008], [0, 2137, 9146, 0], [7058, -1070, -7058, -1070], [0, 6329, -5376, 0], [2134, 0, 0, -178], [5812, -4394, -5812, -4394], [0, 112, 726, 0], [-11030, 0, 0, 1303], [0, 4580, 4488, 0], [2830, 0, 0, -5763], [7172, 0, 0, -1301], [0, 797, -1534, 0], [-1744, -2025, 1744, -2025], [0, -8085, -3262, 0], [1648, 0, 0, 3555], [-4966, 0, 0, -4152], [0, -1566, 2994, 0], [-21510, 0, 0, -4416], [-1772, 0, 0, -5578], [0, -1559, -5634, 0], [-9572, -1134, 9572, -1134], [0, 9088, -194, 0], [-14858, -4610, 14858, -4610], [11226, 0, 0, 1534], [20666, -1235, -20666, -1235], [-23590, -3976, 23590, -3976], [12290, 6848, -12290, 6848], [0, -11282, 7288, 0], [0, 7682, 1918, 0], [-1360, -7028, 1360, -7028], [0, -9636, -2298, 0], [0, -6007, 4238, 0], [7254, 0, 0, 1538], [-19418, 1986, 19418, 1986], [0, 5846, 6778, 0], [7954, 0, 0, -10266], [10558, 1964, -10558, 1964], [-8656, -4854, 8656, -4854], [22248, 0, 0, 1238], [4926, 0, 0, -1199], [0, -8978, -5986, 0], [0, 729, 17278, 0], [0, 1704, 21152, 0], [0, 4218, 6426, 0], [-15762, -7068, 15762, -7068], [0, 474, 5886, 0], [-12606, -131, 12606, -131], [-18738, -5978, 18738, -5978], [-14234, -10156, 14234, -10156], [0, -10489, 7958, 0], [16702, 0, 0, 1522], [0, -4201, -8574, 0], [5584, 1350, -5584, 1350], [-5606, 0, 0, 540], [0, 12764, -46, 0], [20550, 6446, -20550, 6446], [6590, 0, 0, -595], [17242, 0, 0, 15436], [-8920, 0, 0, -16580], [-10542, 0, 0, 1397], [25946, 9371, -25946, 9371], [0, 2725, -6190, 0], [-2552, 0, 0, 13307], [0, 4164, 30020, 0], [0, -4875, 22952, 0], [-33844, 0, 0, 690], [-13260, -4756, 13260, -4756], [-49458, 2450, 49458, 2450], [-12246, 0, 0, -3532], [-24546, 0, 0, -13027], [0, -2964, -20698, 0], [2514, -3859, -2514, -3859], [-33094, 0, 0, -2509], [-30852, 7249, 30852, 7249], [0, 7136, -19058, 0], [-21818, 0, 0, -10859], [0, -8902, 13828, 0], [19586, 18784, -19586, 18784]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_169_4_c_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_c_g(:prec:=4)
    chi := MakeCharacter_169_c();
    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_c_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_c_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_169_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![16, -4, 5, 1, 1]>],Snew);
    return Vf;
end function;