
// Make newform 20.10.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_20_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_20_10_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_20_10_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_20_a();"
function MakeCharacter_20_a()
    N := 20;
    order := 1;
    char_gens := [11, 17];
    v := [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_20_a_Hecke(Kf)
    return MakeCharacter_20_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 := [[0], [-48], [625], [-532], [-33180], [-99682], [-443454], [-357244], [-142956], [1527966], [7323416], [-2666842], [-7939014], [-21174520], [16059636], [-87822234], [120625212], [93576542], [193621688], [417763488], [-450372742], [-91425472], [-652637376], [-170059206], [-10947022], [1099628646], [-1358777644], [1559878896], [155615342], [2299456146], [-5392040092], [-2666434260], [-2055884598], [-6761768884], [1300092054], [-9202347760], [-6773449378], [11544980000], [3998967612], [10431143406], [3862564308], [-20099356330], [-6092623512], [-17436969166], [-17618292618], [-27211845112], [44038251164], [-24752350900], [52233132696], [48518735174], [-15597760134], [-78774848064], [91232959874], [-1227792300], [-56445682974], [-140882893692], [46218406638], [-48888305800], [16073433494], [-14626681014], [92449119896], [148755577350], [245184085928], [-92959784448], [244767736922], [-280185289074], [-44320793452], [-412784396302], [27096157920], [-188319351490], [101309430402], [-71999204376], [343665128132], [-142006871626], [-79043454868], [291619740204], [847185014694], [-85608130402], [605160826770], [-209246967094], [-540287820444], [-358322867674], [583728991416], [-1257535351102], [-214988321032], [-1509978116376], [1066387221906], [-389933706166], [549112375950], [199371487196], [-793834531080], [1771751773776], [1621317414044], [-907687391100], [-268633343644], [1302596692548], [345797738526], [-1026113077686], [1972888695176], [-3198568677346], [-295149549256], [-766146556050], [1827708894048], [-1825834826070], [2725624570148], [4119380054402], [-5277128507904], [3368363412882], [2927134785960], [-1006363924150], [3319106638052], [969366007478], [-3769238433414], [3724538390780], [-1862290364368], [4041047003922], [-4437517192864], [-6018349990212], [-7431348250242], [3402886154676], [4852829388278], [-3907400195374], [4282341171702], [3059775688872], [11439909466508], [7768615827390], [-6472445866234], [6995610895776], [-2680442663524], [-8185301494018], [-3295135577500], [-1286349366156], [-14100921071224], [-4942220825578], [-6658606615878], [-12079714013278], [18484707522870], [12737195859704], [-22689480778482], [16884104208234], [-3726956315836], [-6879505123914], [16251447693332], [10115993934240], [-452458295458], [-2434860423528], [6227367394310], [-13776820391238], [-5052475594564], [8533235879964], [-25454154906226], [9535577993442], [-25929755139904], [-17674373160228], [13309723172576], [-15864729139128], [-29544650579896], [29711277798258], [8007239676122], [3120327627342], [1954977886632], [-13018974067014], [36662141775692], [33668576766516], [-13224071645118], [7765230634548], [7588534751096], [15907735689014]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_20_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_20_10_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_20_10_a_a(:prec:=1)
    chi := MakeCharacter_20_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_20_10_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_20_10_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_20_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<3,R![48, 1]>],Snew);
    return Vf;
end function;