
// Make newform 9.20.a.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_9_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_9_20_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_9_20_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_9_a();"
function MakeCharacter_9_a()
    N := 9;
    order := 1;
    char_gens := [2];
    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_9_a_Hecke(Kf)
    return MakeCharacter_9_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 := 20;
    raw_aps := [[-456], [0], [2377410], [-16917544], [16212108], [50421615062], [-225070099506], [-1710278572660], [-14036534788872], [-1137835269510], [-104626880141728], [-169392327370594], [3309984750560838], [1127913532193492], [-3498693987674256], [-29956294112980302], [-58391397642732420], [23373685132672742], [-205102524257382244], [177902341950417768], [299853775038660122], [-92227090144007440], [-1208542823470585932], [-4371201192290304330], [-635013222218448094], [14225184990751604898], [4907294374153596152], [-26462522729977956756], [-18417779076742725130], [-25742101939950990162], [88072027398734416256], [-71928888557627226972], [295426173292082995494], [138478242598622657780], [266021193813555196050], [575578481645706044552], [-1072375480931336531194], [-580764662999007691588], [-243392417169880585656], [1193502882659918351178], [4146639259449756929940], [3323637583685998127822], [-6193795096341916302912], [-5206971294736770240958], [-2423836313237712027006], [-1059072621521283111400], [-13242387429757990150708], [20092136783007908058272], [20349408367227979917444], [-39990035937447287206690], [-24217033172511669439482], [-26241066116230112579280], [73644506169104941694162], [-72930920304932253837252], [63812030516904649354494], [135819824783556684808488], [133672004682359243213610], [200547574021219881238832], [200222747174096559828206], [119239496202616376591478], [-3461084772055526965468], [213235512531799832489058], [191887086781507035813356], [154521700942476731981208], [287407616111100408566762], [-26353269327755584850406], [-1053375627397004455028428], [676159899744517170641906], [-1063246225907504447440356], [-714666649174805721201850], [507320765589089335531998], [-3320064150855122758012920], [-2244298570574857699866544], [5106064897996372955121422], [-4289751162168739890186940], [1868030627574609716765568], [6474481839876985824443970], [1227597115000120170846806], [517895245995397565310798], [2818767450506973223394570], [-8655708588109946594551740], [-5216517712913512492334818], [-10436381488383884270310672], [14510951301856278108779282], [17985690752159857937736680], [-8736849476979332061375492], [16057594567447141671042750], [-2675866234039257366875494], [-6968065181802302254603542], [-24984337558293435796179088], [-19453069178829687522404556], [-33415333454065222785017760], [-48618082719633037051166344], [-9867302508639730608865812], [-35415047001126952989663700], [-14720352635696539116278952], [48829005109648323366368730], [-71444530606997803992245082], [89989490933798334932096372], [-93360210082563133894716538], [-27467041603607834560578244], [-142588699490134503664158006], [172252275378180515000704788], [-52489258032363872534509290], [-3216737162983851125343868], [117452828132996543064218306], [-1868862009035096102933556], [150165073713894823114150158], [-165804217294620955662035400], [-254795398167551616303036598], [314191293450732569600135456], [-391477672780633338462808738], [-486066221227871859201962106], [-451098253696556017748426860], [203805071384693884419692072], [-577926950333012117394136962], [772049268275202746206982492], [1392256800135635419647144], [619470222760661832598870698], [79662660304446879314894580], [-185436309592288012497255058], [591531975816112122825354722], [297418062223597907916800994], [1259426577099632388995338668], [31996453716239418771490412], [-2064629078554311470359104102], [-1440955677319441783081036930], [1263930301200965104242038160], [554467748906702914995440456], [806563014637524944378250182], [663849768850552007374925180], [3640947602991580851653680008], [-1554577041827633477827398448], [1334060519009086810602782606], [68982915656223958871596758], [-809673088318519394490554110], [-1009238467990545727481244222], [-4883074190805508431608093044], [-1183457735592477326828764806], [5084951459230685387890761030], [-27213304830712131339736108], [516380791107383402431504818], [-1976238814126263760979269528], [1167945368430261889372056444], [4208089612099645624896372710], [-8492535037153362379496144280], [-9333449430453710848291401298], [11103739699848511536058131894], [11147795036950497002528898020], [-11544545264508987293598868512], [15591174053274879720999887606], [-335920487479087202598947922], [-134374308692347566243606868], [-9238872428845976272837826856], [-18058501386448135781249208844], [19930387918681950794667229008], [10431956843245634836995714440], [5177357237229028065304968990], [9332015844322152887347483706], [-4501145845350783495352219062], [-17612552500617257594061934956], [-5937301978653944117499620202], [-25848879904223060783499866344], [-20573213660753757730929994932], [23452372740546363458648241294], [-42552577586447649578248834632], [52147222626560601969944350112], [-1615412567054620685341032994]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_9_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_9_20_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_9_20_a_a(:prec:=1)
    chi := MakeCharacter_9_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 20));
    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_9_20_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_9_20_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,20,sign)));
    Vf := Kernel([<2,R![456, 1]>],Snew);
    return Vf;
end function;