
// Make newform 1386.4.a.a in Magma, downloaded from the LMFDB on 24 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_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_1386_4_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
        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], [-18], [7], [11], [56], [-36], [-28], [-180], [54], [-334], [386], [444], [-316], [402], [486], [282], [380], [176], [324], [800], [-1144], [-468], [870], [-1330], [120], [-1210], [-1236], [-694], [-978], [-1216], [-1680], [-1062], [-508], [-2598], [2648], [-790], [-160], [-264], [-1632], [708], [902], [-1824], [2090], [1602], [-3274], [-4948], [2342], [-2064], [-1666], [-4158], [-72], [6860], [150], [2430], [-3048], [3834], [-3508], [8294], [-8022], [392], [2748], [-3064], [-4062], [-4870], [-4806], [6620], [1094], [-3468], [-8188], [5070], [-1656], [10166], [-2722], [-5872], [-12330], [14586], [1874], [-13338], [-8200], [7362], [-11710], [936], [9038], [1964], [-10068], [-3270], [-15526], [-10548], [-3796], [-7122], [-2292], [5132], [-4188], [3848], [1068], [6162], [20946], [-4696], [19358], [18020], [-14622], [2244], [3258], [-6604], [-16594], [19062], [4776], [-7956], [14348], [24488], [-19654], [-2694], [10178], [-7648], [-270], [16250], [-10242], [17322], [-11676], [-20710], [-10354], [10920], [-27804], [-25834], [10590], [-6802], [-23010], [4682], [-17860], [6860], [22752], [7364], [-34378], [-27456], [7952], [4986], [-42748], [-35610], [17046], [-2176], [-2094], [7328], [12492], [-37486], [17574], [9440], [28440], [-24334], [-39264], [32114], [-41454], [2876], [-13932], [-19768], [-43836], [31544], [-11118], [10568], [-14964], [-3324], [-3906], [-36448], [20526], [-37434], [52194], [-15220], [37664], [30842], [13578], [-47988], [-37438], [57120], [58382], [43382], [32322], [61652], [-9522], [42860], [4016], [40784], [-20268], [18902], [-9444], [-27168], [25026], [-49210], [6530], [-62314], [14160], [52862], [300], [-1312], [-33834], [-54564], [23238], [-33730], [-22390], [37656], [40092], [78546], [45608], [26966], [-31408], [-84666], [-6138], [5654], [-10956], [-46290], [-21202], [11942], [-66270], [-20032], [-7926], [-20244], [46466], [80768], [10920], [74484], [-35622], [7586], [-92716], [-67950], [-6856], [60216], [69626], [-54918], [95016], [98756], [29268], [-36118], [-4336], [-16648], [32766], [83018], [89028], [-26434], [-91422], [69984], [54318], [-65160], [-19948], [42122], [-2230], [12516], [-30456], [69986], [-22542], [-111364], [27336], [97478], [108552], [46692], [-83326], [53748], [-99588], [-49582], [-15844], [57750], [79364], [-40984], [-10956], [17564], [67184], [24990], [-108286], [56766], [-106998], [-48436], [49278], [-20854], [-65896], [40358], [97784], [49160], [81902], [106020], [-137650], [-103318], [90780], [-25776], [-20140], [129636], [28664], [-49660], [102612], [-78754], [47922], [-36706], [63180], [-79290], [-85194], [-77886], [11724], [-130714], [64812], [41426], [-68406], [-102636], [41876], [116984], [-50946], [-115360], [-114408], [-50872], [144782], [-121236], [-114550], [35112], [-107650], [179928], [-27078], [15738], [-109060], [160296], [-70888], [73686], [-27192], [-7474], [-12270], [-172216], [-103342], [-181764], [4880], [-63330], [-135718], [182792], [20834], [65904], [-155466], [-47218], [119430], [48296], [-43692], [452], [94782], [-44998], [-65268], [159530], [-178948], [21566], [-169638], [45096], [-122560], [-175638], [-81516], [-133978], [34688], [-105624], [-145854], [-102364], [-7666], [43146], [12908], [-72058], [50244], [81330], [-146040], [-121386], [56568], [-54034], [45780], [170256], [169788], [107618], [177878], [75168], [94448], [-98134], [153624], [28790], [-17592], [85242], [-158344], [30266], [33030], [-116184], [-165220], [-1572], [37532], [121230], [183138], [150824], [33006], [-21364], [-222966], [-205000], [-107134], [-38428], [-263766], [61310], [-40038], [-58680], [110834], [-52440], [-187306], [-94876], [-30618], [38528], [-105642], [-119932], [-169650], [152624], [184470], [31872], [-102664], [281414], [-133290], [-170146], [25290], [15032], [-182250], [259596], [-49228], [-59170], [161502], [164928], [-94654], [100854], [-249696], [-255462], [-21244], [114564], [-45408], [-227518], [139650], [22656], [-167598], [128558], [-166596]];
    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_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_1386_4_a_a(: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_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_1386_4_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1386_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![18, 1]>,<13,R![-56, 1]>],Snew);
    return Vf;
end function;