
// Make newform 54.12.a.b in Magma, downloaded from the LMFDB on 15 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_54_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_54_12_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_54_12_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_54_a();"
function MakeCharacter_54_a()
    N := 54;
    order := 1;
    char_gens := [29];
    v := [1];
    // 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_54_a_Hecke(Kf)
    return MakeCharacter_54_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]];
    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 := 12;
    raw_aps := [[32], [0], [-5865], [3983], [500433], [-538012], [-1847880], [-10461022], [-40549182], [-41169954], [57873005], [-688582366], [-580836894], [-73179490], [929053878], [2611072053], [4666302732], [-7930719832], [-18493811722], [-16075533240], [-25404121951], [34794772952], [24133917129], [1666560942], [82667879663], [-1091065287], [149485847948], [131433000957], [-84157400302], [354601117938], [417823301441], [328647118125], [-895198596042], [-311499426916], [1248663593703], [-1507697212783], [55944113372], [-2349801784420], [2590853072682], [-1180179569685], [-1062974113749], [759595897424], [-4258084770228], [1100868314165], [-4359146358123], [3010694138777], [2125742849018], [14240673910184], [-14808473825196], [8784746073206], [-11184487864974], [-9301126691850], [-181431418810], [16046246996640], [21676196711316], [-5816395827750], [-7150486201578], [-35930033672785], [17123745976376], [8048948218656], [24094857192542], [-23308865490462], [28156745557208], [-81788767569294], [-81311822682907], [91005279244881], [10826543740910], [140189656685786], [-149738179143201], [143878749951110], [120175671123582], [26140568093298], [45227179947209], [207834486480296], [-190687958362780], [-75772848376608], [-24829164463521], [-96789178195540], [-127568463894132], [-375595592575393], [-80003019607908], [21466150131128], [-324740578320558], [-472181749853443], [155507093786669], [-564703989039804], [678001356496422], [-157754794890937], [20886792226071], [249142655765051], [-162555544716831], [-510144790300554], [375627537210368], [650413750069011], [361463252036246], [-855449804681226], [1417764571021605], [621855918058284], [811670621576312], [2038100995848980], [-975328110934384], [1139847668950311], [-353329090500489], [1944923748607428], [-1102887336697804], [166932033841046], [574669764818241], [2642890163141826], [180176390580918], [-269949526048885], [-1591563762240976], [-3046795412189002], [-1415470525133586], [-564982949374852], [-1569974855119225], [-2397118804245102], [11786959315628], [-1888519451909040], [-3373065385506717], [2372516249115039], [-2748885421444786], [2907837772268333], [5889259192719426], [-1987773229726452], [6744563184891164], [-4297335999068259], [-1333972913813284], [6016550661830916], [-1000303682473777], [-2865802163226166], [2903616309004616], [-7557817164998316], [-4670291071060399], [-4618047872268646], [2592646124624832], [12681451974775121], [-7023230600894586], [-14270610956206816], [-1715336826866493], [-2126291148289422], [-5501545876234078], [-17957121459688866], [6407574281598257], [5022732420023040], [-7849981433979862], [15398935483923744], [10164385639330430], [-1233388357362984], [-9137909615552920], [11566549000168668], [18568347368053346], [-15401629662398604], [-8632067728573294], [3545044487680836], [35493130283856602], [6712934961362694], [-13191630684006499], [-18270841396974492], [-33226633986948493], [-13589331331027317], [-4016663553981657], [-16351945334932212], [2408394087807191], [4967150407850457], [-12875539791529626], [37182358786333542], [-33081386816686633], [-32044401246750196], [-22428430768677301], [-50179618319667342], [43620555781355595], [1397893799329982], [-32728203487810770], [-33944900674608742], [-76028466725142679], [-48211322487878526], [-13105374519074854], [40785392596647339], [18266790286236431], [-19881789932992936], [-28321627596365695], [-81498165065159763], [85802181736662782], [-41611737509125452], [-64621674919313916], [33076886572639635], [-4453311280249114], [70630274614835984], [74806536917075651], [81077391642717954], [16510122820995986], [-41379966353857629], [-103646557421431558], [145577245093657107], [96099639155314092], [134802964351053804], [-94278218574233017], [-24865654073898874], [35161625244443604], [-55884197111804748], [90435237889800819], [-29840129933911375], [-67545883405859176], [-86250195907172851], [-165511822598294589], [-48969112629093354], [-88037660848587100], [66024361125059457], [-94811602571372910], [-141677859736207360], [-80084252861623159], [258599476601932782], [244466012679681155], [-131996672261837913], [-209592945621082824], [-273359142236957215], [-260796274955852896], [-286722751241188182], [-86669957510913006], [174683692122363138], [59163692478313454], [105586799218509644], [-288151764324458424], [380556127766909540], [-444292979654167899], [133397127348929672], [358430934630418728], [-176855469778271934], [-115122629131794904], [-232630799271556908], [303392690382582422], [-172172071726430776], [-523613406484749160], [-425270166770664132], [436832602191260510], [179074775645231820], [387463447920220046], [-527208369557371974], [-162519076039112061], [358424066546515044], [108508988718724380], [-224862977240508634], [166877112159223343], [-488610756589950664], [-584862634535785008], [690814924296792108], [-580522801636115251], [450520871104661493], [-190930527404986114], [85328304693823692], [-187836468635394892], [-216353170643276592], [-194250864144180210], [-737761978767107767], [528007456180065705], [96531142303163997], [-393919772107403212], [702962570536485656], [772763613652593477], [-135327738131769175], [119889505546537127], [711347132022869967], [635497257458964158], [1030987364542237352], [-135358284419774484], [-556389100901675452], [58092600158166582], [-1186204206909345906], [426261010417271828], [573422664896695218], [632455001364355352], [-449471500519458112], [250612519838118989], [540572350104372131], [515445333803014913], [84886996205584433], [1281982735438039137], [-681033132535973728], [-742069705643393353], [850494194327321412], [1066035199015805700], [-793231294631663683], [-1107532536883923270], [-785566682867364214], [-1071642748018548574], [-856562048452027278], [-897188370519718573], [-300263723691780921], [523595158325419424], [-329684106548442240], [385634723872950978], [328398588698628105], [-1932189651836879820], [-923343141746884281], [1136031635974313030], [1865383788198258426], [-1008345205731379783], [940061237430185637], [1261061150933893797], [2109289785945522962], [2454264387242154314], [2426607675841823271], [7759992951252335], [-1352693972462118567], [959351476428984338], [626321575285726799], [972416147080797420], [1623756005984123414], [291696114579009816], [886959309494938232], [-2025943858323903468], [1770358294753157349], [1139878860971869050], [-1376739069901401916], [2253621321457319304], [1963617822084496250], [3280603324426687221], [-1316335350663613932], [-878486914516972558], [-1725948265308079650], [-1200831342725086348], [-3583300866748224139], [-1816224977293614423], [871566286600826003], [-313054753183286232], [2195390819029309247], [-431704017794306734], [-3297881258435163352], [-238476102780062424], [4474817073378987651], [1778857800771101954], [3609962922167583762], [-2796851165133244567], [-346515488786065512], [-4724530641532118536], [-550603772897550723], [-3222880433746995742], [-4012330974329637366], [4846233618188653841], [-2894554576829936437], [-1175612098388980918], [630956530004300736], [1875096247885601673], [2935662874170628568], [1494417559512765381], [-3620938909353152436], [3333426153698818874], [-33844684169564914], [-4664455839668978382], [-2126062511141922018], [3006835478265734720], [-1043118594896283133], [-1044811093628014566], [3005422934009485217], [3623094188036304326], [2564692618742611866], [6060610910675025030], [-2512195290969503733], [5579848783336409706], [-731989469973536886], [5139433369100565392], [-2992301414992581024], [1795719541662174708], [-6070164516215663925], [2856015621391211498], [1620886625225321486], [1435876424827255734], [-6757484825941247203], [-680988474144945007], [-220724232154119759], [-6050146102518340060], [3014004747008295030], [10417714331778235293], [4349271891577293089], [690996109722415436], [5355686174544411744], [-1061584023727870434], [-7552829881003118677], [-3384046272037446054], [8058012417938068286], [-1029004764460438089], [-5903194526472171570], [4089815247723519944], [1030193352882276936], [-517918709130686236], [-4438797804936394002], [-7139633757828053548], [7763063545665017972], [-13734006543481834438], [-829971339075872124], [9334415414889032513], [-2020802225940956550], [-13855400175580788276], [9833825079071636882], [11655339716097622212], [-13477560310284829138], [7705763433567538256], [8822434015135132170], [13578558399756310778], [-11182190503075170477], [471514824845998964], [4526359531732399950], [-656634397342438360], [9201290846929664808], [-7624585560541145895], [-9071450306053817083], [9802464211687560962], [-9922890668368234548], [10790093435522575868], [-1824334875724823121], [-12287632886003597953], [-11680633757827462629], [18635882522261473557], [5852407664020976924], [7768194785384933537], [-14874169190349120507], [11708583976625171778], [1271939676937006856], [4853317362944122098], [-3476422670217577782], [-6503119287291518106], [-8992829498795348686], [15447972290235997740], [1587021812408857197], [6819590599468448141], [5726016771429189261], [-9432618356696678283], [-23020770849367364226], [11222854014137775176], [-12890005443835587732]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_54_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_54_12_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_54_12_a_b(:prec:=1)
    chi := MakeCharacter_54_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_54_12_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_54_12_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_54_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<5,R![5865, 1]>],Snew);
    return Vf;
end function;