
// Make newform 2.38.a.b in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_2_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_2_38_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_2_38_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
        poly := [-55893200460, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-1920, 3840]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_2_a();"
function MakeCharacter_2_a()
    N := 2;
    order := 1;
    char_gens := [];
    v := [];
    // 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_2_a_Hecke(Kf)
    return MakeCharacter_2_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 := 38;
    raw_aps := [[262144, 0], [-250843404, -1], [2091300429750, -2700], [-1756339478768728, -5318082], [12829972861280186652, -7107001587], [-75190417767106126114, 695679299028], [82583728581519357747282, 14083120550232], [157734510926589561887300, -556602440642757], [11730167126699081764066296, -723601869769926], [-77193309307104829302009810, 99413651854516356], [242345490521719607352512672, 3512250494601498936], [-64183257436575810206800049578, 64367517886888263012], [-555181863109011680513685527478, 433857852281879578224], [524681367553369530773603590076, -1420025986708045603803], [803185932819776720231779072752, 3882577377095549731188], [18529665684778853516749978354086, 114263748016023614631588], [636440602759043143694605960435980, 85674882872099729603457], [-651684237932367832987566539057458, 675338640925321511048052], [192611717225080833640248603980852, 9238151655313059884415759], [7120434787403425843436095029755112, 130327615300852695415662], [2925667665182397186118823674622186, -21458872554905915303837832], [-84385598461387674540162380103625840, -15605010885711527099893332], [325197059993447419690710947024711556, -309005802320316246133112565], [347852325974829999501854215743602490, 1511716442311596756074738040], [2944294608774275501224643821403351522, 2063941447331244225118724280], [7515745053650791140951397948766261142, -5825564685191822693340176316], [13794393599453130669911154196982974856, 10379352802122200476868454630], [-9300985790376949391326061741358596868, 5318419398749771492531820789], [-41642156416499494905718423007772669250, -53147221073215679561419193436], [-15167139153629939102360579460511675854, 26150350188636096736977082944], [537933395056973590853581288531761529472, -249588227140894683950382796896], [-526908250930809610334655787436988241548, -168880192017088498253583231681], [486889426405455447059730677487047677482, -3133877094265546834295012757072], [-2261963913925733908371127801614755619940, 1944222404430564938285420362317], [18143298014311227544235007666156540918150, -3689436893152836165519321345420], [10054393116382803340600078296830598252152, -21174907131702236797778588996070], [38891747956723493836484515559437880795822, 9134573009834780708504330315988], [27561818349598236652175087429908881140756, 107458727859853403797928456013015], [-141197402548844564396442371769101490318648, 43520512825087752114321230544534], [-2314017953871951176705758421221348974594, 408512627300328282823831482392820], [-580527473048594484645106084064949978906780, -264018689378857962225150653954061], [785305976441379124973014328255910504797222, -136278254274161568874533866967084], [-118692385944847881980213773904627831696448, -2430834167069291944675076681451888], [1864327900929885387431035859874006734983106, -1949419755048353807101173965863560], [-2490997377093702261757324285645941521510538, 1751863683978985218786258583922964], [25852763050387751605940794294127024729960, 5723669193407495311249573866126], [11127581035949498014147363551584129487559172, 5265350621190022787089960642372395], [10526314303956958720279269443808794680632416, -15127267966043714854283091071157624], [-29677040585834872313230571335565931630296748, -29351269439803789384813147035687033], [-9766171729071557464701865303610895320387050, 39841048958111264184584646562271892], [-13416210393439496853415704908458901524470774, -47950337535651994956157336149081288], [56701391116204373689980264936113455399059120, 11327090609003117991791552414069508], [-94664873892479387622895042445412864418512718, -85605934664754210910341869504637480], [298296412213692071436791181303177902849691852, 40460968885385127948352707879886545], [305278756084170587503457316296104909639394562, 352593785002491141333832692158994432], [-310954960783957955652790824012510131404695384, -184988215991619997706322166009187394], [232331381854719024946357625475253861198265310, -616760918893092959353552693949942076], [-938683010980483138618287318590914003207227568, -886945964472463373461010757343145796], [602330772683624472364529342367127427285421062, -1448341115590919084463987655968252252], [-774872357229904321545471866085249048579378438, -2950522907673630148315394064268481640], [2539834842114056176979945978663887825503627116, 585741016528471542735798029282951721], [-6636745845267194875007595906885411819117082794, -1220080407572703405977488113808134060], [13025871952467445127652340030098501083920734692, 2629521862286417699481326646782016435], [-2482047397569576698101074805345012104257380968, -11502930787326823109933162189951348814], [-944452626120441861128424031339681193506435174, 17154683458352604658544213085249799152], [-3987245475208093068829625020819428238950734898, 26041967603613097361122230288533321604], [16025711162605722400927086021860076607756972892, 21676243465409370960963639146027811453], [6907545759363662264719391698282424518938733202, -62721532850532731436903607479127854240], [-80129448599261057191399009723267328802408893268, 14241546408705129370262983208090941881], [-46075367751895659753434157835872689723906710930, 130416532074874254976824637289408366052], [-18977586422643530831213202541064615618257507614, -114937544571859520387428205272852848672], [5996569156974005236842548197060919022522166920, 120919525942783560542955288119231893926], [-201973426009596162388114474520492716055983234768, -110474876021728030718922573889593135708], [-88326397658133556374963783807926783565609325594, 149390114772247654975954932375516079620], [132300778622273955817967339359082645740789099340, 318665290092762161607137130129919034481], [412860377801956892463504656236585588071034824576, 655620620662008172498651261379942051232], [-749840819069822834474132472646986984900863449290, -93137289134325151581966644783703719388], [140085969144265946076682501904020247551540069982, -366019483121553769172712101759016216924], [-920466134335950110101007420011704951155701409838, 926382260506030297444826057693676185496], [917485708611917714424380082957384207354594670970, 2393591101120344379394696406092614474416], [-346865828662687018780465249297475515892885876460, -1944824269552704715436223699891943292553], [1903219719024229213571524840955024358462177411542, -1174707302113381129503129527715733645260], [-1613223956133349654625127589839494706346482572688, 6953867998779512301368826067087399501140], [350334914387050056888237097414187614444405517426, -7477831157542636999176983312383373734248], [190134449914859780096493209873948421830216831000, -3093478161575693505228724244666536225134], [10847899289318812054766656492857026738648672721036, -1802246075263815263058101822627149082367], [11702572324520517459534438311047003721351605236930, 6904639627256302075804776873405769258488], [-14672194671930739036756531930462848149917795595158, -3469697377050912775284107656424865859920], [18182773567756292079224392288412006004484583429022, 13609959904129571751370948414462567377540], [-4051852324792339639862715136962020281887442330864, 24162373665401575671670468164298662970188], [22564909747319887580689976147766266075924431970052, -17842591461962858615788191577537713139701], [-32933141963293833976408246207127551331968828905120, 35875752793126485055374090087585738240840], [-19278828657800617003290288218377768458127420628728, -18825971318393350423376687427729413496378], [43489176794752489743104310177049929550023866375292, -4862005256370529857587287158651981303819], [-57491935807342212522686836774388336573583378933340, 31464570593751972099667049363086858442211], [108631578607051755895947766327862340819328480479576, 19660949334077910253354978740920380690242], [-46181984449731138991914985298844646912097892842610, 37255320425648092247898683732723982179988], [-168915983462263286795718096747167054567927625359958, -141206586255427512575056000495462695432720], [283008997777862747089893845648648171594237072866076, -48594244149803022655202615505654621996723], [-2635067745185062738203405323943824920565961465298, -343652622507839867556612194747512518460428], [113365677274769745554552354265149557804474256767892, -61699844808459756602021630084510807404713], [-553721753976425521074980955175618394807788579463298, 179349512411533517285978132395154898888276], [-264531409730817541519212224257612945543089155952924, -417796022543323305304652120132147797078413], [337347207603224848395097683751235787156066523944090, 1233991339498475034525234176154978018208752], [399859019412778438823181802620919268635059961844492, -440006639257772829563805156937236871524255], [405503813734181307838139803591590643388076570657602, -502841185268718633258690588522447871983648], [364615271315132155821833033909205538303416854579292, 1232280587994821185821466524159997991672829], [-412050347590738312273846511806719321751920335970414, -454877330419351143740683467018894255204672], [-1042119744167856467844060105054936510593574652610760, -2362144971113588044341383567217951184191126], [-1193396212558978640631067128418313740134201178846918, -3516692752651475269752253450775415752708232], [-2644687805411112738105820543282494253284401922937888, -3256613751125751922585164106265986683421848], [363801603856822857073974497194377119223761984266646, 4762795816066714893363563531549825969401524], [-1698732909681963398637306919021672102150999894738038, 2106772455213193281106275659778178384145880], [5388568259051624343449203462537568226639719342049020, -3001962777213741967912660006836572332558155], [1842017435372917114319869451216346617977199382592472, 7323531969229723651480960027377475055977506], [-5493276474532810929853183049119211675873905496731518, 4364391324248666001877306434462111712660920], [4452640269465844852741700582734816871233744327177076, -6571052077359647644606050864427461992348673], [663084780344914044802723138336322126835332974579752, -833379106567880131062333894307180868037282], [-2253592901672175326775932601627057172920387025892514, 3722152851762177528885778337944043782052868], [786740402387496535195878877049660807678327081967300, 2634655443705838009635374223777386266346619], [-11986479733027429001274299447598942161291829521386618, -7281289858198882343549040894273552941196204], [-3205374547952423366524832596917433572319867328553694, 28748667984704521172151651908458315716906360], [9793925878622415852707049413751464168066695338065622, -12101433864667878769966961825618333800530396], [31071062693021428465355545801499495021518642494637116, -21869690544876254151279124063567375293389499], [-27634024072724109783190397997024701699835776731059868, -38352733532489258199718928801526120175559565], [-5819738294674053372129850749902604986230416314762418, -17928887304224845347145200710284095803993292], [32670613243604768312209415693485427661863064519561590, -13346434059782751147764115184868246789693532], [49575434686888704240719911064944999493961779964207120, 77548722415023942955907351053627452801986700], [12726691895927562363829108958088270527985190835792312, -33773112334232883761687630386180929476143542], [-44662108517992673168899188291543268672012256388660754, 68870395239011486467974955188910337683853124], [93439388112044508065414090622690562486575071201405780, -39563111563870832634132273606712051428049401], [-67908272904820976080162375272304446231048202730404344, -157241647747980608300180726742699962024777850], [30015265714631907008676070040465771323634587219593392, -6489331626812189912657939981366950913823996], [118792765069039369661528297014865507902004971116450662, 105637006258948368519419425374442027256136372], [138987635318073792184367633801102207058598894185511002, -15291533899917754161452142286676867328279312], [-256971066433622786663084444800476305652435012069457150, 113180201435050542201953295845626424055906168], [-164854287348272372737234501128612950476553886552766794, -272436082554262306805283097693484261573132700], [8399794463264516363927556147862657654214337732036932, -163026158672343534466591043170738227899178597], [-82773516106989238490602689909516518502779972818434258, -612008333168979517668432648511505804798012268], [-892518484138734111123387413119161888134771146066889910, 14386610983695298625915719159923163542264048], [-280989454169239348208763530770551006724095865447911748, 185903252934064473147201600898671845961259653], [-517500901949604608518583386851695892956522506174295898, -978575489231976219990594177739967687517019644], [-821942724181252936143040520077992448160574891368929384, -244994584382208260717758286829028456136774094], [-427424510399147674501285624966305351307144540675921908, 1018066820548954178073561638225856374502424161], [-783466126097525957534208155168730241873247073945459250, 584749704951989620370211467982969973955329812], [653896948063527509542091206067694312782983827704761320, -1045427251157598384021635762043731022402367122], [314778025683797223729344203362728662583485924085291846, -1192318468669841023663845986204692494544835676], [599966409984287231755002360621180325175998334851572282, 1812068967548215614228708162213491100670775664], [-2161737192798040450234565575891580549767989429767332180, 1343777100851950689688397060071742037269591481], [2896200698236546206287819083119497266782163996433354976, -1148645003484250450348650671321858397592727448], [-1203820804222344202598730009735980569275697996020516418, 89891327810033091958824765027030198868491780], [-987869797273287795398415329793948614274516755043538638, -61647448128670765167618344677783056211439520], [59995912742677321884059413512447051626172005434968356, -5541461234953763493251115959979116240400576285], [-1539951344937187185378489747061511330291251911105675048, -4723567325438647400510224774441846817342126430], [-1938136398001452485401173203436490163664862348806874468, 4502862948589659085094228781802759371235964109], [-3652358232317841995262061643252369019583653691391278128, 3281152096742891765092896967557547292659693980], [5259229832184172417752635325675470591808693582843311480, -6634892642656543489890154637658193225936880934], [8683571937222735596764280975593879918110427071715344930, 870064906375551518606693965325466803864818680], [-2379797587207498241027813388560672339793329871826476598, 6989711719378395909327771301359871337681306040], [3343152827699366763632673518361430371848228542241433342, -5419474370814791995355395578147816344096773404], [-947782845916844479905977684028654194933800425559126428, -12425834277135288553298413042464420759940561005], [-2590127581293578881104117664544410522422970596073702374, 11239771373130287021458480896986658541001900592], [-10505766680174685017601375512243652530300794383543624088, -20585649960980464357455618981120043622251561650], [1825272816688659946869549309400646466583915802437552092, -48770033459018265634969281391668151022271555], [4663557990399337364937517181096812502978481445162777362, 20316251797900101198712977230694673896276823128], [13064832899568843735672925335805149753526153665856097976, 13385176988349172590998211947445514770374298762], [-25190934868053460041844681422557689797294544465641335968, -15284816610620084976951862018505902824166821240], [15959452785622408084334526764282235611390926956755503382, -17515906636020721798183950026129377610488394844]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_2_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_2_38_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_2_38_a_b(:prec:=2)
    chi := MakeCharacter_2_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 38));
    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_2_38_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_2_38_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,38,sign)));
    Vf := Kernel([<3,R![-761256363376355184, 501686808, 1]>],Snew);
    return Vf;
end function;