
// Make newform 1386.4.a.l in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1386_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_1386_4_a_l();".
// 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_1386_4_a_l();".
// 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_1386_a();"
function MakeCharacter_1386_a()
    N := 1386;
    order := 1;
    char_gens := [155, 199, 1135];
    v := [1, 1, 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_1386_a_Hecke(Kf)
    return MakeCharacter_1386_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 := 4;
    raw_aps := [[2], [0], [7], [7], [-11], [-67], [-30], [-7], [-28], [-121], [-310], [-71], [180], [-108], [-71], [-128], [429], [22], [-803], [-468], [-117], [-96], [1122], [1146], [-92], [202], [-1798], [1431], [-278], [2322], [2254], [-2196], [-766], [-1080], [-1373], [-3210], [452], [-11], [-504], [-1688], [-460], [2296], [-878], [-2268], [-5298], [1044], [4566], [198], [-3762], [-704], [-2210], [-2451], [25], [5267], [1623], [2475], [-1746], [1653], [-2056], [-837], [8969], [-5120], [-2892], [-3208], [3550], [5524], [-2144], [-884], [4040], [1225], [-2483], [-7804], [3074], [674], [12051], [12664], [-264], [-14394], [12304], [-3154], [4635], [-7265], [-7589], [5164], [14221], [-6924], [-2524], [1928], [12852], [4741], [6809], [-1734], [-7368], [-13201], [16539], [14780], [11766], [13503], [-18681], [-2000], [19368], [-4669], [9324], [-25646], [-12928], [-14862], [13383], [10152], [-16592], [-1001], [-18427], [5606], [-6378], [-4070], [23252], [-3758], [13068], [31027], [18456], [-11141], [5172], [-10460], [15546], [-31138], [21574], [-2074], [-6829], [-6921], [24064], [30262], [24024], [227], [-23435], [1609], [-6976], [-36079], [23507], [-35145], [-34155], [3439], [-1625], [-31531], [-31003], [15015], [12280], [-26599], [-3798], [38726], [3296], [-13488], [-17144], [-11993], [10483], [-4562], [18556], [-5826], [41492], [15381], [-55054], [3892], [36158], [23959], [-56106], [35079], [26184], [8584], [-51331], [-38542], [34942], [-53553], [16295], [-53846], [14120], [-56390], [57358], [-55890], [-15420], [24366], [15703], [64031], [6693], [-33264], [-9694], [-140], [-23028], [12871], [-32516], [-46314], [-23834], [42858], [65446], [-51615], [-57451], [-40795], [-35369], [-42075], [17070], [77602], [-66288], [35292], [-53310], [39479], [-30144], [-68129], [-68316], [58966], [-48060], [45077], [-38310], [-52392], [-46198], [-60191], [-3193], [36120], [25046], [5350], [-6248], [41926], [-8966], [-33330], [-44134], [-71185], [-18684], [74279], [-63256], [-10966], [-86763], [41274], [13855], [-100059], [-83866], [50099], [89054], [-56529], [25084], [-37762], [-8326], [-20457], [-72834], [60133], [-6816], [-6884], [-40162], [58634], [56460], [44340], [-11612], [72805], [-105724], [33987], [-83446], [88212], [45042], [84590], [44398], [54004], [50885], [80503], [-34767], [-49125], [-71322], [-9332], [32578], [55], [32817], [-64362], [14514], [-28921], [-141852], [-58878], [64136], [80461], [-138996], [-21898], [73351], [-92894], [104960], [-36466], [84070], [102296], [109176], [-128760], [-38314], [-48853], [-8915], [-11888], [-56629], [-32014], [124390], [48602], [155454], [111008], [52895], [111328], [-30496], [12664], [125962], [-61754], [-143980], [-70167], [-76543], [59017], [117292], [-9412], [85329], [-109213], [85328], [19516], [124080], [64216], [15598], [-12860], [15063], [145084], [89216], [-107258], [77956], [-146416], [-33268], [-125974], [84729], [59023], [-24618], [46736], [147705], [92480], [164540], [117652], [-14111], [-120877], [-30268], [-80988], [-182951], [-144526], [204952], [11121], [55207], [-59128], [-107894], [56440], [160466], [71254], [-159834], [124951], [85006], [-76737], [71094], [-138040], [110272], [-156246], [-13460], [-189579], [-71832], [122649], [-165922], [62738], [-121827], [40660], [-19081], [42428], [-17732], [-116332], [-88308], [73311], [138920], [-19081], [8314], [-110050], [-8802], [-76570], [-77966], [188417], [-71615], [-116296], [-165489], [-24953], [-132459], [-53045], [-81654], [-85846], [74757], [98656], [-173454], [164151], [12804], [-115703], [139957], [4714], [-27306], [-173333], [215941], [102786], [-197608], [-44996], [65058], [-129958], [-164087], [94273], [172400], [-56141], [8153], [-64958], [-34526], [115206], [-59784], [-19497], [35967], [-95598], [133982], [-160056], [22562], [-70126], [81428], [-108040], [88686], [-15661], [-105993], [176414], [165178], [-192650], [203046], [-955], [-203334], [99794], [-295618], [-249761], [146614], [6443], [193406], [-272344]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1386_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1386_4_a_l();".
// 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_1386_4_a_l(:prec:=1)
    chi := MakeCharacter_1386_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_1386_4_a_l();".
// 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_1386_4_a_l( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1386_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-7, 1]>,<13,R![67, 1]>],Snew);
    return Vf;
end function;