
// Make newform 9.24.a.a in Magma, downloaded from the LMFDB on 23 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_24_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_24_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 := 24;
    raw_aps := [[-1128], [0], [48863730], [-1723688680], [1428263180124], [-8220964044826], [5989210330446], [680005481275676], [-15440648191080], [-115094192813324022], [-90829724501108800], [-1297873386623227570], [-5214036225478655130], [-2410434516296794108], [23132669525900803824], [44512631945276522850], [323974479000840790476], [-199406203121599312522], [-646392500721161158996], [-3551461551813260928312], [3353187900182300778170], [-6872134095241809038320], [1169769717495414820644], [23457212631337905637974], [-30603881563463466110686], [2394111446254900530834], [-29873484317297795248648], [-352638831195539091905796], [-152075852474609095502074], [-51868498468828292746386], [-334992482784841778911456], [894766524681837467354772], [1525498636291337052000582], [-2870522952002117276370076], [-7067082703199233955672958], [-5448823293607282749138808], [-27917873121881245513218922], [48370723387502830329931148], [-35966590038232005843219480], [61803963132837618496694874], [-41818176913089794695363260], [-104651733966083556885724930], [-125691443141572489012147776], [-85541767208615930882483134], [94137045106869171624232818], [74648367348776997750892568], [691337708429605175432678300], [798521194872308666505568064], [-1192854988464194324324835660], [-1640629282547461685439664306], [1478083082975235561260518758], [100529242417957704778426224], [2890666464081799519678350866], [303848336248193145859544268], [5747613031480072290568606974], [9084696735591240574132711752], [-2454452194434945976165221126], [5653291363551821637876179216], [3555312174946541875247829662], [-28390598161233283910383639914], [-22920238336732940197766214316], [-34310931827400238200925695918], [-11688625117698275010397748164], [24982473532128450105836559864], [60388478134982500116857827658], [104254615411967722709927981994], [138759029739546778456630221092], [-150794700164516475087569288398], [145030270742041972026087968556], [103223862700200363534426441302], [99668089198937999392904369886], [-199928911442463973637676480024], [560763585670047678737573524592], [-446929755829707359391897850498], [-366574405298519874468110628940], [149291193939606997409496588672], [197534403918111554403156479346], [-26239300847898285111845340634], [-434375380228256742374732084658], [-814589385944079349230692590486], [-128768781374772123935673531276], [-1447245791394523822123317020146], [1552289102045126158541120394480], [3690553598223936339482810471378], [437827889637439203862811140520], [-1470079180826286227707032064884], [217842287958381505174524205182], [-1903857173730679086091352371270], [4969191870702311052038567689338], [1119560665749505055894869569872], [-8699609105854684749538300403676], [606140030125166549943880511520], [-4303480526582627675003697234376], [11356567330688150574219249855612], [9998153522870383986304854495356], [6168062007576516612067185659256], [-2986367545276721290697818166934], [8517054791964292978502018489670], [12747219469965115425164796265892], [-10062598003955600137682597227690], [-42721595007714513784218022628980], [68169590013763982891045296786266], [-6179061570709184679565754905596], [18703069346795330035044216830454], [25659853192368116767276503854516], [-23547713216720618379881255934526], [-75495486546816822113312440738596], [-32641248380781672558413860669362], [-53303757087387914933490127434408], [142622310074364411526509875696426], [-151222011488510065424128204613056], [-124131974977008333916269172341874], [189290825391560564551522121890662], [-133375038612284888866982241756796], [28091325424637156961759717254312], [37862752634393935301214532453950], [-143261534513693335373543188945108], [23275229639615270074213890070536], [407291029275957104766127098103482], [435630653487060200481435564073380], [-422347413428375749285636352342818], [327630923927759218248966893436770], [-165241339861673441180071330844142], [-117826218993076027058568273056388], [243790209287891278565758300742396], [379930660467815576267287926447402], [-14456919471862966863674279541394], [133386739251389070763198022270352], [49385926725606765444723125190728], [1053887297272844543034354641447702], [1097761118736255815923267637612684], [560677712329108794327740739916968], [-1012150964383156735860904720457104], [-948446912993569493127468902663362], [-981408149211920195767655987420874], [857155054578536419878601495977218], [94159330790638410425531631402162], [2721605409605208650153466084834716], [3495438684345445603682551408092234], [-1042256662219790895765584521794330], [-2227706076204089396354282514666748], [73686374579952159232275098217186], [4083686785142933695820646306316136], [-673455019443265883922425884456884], [-3770860587649010198118047711451850], [4991996674565588729443684988790600], [-2528255936155924091808646918196962], [-7450151095781573119776150637239786], [-527466213079705296717005565364780], [6728471102690829528222312612936288], [-7451237276623588829912340411000250], [-11724517790161973089644742615316754], [10529499676020366758790106312524092], [6864753384380630045142564968948664], [-9015450274175208004452675053009884], [-15425915333209401123739571459891952], [321778998845469108097128966980360], [-6891445667015541760464910402080546], [625702980579639282233928187507802], [-24328096060960849880228895806081190], [-19590772041867681512634774181216188], [-2998184193559002667403502085698954], [5374765698008638116362920308824984], [-11223771908811949516595290888359780], [-24145957966486531994478580626132594], [12126265282691373437100885705935640], [18334957614751971733530727668017792], [15922714263181674310615692842913038], [-16035883682164434635472391609086190], [59864497601370153522778197825131874], [-28784914458415309321088588821218036], [-76350582085445186355841634091790282], [-32475158743686576367017408474247224], [-24983911769699425342794558214218502], [12328647866234406124283379112970192], [-61960450819003647202283380614586986], [-68689932703191414238033824567299500], [-18660273289515259650210008013156846], [-20480184052984690937644695858203848], [39790309729358270577260013463407110], [-91419566777209937079353282248726240], [-43979412057606709690205434273079916], [144296133148582718613763358263088366], [-61432643853544751885041461680724666], [-8062521774626102582669500772264112], [-77647855296067757761314272587518270], [-33918108744284672134419305927343466], [-228315632417928125961070896523650676], [220129916292066663694197147606794522], [-181123446559077979763504415960345984], [-74237882721510567627616659824695678], [-38253942359404651055546052041674596], [-52706075325909857285789404607531812], [-19181079705090799720360426283968566], [328264581824809683915410446729119156], [327622867687418633396184427569007014], [61955587340893345232279045764530386], [227943427205958608385020501713628726], [143452989703778299455674798245808574], [-208547827495606875888552481300693368], [139406853861899145196418945492869242], [214006481245815786974480185481169488], [-344332484645563458269463069185620258], [-716867642945961888535191704297881054], [-427793220415959078906648593138744388], [125410879941799497633535012489731690], [459519237722837403405051042480699296], [-275926744025063140231021208185308588], [692488097381686868101350385587559302], [19325862495657653812754510164853924], [-35355494967198822167578850475348622], [691572924957042755435094243713520258], [-682655862509458291265109240133887160], [283485396992349967416121377482415276], [-419530851079395887235528322589764824], [504296899291004468957079580434547418], [877175756272186908276913127785011248], [-155256682810030317183646682007270258], [-262266216418048100778871498498862952], [-2149964976741438363571343310841295862], [-1700356140593882395010098250300940466], [134479120202393020520546743193152232], [-2946765012361348710079995018151885890], [188901995481301489417807755856804496], [2404087208407668389286888558823660644], [-1315274544028798407989957213487154210], [-795696395775132499726529239642441578], [3301283555557889951163393670281783264], [-1570251748669797291487545913312945864], [2759796822225083934375726132579549756], [3263481716387687472493578528140924870], [1993009909845215137755003195239134076], [4491240404560912139864007631543627616], [-1909112942484913314938343028921602618], [-5320946099763470474683677850473302044], [-4866088034620464058713285846707484144], [1998965266580682839609655602730120242], [2613929687529101982656033090221712322], [3126713958251515030402557116713845804], [1815415857445052004619963974504878952], [5570760379478836302572385081914010564], [5478903517996440976878677258574734324], [1522460291418133420043917648950408728], [1263138668035404118770687563187360038], [-166272592976093546812803176857142898], [7481439461125153379522619769895868696], [-5880111647566541235481484940341716288], [-5703410725396281121895639348128004940], [-4713863149589136488926982196536350396], [-5666603581405848560644361537753079888], [1626825132833715632688796693243690550], [-6804760346405793122740181139670169922], [-10952413437386397502114206023439940920], [8233429399556176483339277529602978234], [-12554229789719706357690373304621495238], [-2707132672817446383923941003649554332], [-1149450915825982852999856297712549538], [-3293055576933898962230041419583253356], [18130529692862362429763125641314688018], [5209386799072215320925167268540612362], [-885938328805567688542054963298753248], [22314064803777052717970048455687907220], [-13850436734892613404174279789951209938], [16680588590524428782473734465093591446], [-14410117803479281207969620898037139234], [3613859031271878940079187517143498188], [10813435863379455844983675504625110234], [4011346977523260612263600659838948022], [-17167803235651353967267766830336953100], [24902099768349511550482890875152833714], [8178714046486826391572807736094339814], [19682333674522640672433730059733516316], [33753707554462357592034258446957676458], [-25441175077328574038487428939249423488], [-10298797708085442549474692934334632814], [38979313959677607395379428890349174888], [-23486135016578269948734073876924192692], [3248345314158539425852487602117882550], [-42366849565057601940365366009675001094], [-13921454772521785558160063267801246940], [-17706715008251637279297965707630430880], [26868791635835418365196272313195092024], [-26828402969157212248021557903228958920], [49478739891204379204035518036094832046], [16769406165081720836299356506822982116], [-68468793980910641706390707726091612720], [-43852440914132349612973253276646244942], [-18207254814085416746860935726105673278], [-54294716495851943037633620870391080248], [6331048789837659842202128728630146270], [-6324449625274190704927058638160953446], [-50205750686421328871657896631968526076], [-5657650109789134641684247091044439178], [71802377546482102216596239722843549596], [33821931780632243388083967720899794598], [-79273754142613177565340019368446568630], [-10122495985084486742081699890462947328], [-117591659782237391651204668037674458654], [106648678734587332595200154563359134220], [-6595489462520658429558909564210780052], [-38678385793676991935025681312042866374], [9466743807208797032345229267417489402], [-86069701212243232628289947732002160368], [144365188125428153997608240454015352164], [4552710205769663847163904696648883668], [44438182581781362751374531816155113634], [195705335461102653442446043350060455484], [14109898356463461893941207352895188486], [56205599365010318895332373536678231544], [-63295447143082422571174661092788866578], [-37563011275994172864000482571787667952], [32407921102112906747020183905890731074], [226517920585684760341448668242020544990], [-108859150522627783201942892741888558452], [7479026911073971301345602376084083944], [-36312140012872021622025727559728660774], [74948660860472507012835748673733333828], [-161956795913673484341572794179352396032], [-247945604744500137960886039789796407870], [-208976821821559521186682157185277118642], [260079846842271806702209845577790159900], [15765045050895436394936092061216473130], [228435441240344381413657670783934442314], [285339237358468418788411383495955824512], [33873144742001540637737729994571749798], [-391585797538357715631336108345071719534], [67082204531877185835008151151058132780], [-113060213949828795760102065436847680492], [-457892282234910643848069826410451352928], [146417255554034818911303967397313338514], [472306318764238569033615148084273697222], [645184193478240508091769142050448962666], [114566768113358288515497793140912660320], [96103765901938088207841452939529484116], [-438146962043389864097813764383247157212], [578647052377803728298884946892522567212], [-599827024366226985456896461629456868522], [-343438162380883067078158311366850228386], [469017657525078904678863720618094060058], [-65507707465987090590809281624597704016], [-561836386348308959770773801817524822658], [4225564207727664823681655021841906870], [-27337629995350124402126092726909213134], [-555338599632683762954911334589303963688], [527598793861669694394407854273604226954], [559387732232491774110020090971582891380], [628758639486649733048550401423778104078], [-25680464483004752568056917773831046588], [-729795382892836527363546437908778683920], [658049625079772731689952324331415303138], [1112494867427294953878359512403607298796], [8185112192413646747419059296748680762], [-1050323475802702295616192778632212545350], [-1097726978345398500901257958396042983856], [82423786210204244398597025631528647966], [-482258579243750776084262223161355446826], [-640156711777281898184241730480231788576], [-498436575323266894061935792246402077444], [-528336672768728981224243518168148640082], [159974928058651738789480740241221146424], [-607793553153501114687629317958959598866], [-10501236846975034291715204526517098234], [-315734898233553362288839772014837844592], [-658021791726617071034367454460466905364], [-186781632743060874606645416443817083444], [-946103603210442800718943361769493069990], [277805181953607319000886105328206922906], [-178643419513138650060328786209458828584], [-2243717063981377572447241540977296649814], [-48214964558859295708623959865663918924], [2198909617804249008854979348039725340356], [-1096317688046880859787675846841761867280], [-1604249454399540649560865798566584826846], [-2356131901660260945834474380964462238936], [2617929909133830980680582520201255877110], [2995457841473727579463092345596647105764], [-626981471724459491268487323864360546528], [1692405790318623751924242606158280383714], [1747291265729996489237288707132849102062], [1572433538510288769510187311140225039306], [-2306159934059242200192698699748016755222], [540018958805481394564452200652715168518], [967653374463593347897483468416952238792], [-379902489847353292478536105856783818914], [-961587724806568460099204402941429516468], [2117326113221897550980430322558653711528], [-788914310102775411324300684878296001296], [3791944868288455684346445761008653252926], [-1751536317219391354091793240623231808268], [-2065824457274978706091797685663491437376], [-2777699104684450356628266274936866738046], [814887651934982228114302874493576543474], [-2198019709762604899336984082983590862692], [1119986450786704436315001715873005864860], [371623591637579871375613692815447673432], [-3472732294816930915566796606536137404822], [3393203808052400590551735384469539737600], [-5729360398576876458707885135117639822874], [5730551749689601510562938940278558183940], [-5742935357499674805564345980925288692574], [177216383966947596179392684515397890038], [3204310372251608470719450286232487884286], [-1780462236403918993376242335385862927920], [7008739834145921047907958475034706992790], [1541144672512150382262075540059046637074], [-2599833623906666811549038715269349457480], [-6702305772468065291989493781728129249146], [-5526358952766601114120950103069798586706], [5471441991481088280202785470982069187388], [6889870090974748313796046851678979832916], [-2090984450925641594007645045513657752206], [2223326162106106081947987821279907624834], [-5547832489499940092958790426951494273300], [-8633937153949849289893594025801284688884], [-6761728917924980785172539651782655894054], [-6749513901376689559831110573546636419814], [2717473874579451610349995165344437265984], [392305282690821155316453805146861250136], [6582684212644025288064839270869678763790], [3038009302986423286483030379793045986136], [10068241417052131297974996873143643921162], [-5555006772122001723340695699271356406258], [-11809389667650493650558836245409975230992], [-6375442330980368518499986280525114872884], [-9841191352638325180001865068675772280966], [13751553310489829422518676853751846760314], [5427137860448957082078584076164904598500], [9176685481065732751441777301605967854230], [6179568406224019361341838821873667742356], [-5108768923830898351984059151490515715784]];
    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_24_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_24_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, 24));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_24_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_24_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_9_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,24,sign)));
    Vf := Kernel([<2,R![1128, 1]>],Snew);
    return Vf;
end function;