
// Make newform 2.42.a.a 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_42_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_2_42_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_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 := 42;
    raw_aps := [[-1048576], [5043516516], [-48504195130650], [-119392445696650168], [3153808852281809358252], [-11410299686425943429074], [-26723760622401267203746158], [67975218671585815673353460], [-13505073711965391061204062504], [136715473782261984486517330110], [3061422960031285210493644903712], [-221949310451710778755633026909178], [-501985001900072034529449934630038], [-3118478207799237814439067265458484], [13155518377394721101101992302953392], [-323998598696161709213951685217879914], [3459574233993702447512816107676048220], [-978043389864200648530692366749996578], [16627547762843789524411885842685183652], [116968857862178710569082285660379434632], [190708537939407193015711554853622755466], [-561362454250092673715323867778422642480], [-605770926784301808468900622973886535884], [11915426835300121180468989518247227755290], [-63576010574535484047856854187104758743198], [6209870560863188511855630172901175844422], [173412308785742524089103102162200434678696], [177847433660272904624909715426897661190412], [-1027368167065303051149114840122320903853170], [1360758254489687447952462592309696108152306], [-12570076233104936101873704650956460396276608], [-11499737546541636409717941682828750396286748], [-19912022156332934513543804148527258873074998], [85743579476274966472453389625443139743699500], [28550870321029638346696576186340191490575350], [189816016146448549552140432741832343341982552], [384758169711821099551628050485036430489662782], [-475005594633603929377707223899901409543187964], [-4323452970930976947812155303871000429301901848], [14492424890701607349912673383555378573431922126], [5676686489228542394462945029568908154947338900], [-22089529317841423587996101960491895673748079338], [-62339529346959540516259383371291637669812298048], [106447616752532276188354028743337617564675456706], [84929575727690505900076894428517412703286067622], [-182716141257067664485926829367987022122927209720], [-787225572486249858702645233299072583161133478988], [-1321596721747418235973399371450961884561640819744], [-1434557387582099351278051888044790625536725441468], [-3689225344799079435593704775136478104185098935610], [6325614066038401062189777832328880235682935325226], [8393853877048816827611112145782372231038265901680], [2761681183084624759837514857742159990817877949042], [5475055185715595307214516201243254613890989556252], [-24140275651277531728956439990189369348657396831998], [-10076720984756832842794720934252206053085579788984], [-34758011564092397806730976980511571651187985139410], [92021929212468701409139644541388272964266038011792], [-112019129125196131947663785484913532414641670598538], [113941091180964391156503342900855706679920803576282], [206091256894473803642270799416246190626671625659196], [-336281053573324192647851612154812513721258885446394], [-652252445985124528186623539300939484329090613942508], [-773007434499275923644739564147602580817396374547208], [2768769928698185563491069834117561109469392539076346], [705058169012170415521550205529531193432961011643102], [-2051366498739809134036829050439149626655453590103828], [-10372473489430296706605112922726780998363790202926638], [285107480482638588286920853214617323279931610187132], [-9316732428077037228903797301985086219325281174361090], [-18172071574557844088435154529355217704579186177472414], [19079131741305111662546169433876673473560304982787240], [-21175960501572018467122754611940654254660208298715408], [-26456562240646455310112229110775591229601443165944874], [-62838796564766649415622918011943520577466642646186340], [-79622129477200989528904507772039461537128118042641024], [-109704885343643911621847189834306272965894565313943450], [348160898301227745757281186708202291818500887360740782], [27818025021473470114530545323551865400936235829515282], [43454050314980769058590865127601731946130650320883290], [-548344128201555715530724086296298957146668753348666620], [335909206169911944658789281350973928663445375506860422], [-752112940461997822430045571289147075529025075444661968], [1011712695291593121431015693106074401479542613433579826], [1177953519188392011211796112417036894485685958882822520], [572043207136915434977177728541724960855261816787429596], [-2892698879467099227201296981446991046275934608242382910], [-335307047147176648142594366716944013899949213872378358], [694063809824488935398985758325243359857069510556897262], [-864109199602950483060710852518979833489962244930312624], [-5758659874416847925173906164161562916402844503227951948], [10624129217239114180624341523356197241352964512363027680], [2537314661743659199683015402630197566730756784981375272], [17401257154325921893958578381035822507485511202264018572], [1715323100366988265421667748269762399622377307328986580], [-31629073560930432122638787806198487157890352768496131144], [35911570648780706637826396322878840273399262546538817950], [10353350005147116813080920638108924204477188478648322122], [1167960802092566732578819818166639106761173405675806636], [-127601379811206900930904314224912524555698644995863960898], [190287487027217850595444651067868575024968129225193127812], [15247343284851037497593073555257395461751900469288012622], [-89983233635180211453199070266462201043149997787361562604], [381739835097165351193917479526822821588455253746880065530], [-286684142964083157226200516626877250998390752828987953828], [-26623793891095157072930428954820298699330038446175478718], [789073085293503822293268306699758532182518498736255754732], [206394172915878252057121574325846976899109350389531521746], [276253524523976468281232333117027995731257618813043530520], [-831600161296620424818582327206370923636275922514520253158], [-229755158259506719121927657533157459499738589940866764448], [-2250258156147730571361329997047708515892831247005653028794], [-386090046534193780155932661722365151204284725966527045718], [1662997879548283682582762391000335426112173510188359742220], [-1113224865620809123830482912571908574600916849396141369288], [-4230056610755595484349920345088316665661030362071627544958], [-3654157014609104429737071208684356551597294085930593408284], [-8110398205425932789848159166114144519213861110165888951608], [1523560551640510951486513052212112770366496641724421760686], [4798029887281516937926436962867649802875283735419362257780], [-723138260011012367885283194723036170388786890912415605258], [4084435161836125296298305914286277220826678448504108293026], [-5759682117423034322405036531356991753418530099117001108858], [-427246180869525221036972272248870628104847976475732724404], [21561452167455680327361689344419178364534526942686993388692], [-13458274286153045225664739803914385388222189117382274506658], [-44989257064125664829763946217491656024544416573752621378650], [-36026415150982244744829088570976164804387189265392664572080], [-12107213027413865910513478148690027434939589944918688067688], [-45607050762635466540112996884064300426889403908405401757314], [59899687304386794497493204407342800645698818357022311265860], [-16079952442294633774466655450929441579120800652882531229144], [-129410199216566756904674518790891437961403665458372598819728], [104679289549862544866455176052287270194433684590324896502102], [-52202308778899190798286966407101820402276558970689989353798], [-114958155623623589257734044674620384760649674592758398657790], [216390185796271373664384726910174076936571307428670642468326], [326265488274930718933952777338965601057373139572037651697652], [-537863743149918688301026336180647963044357737898311112758338], [-70442528987479658236572779512014030577491522593251711545750], [675288743630278580511782062789032163060247990186056494856652], [104794765107732393423744532479761242802957031499425146180502], [2141071808824343856164853721672343026347353232956300011256], [-32829546716909040443180558181126890833696203224310561098148], [-720494960099168138694963733366865288008105776828514350840610], [-237195705680509229122150992877222453927969685438763457574520], [103257876189469901316932621609096097185955813460349964739766], [1280575589566664036269155556547992898280042224418973353780762], [-1160673560734201442139860465095837983255456768201487328585860], [-1198967936436788747224798313869870832014066195359369519244704], [-1626180285919007604595299026616367660000507312060695084792178], [520975010511479913142779182769406282993634557418964174192882], [-1273766882807047179564321364816441643619548297148533233063084], [-1402710507816747167798796058464056929089565761355408951295688], [1323235433753559157327566525221007018071864565359778292493612], [-4519727020356695598051591218875199076443934440947137346875888], [-3081440647762455313309967006412687847087728236316541644005800], [-3193200311716018921131803975938228095320653718360355618143070], [-1074267705087330278409574389017713154142685903754856428150038], [-14784118473155228654644985854427924039175553725249909486454578], [-8720667421231972162598709867020583282453465128617794364285548], [602037285895275700435173854383834326414736134981023734730106], [18573042641072701798297187574647557869627534046185196795559432], [-28010139013042944658276817995322281678360686376817143076065428], [2947131348402834484380079021116125209322329896918291454791762], [11171817215038535230804600984512719420462240498407724265601176], [-9786820333310034575086317451402180929513287958088468955730208], [-16223049070470311801252287033205215455447676569381522748162618]];
    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_42_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_2_42_a_a(:prec:=1)
    chi := MakeCharacter_2_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 42));
    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_42_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_2_42_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,42,sign)));
    Vf := Kernel([<3,R![-5043516516, 1]>],Snew);
    return Vf;
end function;