
// Make newform 38.8.a.a in Magma, downloaded from the LMFDB on 25 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_a();".
// 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_a();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [77], [440], [951], [-8398], [-6223], [26211], [-6859], [-64213], [65845], [-32708], [-436694], [-28808], [650272], [58736], [-918703], [-787635], [3106862], [2726001], [-1800958], [-1436223], [3402110], [-9454038], [-40980], [4281646], [-2483638], [-12503178], [2224691], [14141055], [-7383538], [-22490574], [-3907608], [-29867969], [9352140], [24826200], [-11130298], [45292186], [23356532], [-68369244], [45867582], [-35047920], [13037962], [-75231053], [87742542], [169048856], [139490435], [-225997743], [159102002], [273407581], [93633650], [-27793878], [265907115], [-115585568], [99160502], [-372576604], [-358686408], [509147730], [312525067], [342007336], [-428705648], [452256042], [417394647], [315746196], [525058867], [913905557], [-356879589], [-1197198853], [291486676], [216207466], [-1038960170], [116693217], [-1281937875], [-191664764], [100128227], [-1032852435], [1414745482], [684401610], [-321394204], [1498733152], [-1670425280], [-815512020], [194494277], [-2624958138], [1904043962], [-1515637900], [-2833613258], [785103790], [146139161], [1311761012], [3881635612], [1169165986], [-1999478780], [-1343061494], [4306130492], [2282882500], [1753647747], [5933371830], [3430822292], [551108087], [-1746579598], [-5181460724], [1083966736], [4585474452], [-6156943280], [3857568692], [-5154480229], [-1465848684], [4202533762], [-1578722760], [9224284132], [3367468706], [1662526402], [-4024837734], [10507566670], [-1438321648], [-6453564018], [-5471985638], [9502754531], [10209743352], [-8466756025], [-7708632353], [4393074932], [-7422401189], [-13404008588], [-14302503618], [7956142652], [10700786430], [1292938855], [-4135035149], [7761280872], [8137106720], [-1460143828], [1249404032], [-1726334534], [-18853780173], [10243726405], [-17043478113], [-22901234819], [4916831301], [-10829353455], [16076381607], [17412226992], [-12664515343], [28629920981], [-27517060305], [9771819320], [-3515191758], [-16145538924], [19738159390], [805160382], [6230561131], [9819470862], [-9862198058], [-17833591034], [962665271], [309938472], [-4478725375], [-26239438015], [-5222177599], [34332494257], [-20080842984], [49940079302], [-39477339744], [-18364616308], [-47286732124], [-10319565678], [-23280334868], [-5088021944]];
    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_a();".
// 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_a(:prec:=1)
    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_a();".
// 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_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![-77, 1]>],Snew);
    return Vf;
end function;