
// Make newform 2.40.a.a in Magma, downloaded from the LMFDB on 20 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_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_40_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 := 40;
    raw_aps := [[524288], [-735458292], [-16226178983250], [16050065775887864], [-167429747630019631548], [-1323691058888421756442], [-496480799590583480551566], [-11499782498758130928946180], [-666241934758249389046846872], [44018303948798095210174459350], [15831006217138043611820986592], [-2586566128509039416001203660146], [51237316431927477788465354103642], [78798404752245159214502811925028], [242751106429257069824806165133904], [695517143534500492850281534063998], [-20181524948439110783048183071512300], [-128433361186908960737422356033926218], [456917726301331948233545546175061964], [-99458563668407119053976138500473928], [812887758504563318241014461088181818], [-8572745601192170985075907678235922640], [7125890269767880907477351827779977148], [-130969521476936175444631870523462811990], [761042994574827986180495148895603139234], [-714964484776919873992457167802680413618], [655258604515964711163616001555235887768], [-5914562962061465232645242003479680071196], [-4185041836769264128696670873351302842490], [3824689324548054249832479260871681062418], [81415008658441407433844313742269341258624], [255072419580034173647590002807016383239052], [-157559580637672516670157103702005918028806], [-588829467631347805153391042196053251832380], [1207468095436203795680955433606725593022750], [5685078770571915055615639967009085337150952], [-9750642781149204731232375715227619087510186], [4570338059050573545178912138325683670770028], [21618029318358468376113504832264309622399064], [-36190839482135957552309576436878267199691962], [87301009530057872451477094138693019315131740], [-151508334867606442604455452025314037108595458], [432591245287308117249594444031985125761682752], [-704390237977568367554048507553799466811044542], [-181044287048822846377122871662040687638425106], [-978459617827575790886017454956637968363550920], [187843601994419420600745425982917679926949692], [-7178385275881593113777518333106096329802371552], [-7821838076856411148703061623034581485361273556], [-4607137508686822261642116460980747270393413170], [18088843067261541183791113706541451305751921818], [-8375055536858292882008787300303750979828441200], [1671672116451548637969748949239043320355625362], [-7605449976998310054535220299034714094819090348], [96464222883623141531590426470346263940073596674], [-238043372720282238750611637847854357513816335112], [-343729860079334304535696247162817381177988013850], [234239150235147149214788633417285307934069580912], [497473387755029471456765235887476539272024703134], [-228342911781247920635360107332927279731780243478], [609891262976240824063492560779186473936260806708], [-2185356495841585652506807567592929962997908899442], [-1847111296233690584688935273612443145967570277156], [3226736011058023558452591459065994724794682506312], [-6993899741243929354505015481752565171559661064502], [5379162700312569970657677341642313321405568441014], [-14001778697581696163234810895774420022204589718268], [-3355765358344739444607277252625806654292965810126], [-26083896061620233085866720108501611692232370417676], [20363490378656526592934422947125690876745674178710], [80831896134418748984940627645496356493826524129698], [73536253707944887224546629542048821948395975258520], [-99943657977765360104375128706189478135735513496816], [-106368229529124179592853920566569399555221357517762], [309568250305884774558169099255046969955977191788500], [131361625111559507895749442905790351856900412439168], [127217312718467926020580411048785871861276350343470], [-410443350641037538325406100458263502807332215932826], [75878734117426408606137933894609929491288732852722], [210087362436531530053236194224371870541558162488170], [177773473827741539750131211142423558784524753580140], [-765652991396037541942717225836209700185912814450418], [-906011793089441014685668998778580367341728677108528], [-2959579207459820569655051314996195610525052614881582], [-1932854913534546092993517344305099984876321574856760], [5234731593016856798415509299521035298214143256835988], [2644976242053728521957798137658467694363323199386690], [33721834303491529514963917094176421120541529284794], [-793486576663612133720177695840916353080789139229658], [-7375746877925635671633548451388642422062629236557392], [-8022860466343274064091910506105488528502643266116036], [21087115475443494964919542435819904694820266828076320], [21733050203091570257635449675146622610667381982567704], [-46457961683038238851229086999284050311671940426528668], [71883419052078109397393785585749786421901500122775580], [-12324832666291081225353142339025974398241706994657912], [21873765175844674946793072234598660769614246982448630], [-75092519574640538844109423181052576122461363893731718], [-156095374963145867510050113066066223165214071683701692], [26170976648279629065936622682354541220649107316870102], [-407794903266657474628355384907026758475839241868732436], [-67294848386426680459064538676524037412034484535326266], [-455904361394604086202101433912098393239207115130479652], [598038718613465240293433352719866482310163168933266890], [35405592018459996745673377518628365587708140583200532], [271624333762528789330971657383370941012816797576800194], [-917024742756850238269006385390301034629555428199070716], [-1826613903740050812311108719029895578063238431057811662], [2029963157263298697792610362490764170777298237547374120], [-1351729903309332901535091349221513601036019708829815638], [2316497338581441083269711306283591962802857225795569824], [-204560753032764024660146398299127635144913489734799282], [-594190419529836180746241380674212387644802039783049446], [1600534796345891731013070759197938115098609188064708580], [3927564185599801490430142382615767634836678734868656392], [3850057676376216793995051816455739953962436319093784962], [-3532583761695489825246598939684672417863773397036371572], [6407787720383212160647617089407687792240149503864995704], [1992102564948075485744018066932898259733095584346007398], [-11724848659436324789412557092883354289775790344111339140], [-13140630981905141489545634915756477483930205277875290338], [9019812926270510633168353634723981391449084235495651938], [24739870667136070423104965855108429834985879710980188174], [3208671571901919778805722162907717606771000861334271908], [-21639319744074297414754792464435241561682611836462746788], [-19253343008550534340909427516004542929021829079693364938], [-50042810191893758198307413892401634925367558539707953170], [29483718357477969737333468276967981978390878850031304880], [90655101137604719509348374498057277522146551888980418984], [121736886522046124943175825425499073705786831194961180438], [-74260503869016129479838804085692698569134554288377822420], [98655829156433218448524765379032534777009572588329923608], [197248570802987251773057207023106407124612339379131231632], [198250827928398804518012136384510115095971311449495683774], [183094723797207298712691889246878689952836798174172776202], [-206386507653119631540286271579143366957845705634331634430], [-130612594791932859234000088417932730696272416656220948562], [104230822675320730904038778694713810971547623526382190844], [194067403874474292791615017170725909012862484268769958614], [223938195122000714141685297485226526417138641954282455130], [-547814091342870234934536973480448646217826401967823587548], [-498979602251898829596500342247945039806093755842672704898], [403098414458610167056382032912121030071063346862546315848], [5983932037348227565385127149671379131506030186710992404], [-622264926756803198616753330563469246990647799584180324170], [1719249356031371501410099332532077449042812119219216367800], [-1474982396646661144917244662644814574839293479469525889442], [-2259224265989947039806569229617834403271544713351920655446], [1626023075498902462539724164460558626230520158528200658420], [-493930572553240276682015061834139632447498488399194361952], [3262531681918429613512273432589434365544647946472903623814], [1491441497805828310306253775308422528281341138343083330322], [-1825757585776992661101681816333848250151518206551248337252], [-3771834696265705730914541534882680518577776792681022429176], [3059049925375584252942328785691161552592792065820835742404], [-3490117995038090393578937302112675443213644880425824225808], [1500667632727578019964527651225002113009926555170598947560], [10722947589201674915619359228902511937386402763819697545570], [-2190366736391909360089806302908009421737562650830243980326], [-10810142233665870029577679274887287171123399567641650597818], [10136396236443351242893809233253262207602435220379775918684], [-33170170273237510892544149941821777969600913615499073462], [21973009079673969374344193735122800191696571319079033883704], [17683547218806398585120493704878451605626798280837593408132], [-5669634124224890666698450985125543799805503384925737061966], [-32254455515735164584376270208657195296294785147578989209432], [27397425754471157223875246797229329244976920229318164570912], [-27126042832057549191002872233822723561207399243794295071026]];
    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_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_40_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, 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_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_40_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_2_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,40,sign)));
    Vf := Kernel([<3,R![735458292, 1]>],Snew);
    return Vf;
end function;