
// Make newform 4.18.a.a in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4_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_4_18_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_4_18_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
        poly := [-2340, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-192, 384]];
    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_4_a();"
function MakeCharacter_4_a()
    N := 4;
    order := 1;
    char_gens := [3];
    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_4_a_Hecke(Kf)
    return MakeCharacter_4_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 := 18;
    raw_aps := [[0, 0], [-2940, -1], [302022, 36], [12675080, -594], [629824140, -38115], [-660026290, 162756], [-13749113070, 175032], [50566816916, 2861595], [67383745560, 14187978], [1168577791326, 136229940], [139418056736, 128935800], [10464901444070, 494092116], [2083296157866, -1902597840], [-55571574267220, -1581547275], [98386325578800, -5178600108], [-243982561368330, 12154446612], [1417952098906812, -9148192335], [-33772017497218, 114760782180], [-497585660773180, -109849087089], [1941122746607688, 347319240030], [-6373212940884790, -6950534184], [-7492135767032752, -360485585460], [21949708904946900, -232562921877], [25954503979423194, -224724957480], [-24640503894924190, -3147743142504], [103793379193282086, 2625119677620], [38753395895033960, 3699440983062], [155212550420314860, -1521512512443], [-213076908720865618, 2815939263060], [-291147714764300430, -10003493400768], [-351860939992608640, 23705120378016], [-839137225964180220, -6570108903105], [-877873359218325750, 57629905233264], [1463874306258785036, 12551416092765], [-1509666564827362986, -207041770601820], [517886702965521560, -2139512226390], [-3018070771645103650, 114736277917764], [359834277777876260, -543079363895145], [305247571405013160, -579552191881146], [6847608282391127790, 443071065059172], [9809623888812805428, -280532731890285], [3968189306621631350, 233035723262340], [12625614513406058688, 196301056185360], [-18224147410438178110, 568157406544728], [51189660687408357510, -96325204861116], [9889841272648685768, -829147997857410], [15713525628292885844, -2854169434050165], [36224055106125121760, 2187163514884680], [-22468017907437587100, -2974689062958393], [66427368418068372134, -1562317968990780], [-138590641793041674390, 3716697939420312], [-74650227039824722704, 10861877764597860], [138513564145323662834, -5215696093392840], [-198915011863518932100, 10372162748004225], [270346405401715740930, -7777670582043648], [-234921452618790119160, -17934666004760274], [189723719418467047758, -17936605773086220], [410946897156202872080, -1948430719396260], [-499963762121497543210, -19578150386711532], [378571414734939802266, 13575362660770680], [-355191540180868592740, -13836085348812903], [1232962539937392525030, 11679334370001540], [514143613694258013620, -28789872030163629], [-1748193893846321003208, -509528497849470], [-903851185162889698630, 89369046294066864], [837815615094711066750, 61599972315584052], [496104326627828566988, -224581337439835635], [-1080765489293875415470, 30368749301374176], [-3619246124476223253540, -145315401069370455], [-1093706727469438729570, 7826697400791060], [545831935340019150690, 151090617617511264], [-7234840176734036237976, 147127602803580630], [5590974725609238746480, 22587135302451204], [2931676111567697478710, -414641290593696012], [-1035502461055597231108, 310907052405115425], [-2722352459240185994880, 211680650938769568], [5640247385172725383494, -565443506807667180], [-11181715420738711324210, -656977515396906924], [852820939259653249170, 760928372096369400], [9568694740589232933146, -876665272655644560], [12606652307272305507876, 65273414316830775], [-25116273240594997125274, 241609465188921060], [20831690170702013912496, 121525115075913780], [7643981331574565919410, 319343867090192376], [-6865714652983333542472, -2008681531807516830], [-1461103748636560880580, 1122760592359879473], [-12948765713628476511294, 2412966332800560600], [-9948807054538195190710, 1875432482519138928], [9892196796303118829070, -3473950163202155340], [-30278520895769724478000, 238364291787416172], [-44063300109116002036140, -715835693647584405], [-32627468449105911035424, -405983996711445240], [51526530258262481375720, -3982527958533327882], [-2020001837099846519700, -2959952349767334075], [-5954835422401361625484, -1835901105769314045], [-18886611262619528837640, 1901838814439395986], [-147890494243783022517954, 1632885141060248580], [41032331999749730513034, 3254588794472871600], [-18511445588824236733300, 7927078503323745117], [12442266428169992569694, -916499067586710300], [-46856520272513425457500, -6831446750454490857], [-96032616685090274477970, 3479488234681660740], [84283484580036367787700, 9409116843905633811], [90064231160681298457530, -17941298601949707600], [-91466473861728171955012, -4996323176769176175], [91534083575708022070850, 7479826472160415584], [364722123066743084399820, 13919740572662351757], [12297084544616996020050, 16823820916845480384], [50122901099651516268120, -5670220962058184070], [21946556013430947739610, 5008877004299941080], [60528124695352994445920, 14442528633763449384], [697856296623183685082150, 8118074274887511396], [-282611680237904562745110, 15990406700910603192], [-724448814439700461187860, -7610880946112959995], [934383331930552781891192, -913903876412334990], [-1012482510601290433927038, -1902643676424507240], [327423667515416408932100, -12328259322628040193], [-165425923710406493963640, -71341444769400938034], [-79703213928834122210610, 48864551382401913396], [-1124426283491686275432684, -33751199570549176485], [47448476834972569166678, 65016960871980667140], [-350593396253362942346590, 42051170928825622872], [758828258347858392542310, -18052564594079680236], [59642788270052551234860, 2643281880212214549], [-1415114739373434098495692, 10833101211979644435], [1173532619572259347891710, 53115214951193485860], [-1643047971531905320032634, -76671059952381391980], [-1140154021150418726175024, 824882655197120940], [-3475838179464793259270440, 5180492914116498522], [493004590639364575155230, 52937817097158575604], [2162218526171041737537380, -618940430909708985], [1028783985607153533997800, -110938739245106818122], [2539235382644307142371056, -109950140379177026460], [1976992320838790581594550, 87954446749450984164], [1163600983954821785313402, -99540652545818629200], [-3299860323905345840520958, -168893657752594939560], [3521027768264564378310, 239188555008528481620], [4951716034554846575955860, 107461610648873852283], [-2731849369053617775575970, 176492542482981784836], [1443395580764103901967274, -308112937613860856400], [6372533275786696238140844, 34522417892358694485], [2211133427384197799604726, -13209937036096420620], [-2810140712025036192266440, 390467410723100995074], [-2930162082450235312456260, -282260388044567307375], [5293213679273490172192382, -10660393877608200060], [-4282731661462774409422008, -359206393757588909730], [-9403943928954455799852010, -126899706105284660460], [-4132779958477101613173030, 138948376375297337904], [-1471205824454775606757924, -541386560743131906135], [890057194783469373229920, 345340741069539546408], [1145440663097660191099310, 414163554359376532788], [5395404606884961659263602, -20963519697809463840], [-4087581514465440166217740, 727392625924770783747], [13285972552150862132156280, 141254934024053758194], [-15126220751885429162056180, 64424850591668440797], [4802560744686444337414032, -527256470253478456260], [-729692095574403116982376, -138313503138839566230], [3815077789141660292426658, 591027714171297425880], [-12805731010229652103823830, 215445235933887709464], [4521624336542044968261102, -1366906574946911369580], [5809707927947286828075060, -230551083634142395341], [-9745765306226195253207750, -1060762077087953941008], [15935012029823484209560520, 1106539924033865861694], [19415661500197152491578956, 1126272450815540048205], [-18465289724943256558932270, 170319337181323404984], [-3146999926382801011834920, -759093745990199200614], [10601111220049223154531296, 1703169859988582120520], [-21469586937239847792340570, 3650879878155046548]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4_18_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_4_18_a_a(:prec:=2)
    chi := MakeCharacter_4_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 18));
    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_4_18_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_4_18_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,18,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;