
// Make newform 38.10.a.b 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_10_a_b();".
// 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_10_a_b();".
// 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 := 10;
    raw_aps := [[16], [102], [-1581], [-4865], [-64189], [-48516], [314477], [130321], [-51088], [-1543218], [153108], [71578], [-24190606], [-2906529], [14687405], [107478052], [138112586], [-122366017], [67296612], [253992790], [25518121], [-264202112], [-724058420], [-1075037068], [1173230648], [-1202508938], [-1304186466], [328530622], [1269122984], [-603462370], [-919866566], [2133722721], [-4835128611], [-5898550189], [-1646632379], [-1216421450], [-6685957074], [-10977338908], [2117676678], [18411360126], [-18202016938], [11124675666], [-22331023981], [4359803060], [24757662098], [25711379675], [-48765008930], [-19921289682], [63095234620], [-29803116491], [4490485995], [-15589980321], [1027072078], [-85772606187], [24560340432], [-73092394167], [63100812592], [-98888929552], [-118609173683], [193960913222], [111144187843], [183473454148], [-104516438196], [251393818445], [-65863470906], [305616755334], [-14240389332], [141523428896], [-529147583371], [57649301329], [389168524842], [106143159855], [-69583742872], [-174159048628], [86487305822], [89381212756], [-419755441169], [-297494529831], [-92795677116], [-1051269872748], [-746397998556], [-1026710676704], [601842178264], [380261435274], [552278168206], [1445991578301], [-428462219952], [-1404847159429], [1377381139113], [1353012721617], [1135374084037], [-1035139262700], [-1778962974958], [-2224247273892], [1972995562037], [1019033874764], [888315332272], [1586304146504], [149700596806], [3193622580551], [-419271949180], [3222315738677], [3542473786590], [1059513764424], [1878835946876], [1102451829403], [-5100263698147], [-2731278483610], [210529211284], [3314263390362], [-3758968575136], [-4359888473875], [310181998809], [558230649916], [-3129502729237], [1742338019408], [-2164617652829], [-8452744847541], [1942872872169], [-1784789993422], [6085887530080], [4241602108886], [-2619783206514], [-4813123745484], [5441265499873], [9701242334294], [10808823732122], [-12825876739057], [-8264279787187], [9233049073306], [4263799314475], [-5503575407808], [10099548784144], [12640106171159], [12501629731201], [-6066948566217], [13655134797114], [-483992694404], [12748750729516], [-16223914179225], [164957596912], [-6604292360607], [-16559896757393], [-8024036358588], [-21901301399680], [-3116936116734], [-5540346367158], [-6975472256670], [-419925578273], [-14265006029894], [1637141105738], [-420162685995], [-28854037119825], [8998269764082], [-25770916920656], [-1300969425702], [-22221501412940], [30253864159902], [-31015334606263], [32418497564894], [-20765939210404], [-34669211511624], [-44570246535144], [46161515076348], [-12759632375472], [-24992450872932], [7397639816710], [-54408399719695]];
    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_10_a_b();".
// 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_10_a_b(:prec:=1)
    chi := MakeCharacter_38_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 10));
    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_10_a_b();".
// 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_10_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<3,R![-102, 1]>],Snew);
    return Vf;
end function;