
// Make newform 169.4.a.f in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_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_169_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_169_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 := [-4, -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_169_a();"
function MakeCharacter_169_a()
    N := 169;
    order := 1;
    char_gens := [2];
    v := [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_169_a_Hecke(Kf)
    return MakeCharacter_169_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+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 := [[-2, -1], [1, 3], [-10, 5], [7, 1], [1, 15], [0, 0], [43, -16], [-73, 5], [89, -33], [-13, 60], [-120, 100], [-73, -44], [259, 20], [179, 97], [100, -140], [190, 165], [-377, -55], [351, -200], [-283, 91], [11, 105], [250, 85], [140, -40], [180, -100], [523, -125], [27, -469], [151, 320], [-1080, -320], [-367, -601], [-230, -260], [659, -388], [1853, -481], [820, -840], [663, -16], [-297, 245], [-1913, 40], [960, -540], [254, 417], [-781, -1103], [993, -1721], [1771, 223], [473, -445], [-970, 105], [2159, -335], [-1421, 352], [-1313, 1731], [-2157, 1325], [-111, -945], [-1111, -33], [1823, -1051], [330, 1700], [-4162, -276], [5576, -300], [-989, 2680], [5691, 45], [-1493, 116], [-3831, 1587], [-2553, -45], [3921, 695], [-5093, -804], [-7990, 725], [2601, 303], [3359, -3148], [-300, 2120], [5080, 1120], [-3450, -1980], [-630, -2355], [3299, -2815], [2690, -1295], [-4717, 349], [1323, 815], [3511, -3552], [5304, -1200], [-807, -3081], [-2069, 5088], [193, 5355], [-4729, 1653], [3890, -3135], [6287, -3189], [1139, -280], [5163, -1020], [11373, 395], [8862, 625], [-10251, 2455], [-2021, -2488], [7677, 975], [-11260, 2100], [-337, 4295], [3143, -7016], [5299, -5860], [14120, -1080], [-6920, 5220], [-3867, -2225], [7763, -1111], [-59, 1175], [-3220, 6500], [3951, -1027], [9287, 4540], [6738, -6575], [-6299, 5243], [-3110, 7785], [5860, -6020], [-15133, -3004], [12631, 3513], [-1253, -3645], [5620, -2880], [-3214, -4047], [11853, -5001], [6522, 81], [6380, -1600], [14291, -3080], [11363, -3351], [18799, 1492], [14607, 2876], [14420, -2840], [4619, -9175], [-16029, 4840], [-20949, 3973], [9997, 2011], [-2229, 3743], [14027, 5585], [16479, -6000], [-1081, 8612], [19210, 4580], [20711, -5967], [-12519, -8305], [-14150, 8220], [-6413, 1720], [-8753, -1575], [8920, -5860], [6890, -17855], [20157, -2225], [19709, -8193], [-3671, -3645], [3747, -5649], [-249, 5415], [-16833, 3255], [-1361, 13907], [-15053, 941], [-53, 7471], [-10573, 8660], [13968, -1700], [-33641, 2795], [-4621, 7197], [-9236, -13728], [5843, 7220], [-14293, -4055], [4590, -5755], [-18630, 8065], [-4820, 4480], [16192, 11516], [38583, -3176], [11691, 8140], [-11140, 15140], [46117, -2349], [-3143, -13149], [25760, 3420], [-11737, 13765], [-24973, 160], [-21350, 3605], [10390, 1760], [1033, 2499], [-1261, -8693], [-2160, 10040], [25699, -10135], [-7113, 15716], [-26640, 8500], [3069, 3695], [-7393, 1496]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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