
// Make newform 6.18.a.b in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6_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_6_18_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_6_18_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_6_a();"
function MakeCharacter_6_a()
    N := 6;
    order := 1;
    char_gens := [5];
    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_6_a_Hecke(Kf)
    return MakeCharacter_6_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 := [[-256], [6561], [-72186], [-8640184], [1159304460], [2801062862], [32979662226], [5778498836], [169116994200], [3631735478814], [6880978560608], [-35464500749338], [-8923766734806], [-129966457018324], [129499777218480], [218262107088054], [-1783401246652740], [1469145893932670], [5051560974054596], [-793480696785720], [6343500933237962], [-8292883305185392], [-24031501915598508], [-15466463339248422], [79745962551777122], [175677759352523238], [-100291893446960344], [-265518533305019412], [-22785042253550290], [56621972939186802], [-87046639270509760], [-291549572371166460], [1046112600962512266], [-2658520752132073204], [3059411321900707734], [2175984693454883288], [-5885411044465864738], [-1486877489574133084], [-8412524946090259800], [-13436931431786925522], [7868635363999246644], [20960871047274814454], [18859502676328149696], [-38628547028020225342], [-46052640653977067706], [8822744580056914568], [-52364196104193750700], [-29483848842204730720], [16685347507559524068], [16785975053486579750], [166092898884473294442], [-197773581854213504016], [120673658743619366642], [9476950003076922876], [144667851468351535362], [-165479848326316437240], [765963371346168916494], [-520542578911414482736], [-1093216137330405440938], [-424847879779312339302], [-122463743781547150948], [727954982157066933414], [1005252255579613687220], [-2323496172215958857160], [-916081290063323328838], [-1094589144734135273922], [90641184430944416972], [-5804036064165082220974], [-6712270729702768655268], [3891194297556060517790], [1496165779888419977826], [7578067497970434639336], [-11544706195082833515088], [2921174309179230785846], [-2670869639554197582724], [-7545015537822469484160], [17096102155549991668614], [3540924939937737165518], [-9136317226324741085166], [-16623235093642838161894], [-25753026774646988221020], [26935175637243579928550], [22898569761114481715760], [14834490498960419366066], [5898076413240070097528], [21367273232477178640572], [30597580461238927669314], [-33893127118235006170294], [42778615043407360580430], [-15989253070608240248944], [32804710655527500604884], [-7313033842947786124704], [64809324197450548801832], [87014090888929710587244], [103055915750727562537076], [55744202591084471414904], [240566486195582525694], [-83170139497192930995702], [-183385509783811343771380], [144977709796663614372830], [-32668696610552341744348], [58956545319738540601518], [-104476746159552982468812], [-322207346977641161094342], [-125037617924645435674180], [27454530336896530797122], [431074307935051140904908], [-647623958884219774438830], [-119466018560868739226664], [614541426771929995050458], [-52288189589396804879584], [188812148152033675632422], [779200774819457286821610], [91343352240180037475564], [127853076182494639729592], [146204680345535161124610], [-518713761080833627607164], [-716385630090760443816696], [1122660500000277316159758], [807222245993307114101268], [-1821656753669091603155626], [1676361700586355316427426], [-299561092153995832559706], [162279764183211465107628], [2061486871205378691697844], [-2138379862334493741342786], [1185970673377252179878918], [-2440862326938609699868080], [1827756687299437501779992], [1103707018721031076220318], [-2282473756287798089956252], [-566594224789948421045784], [3958732004428428078405680], [-4718598724584336272019658], [1212124850996846216027898], [850447281223390027672322], [3880221735560377351184262], [-6887451136550419635942124], [-2019515132783836390389090], [3683950789246829784528426], [6753567402353299544792876], [4186543693878082012827318], [1809393110303015884340600], [-99875108128514922146628], [-12036368138481116026868482], [-4295125253076801118577976], [-288158716892683723056106], [-3738250920613818354313638], [5445797481615106531498076], [13586444487638829106030944], [-4393607264492487695329618], [1211307377304836119637106], [544602199544639505212276], [-3395962869304708425749640], [-5671386255258848720457076], [-11611076260199317796692848], [-21673195618598503081176616], [-25657513393522986950291166], [-9789974149090113301259734], [-9974022852869782968295122], [20530475177014849429965876], [-5550898323145055496184134], [7038755234804881209794696], [-13292103964980636327056436], [23815099371702341923936338], [14070631223040808870268376], [23537006469258488811140384], [29221271103261101903787686]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_6_18_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_6_18_a_b(:prec:=1)
    chi := MakeCharacter_6_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_6_18_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_6_18_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_6_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,18,sign)));
    Vf := Kernel([<5,R![72186, 1]>],Snew);
    return Vf;
end function;