
// Make newform 2.40.a.b in Magma, downloaded from the LMFDB on 19 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_40_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_40_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 := [-1050523661880, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-960, 1920]];
    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 := 40;
    raw_aps := [[-524288, 0], [143709132, -1], [26811369083310, -21924], [3748995812923256, -26490618], [-216464436774763126908, -1640149371], [-555358442188990199578, -1090692827172], [-180863900478697380327054, -81667210689432], [5765545007452074008981180, 2573198844112971], [366699965358309062584071912, 98470758272206194], [-33141739310539510717735020330, 3076733487202628652], [-93395352745575112920429783328, -57411334880600430312], [1487738829295956779794170160526, 2012122109700130455468], [8385163365030009181076203798362, -13999423777504990589808], [-4906197502433425684193565425308, 21514057149920029245117], [336139213174850952833260684884816, 63584329793910877148292], [-4380932285816779162350575084703618, -1186750686692120643397332], [-6409612875335550695584766960780460, 1228961352776756385990249], [-10478462161275285225277213296022858, -4570710166670317211609604], [-215105455398693241323848388077094004, 181563338922839951529182031], [-252917563704386856813202393942701768, 247141447492263752024667126], [2612504306684446922477485848076693562, -264617760162734349768131832], [2566553029615099572023829168754652720, -7491707845823613341493730404], [-32434905856665380430890446251477647748, -2123837302331396120367563685], [-17927457103983536540294193563623055190, -61471632058232089351289510520], [-72785353825436950758650400647178320734, -198989742574144237787363431800], [-1089132359613576802507940663685202680498, 567145371224440484338683174732], [1128911170976399852182275194694034923032, 585795120244258648654401255630], [-452240973796772104774576980920566721244, -374959117187182299192782942259], [-2070317723782382672054690814780664518010, 3682806937456703831467361169228], [-10152843798302231393901428385600424106478, 1929580585551027534101935541184], [23176849056704011442017586939890793995136, 45237832746540507214344975483936], [-69241435773060678699796720607179923944628, -26388744925931743780409633790273], [231837550145471878486635104497814176530426, 141490707143008316627881995358032], [716537732340954563002139112502631656435460, 6413930750417721038172301601349], [-1221512812628085269908936769457177065134050, 713302050583203326811251432456220], [1055310179515388981265160548649353263221352, 341312255811923347110349857972690], [2596258745002886692297274912659046038379606, -442549759786308472006864188389988], [15807772046126333881062481900273302325110572, 118050939684362026407603809170935], [-15558973291539190927229410000143432970993704, 599732430536250555570617018166846], [-53196413257029162455904120277575662391889338, 8676554166082913056015307262245820], [76906066593578851890752128178042952770504220, 27349275761620725752339132771181603], [84517227427874608930414630946175787598132222, -40420744442056102852919349524522052], [186679512277255981011399074059059530780066112, -133304931527386898496782721157760304], [-38073351446328871887978303450955719212679358, 272667590485489403298905889074076360], [-254954518337810474609159049799664711979138834, -390676577264894111772491766658143684], [728331891465407393236582846120013939663420600, -16071021240726099386686322535030378], [1108300019158495297046551408109718249475677692, -467988165314860475149906674130827525], [3652967645252029863567500492374533994045102112, -751702122702809915104023184649962584], [-9575985624240279823133073641638230779955468564, 1617279126191767143950886954757984743], [-6087718636319064272424076158509361894473895730, 100492103293878820991767412938650684], [-1250128548117445082329453957485054845642762598, -3124722435215953151749822310321786808], [-7551584401335253894356069523542205272925891440, 15208438472068684513050214846627402836], [13492626879075419580327620134608694553768399762, 17348601701232433950310882319239198440], [20144780276769087539742187830398349116812834452, 16045108570849491624293193846787165305], [43748955837129116774555375911115702766988175106, 44550562658247281545708307771580484608], [38288373865097600508253344844378965330373873272, -64745168512563818025891812281164612474], [22083427338526164118777072657177808121149459430, -42985704799250589715472004798060014292], [44598157188485966514516944320507344757126578032, -20626358067360313117292391706792814868], [-251411679598169066398780031700840952651649954914, 18460353667701005289380148309405682092], [-246955237050310281050220946313722118501517702678, -171637650225078232539895546755106068120], [261138250548272129377601254342093762987694632052, -46076479492520242554753912046912717839], [-353443810616046180523943792444253596487517849458, 750221185055386136759031195851770228860], [3175490344730714194444548419228785861148073094556, -862967811616386921506211421856611555485], [812741554859971346322202356873625128776231829192, -467704264113542293233116691956661664182], [1528353120805162354921560387552384495771261022922, 1765146711024079102359012753513523306512], [5504648889399567663760692073465819726545717470646, 1775728448879349766996844523981585327276], [-5461419144891277687322957554139107189010032989628, 1778720447036309769502094094549037279029], [12781836280506358770098513697182739353631490738226, -7042007376914808866683914759502208953440], [-21422359170299315093980482878493996027114686580684, -17213210492784108655880987223408386500671], [-9980087992563335218568250890502452506022335417450, 27858604508102992588105044106835241179724], [13179382846004135485504394406720883395340489355682, -6094822303102980537496298257180523089632], [79797714422074753251525026963369933241632352527640, -2365655406477377074043658542044982978418], [-114933987283144302886960755850645282773300131102704, 16599049978187803129035121122433125306228], [-83981193127950810200057812105311057598496680454338, -47177297268353832896043977879564255988660], [-59853248388543379964078759205339574016172522508780, -114072630358108339356466530786445962855783], [161046957254100373757676050708840909588278427943552, 153489075406406454524864404613999006479392], [69056721133488860360719760734864471895799523288110, -163023027345754963303606251717627431598996], [86768911426050140637182176868504652371960890936166, -152331135025868334868526364486291945549876], [409931843736047886570567150191358079654234994748402, 217575244908114009432581275677005616094248], [-603000395532487192851333553253368889595207036252310, -303189699763430609687854144430081187684528], [-496411073158973810009798224247004928504329665176020, 290489388649083725649408534543558604917399], [444842396079310937868175786684464800338186659360782, -123611456078684654267482679527878867102180], [2425635444801595156106634180574330769129289314680272, -527489142633240173899824694703764776238620], [2441038463692982567988607346600023238652921868654802, -182347904579374766898556748142645177845208], [-2394309217296370296662049719895332779978653937126840, -1501864835113563753913027699570690297706838], [-487416652783323761133413738747121776365100716209708, -2195107937238788915680951358878943411465367], [-3766309718472984000029001378327880697054023404796350, 2447161548527492257927219222111605931267656], [9882731514005799030367763953376225304251778816619706, -1194065103131150500800161922269031262369200], [1106448862185072696484572692304616341692963592335142, 1748585109061538688231681268450627157167980], [405368485480074580012661352666285466663830077393072, 1165696515541068690424288133475313736585148], [-1556372390479762970214601374760834813118445028064004, -1112278465364470552926276144158486415122469], [-12172071460368877272070473655056817094268453146054880, 10867388291490452069572263149454406971256680], [25537472569556684363373599020468811293673193168205976, 6812923709451997969520016300599949096004398], [20164649213186599302678441944124211548070422853858212, -4610399694741438571960250638010568597929907], [-19008024459195049698524611039147615413763467871339300, -28843208201161311685013664839406354858200493], [28110409372620571276026926201362057659497472585370632, 2677483085712739667313475992122367814594842], [-71246821511198798726983775810578409620895011624620810, -22244165783144928747146443984810413758840484], [118412394849626135599865643730276852482334283372085882, -12386161485862365916205645689414684982784240], [33242407849209588604484176556052261904841072047566212, 57163470898493478498140986985391441421196997], [207054181750697300779444254960950400516408830736706262, 50643698314377684174776733577626878933793276], [16370068136114876234987279483965846183373948713049516, -98927581595472466990032562769265842454812937], [44737190425003536630150234099389234728591358672068806, 70906907779759472867201577168909993823937244], [-176144315563464801486754720899563816096735018492457828, -176353422357959641237387166538340469464901853], [-346082319517801706815409507096406322692500587614844470, -299359655567833024714228681087623836230669296], [307912147965236855560678656654813276502167050194235732, -94986491621307870291731879771960779637686455], [564453671574855886450551776230754126316909441686990786, -245550920087866739916496663276176611280015072], [1060332083473150467415706550890017165090288254348183876, -85372670998701494112624855492608953916941899], [-84801746579058649803527621933401406911768065434110158, 266832855775768611404635369777940754041149248], [220438541709779211923847383421506911970460494457810600, 102038683416477544625351233074275799960590178], [-859790565731331014056278721527920446159010244969312598, 122167395469612763830907671520625317527793544], [-1321623497562372045797115050642122995853048955516040544, 435144895046184358494228956428739180401984008], [-1997736248851585132118629305467661513696368217852364978, 819138277280538505877622838002842056412471964], [1313658576513456134164238254569148907171224364455256346, -1074895274737898523357642126005422682914844760], [-3803152818276584877103774374776917417998790509587021020, 278799449193426309046783184391346897186439565], [2714856262771157106341416211205884215324248694831726472, -2199857563260184477324355431365684125478320262], [-583821266682404907222175016985388349239506985539443838, 1314067461464598883260040168229328029136848520], [-5610061508203477509892513211983504231006364184856995508, 2356450674775157031348492559427361544626018687], [1882063151951763731302365582403072010134905168005921016, 2458769578238785301011769012142776968866699942], [5293356968072550332114904952429680968245600536263508582, -3125046739489329185340955305195004757834359572], [-1497131793536746233356268587934172368882214206178053060, 3007229266750205161732227304932682811566404363], [5520757338179272461198195555667842449151782800607923742, 836069012613845060471714969448432029200864188], [1134002615447386933642044243070508420040238611048459362, 4350683301315111487006933449511683995873563080], [9320018637062033918842743764711352911194281782713238286, -5384387589132330165430513360830532805130471444], [-10311638638335092121944379772065561006385597169035761948, -10151451430465141871349684947616231791292237219], [7038173539682098575209914914834740571666616931401974812, -9869123662048312989855502003954225676362044765], [1533554890311680965027507411035494462556764619654094902, 7791594658812956700910054134512611959514001724], [13774880430647412592380315855278197058051695529656740590, -20952773473247033787711572772115471441596319764], [64601647139285984442531066052584056597700108606402191280, 4683981159659347005344965516258881041799929340], [60853549061648544808925136879597293364272580691909444136, 1404799671882574706538655999069809171074860482], [-2708519393234242948796591482311016535834553047878929898, 58902693486195251700233190924970777649511457644], [-26630687907775257012736660908295615241605065083654347540, 5390280671940445627391762546029331623907248743], [20264819625026403998866983508880139931807156566889987992, 20531783563603870772030194957496940359390665710], [-41680694733998836909295168286520710240970691554070420848, -37992113729784278930615621572536887877697062828], [31251270881672449541394394195522843222422412603153398206, -102557989252023503780385857681309283534638775972], [-20762973612165348091778315005122056169081521289887757558, -23718647840166941409512362410521495996053687536], [174105979381302707379680695523864660524446317433938396930, 10939798809427046233939055207069380643178359496], [-73537110948644772957715710576736458779529089456164019538, -39055241334735314950534494875784015569545243860], [284291727529832359383881736963099507609129466782989292476, -135884278875215501190647102733483283912707231573], [-311896677113334971105510209749144832664229939960088776234, 81927662812448492596198354594255767747517058268], [-13771558833863954913859314156775191494326528018377292710, -63922542791235163891233653048604523952059133424], [220684742979255977149720246558982030886329338913507579492, 120062491676686521819672823114033701161536368669], [63102278268809769926685355285675852803404729538344269182, 192962702181194634800784766274361958696129478988], [436248241940117423218363875280839093526218994261738048712, 53811996330131387355137055969346972359179339466], [-487232432465316823010859610125310938673064893287495118764, -378580746937051967038513723518372929766949318071], [-183765251718601809400705553700837615551894326232582598730, 37790480237829300658467933512851000603469790684], [690653723815152552014148519408898299628214358774702575160, -180186907469139666952889302199992344577841222154], [-622188281239157255347123066380807837124369733967303763618, -530551776497341406945953597052169266038097221716], [1840889368971063222921533254431897680758395277380578772906, 73796375867820180759204509877802750808223271056], [424189146090776344717647552163824326043794151033978009140, -741868593347592214295717715665962990705627230783], [2325073777322830810585349510747606949276654327590473363872, -387890636408159906991795186352249823933176752888], [152624704779892680067284565763346665942162199639636164486, 464388865386647315474152512923980903058175305580], [-790263470075833303580167590101063823102334739618943012078, -1702921874088109235638445247365317985640677670240], [-1753353525616431981532682597172976533676717082659786333348, 75731020547062062943091377623022649271867317715], [-3432083946780822859406705211933739221272703036807873948024, -187863265029996573347555811290288597339439473670], [1268758214129883416871092951666407718191072513560819223556, -3153468236390382064987076387322292463700625653819], [-4690255978302645020560586059705278579805638196118200424208, 1415282187575681808571074872941552709605528611660], [-9167097998178502467654635614919605182115093905894987604120, 1119430369076684166495820062788037495158552448402], [-4960298395533081674045180495653197823216422126665326185630, -3047298008216363557882264092833997836720247412920], [5671546092467754700438209718856818835267684805714354916826, 4506357069377380101148393549967642576154638176200], [13842609174437211098315824998976701672318139779921248129862, -2109786945636327161563319457986924578456499710452], [7074060620034668426328982497670664545629248486071951801116, 5891156152163518919378090018979723724815937801155], [-15327469174006742219554754491686072813132543305979681416118, 1189888060956525361157832024402189296805411509712], [6578883150450132316113062557582214341350820404669211079096, 11031516556872223626109108114242359576618419481430], [8925580660054914393568099350331699657424885225555853883332, 1208494308410510013261622042782061547161044621045], [-13548960774313552595689842566053531006028624814213687899214, -9917319147991842483661506009769099101367150391448], [-4529551813450632187667787849441672148282616899842896082648, 10463960165822683992327969483870867914113164540162], [-6710646918184853760098437995237450889735228449974452581088, -6196650920850825854288527662306883924743261619800], [7204893555532752711296500584451138362512522427676673890766, -11885097498961360176975777336839842529318189708436]];
    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_40_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_40_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, 40));
    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_40_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_40_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,40,sign)));
    Vf := Kernel([<3,R![-3851998112535160176, -287418264, 1]>],Snew);
    return Vf;
end function;