
// Make newform 2.30.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_30_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_30_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
        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 := 30;
    raw_aps := [[16384], [4782996], [6065841750], [904018883432], [2348011244715132], [16003222193389886], [-535853837930780718], [-4500449383992496540], [6054145763195418936], [-2361414770177543957490], [4580859651056014465952], [-50376596834828206261258], [157191196896822750812682], [-210053142195379460736484], [-885237004192860923541648], [19545079942227702370848966], [4147355257120207357648620], [-63157257621786600629654098], [15782714250030816425172692], [-458578117673329990520126808], [382939334311990038717870506], [4055656551380978437159131920], [-12017293371822907068052905564], [10442412205106441731707520890], [108412279645050844119569076962], [29662752828503092393837901622], [-126649138365173392033017028024], [-174840464970131276086933133988], [555473702256859041233647040030], [-301768740711984758415809715534], [562673767973448421854429433472], [-3863479487497224867394033008108], [-4011247934984661087286244032278], [19195970975522189217827627176700], [-19789069284575833802499228034650], [-25762500142362041877686190770248], [-81597017354417296449163773724018], [-103020694145584799691346354173004], [-173039393275897897415799409356408], [143304128344610449086071949961566], [36110973860518760125028274759300], [629091017268518187122259492620102], [-906844359720108133095837205604928], [1289100833374956312795664849888706], [1931872948911906610424429488732182], [2647393872570228057349985517379880], [7495056540657699516288226330413092], [6525226101248007489533975558508896], [-2607960477571046442682516155576588], [-16896167935897626647057129678107210], [1445567271201717427015634639923146], [1736762707308993696764887510117680], [15905357280136226649764681093267762], [-34339356801021640148383430422097748], [106321106911659798708306107417632002], [-36912873464923006314286404227452824], [-62991276472930402453133751108342210], [110313526781400884836611904867999952], [-107963011209857241084756110151069658], [-216048757094498427439081763414100678], [421470477792579705823415231307466316], [769991110562611969807534564316252406], [1168086836757073413243467743375199492], [560555562020667560908280286642560472], [2389454458116592750531680348291929306], [-199837555881177920195510318534763858], [-996478637833749908436568173609470788], [4474234617356918694544310502418033682], [2598415554897712451032825873172177292], [-12872887786833951510206162426626259890], [5879755795145505529542762633048784866], [-16125429774133863775862598281357336760], [8448921838242492097404597350573120432], [-810327679917053163787662804686083834], [36646846384893052785259582084104374060], [-18653896775911761194593404875803595904], [-16196889720999548641206523057765406250], [8084684840282556793684026364035454142], [-28793495752529381432712891069991455918], [10810817265663897975818218514527503290], [206118968362426072428255382812608256180], [9380715909365226371791906917795921782], [-271867301324533473128342602568358931728], [-282168215447863604883440933201809168654], [-139515113598157676731174255083371783080], [-36259636293595788751883766000998579604], [219187203848290389150050159226242269890], [398729528063789203817046545479497555242], [-556293270656389268535618931144480454658], [-629012111364696027432720173665197726064], [567745550924386188940304532978739942692], [726364487814975592126523452488860266080], [655809210340757631841609138590770625992], [-462816330791321551480268241070897174308], [-745396236971060768025595712494583445820], [286073495219223824861197453955967933336], [2432009002830677386850644853630851056750], [738459103075439895267525978320915250282], [-1236400172423558368871079391657218779524], [2160856269698986754518044216197389291022], [-1502269264725456445070446084169294209228], [-4628469223571149915420319112449906303778], [6165688162510558567233714043187890084356], [-10637419943235651629159707809330220805670], [-14170659775085994786709258737891733653268], [16148233990231858620499288305624085258562], [-17998077351099110026840919595767187873348], [-2037297940763653109248899082717154326254], [-26435707227136188140089639800031179763080], [4750071883355457304396861988025602392442], [24571873571705680927266315122225332681952], [12744672399471566445377964185090766893366], [-8256782039452390274081195133001901956278], [-7428266991607391502623951425367042441380], [-73512955731664485519955869647610871670248], [-20735660331646030139382201422510535559038], [-19911344323666060925527781904446956053484], [90977077313243705998720484776020926155752], [103917416451848421887338390566516737106366], [51461724211928057230820808634193605258980], [5736064814332906732395929458471886861222], [-146250267252530311035574789007830229101534], [-152559479890832733882707099508031453213578], [41383093225561999241331364634734157621916], [114699166447846598932041893864770368635012], [-226468095543409202266058480568667503522258], [6459825628849848191915085595602562544150], [-387911428403886584380898794763077126765680], [-381152241157718067341946542343574698848008], [495732923845407868645926166648332968237006], [-162284502718959732813150323698896108099340], [-836521790408747481794732019861220169106744], [-56305070251297559298602917865050890885328], [602905228018256483240905512862330741460102], [918079623136143237011132559735062692061082], [-135694073332488159539028307672236741441790], [-203133210605374458415134874886160562123434], [-777426705428687574258861857926591697808028], [1066963390655283843934423759650838435748622], [-318485166110413875107356314028425201544950], [1963175432194338757805039864623591853211932], [607838171726304723475536308533145356933062], [3166007623865040934486815798127593491060696], [-1836878283832000134849902407449928265947668], [-2768180603596215252867732338951591306128210], [-1225004817044987366266067353797565317842520], [-561430406489826199197090763805126272780954], [448176334116611144706913394028518643658362], [-5546303063609409512892313028381230568735860], [-5060867158855853504827038633729697059661344], [-1956277960439027559067722636564490502595298], [3908567360414357770603928193573179397388722], [-2045420760926449976499258321714360980178364], [7391196781347761558718908239320410937431832], [4044378783080965574807357096660688406780412], [8235422319140255261906541320220831810963792], [10396022673226281155955617651325390067887800], [6682594452504125540036265009400069945815330], [7994614468706975781371587673893899876556682], [1840568419452614592308902408091689693442142], [-7875407058859646430530446402993569792356988], [-26953521374285117587645785673470256966619814], [5344797782189698948598793671634107766495272], [-18153124691372901470567616816890330944297668], [29675220753064568006439799829547254688695442], [-25445765519281337222661586312447220772368904], [-21936777967954584873614356084651737793091488], [11399971495364785018116119052709033135273142]];
    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_30_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_30_a_b(:prec:=1)
    chi := MakeCharacter_2_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 30));
    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_30_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_30_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,30,sign)));
    Vf := Kernel([<3,R![-4782996, 1]>],Snew);
    return Vf;
end function;