
// Make newform 38.8.a.c in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_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_38_8_a_c();".
// 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_38_8_a_c();".
// 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 := [-4488, -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_38_a();"
function MakeCharacter_38_a()
    N := 38;
    order := 1;
    char_gens := [21];
    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_38_a_Hecke(Kf)
    return MakeCharacter_38_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 := 8;
    raw_aps := [[-8, 0], [-5, -1], [-36, 3], [-181, 14], [1362, -1], [-7045, -23], [-19341, 122], [6859, 0], [-37137, 377], [80871, -1929], [-129736, 4], [-262378, -3412], [503280, -910], [139412, 7393], [-699120, 731], [-682701, -20567], [1361619, -22285], [-1977358, -21231], [-40657, -53243], [2087898, 26944], [434897, 30704], [-3440194, -13342], [-1220838, -23654], [8690568, 50588], [3253238, -154542], [3713370, 112804], [1621490, 231520], [8487477, -137027], [-12238027, 159243], [-941658, 229160], [-34867390, 147860], [-7042308, 122391], [-24112953, 8574], [-15211384, 329577], [6833376, 947401], [-16459630, 189800], [20463038, -808568], [15285344, -710484], [24947340, -324738], [-30363102, 112020], [102069888, -524974], [-49109842, -1233572], [-7905849, 211862], [-114374998, 128778], [-112924272, 581834], [33713039, 1417326], [84484151, 1067003], [38698166, 654892], [80392635, -3982607], [12849446, -1858093], [-157198254, 2555289], [-182910753, -66174], [-37291600, 2809502], [-6604278, -3585507], [-15474960, 5756352], [137913264, -2591097], [-246734154, -900806], [221653775, 4991865], [594214868, -1122499], [191080920, 7294574], [-358426990, -827089], [-13722507, 6389863], [-285223564, 6378112], [296747439, 3127340], [285791981, 9083041], [-143614071, 3244557], [199553981, -2655329], [138570248, 4756824], [-1173017682, -2204263], [521936402, -6565573], [1183886337, -5899839], [384763593, -4242960], [374798300, 11599644], [406391657, -5674629], [1586824451, 1975743], [-753327618, -7175282], [-295728990, -23028263], [-14655280, 13828453], [-825323784, -17544708], [-895038964, -22139528], [-1648805400, 10672356], [737231591, -9710527], [13168830, -32194310], [2214722102, -4515068], [228179828, -31053586], [-6762666, 15907497], [-1248395430, -323034], [826653809, 13213602], [-1714703892, 1389177], [586043804, -31100981], [-349230234, 12079033], [50601612, 25289760], [1678260566, -48686808], [2018755044, 23578704], [-2008144264, 30679047], [1441197063, -32725723], [-2705007078, -5561786], [1514474028, 56694572], [3273032297, 44745953], [292939322, 51155217], [-4152845668, 9570384], [814366284, -44083051], [762768396, 15006366], [3914092896, 50802984], [-2105019748, -46757544], [-5178493525, 49063568], [-726294804, -57024781], [-399584190, -30976252], [-42461232, 110790412], [308421704, -21542318], [1771653602, 33203790], [-2386089574, 60058071], [-578370774, -43645521], [1534256162, 8857634], [-9434033176, 23291997], [3852688146, 84883502], [5231883518, -86248117], [4254385935, -27342210], [-914446188, 95143089], [-2660170167, -139955083], [-6208733035, -21531277], [1089476180, 37931250], [3970588185, -98053947], [-2509067532, -66861816], [1372632266, -184671799], [-4920695676, 112055258], [-9938814022, -49363560], [3920627883, 136581926], [5014124999, 118380756], [-7227895420, 43231310], [-2564886160, -2785087], [-15270600864, 64434384], [-4630132168, -186100648], [-6908236498, -121342779], [-9699232749, -117881594], [1396567181, -127272422], [11591041701, -31522779], [-11898504661, 121445153], [16234996479, 112314181], [634048617, -354879786], [8109739337, 128545103], [-5843590764, -247737663], [-8428427227, 191775620], [16666752219, -114502503], [4423512773, 73208259], [4364676540, -277512702], [1845954170, -101922488], [-7950861756, -297174306], [13737152630, 271172639], [4833150246, 247565296], [22116076001, -14663991], [3812231238, 472316679], [-24106100734, -12554789], [9742214190, 269777712], [12378346913, 142108871], [-7707911676, 25786290], [-9248383867, -266550289], [-6046584015, 41003493], [12929827649, -463127808], [-28310414973, 140734043], [16708735980, 65982344], [-6423173478, 208767198], [-22402297840, -185545640], [34147305348, 153180528], [-26166283848, -56849112], [30955101306, -26935686], [7456019024, -687188566], [20266183700, -182507103]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_38_8_a_c();".
// 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_38_8_a_c(:prec:=2)
    chi := MakeCharacter_38_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_38_8_a_c();".
// 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_38_8_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![-4458, 11, 1]>],Snew);
    return Vf;
end function;