
// Make newform 208.4.a.h in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_208_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_208_4_a_h();".
// 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_208_4_a_h();".
// 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_208_a();"
function MakeCharacter_208_a()
    N := 208;
    order := 1;
    char_gens := [79, 53, 145];
    v := [1, 1, 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_208_a_Hecke(Kf)
    return MakeCharacter_208_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 := [[0, 0], [-1, -3], [-1, -1], [-1, 11], [-46, 12], [-13, 0], [1, 17], [58, -32], [-92, -12], [26, -96], [60, -34], [107, -5], [-104, -22], [-215, 143], [-157, -121], [-44, -30], [122, 124], [-624, 190], [82, -232], [181, -231], [358, -260], [484, 40], [-888, -182], [-554, 388], [-210, -508], [700, -142], [456, -304], [-1076, -140], [739, 43], [1010, 236], [16, -344], [-1171, 567], [-2124, -202], [-171, 847], [-2378, 244], [1665, 441], [878, 924], [-2180, -430], [816, 902], [-2756, 1114], [109, -29], [200, -558], [-2042, -874], [1000, -206], [-911, -1395], [-1350, 670], [-747, -537], [1477, 1737], [-1712, 1018], [369, 101], [713, 621], [1999, -993], [-1070, 2536], [2424, 1092], [1045, -151], [-4476, 480], [3024, -2190], [2067, -1193], [-572, 3402], [2774, -484], [4260, 1056], [-3169, 1487], [4578, -1748], [-3442, 2858], [4779, -3345], [-2202, 2580], [4444, -122], [155, -1841], [-5579, -1753], [6927, -409], [-4916, -1650], [2616, 54], [-5238, 858], [-236, -2010], [2456, 378], [-6587, 2721], [-2662, 4392], [-9022, 696], [2912, 530], [-900, 5646], [-2793, 6781], [123, 6139], [-7065, -1417], [-11189, -1909], [-426, -2190], [-9917, -111], [-2554, -1276], [10298, -2216], [9403, -1669], [-10928, 1734], [-10396, -1008], [11283, -2137], [8128, -1070], [953, -6329], [-2312, 178], [10206, -4394], [-838, 112], [-12333, 1303], [9068, -4580], [8593, -5763], [-8473, 1301], [737, 797], [281, -2025], [11347, -8085], [1907, -3555], [-814, -4152], [1428, 1566], [-17094, -4416], [-3806, 5578], [7193, -1559], [-8438, -1134], [-8894, 9088], [10248, 4610], [-9692, -1534], [21901, -1235], [19614, 3976], [5442, 6848], [-3994, 11282], [-9600, 7682], [5668, -7028], [11934, -9636], [1769, -6007], [5716, 1538], [-21404, 1986], [12624, -5846], [18220, -10266], [-8594, -1964], [-3802, -4854], [-21010, -1238], [6125, -1199], [-14964, 8978], [18007, -729], [22856, -1704], [-10644, 4218], [8694, 7068], [-6360, 474], [12475, 131], [-12760, -5978], [4078, 10156], [2531, -10489], [-15180, -1522], [12775, -4201], [4234, 1350], [6146, -540], [-12718, 12764], [14104, 6446], [7185, -595], [1806, 15436], [-7660, 16580], [11939, -1397], [-16575, -9371], [3465, 2725], [15859, -13307], [34184, -4164], [18077, 4875], [34534, -690], [-8504, -4756], [51908, -2450], [-8714, -3532], [-11519, -13027], [-23662, 2964], [-6373, 3859], [30585, 2509], [-38101, 7249], [11922, 7136], [10959, 10859], [4926, 8902], [-802, -18784]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_208_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_208_4_a_h();".
// 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_208_4_a_h(:prec:=2)
    chi := MakeCharacter_208_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_208_4_a_h();".
// 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_208_4_a_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_208_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<3,R![-32, 5, 1]>],Snew);
    return Vf;
end function;