
// Make newform 169.4.b.e in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_169_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_169_b_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_b_e();".
// 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_b_e();".
// 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, 0, 9, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [0, 5, 0, 1], [5, 0, 1, 0]];
    Rf_basisdens := [1, 1, 4, 1];
    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_b();"
function MakeCharacter_169_b()
    N := 169;
    order := 2;
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_169_b_Hecke();"
function MakeCharacter_169_b_Hecke(Kf)
    N := 169;
    order := 2;
    char_gens := [2];
    char_values := [[-1, 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, 3, 0], [1, 0, 0, 3], [0, -5, 5, 0], [0, 1, 8, 0], [0, 15, 16, 0], [0, 0, 0, 0], [-43, 0, 0, 16], [0, -5, 68, 0], [-89, 0, 0, 33], [-13, 0, 0, 60], [0, -100, 20, 0], [0, -44, -117, 0], [0, -20, -279, 0], [-179, 0, 0, -97], [0, -140, -40, 0], [190, 0, 0, 165], [0, -55, -432, 0], [351, 0, 0, -200], [0, -91, 192, 0], [0, -105, -116, 0], [0, 85, 335, 0], [140, 0, 0, -40], [0, 100, -80, 0], [0, -125, 398, 0], [0, 469, 442, 0], [-151, 0, 0, -320], [1080, 0, 0, 320], [-367, 0, 0, -601], [0, 260, 490, 0], [659, 0, 0, -388], [-1853, 0, 0, 481], [820, 0, 0, -840], [0, -16, 647, 0], [-297, 0, 0, 245], [0, -40, 1873, 0], [0, -540, 420, 0], [254, 0, 0, 417], [0, -1103, -1884, 0], [0, -1721, -728, 0], [-1771, 0, 0, -223], [-473, 0, 0, 445], [970, 0, 0, -105], [2159, 0, 0, -335], [0, 352, -1069, 0], [0, -1731, -418, 0], [2157, 0, 0, -1325], [-111, 0, 0, -945], [0, 33, 1144, 0], [0, 1051, -772, 0], [0, 1700, 2030, 0], [4162, 0, 0, 276], [0, 300, -5276, 0], [0, 2680, 1691, 0], [-5691, 0, 0, -45], [1493, 0, 0, -116], [-3831, 0, 0, 1587], [-2553, 0, 0, -45], [0, 695, 4616, 0], [5093, 0, 0, 804], [0, 725, -7265, 0], [-2601, 0, 0, -303], [0, -3148, 211, 0], [0, 2120, 1820, 0], [-5080, 0, 0, -1120], [-3450, 0, 0, -1980], [0, 2355, 2985, 0], [0, 2815, -484, 0], [-2690, 0, 0, 1295], [-4717, 0, 0, 349], [0, 815, 2138, 0], [0, 3552, 41, 0], [0, -1200, 4104, 0], [-807, 0, 0, -3081], [-2069, 0, 0, 5088], [0, -5355, -5548, 0], [0, -1653, 3076, 0], [-3890, 0, 0, 3135], [0, -3189, 3098, 0], [0, -280, 859, 0], [0, 1020, -4143, 0], [11373, 0, 0, 395], [0, -625, -9487, 0], [0, -2455, 7796, 0], [2021, 0, 0, 2488], [-7677, 0, 0, -975], [-11260, 0, 0, 2100], [0, 4295, 3958, 0], [0, 7016, 3873, 0], [0, 5860, 561, 0], [0, -1080, 13040, 0], [6920, 0, 0, -5220], [0, -2225, -6092, 0], [0, 1111, -6652, 0], [59, 0, 0, -1175], [0, -6500, -3280, 0], [3951, 0, 0, -1027], [0, -4540, -13827, 0], [6738, 0, 0, -6575], [-6299, 0, 0, 5243], [0, 7785, 4675, 0], [5860, 0, 0, -6020], [0, -3004, -18137, 0], [-12631, 0, 0, -3513], [1253, 0, 0, 3645], [-5620, 0, 0, 2880], [0, 4047, 7261, 0], [0, 5001, -6852, 0], [0, 81, 6603, 0], [6380, 0, 0, -1600], [14291, 0, 0, -3080], [11363, 0, 0, -3351], [0, -1492, -20291, 0], [0, -2876, -17483, 0], [0, -2840, 11580, 0], [0, -9175, -4556, 0], [16029, 0, 0, -4840], [0, -3973, 16976, 0], [-9997, 0, 0, -2011], [-2229, 0, 0, 3743], [14027, 0, 0, 5585], [0, -6000, 10479, 0], [1081, 0, 0, -8612], [19210, 0, 0, 4580], [0, -5967, 14744, 0], [0, 8305, 20824, 0], [14150, 0, 0, -8220], [0, 1720, -4693, 0], [8753, 0, 0, 1575], [-8920, 0, 0, 5860], [0, 17855, 10965, 0], [0, -2225, 17932, 0], [0, 8193, -11516, 0], [3671, 0, 0, 3645], [3747, 0, 0, -5649], [0, 5415, 5166, 0], [0, -3255, 13578, 0], [0, -13907, -12546, 0], [0, 941, -14112, 0], [53, 0, 0, -7471], [-10573, 0, 0, 8660], [0, 1700, -12268, 0], [0, -2795, 30846, 0], [4621, 0, 0, -7197], [0, -13728, -22964, 0], [-5843, 0, 0, -7220], [0, -4055, -18348, 0], [0, -5755, -1165, 0], [18630, 0, 0, -8065], [-4820, 0, 0, 4480], [0, -11516, -27708, 0], [0, 3176, -35407, 0], [-11691, 0, 0, -8140], [11140, 0, 0, -15140], [46117, 0, 0, -2349], [3143, 0, 0, 13149], [25760, 0, 0, 3420], [-11737, 0, 0, 13765], [0, -160, 24813, 0], [-21350, 0, 0, 3605], [0, -1760, -12150, 0], [0, 2499, 3532, 0], [1261, 0, 0, 8693], [0, -10040, -7880, 0], [25699, 0, 0, -10135], [0, -15716, -8603, 0], [0, 8500, -18140, 0], [3069, 0, 0, 3695], [-7393, 0, 0, 1496]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_169_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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