
// Make newform 1386.4.a.k in Magma, downloaded from the LMFDB on 25 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_k();".
// 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_k();".
// 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], [1], [-7], [11], [-8], [-22], [54], [-213], [-190], [163], [31], [-110], [4], [80], [566], [-645], [634], [-729], [-431], [-918], [-254], [-904], [-901], [-89], [-332], [52], [-366], [-184], [1131], [2126], [-2610], [-349], [-762], [-2486], [722], [-2049], [964], [-1194], [-1572], [3693], [125], [4445], [-718], [-18], [-520], [630], [-2077], [-3708], [1417], [3222], [-4072], [3732], [-5101], [2478], [-2378], [-8518], [-3716], [2564], [-928], [9328], [3982], [6584], [-3920], [4293], [-6493], [3269], [11114], [6342], [-1790], [6939], [-2332], [11039], [364], [6145], [12381], [-12061], [-774], [-5154], [-5266], [8280], [-2610], [10636], [-13317], [-14822], [11637], [15489], [16888], [4938], [-8645], [2075], [16472], [9133], [-354], [8956], [20804], [-6241], [-12867], [-12532], [-4404], [-1904], [-7250], [9612], [-2], [14900], [12187], [-21788], [1750], [22104], [3792], [-25414], [-14416], [-17858], [10279], [-11067], [-7239], [-17611], [-17225], [17757], [-13114], [-19101], [464], [20014], [5620], [-32303], [18000], [-14933], [185], [32551], [36244], [32258], [9232], [-26681], [1062], [-24748], [-14612], [-8962], [-38182], [20297], [29522], [42294], [-16546], [-16263], [-3744], [-17205], [42513], [24774], [33364], [19825], [11460], [-37862], [14705], [15212], [-25698], [29016], [1048], [-28804], [-24546], [50584], [9174], [-27919], [-33348], [36302], [18709], [17479], [-34127], [52840], [-53956], [-20824], [-19837], [-3972], [-25238], [-49626], [-15780], [-32443], [-51555], [64906], [35205], [-59482], [-10542], [-26612], [-8880], [-1119], [-49204], [-25559], [-62382], [-16412], [18116], [-44344], [54294], [-26201], [-4778], [71623], [-69018], [27974], [-1081], [-20240], [8843], [55578], [-2434], [11350], [15156], [-72066], [1834], [31213], [61369], [-5515], [-37306], [-28980], [-47668], [-33532], [-29421], [5969], [39580], [-53184], [-52891], [-26440], [-28290], [-57836], [32637], [-41452], [101058], [36571], [-39213], [-90332], [-21638], [-46454], [-70056], [-21780], [-86556], [-54079], [-54902], [-20000], [106980], [38621], [-53846], [-77137], [-4886], [96747], [42135], [102363], [-1534], [-51244], [-113975], [13318], [-1346], [-14768], [13020], [30830], [81918], [-74800], [14968], [-111535], [27974], [-117396], [-9270], [-10850], [-16294], [-97191], [125836], [133276], [133048], [-93366], [103856], [-48270], [36780], [-67155], [69993], [98962], [32248], [-91449], [4325], [91902], [25340], [23226], [59295], [-123753], [102986], [-68254], [-51224], [-93356], [61851], [106758], [68402], [-58386], [57753], [43818], [-20398], [72225], [-59400], [-75789], [13108], [-114988], [101956], [32166], [-100560], [-33263], [-88533], [150058], [95648], [57792], [123036], [-58146], [19524], [-119379], [43591], [21387], [-133837], [-16680], [-100524], [-28478], [43006], [107462], [-67753], [-34386], [-9674], [37309], [178422], [2294], [-15438], [-69780], [72629], [174502], [-88017], [22950], [97097], [69405], [99583], [19522], [199680], [154512], [66978], [-48266], [-121686], [-76898], [-153117], [85545], [-166110], [127645], [55124], [-146707], [-36335], [108292], [197957], [36429], [-22640], [-142283], [-202487], [-100016], [81095], [125584], [210231], [-62490], [72786], [-215444], [144532], [-220209], [93790], [130320], [-193391], [-219882], [106634], [-28713], [139824], [29139], [-155883], [-5776], [-34732], [124980], [-24639], [-207932], [-152196], [214472], [187592], [-5635], [7164], [-64370], [-175082], [-246792], [-27536], [-5154], [182977], [61594], [-67562], [88884], [-91275], [233760], [-123237], [178358], [78025], [-133999], [144147], [6081], [23399], [54447], [-131018], [-179032], [-222322], [-94324], [-130200], [-150854], [-102429], [-164104], [-65418], [124584], [108538], [272586], [-233632], [-204236], [72937], [-128320], [137480], [-158036], [270696], [-187554], [127253], [104736], [-89525], [-26762], [96982], [-177575], [-220808], [-73956], [-25506], [-88026], [-195229], [307751], [-70416], [195095], [324950]];
    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_k();".
// 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_k(: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_k();".
// 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_k( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1386_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![-1, 1]>,<13,R![8, 1]>],Snew);
    return Vf;
end function;