
// Make newform 3.14.a.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3_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_3_14_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_3_14_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
        poly := [-492, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-3, 6]];
    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_3_a();"
function MakeCharacter_3_a()
    N := 3;
    order := 1;
    char_gens := [2];
    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_3_a_Hecke(Kf)
    return MakeCharacter_3_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 := 14;
    raw_aps := [[-27, -1], [729, 0], [20358, -128], [-10504, 3456], [336204, 36608], [8766302, -89856], [41919282, -72960], [128146772, -767232], [429790968, 3697408], [-2364237666, -6108544], [-2991275824, -35040384], [13705597046, 17335296], [7629487146, -98057984], [-5657249620, 311613696], [-34517571120, -690673408], [-113168447082, 1307299968], [-463910412132, 397652992], [89697730670, -1553776128], [-349157530588, -6092098560], [-392229274968, 4890812160], [928700122538, 7102660608], [-357012735040, 1007856000], [-2287146958956, 1485492992], [1635089350842, -22362004992], [-4937463078238, 37066775040], [1066767259014, 93619307648], [9161922552440, -41059661184], [9759881689740, -16486553088], [857515321982, -160962723072], [-5070000197646, -75044177408], [34132119546512, -12119725440], [-25169585207580, -129686755328], [-33470921132982, -786258631936], [72690327956588, -53774696448], [69624864284886, 700125960064], [91765793303432, -149880965760], [65018329006478, -823828202496], [-197475582244444, 935271256320], [-301071480864984, -1679168728064], [512025326772174, -1031295388288], [-236535607752300, 2840247966720], [197783844317606, 514817462016], [-503177356380096, 2585016286208], [-308410290368446, 6661064650752], [1112016906307494, -2754446512512], [853732547267096, 455995350144], [486401435868596, -432441345024], [1715609383614512, 2394894847104], [-1876418404752924, -5449809028864], [-1173129478892842, 14190808995072], [-969361572989430, 25502654931456], [-1036303447501008, -8531478605312], [-1629476032111246, -22514371932672], [300510175913532, 36541482581760], [-261317913957630, -47785475821568], [4223834510514696, -25395321702400], [7432839167159502, 36203829936768], [-4309072788272416, 30645132236928], [5036055116267654, -54463735003392], [-6581377903406022, 20215022618624], [-285505569241348, -102420928106496], [14005357308742086, 9149976277120], [342499853548820, -8948274674688], [-3925493814515976, 10830294404608], [1391914617122906, 22590204549120], [4771883886689790, 129092546156672], [20734468288158572, 43666879818240], [-8695708755211246, 171108681043968], [12166040821209372, 273566847128320], [15384796794439694, -25234374680064], [-34197706577254878, -20237301945856], [10338513634878408, 95239960219392], [27026450106172064, -153508732371072], [-422794492561210, 75335772335616], [-63886129453811140, -189421488057600], [90847007930261376, -131956955955200], [-68885944215641082, 210355314564992], [-15325228969184770, 30705967130112], [16072723215595890, 872754024984832], [-67461547860987142, -342532057471488], [-34427802919474908, -202875204768512], [27477856909011158, 1017338257592064], [-84386036320829424, -895342225676544], [78085720069100786, 716057857428480], [85744491386582600, -1181166110386560], [48407014034214012, 1209582178237184], [91357082195514786, -338952956119296], [319397923099792106, 65629586244096], [-81278007416236818, 1202425746416000], [23879583429355040, 296619261918336], [-59956332848396460, -1294193689621248], [-229319440510150080, -1878147492778240], [-159772580823692968, -998490026471040], [111339536499806220, -316287876781568], [110161583942912852, 2554591497781248], [15962983611561432, 2602176501805824], [-280992650173627842, 1740729803162752], [336051625727430666, -1858577316766464], [405716919289201100, -3672746352002304], [-444232184265304594, 3008876100062976], [143425603930443236, -3922683893362944], [390336661827482382, 2239709105343360], [-488726871313167756, 1969330914433792], [-21536603604118278, 5685389069873408], [-263291219335649956, 2300930855797248], [-331168428177911998, -7828062584371200], [-795596482539289620, 755603459835392], [-267042307207508910, 10288534162807296], [12230343981851256, -6922966011896576], [1280133425824759034, -122462900739072], [-217414611092481808, -268246840406400], [929926266163215734, 9425157503427072], [184698471147247818, 66313318011904], [-253231209331253812, 3239475716754432], [-34544035926826648, 8761168084878720], [326420862726740706, -19903505036971264], [-1442101167386252860, -8993661549421824], [-743951130553709208, 15547194516775680], [-879642667296199314, -9225470345490560], [2287107160095168180, -1699976921797120], [-565412764763221978, 20955509772456960], [-3939788994733874782, 5192299600478208], [2290960998625489254, 8095824653487488], [4836135074025480876, 1418389341768960], [958057713985704884, 21702812719972608], [-1654998743668398210, -10556871889350528], [1128823489525234742, -24120516297284352], [106799210446659696, -27383609244178176], [-2008874377718832952, 16384534155586944], [3065589831168019310, 7617358543723776], [2450993395325951108, 2666822633667072], [-4110878756655143640, 4499045958724096], [1202449722173007584, -20898983834612352], [2720204086405438118, 13962241844999424], [-3135768233214706182, -421811171998976], [1956502929000762626, -37134564709671936], [-1117740833349067674, -36793751806688640], [-2772278588386982380, -40565876965555968], [5256422048390267070, 40167581812961152], [8575243633218301290, 8328877223443200], [-2936333729423329108, -22374031763808000], [-117646772694827562, 6575675155677824], [6721599927804462824, 35829697342407552], [-3392872379831573028, -50869750798164480], [-1491900610950522610, 31700523192595200], [-4925032027879904280, -90694315193132800], [-16406706053120071834, -32639167332218880], [1447814183044081050, 49776549516353792], [-1706635090543710820, 114672395473862400], [-15563629236512962080, 26891880665711616], [-5300862386961920674, -11318131651981824], [8072547663228118578, 32406260133633024], [9986230411980922100, 677525022049536], [5846836416374814840, -42952849544204288], [3601640015531279180, -176102812951504128], [-18607707480109142448, 65054423513331200], [25245362826411811400, -10555954203047040], [-2271801405690447678, 128149489621476608], [-9614727414959543926, -129184887435835392], [-22291988647457657394, -90447389494135424], [24276620893821347988, -43796810389206016], [6434281541404773882, 50134884617059584], [10134913428196336472, 149054492461921920], [6964255569080065356, -112245776551597824], [39408298424199081522, -106831385377391360], [-3284164598280593832, -139034549010998272], [1908726462987388112, -26056151775642240], [-10430165006830495882, -56982570174521856]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3_14_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_3_14_a_b(:prec:=2)
    chi := MakeCharacter_3_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 14));
    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_3_14_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_3_14_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,14,sign)));
    Vf := Kernel([<2,R![-16992, 54, 1]>],Snew);
    return Vf;
end function;