
// Make newform 22.10.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_22_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_22_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_22_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_22_a();"
function MakeCharacter_22_a()
    N := 22;
    order := 1;
    char_gens := [13];
    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_22_a_Hecke(Kf)
    return MakeCharacter_22_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], [137], [-595], [11354], [-14641], [55620], [421550], [-435872], [779077], [1206768], [-7626195], [-19473681], [-9906168], [-20662730], [2751600], [78527114], [-105727893], [-24639860], [-94817369], [338924343], [10287972], [556386626], [22479190], [-213504377], [1512644953], [-160812934], [189055992], [1155126910], [-2005719870], [-979014559], [-5292747144], [4307459266], [-578069383], [4928318342], [6673746494], [-5369648666], [2161831061], [-3967462724], [-13630473204], [-18395463886], [24461767247], [152299795], [32256460515], [13361454004], [-29029944218], [-37227689464], [42997487636], [35924109897], [-26253906402], [56288373587], [40907982328], [-49876863946], [83216514040], [-98984085601], [96708211934], [75341318250], [-52971715270], [65601180812], [49080136934], [-7521849250], [45821025484], [24184889368], [-49012496984], [-237939127652], [17349211439], [-185454630927], [154510435697], [-329013813654], [-43722963628], [-148114531914], [446087163579], [-94211305164], [233224935893], [-449565866882], [-71993417563], [597345144413], [-282032519259], [9952293166], [384989214466], [-707848122494], [-191015743620], [-271445588794], [989073021402], [1009987993469], [-316486846280], [-1117713997901], [-1525156997677], [156709762228], [1374441153404], [379778227743], [999175769547], [855298157436], [-920768472619], [2256062256824], [1368963560540], [1589180949986], [1430502636491], [-1705869837723], [1093554387824], [-1609874341016], [-3119013239944], [-1029041030202], [1412942361868], [-443150783600], [-1148672251892], [253081000193], [3253676165700], [2087171880156], [-3139568250336], [2330236641394], [4361341556686], [-3141506295936], [-2160885642958], [3967890015545], [4277597687389], [-1411374116697], [-3061504840021], [-3292078692325], [-3735740795805], [-8796123760842], [-5229622336431], [4487372922014], [-1775609122994], [-2186956620000], [1706565353367], [1372289232570], [-8923921981701], [1557148030629], [1823618792745], [2012771743820], [173842409294], [1075545733996], [2528160425675], [-6794931078358], [14082619777692], [-6040310393612], [-14734921545174], [-16343428017392], [8085061564425], [9722963164912], [8838090207686], [12376698589246], [16634308896365], [16327055760932], [-18208039681091], [10983877281393], [-22508462316090], [-9860026948552], [17520846034111], [-15008727545296], [-12879228525468], [-21844228532267], [3248098452076], [17852222997534], [-16655756984516], [11082476619132], [6084930745022], [-8452477896798], [-119649115736], [-20293890701934], [-29904533782605], [32264190232914], [-4710990591296], [-18394205385111], [-1682098081603], [9224248271781], [-7021486271248], [-10018037325074]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_22_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_22_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_22_10_a_b(:prec:=1)
    chi := MakeCharacter_22_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_22_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_22_10_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_22_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<3,R![-137, 1]>],Snew);
    return Vf;
end function;