
// Make newform 22.8.a.d in Magma, downloaded from the LMFDB on 16 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_8_a_d();".
// 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_8_a_d();".
// 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 := [-3720, -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_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 := 8;
    raw_aps := [[8, 0], [-11, -1], [165, 1], [890, 14], [1331, 0], [-2644, -118], [7398, 236], [8216, 484], [-25959, 567], [-103200, -730], [-10183, 1295], [177359, -3385], [65808, -8006], [-73570, -12682], [222696, 5768], [-635070, 9308], [450927, -14307], [-292900, -12118], [1449827, -34943], [673515, -40763], [-2303428, 29326], [-1445542, -55258], [4995102, -24754], [5126607, -67837], [-13882111, -1255], [7871202, 21100], [-10699648, 151736], [21241086, 4758], [-10796782, -168588], [9456849, 179769], [-883552, 304416], [17670642, -406230], [-12133911, -73975], [-13803418, 13198], [-23472426, 198376], [12878, -678006], [-15971323, 838537], [-44488852, -889120], [10454340, 1153524], [48207714, -1186716], [-50313909, -1358871], [24486179, 49923], [27742191, 1346921], [120550100, -852162], [21058566, 2241288], [-42843712, -145128], [-120645052, 886744], [129706013, -264885], [69535662, 3250542], [-193395589, -1052101], [-174567240, -369582], [-209842242, 549122], [-55484872, 214474], [134335995, -544783], [237889806, -2428332], [50725866, -7583138], [-369622638, 3003188], [429346772, 1439420], [-273643594, 3923208], [167984622, 3027740], [-23134756, 3342584], [195739008, -3112130], [173644688, -9466164], [722558532, 1364740], [184589183, 812275], [153521001, 925733], [-272210683, -10282905], [688836530, -529536], [472842684, 5699160], [-310517026, -3473704], [214406931, -8877033], [-1259187252, -5655508], [-474288607, 7308951], [-124658218, 23743496], [-229115071, -4506469], [320722161, 2071287], [-452695899, 19105409], [126190406, -17848352], [979995090, 3955872], [879448178, 14311032], [1492877580, -7655040], [-764347858, 25568800], [20891082, 33988630], [398204237, -39873059], [662531144, -15203584], [-120703713, 6087981], [327305067, -24521697], [282826004, 18962870], [-865513668, -22331910], [2300214827, 7380221], [-1016366793, -17323843], [-1348234956, 29261516], [-2436302719, 15535103], [-2595156504, 32321916], [2011663100, 43597760], [-3002528070, 7106078], [3438706899, 34356491], [-3218991363, -11701995], [2778878072, -24326004], [2239573568, 19452934], [-1716043288, 25946372], [-2941177074, 18236248], [2599954908, 69460320], [-3309422496, 43561306], [478391372, 24759720], [-3193724647, -44443039], [-460469916, -17620160], [2997103644, 53583334], [810842808, -65316528], [-1335072214, 48580096], [3765050726, -43620822], [-3607119232, 88965630], [-1275467742, 13859896], [-966768331, 54418391], [8458702385, -18186113], [1624845687, -104259405], [192612767, 63981861], [-653688225, 44417329], [1094895243, -186300285], [7659725982, -15227122], [-9735460327, -27199331], [-9077539090, 79600788], [4907489622, -24013928], [4286786208, 194212420], [-1758148885, 138455177], [2969858226, -167467644], [-12136785709, -27717181], [3751203249, -39562969], [-1729651675, -4288501], [4575075500, 65198138], [6060224582, -19232682], [3167016924, -75393956], [-4179437545, 186171857], [19954602914, 8904700], [-6240290988, -111474490], [2091037580, 344847110], [8579033106, -253539268], [15114671024, -96919860], [9615698241, -98119507], [-12965224104, -16023886], [6123773918, -93985226], [-6341157282, 219686144], [3376682273, 100054735], [-3292594932, -69747864], [-21133227259, 137427049], [-11952286371, -49796829], [-5659150858, -377392184], [-15173779272, -123305054], [8876612195, -131980375], [-7126018392, 283767000], [11768424236, -248050582], [-13003143747, -397868371], [-2757075940, -386543712], [-23342075658, -259823358], [-3872095780, 491570824], [-33609092412, 87743404], [40511183222, -51048222], [-10449695310, 334603440], [-5780203936, -153597910], [-1812368862, 274643364], [-8324696601, -282882835], [-7584683622, -431698320], [4869280112, -698923128], [17204428605, 429243247], [9046221645, 254287549], [31394406561, -220465553], [38542404680, -126764488], [-9905951386, 88162440]];
    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_8_a_d();".
// 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_8_a_d(:prec:=2)
    chi := MakeCharacter_22_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_22_8_a_d();".
// 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_8_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_22_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<3,R![-3588, 23, 1]>],Snew);
    return Vf;
end function;